qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lqu...@apache.org
Subject svn commit: r1751929 [1/3] - in /qpid/java/trunk: broker-core/src/main/java/org/apache/qpid/server/model/ broker-core/src/main/java/org/apache/qpid/server/model/adapter/ broker-core/src/main/java/org/apache/qpid/server/model/preferences/ broker-core/sr...
Date Fri, 08 Jul 2016 15:52:31 GMT
Author: lquack
Date: Fri Jul  8 15:52:31 2016
New Revision: 1751929

URL: http://svn.apache.org/viewvc?rev=1751929&view=rev
Log:
QPID-7333: [Java Broker] Add preference store recoverer

Added:
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/GenericPrincipal.java
      - copied, changed from r1751916, qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferences.java
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/PreferenceFactory.java
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/store/preferences/PreferencesRecoverer.java
    qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/PreferenceFactoryTest.java
    qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/PreferenceTestHelper.java
    qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/store/preferences/PreferencesRecovererTest.java
Modified:
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AbstractConfiguredObject.java
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObject.java
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/adapter/BrokerAdapter.java
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/PreferenceImpl.java
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferences.java
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferencesImpl.java
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AbstractVirtualHost.java
    qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/UserPreferencesTest.java
    qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/PreferencesTest.java
    qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/testmodels/singleton/TestSingletonImpl.java
    qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
    qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestUserPreferenceHandler.java
    qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/RestUserPreferenceHandlerTest.java

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AbstractConfiguredObject.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AbstractConfiguredObject.java?rev=1751929&r1=1751928&r2=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AbstractConfiguredObject.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AbstractConfiguredObject.java Fri Jul  8 15:52:31 2016
@@ -70,7 +70,6 @@ import org.slf4j.LoggerFactory;
 import org.apache.qpid.server.configuration.IllegalConfigurationException;
 import org.apache.qpid.server.configuration.updater.Task;
 import org.apache.qpid.server.configuration.updater.TaskExecutor;
-import org.apache.qpid.server.model.preferences.Preference;
 import org.apache.qpid.server.model.preferences.UserPreferences;
 import org.apache.qpid.server.model.preferences.UserPreferencesImpl;
 import org.apache.qpid.server.security.AccessControl;
@@ -81,7 +80,6 @@ import org.apache.qpid.server.security.a
 import org.apache.qpid.server.security.auth.AuthenticatedPrincipal;
 import org.apache.qpid.server.security.encryption.ConfigurationSecretEncrypter;
 import org.apache.qpid.server.store.ConfiguredObjectRecord;
-import org.apache.qpid.server.store.preferences.PreferenceStore;
 import org.apache.qpid.server.util.Action;
 import org.apache.qpid.server.util.ServerScopedRuntimeException;
 import org.apache.qpid.util.Strings;
@@ -109,9 +107,8 @@ public abstract class AbstractConfigured
     }
 
     private ConfigurationSecretEncrypter _encrypter;
-    private final Map<UUID, Preference> _userPreferences = new HashMap<>();
-    private final Map<String, List<Preference>> _userPreferencesByName = new HashMap<>();
     private AccessControl _parentAccessControl;
+    private UserPreferences _userPreferences;
 
     private enum DynamicState { UNINIT, OPENED, CLOSED };
     private final AtomicReference<DynamicState> _dynamicState = new AtomicReference<>(DynamicState.UNINIT);
@@ -2974,12 +2971,13 @@ public abstract class AbstractConfigured
     @Override
     public UserPreferences getUserPreferences()
     {
-        return new UserPreferencesImpl(this, _userPreferences, _userPreferencesByName, getPreferencesStore());
+        return _userPreferences;
     }
 
-    protected PreferenceStore getPreferencesStore()
+    @Override
+    public void setUserPreferences(final UserPreferences userPreferences)
     {
-        return null;//TODO: temporary change
+        _userPreferences = userPreferences;
     }
 
     //=========================================================================================

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObject.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObject.java?rev=1751929&r1=1751928&r2=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObject.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/ConfiguredObject.java Fri Jul  8 15:52:31 2016
@@ -271,6 +271,7 @@ public interface ConfiguredObject<X exte
     void decryptSecrets();
 
     UserPreferences getUserPreferences();
+    void setUserPreferences(UserPreferences userPreferences);
 
     void authorise(Operation operation) throws AccessControlException;
     void authorise(Operation operation, Map<String, Object> arguments) throws AccessControlException;

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/adapter/BrokerAdapter.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/adapter/BrokerAdapter.java?rev=1751929&r1=1751928&r2=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/adapter/BrokerAdapter.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/adapter/BrokerAdapter.java Fri Jul  8 15:52:31 2016
@@ -72,6 +72,7 @@ import org.apache.qpid.server.security.a
 import org.apache.qpid.server.store.preferences.PreferenceRecord;
 import org.apache.qpid.server.store.preferences.PreferenceStore;
 import org.apache.qpid.server.store.preferences.PreferenceStoreUpdaterImpl;
+import org.apache.qpid.server.store.preferences.PreferencesRecoverer;
 import org.apache.qpid.server.util.HousekeepingExecutor;
 import org.apache.qpid.server.util.ServerScopedRuntimeException;
 import org.slf4j.Logger;
@@ -493,7 +494,7 @@ public class BrokerAdapter extends Abstr
 
         final PreferenceStoreUpdaterImpl updater = new PreferenceStoreUpdaterImpl();
         final Collection<PreferenceRecord> preferenceRecords = _preferenceStore.openAndLoad(updater);
-        recoverPreferences(preferenceRecords);
+        new PreferencesRecoverer().recoverPreferences(this, preferenceRecords, _preferenceStore);
 
         if (isManagementMode())
         {
@@ -503,11 +504,6 @@ public class BrokerAdapter extends Abstr
         setState(State.ACTIVE);
     }
 
-    private void recoverPreferences(final Collection<PreferenceRecord> preferenceRecords)
-    {
-        /* TODO */
-    }
-
     private void initialiseStatisticsReporting()
     {
         long report = ((Number)getAttribute(Broker.STATISTICS_REPORTING_PERIOD)).intValue() * 1000; // convert to ms
@@ -723,12 +719,6 @@ public class BrokerAdapter extends Abstr
     }
 
     @Override
-    protected PreferenceStore getPreferencesStore()
-    {
-        return _preferenceStore;
-    }
-
-    @Override
     public NamedAddressSpace getSystemAddressSpace(String name)
     {
         return _addressSpaceRegistry.getAddressSpace(name);

Copied: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/GenericPrincipal.java (from r1751916, qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferences.java)
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/GenericPrincipal.java?p2=qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/GenericPrincipal.java&p1=qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferences.java&r1=1751916&r2=1751929&rev=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferences.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/GenericPrincipal.java Fri Jul  8 15:52:31 2016
@@ -15,35 +15,34 @@
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
- *
  */
 
 package org.apache.qpid.server.model.preferences;
 
 import java.security.Principal;
-import java.util.Collection;
-import java.util.Map;
-import java.util.Set;
-import java.util.UUID;
 
-public interface UserPreferences
+public class GenericPrincipal implements Principal, Comparable<Principal>
 {
-    Preference createPreference(UUID id,
-                                String type,
-                                String name,
-                                String description,
-                                Set<Principal> visibilitySet,
-                                Map<String, Object> preferenceValueAttributes);
+    private final String _name;
 
-    void updateOrAppend(Collection<Preference> preferences);
-
-    Set<Preference> getPreferences();
-
-    void replace(Collection<Preference> preferences);
-
-    void replaceByType(String type, Collection<Preference> preferences);
-
-    void replaceByTypeAndName(String type, String name, Preference preference);
-
-    Set<Preference> getVisiblePreferences();
+    public GenericPrincipal(final String name)
+    {
+        if (name == null)
+        {
+            throw new IllegalArgumentException("Principal name cannot be null");
+        }
+        _name = name;
+    }
+
+    @Override
+    public String getName()
+    {
+        return _name;
+    }
+
+    @Override
+    public int compareTo(final Principal other)
+    {
+        return _name.compareTo(other.getName());
+    }
 }

Added: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/PreferenceFactory.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/PreferenceFactory.java?rev=1751929&view=auto
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/PreferenceFactory.java (added)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/PreferenceFactory.java Fri Jul  8 15:52:31 2016
@@ -0,0 +1,192 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.qpid.server.model.preferences;
+
+import java.security.Principal;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import org.apache.qpid.server.model.ConfiguredObject;
+import org.apache.qpid.server.plugin.QpidServiceLoader;
+import org.apache.qpid.server.security.SecurityManager;
+import org.apache.qpid.server.security.auth.AuthenticatedPrincipal;
+
+public class PreferenceFactory
+{
+    public static Preference create(final ConfiguredObject<?> associatedObject,
+                                    final Map<String, Object> attributes)
+    {
+        AuthenticatedPrincipal currentUser = SecurityManager.getCurrentUser();
+        Map<String, Object> overriddenAttributes =  new HashMap<>(attributes);
+        overriddenAttributes.put(Preference.OWNER_ATTRIBUTE, currentUser == null ? null : currentUser.getName());
+        overriddenAttributes.put(Preference.LAST_UPDATED_DATE_ATTRIBUTE, System.currentTimeMillis());
+        return recover(associatedObject, overriddenAttributes);
+    }
+
+    public static Preference recover(final ConfiguredObject<?> associatedObject,
+                                    final Map<String, Object> attributes)
+    {
+        final UUID uuid = getId(attributes);
+        final String type = getAttributeAsString(Preference.TYPE_ATTRIBUTE, attributes);
+        final String name = getAttributeAsString(Preference.NAME_ATTRIBUTE, attributes);
+        final String description = getAttributeAsString(Preference.DESCRIPTION_ATTRIBUTE, attributes);
+        final String owner = getAttributeAsString(Preference.OWNER_ATTRIBUTE, attributes);
+        if (owner == null)
+        {
+            throw new IllegalArgumentException("Preference owner is mandatory");
+        }
+        final Set<Principal> visibilitySet = getVisibilitySet(attributes);
+        final Date lastUpdatedDate = getAttributeAsDate(Preference.LAST_UPDATED_DATE_ATTRIBUTE, attributes);
+        final Map<String, Object> preferenceValueAttributes = getPreferenceValue(attributes);
+        PreferenceValue value = convertMapToPreferenceValue(type, preferenceValueAttributes);
+        return new PreferenceImpl(associatedObject, uuid, name, type, description,
+                                  new GenericPrincipal(owner),
+                                  lastUpdatedDate,
+                                  visibilitySet,
+                                  value);
+    }
+
+
+    private static UUID getId(final Map<String, Object> attributes)
+    {
+        final Object id = attributes.get(Preference.ID_ATTRIBUTE);
+        UUID uuid;
+        if (id != null)
+        {
+            if (id instanceof UUID)
+            {
+                uuid = (UUID) id;
+            }
+            else if (id instanceof String)
+            {
+                uuid = UUID.fromString((String) id);
+            }
+            else
+            {
+                throw new IllegalArgumentException(String.format("Preference attribute '%s' is not a UUID",
+                                                                 Preference.ID_ATTRIBUTE));
+            }
+        }
+        else
+        {
+            uuid = UUID.randomUUID();
+        }
+        return uuid;
+    }
+
+    private static Map<String, Object> getPreferenceValue(final Map<String, Object> attributes)
+    {
+        Object value = attributes.get(Preference.VALUE_ATTRIBUTE);
+        if (value == null)
+        {
+            return null;
+        }
+        else if (value instanceof Map)
+        {
+            HashMap<String, Object> preferenceValue = new HashMap<>();
+            for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet())
+            {
+                Object key = entry.getKey();
+                if (key instanceof String)
+                {
+                    preferenceValue.put((String) key, entry.getValue());
+                }
+                else
+                {
+                    throw new IllegalArgumentException(String.format("Invalid value entry: '%s' is not a String", key));
+                }
+            }
+            return preferenceValue;
+        }
+        throw new IllegalArgumentException(String.format("Cannot recover '%s' as Map", Preference.VALUE_ATTRIBUTE));
+    }
+
+    private static Set<Principal> getVisibilitySet(final Map<String, Object> attributes)
+    {
+        Object value = attributes.get(Preference.VISIBILITY_LIST_ATTRIBUTE);
+        if (value == null)
+        {
+            return null;
+        }
+        else if (value instanceof Collection)
+        {
+            HashSet<Principal> principals = new HashSet<>();
+            for (Object element : (Collection) value)
+            {
+                if (!(element instanceof String))
+                {
+                    String errorMessage =
+                            String.format("Invalid visibilityList element: '%s' is not a String", element);
+                    throw new IllegalArgumentException(errorMessage);
+                }
+                principals.add(new GenericPrincipal((String) element));
+            }
+            return principals;
+        }
+        throw new IllegalArgumentException(String.format("Cannot recover '%s' as List",
+                                                         Preference.VISIBILITY_LIST_ATTRIBUTE));
+    }
+
+    private static String getAttributeAsString(final String attributeName, final Map<String, Object> attributes)
+    {
+        Object value = attributes.get(attributeName);
+        if (value == null || value instanceof String)
+        {
+            return (String) value;
+        }
+        throw new IllegalArgumentException(String.format("Preference attribute '%s' is not a String", attributeName));
+    }
+
+    private static Date getAttributeAsDate(final String attributeName, final Map<String, Object> attributes)
+    {
+        Object dateObject = attributes.get(attributeName);
+        if (dateObject instanceof Number)
+        {
+            return new Date(((Number)dateObject).longValue());
+        }
+        return new Date();
+    }
+
+    private static PreferenceValue convertMapToPreferenceValue(String type, Map<String, Object> preferenceValueAttributes)
+    {
+        String implementationType = type;
+        if (type != null && type.startsWith("X-"))
+        {
+            implementationType = "X-generic";
+        }
+
+        final Map<String, PreferenceValueFactoryService> preferenceValueFactories =
+                new QpidServiceLoader().getInstancesByType(PreferenceValueFactoryService.class);
+
+        final PreferenceValueFactoryService preferenceValueFactory = preferenceValueFactories.get(implementationType);
+        if (preferenceValueFactory == null)
+        {
+            throw new IllegalArgumentException(String.format("Cannot find preference type factory for type '%s'",
+                                                             implementationType));
+        }
+
+        return preferenceValueFactory.createInstance(preferenceValueAttributes);
+    }
+}

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/PreferenceImpl.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/PreferenceImpl.java?rev=1751929&r1=1751928&r2=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/PreferenceImpl.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/PreferenceImpl.java Fri Jul  8 15:52:31 2016
@@ -30,7 +30,6 @@ import java.util.UUID;
 import com.google.common.collect.ImmutableSet;
 
 import org.apache.qpid.server.model.ConfiguredObject;
-import org.apache.qpid.server.security.SecurityManager;
 
 public class PreferenceImpl implements Preference
 {
@@ -49,16 +48,43 @@ public class PreferenceImpl implements P
                           final String name,
                           final String type,
                           final String description,
+                          final Principal owner,
+                          final Date lastUpdatedDate,
                           final Set<Principal> visibilitySet,
                           final PreferenceValue preferenceValue)
     {
-        _lastUpdatedDate = new Date();
+        if (associatedObject == null)
+        {
+            throw new IllegalArgumentException("Preference associatedObject is mandatory");
+        }
+
+        if (uuid == null)
+        {
+            throw new IllegalArgumentException("Preference id is mandatory");
+        }
+
+        if (name == null || "".equals(name))
+        {
+            throw new IllegalArgumentException("Preference name is mandatory");
+        }
+
+        if (type == null || "".equals(type))
+        {
+            throw new IllegalArgumentException("Preference type is mandatory");
+        }
+
+        if (owner == null)
+        {
+            throw new IllegalArgumentException("Preference owner is mandatory");
+        }
+
+        _lastUpdatedDate = lastUpdatedDate;
         _associatedObject = associatedObject;
         _id = uuid;
         _name = name;
         _type = type;
         _description = description;
-        _owner = SecurityManager.getCurrentUser();
+        _owner = owner;
         _visibilitySet = (visibilitySet == null ? ImmutableSet.<Principal>of() : ImmutableSet.copyOf(visibilitySet));
         _preferenceValue = preferenceValue;
     }

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferences.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferences.java?rev=1751929&r1=1751928&r2=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferences.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferences.java Fri Jul  8 15:52:31 2016
@@ -20,21 +20,11 @@
 
 package org.apache.qpid.server.model.preferences;
 
-import java.security.Principal;
 import java.util.Collection;
-import java.util.Map;
 import java.util.Set;
-import java.util.UUID;
 
 public interface UserPreferences
 {
-    Preference createPreference(UUID id,
-                                String type,
-                                String name,
-                                String description,
-                                Set<Principal> visibilitySet,
-                                Map<String, Object> preferenceValueAttributes);
-
     void updateOrAppend(Collection<Preference> preferences);
 
     Set<Preference> getPreferences();

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferencesImpl.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferencesImpl.java?rev=1751929&r1=1751928&r2=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferencesImpl.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/preferences/UserPreferencesImpl.java Fri Jul  8 15:52:31 2016
@@ -36,8 +36,6 @@ import java.util.UUID;
 
 import javax.security.auth.Subject;
 
-import org.apache.qpid.server.model.ConfiguredObject;
-import org.apache.qpid.server.plugin.QpidServiceLoader;
 import org.apache.qpid.server.security.SecurityManager;
 import org.apache.qpid.server.store.preferences.PreferenceRecord;
 import org.apache.qpid.server.store.preferences.PreferenceRecordImpl;
@@ -45,58 +43,20 @@ import org.apache.qpid.server.store.pref
 
 public class UserPreferencesImpl implements UserPreferences
 {
-    private final ConfiguredObject<?> _associatedObject;
     private final Map<UUID, Preference> _preferences;
     private final Map<String, List<Preference>> _preferencesByName;
     private final PreferenceStore _preferenceStore;
 
-    public UserPreferencesImpl(final ConfiguredObject<?> associatedObject,
-                               final Map<UUID, Preference> userPreferences,
-                               final Map<String, List<Preference>> userPreferencesByName,
-                               final PreferenceStore preferenceStore)
-    {
-        _preferences = userPreferences;
-        _associatedObject = associatedObject;
-        _preferencesByName = userPreferencesByName;
-        _preferenceStore = preferenceStore;
-    }
-
-    @Override
-    public Preference createPreference(final UUID id,
-                                       final String type,
-                                       final String name,
-                                       final String description,
-                                       final Set<Principal> visibilitySet,
-                                       final Map<String, Object> preferenceValueAttributes)
+    public UserPreferencesImpl(final PreferenceStore preferenceStore, final Collection<Preference> preferences)
     {
-        final Map<String, PreferenceValueFactoryService> preferenceValueFactories =
-                new QpidServiceLoader().getInstancesByType(PreferenceValueFactoryService.class);
-
-        UUID uuid = id;
-        if (uuid == null)
-        {
-            uuid = UUID.randomUUID();
-        }
-
-        if (name == null || "".equals(name))
-        {
-            throw new IllegalArgumentException("Preference name is mandatory");
-        }
+        _preferences = new HashMap<>();
+        _preferencesByName = new HashMap<>();
+        _preferenceStore = preferenceStore;
 
-        String implementationType = type;
-        if (type != null && type.startsWith("X-"))
-        {
-            implementationType = "X-generic";
-        }
-        final PreferenceValueFactoryService preferenceValueFactory = preferenceValueFactories.get(implementationType);
-        if (preferenceValueFactory == null)
+        for (Preference preference : preferences)
         {
-            throw new IllegalArgumentException(String.format("Cannot find preference type factory for type '%s'",
-                                                             implementationType));
+            addPreference(preference);
         }
-
-        PreferenceValue preferenceValue = preferenceValueFactory.createInstance(preferenceValueAttributes);
-        return new PreferenceImpl(_associatedObject, uuid, name, type, description, visibilitySet, preferenceValue);
     }
 
     @Override
@@ -119,13 +79,7 @@ public class UserPreferencesImpl impleme
                 _preferencesByName.get(oldPreference.getName()).remove(oldPreference);
             }
 
-            _preferences.put(preference.getId(), preference);
-
-            if (!_preferencesByName.containsKey(preference.getName()))
-            {
-                _preferencesByName.put(preference.getName(), new ArrayList<Preference>());
-            }
-            _preferencesByName.get(preference.getName()).add(preference);
+            addPreference(preference);
         }
     }
 
@@ -137,7 +91,7 @@ public class UserPreferencesImpl impleme
         Set<Preference> preferences = new HashSet<>();
         for (Preference preference : _preferences.values())
         {
-            if (currentPrincipal.equals(preference.getOwner()))
+            if (principalsEqual(currentPrincipal, preference.getOwner()))
             {
                 preferences.add(preference);
             }
@@ -162,7 +116,7 @@ public class UserPreferencesImpl impleme
         Collection<PreferenceRecord> preferenceRecordsToAdd = new HashSet<>();
         for (Preference preference : _preferences.values())
         {
-            if (Objects.equals(preference.getOwner(), currentPrincipal)
+            if (principalsEqual(preference.getOwner(), currentPrincipal)
                 && (type == null || Objects.equals(preference.getType(), type)))
             {
                 preferenceRecordsToRemove.add(preference.getId());
@@ -183,14 +137,7 @@ public class UserPreferencesImpl impleme
 
         for (Preference preference : preferences)
         {
-            _preferences.put(preference.getId(), preference);
-
-            String preferenceName = preference.getName();
-            if (!_preferencesByName.containsKey(preferenceName))
-            {
-                _preferencesByName.put(preferenceName, new ArrayList<Preference>());
-            }
-            _preferencesByName.get(preferenceName).add(preference);
+           addPreference(preference);
         }
     }
 
@@ -210,7 +157,7 @@ public class UserPreferencesImpl impleme
             while (preferenceIterator.hasNext())
             {
                 Preference preference = preferenceIterator.next();
-                if (Objects.equals(preference.getOwner(), currentPrincipal)
+                if (principalsEqual(preference.getOwner(), currentPrincipal)
                     && Objects.equals(preference.getType(), type))
                 {
                     existingPreferenceId = preference.getId();
@@ -233,13 +180,7 @@ public class UserPreferencesImpl impleme
 
         if (newPreference != null)
         {
-            _preferences.put(newPreference.getId(), newPreference);
-
-            if (!_preferencesByName.containsKey(name))
-            {
-                _preferencesByName.put(name, new ArrayList<Preference>());
-            }
-            _preferencesByName.get(name).add(newPreference);
+            addPreference(newPreference);
         }
     }
 
@@ -251,7 +192,7 @@ public class UserPreferencesImpl impleme
         Set<Preference> visiblePreferences = new HashSet<>();
         for (Preference preference : _preferences.values())
         {
-            if (currentPrincipals.contains(preference.getOwner()))
+            if (principalsContain(currentPrincipals, preference.getOwner()))
             {
                 visiblePreferences.add(preference);
                 continue;
@@ -261,7 +202,7 @@ public class UserPreferencesImpl impleme
             {
                 for (Principal principal : visibilityList)
                 {
-                    if (currentPrincipals.contains(principal))
+                    if (principalsContain(currentPrincipals, principal))
                     {
                         visiblePreferences.add(preference);
                         break;
@@ -331,7 +272,7 @@ public class UserPreferencesImpl impleme
         for (Preference preference : preferences)
         {
             // validate owner
-            if (!currentPrincipal.equals(preference.getOwner()))
+            if (!principalsEqual(currentPrincipal, preference.getOwner()))
             {
                 throw new SecurityException(String.format("Preference '%s' not owned by current user.",
                                                           preference.getId().toString()));
@@ -354,7 +295,7 @@ public class UserPreferencesImpl impleme
             {
                 for (Preference preferenceWithSameName : preferencesWithSameName)
                 {
-                    if (Objects.equals(preferenceWithSameName.getOwner(), preference.getOwner())
+                    if (principalsEqual(preferenceWithSameName.getOwner(), preference.getOwner())
                         && Objects.equals(preferenceWithSameName.getType(), preference.getType())
                         && !Objects.equals(preferenceWithSameName.getId(), preference.getId()))
                     {
@@ -428,4 +369,33 @@ public class UserPreferencesImpl impleme
         }
         return currentPrincipals;
     }
+
+    private void addPreference(final Preference preference)
+    {
+        _preferences.put(preference.getId(), preference);
+        if (!_preferencesByName.containsKey(preference.getName()))
+        {
+            _preferencesByName.put(preference.getName(), new ArrayList<Preference>());
+        }
+        _preferencesByName.get(preference.getName()).add(preference);
+    }
+
+    private boolean principalsEqual(final Principal p1, final Principal p2)
+    {
+        return (p1.equals(p2)
+                || ((p1 instanceof GenericPrincipal) && (((GenericPrincipal) p1).compareTo(p2) == 0))
+                || ((p2 instanceof GenericPrincipal) && (((GenericPrincipal) p2).compareTo(p1) == 0)));
+    }
+
+    private boolean principalsContain(Collection<Principal> principals, Principal principal)
+    {
+        for (Principal currentPrincipal : principals)
+        {
+            if (principalsEqual(principal, currentPrincipal))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
 }

Added: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/store/preferences/PreferencesRecoverer.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/store/preferences/PreferencesRecoverer.java?rev=1751929&view=auto
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/store/preferences/PreferencesRecoverer.java (added)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/store/preferences/PreferencesRecoverer.java Fri Jul  8 15:52:31 2016
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.qpid.server.store.preferences;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.qpid.server.model.ConfiguredObject;
+import org.apache.qpid.server.model.Model;
+import org.apache.qpid.server.model.preferences.Preference;
+import org.apache.qpid.server.model.preferences.PreferenceFactory;
+import org.apache.qpid.server.model.preferences.UserPreferencesImpl;
+
+public class PreferencesRecoverer
+{
+    private static final Logger LOGGER = LoggerFactory.getLogger(PreferencesRecoverer.class);
+
+    public void recoverPreferences(ConfiguredObject<?> parent,
+                                   Collection<PreferenceRecord> preferenceRecords,
+                                   PreferenceStore preferencesStore)
+    {
+        Set<UUID> corruptedRecords = new HashSet<>();
+        Map<UUID, Collection<PreferenceRecord>> objectToRecordMap = new HashMap<>();
+        for (PreferenceRecord preferenceRecord : preferenceRecords)
+        {
+            UUID associatedObjectId = getAssociatedObjectId(preferenceRecord.getAttributes());
+            if (associatedObjectId == null)
+            {
+                LOGGER.info("Could not find associated object for preference : {}", preferenceRecord.getId());
+                corruptedRecords.add(preferenceRecord.getId());
+            }
+            else
+            {
+                Collection<PreferenceRecord> objectPreferences = objectToRecordMap.get(associatedObjectId);
+                if (objectPreferences == null)
+                {
+                    objectPreferences = new HashSet<>();
+                    objectToRecordMap.put(associatedObjectId, objectPreferences);
+                }
+                objectPreferences.add(preferenceRecord);
+            }
+        }
+
+        setUserPreferences(parent, objectToRecordMap, preferencesStore, corruptedRecords);
+
+        if (!objectToRecordMap.isEmpty())
+        {
+            LOGGER.warn("Could not recover preferences associated with: {}", objectToRecordMap.keySet());
+            for (Collection<PreferenceRecord> records: objectToRecordMap.values())
+            {
+                for (PreferenceRecord record : records)
+                {
+                    corruptedRecords.add(record.getId());
+                }
+            }
+        }
+
+        if (!corruptedRecords.isEmpty())
+        {
+            LOGGER.warn("Removing unrecoverable corrupted preferences: {}", corruptedRecords);
+            preferencesStore.replace(corruptedRecords, Collections.<PreferenceRecord>emptySet());
+        }
+    }
+
+    private void setUserPreferences(ConfiguredObject<?> associatedObject,
+                                    Map<UUID, Collection<PreferenceRecord>> objectToRecordMap,
+                                    PreferenceStore preferenceStore, final Set<UUID> corruptedRecords)
+    {
+        final Collection<PreferenceRecord> preferenceRecords = objectToRecordMap.remove(associatedObject.getId());
+        Collection<Preference> recoveredPreferences = new ArrayList<>();
+        if (preferenceRecords != null)
+        {
+            for (PreferenceRecord preferenceRecord : preferenceRecords)
+            {
+                Map<String, Object> attributes = preferenceRecord.getAttributes();
+                try
+                {
+                    recoveredPreferences.add(PreferenceFactory.recover(associatedObject, attributes));
+                }
+                catch (IllegalArgumentException e)
+                {
+                    LOGGER.info(String.format("Cannot recover preference '%s/%s'",
+                                              preferenceRecord.getId(),
+                                              attributes.get(Preference.NAME_ATTRIBUTE)), e);
+                    corruptedRecords.add(preferenceRecord.getId());
+                }
+            }
+        }
+        associatedObject.setUserPreferences(new UserPreferencesImpl(preferenceStore, recoveredPreferences));
+        Model model = associatedObject.getModel();
+        Collection<Class<? extends ConfiguredObject>> childrenCategories =
+                model.getChildTypes(associatedObject.getCategoryClass());
+        for (Class<? extends ConfiguredObject> childCategory : childrenCategories)
+        {
+            Collection<? extends ConfiguredObject> children = associatedObject.getChildren(childCategory);
+            for (ConfiguredObject<?> child : children)
+            {
+                setUserPreferences(child, objectToRecordMap, preferenceStore, corruptedRecords);
+            }
+        }
+    }
+
+    private UUID getAssociatedObjectId(final Map<String, Object> preferenceRecordAttributes)
+    {
+        if (preferenceRecordAttributes == null)
+        {
+            return null;
+        }
+        Object associatedObjectIdObject = preferenceRecordAttributes.get(Preference.ASSOCIATED_OBJECT_ATTRIBUTE);
+        if (associatedObjectIdObject == null || !(associatedObjectIdObject instanceof String))
+        {
+            return null;
+        }
+        UUID associatedObjectId;
+        try
+        {
+            associatedObjectId = UUID.fromString((String) associatedObjectIdObject);
+        }
+        catch (Exception e)
+        {
+            return null;
+        }
+        return associatedObjectId;
+    }
+}

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AbstractVirtualHost.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AbstractVirtualHost.java?rev=1751929&r1=1751928&r2=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AbstractVirtualHost.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AbstractVirtualHost.java Fri Jul  8 15:52:31 2016
@@ -95,6 +95,7 @@ import org.apache.qpid.server.store.pref
 import org.apache.qpid.server.store.preferences.PreferenceStore;
 import org.apache.qpid.server.store.preferences.PreferenceStoreUpdater;
 import org.apache.qpid.server.store.preferences.PreferenceStoreUpdaterImpl;
+import org.apache.qpid.server.store.preferences.PreferencesRecoverer;
 import org.apache.qpid.server.transport.AMQPConnection;
 import org.apache.qpid.server.transport.NetworkConnectionScheduler;
 import org.apache.qpid.server.txn.AutoCommitTransaction;
@@ -498,12 +499,6 @@ public abstract class AbstractVirtualHos
 
     protected abstract MessageStore createMessageStore();
 
-    @Override
-    protected PreferenceStore getPreferencesStore()
-    {
-        return _preferenceStore;
-    }
-
     protected boolean isStoreEmpty()
     {
         final IsStoreEmptyHandler isStoreEmptyHandler = new IsStoreEmptyHandler();
@@ -1968,7 +1963,7 @@ public abstract class AbstractVirtualHos
         {
             final PreferenceStoreUpdater updater = new PreferenceStoreUpdaterImpl();
             Collection<PreferenceRecord> records = _preferenceStore.openAndLoad(updater);
-            recoverPreferences(records);
+            new PreferencesRecoverer().recoverPreferences(this, records, _preferenceStore);
 
             initialiseHouseKeeping(getHousekeepingCheckPeriod());
             finalState = State.ACTIVE;
@@ -1981,11 +1976,6 @@ public abstract class AbstractVirtualHos
         }
     }
 
-    protected void recoverPreferences(final Collection<PreferenceRecord> records)
-    {
-        //TODO
-    }
-
     protected void startFileSystemSpaceChecking()
     {
         File storeLocationAsFile = _messageStore.getStoreLocationAsFile();

Added: qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/PreferenceFactoryTest.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/PreferenceFactoryTest.java?rev=1751929&view=auto
==============================================================================
--- qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/PreferenceFactoryTest.java (added)
+++ qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/PreferenceFactoryTest.java Fri Jul  8 15:52:31 2016
@@ -0,0 +1,271 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.qpid.server.model.preferences;
+
+import static org.apache.qpid.server.model.preferences.PreferenceTestHelper.createPreferenceAttributes;
+import static org.mockito.Mockito.mock;
+
+import java.security.Principal;
+import java.security.PrivilegedAction;
+import java.util.Collections;
+import java.util.Map;
+import java.util.UUID;
+
+import javax.security.auth.Subject;
+
+import org.apache.qpid.server.model.ConfiguredObject;
+import org.apache.qpid.server.security.auth.AuthenticatedPrincipal;
+import org.apache.qpid.server.security.auth.TestPrincipalUtils;
+import org.apache.qpid.test.utils.QpidTestCase;
+
+public class PreferenceFactoryTest extends QpidTestCase
+{
+    public static final String TEST_USERNAME = "testUser";
+    private ConfiguredObject<?> _testObject;
+    private Subject _testSubject;
+
+    @Override
+    public void setUp() throws Exception
+    {
+        super.setUp();
+        _testObject = mock(ConfiguredObject.class);
+        _testSubject = TestPrincipalUtils.createTestSubject(TEST_USERNAME);
+    }
+
+    public void testCreatePreference()
+    {
+        final Map<String, Object> prefValueMap = Collections.<String, Object>singletonMap("myprefkey", "myprefvalue");
+        Preference p = Subject.doAs(_testSubject, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                return PreferenceFactory.create(_testObject, createPreferenceAttributes(null,
+                                                                                        null,
+                                                                                        "X-PREF1",
+                                                                                        "myprefname",
+                                                                                        "myprefdescription",
+                                                                                        null,
+                                                                                        Collections.<String>emptySet(),
+                                                                                        prefValueMap));
+            }
+        });
+
+        assertNotNull("Creation failed", p);
+        assertEquals("Unexpected preference name", "myprefname", p.getName());
+        assertEquals("Unexpected preference description", "myprefdescription", p.getDescription());
+        assertEquals("Unexpected preference visibility list", Collections.emptySet(), p.getVisibilityList());
+        assertNotNull("Preference creation date must not be null", p.getLastUpdatedDate());
+        final PreferenceValue preferenceValue = p.getValue();
+        assertNotNull("Preference value is null", preferenceValue);
+        assertEquals("Unexpected preference value", prefValueMap, preferenceValue.getAttributes());
+    }
+
+    public void testRecoverPreference()
+    {
+        final Map<String, Object> prefValueMap = Collections.<String, Object>singletonMap("myprefkey", "myprefvalue");
+        Preference p = PreferenceFactory.recover(_testObject, createPreferenceAttributes(null,
+                                                                                         null,
+                                                                                         "X-PREF1",
+                                                                                         "myprefname",
+                                                                                         "myprefdescription",
+                                                                                         TEST_USERNAME,
+                                                                                         Collections.<String>emptySet(),
+                                                                                         prefValueMap));
+        assertNotNull("Creation failed", p);
+        assertEquals("Unexpected preference name", "myprefname", p.getName());
+        assertEquals("Unexpected preference description", "myprefdescription", p.getDescription());
+        assertEquals("Unexpected preference visibility list", Collections.emptySet(), p.getVisibilityList());
+        assertNotNull("Preference creation date must not be null", p.getLastUpdatedDate());
+        final PreferenceValue preferenceValue = p.getValue();
+        assertNotNull("Preference value is null", preferenceValue);
+        assertEquals("Unexpected preference value", prefValueMap, preferenceValue.getAttributes());
+    }
+
+    public void testPreferenceNameIsMandatory()
+    {
+        final Map<String, Object> prefValueMap = Collections.emptyMap();
+        try
+        {
+            PreferenceFactory.recover(_testObject, createPreferenceAttributes(null,
+                                                                              null,
+                                                                              "X-PREF1",
+                                                                              null,
+                                                                              "myprefdescription",
+                                                                              TEST_USERNAME,
+                                                                              Collections.<String>emptySet(),
+                                                                              prefValueMap));
+            fail("Preference name must not be null");
+        }
+        catch (IllegalArgumentException e)
+        {
+            // pass
+        }
+        try
+        {
+            PreferenceFactory.recover(_testObject, createPreferenceAttributes(null,
+                                                                              null,
+                                                                              "X-PREF1",
+                                                                              "",
+                                                                              "myprefdescription",
+                                                                              TEST_USERNAME,
+                                                                              Collections.<String>emptySet(),
+                                                                              prefValueMap));
+            fail("Preference name must not be empty");
+        }
+        catch (IllegalArgumentException e)
+        {
+            // pass
+        }
+    }
+
+    public void testPreferenceHasUuid()
+    {
+        Preference p1 = PreferenceFactory.recover(_testObject, createPreferenceAttributes(null,
+                                                                                          null,
+                                                                                          "X-TESTPREF",
+                                                                                          "testProp1",
+                                                                                          "",
+                                                                                          TEST_USERNAME,
+                                                                                          null,
+                                                                                          Collections.<String, Object>emptyMap()));
+        Preference p2 = PreferenceFactory.recover(_testObject, createPreferenceAttributes(null,
+                                                                                          null,
+                                                                                          "X-TESTPREF",
+                                                                                          "testProp2",
+                                                                                          "",
+                                                                                          TEST_USERNAME,
+                                                                                          Collections.<String>emptySet(),
+                                                                                          Collections.<String, Object>emptyMap()));
+        UUID id1 = p1.getId();
+        UUID id2 = p2.getId();
+        assertNotNull("preference id must not be null", id1);
+        assertNotNull("preference id must not be null", id2);
+        assertTrue("preference ids must be unique", !id1.equals(id2));
+    }
+
+    public void testPreferenceOwner()
+    {
+        Preference p = PreferenceFactory.recover(_testObject, createPreferenceAttributes(null,
+                                                                                         null,
+                                                                                         "X-TESTPREF",
+                                                                                         "testProp1",
+                                                                                         null,
+                                                                                         TEST_USERNAME,
+                                                                                         null,
+                                                                                         Collections.<String, Object>emptyMap()));
+        final Principal testPrincipal = _testSubject.getPrincipals(AuthenticatedPrincipal.class).iterator().next();
+        assertEquals("Unexpected preference owner", testPrincipal.getName(), p.getOwner().getName());
+    }
+
+    public void testPreferenceOwnerIsMandatoryOnRecovery()
+    {
+        try
+        {
+            PreferenceFactory.recover(_testObject, createPreferenceAttributes(null,
+                                                                              null,
+                                                                              "X-TESTPREF",
+                                                                              "testProp1",
+                                                                              null,
+                                                                              null,
+                                                                              null,
+                                                                              Collections.<String, Object>emptyMap()));
+            fail("Recovery should fail if owner is missed");
+        }
+        catch (IllegalArgumentException e)
+        {
+            //pass
+        }
+    }
+
+    public void testAssociatedObject()
+    {
+        Preference p = PreferenceFactory.recover(_testObject, createPreferenceAttributes(null,
+                                                                                         null,
+                                                                                         "X-TESTPREF",
+                                                                                         "testProp1",
+                                                                                         null,
+                                                                                         TEST_USERNAME,
+                                                                                         null,
+                                                                                         Collections.<String, Object>emptyMap()));
+        assertEquals("Unexpected associated object", _testObject, p.getAssociatedObject());
+    }
+
+    public void testType()
+    {
+        final String type = "X-TESTPREF";
+        Preference p = PreferenceFactory.recover(_testObject, createPreferenceAttributes(null,
+                                                                                         null,
+                                                                                         type,
+                                                                                         "testProp1",
+                                                                                         null,
+                                                                                         TEST_USERNAME,
+                                                                                         null,
+                                                                                         Collections.<String, Object>emptyMap()));
+        assertEquals("Unexpected type", type, p.getType());
+    }
+
+    public void testLastUpdatedDateOnRecovery()
+    {
+        Map<String, Object> attributes = createPreferenceAttributes(null,
+                                                                    null,
+                                                                    "X-TESTPREF",
+                                                                    "testProp1",
+                                                                    null,
+                                                                    TEST_USERNAME,
+                                                                    null,
+                                                                    Collections.<String, Object>emptyMap());
+        attributes.put(Preference.LAST_UPDATED_DATE_ATTRIBUTE, 1);
+        Preference p = PreferenceFactory.recover(_testObject, attributes);
+
+        assertEquals("Unexpected last updated date", 1, p.getLastUpdatedDate().getTime());
+    }
+
+    public void testLastUpdatedDateOnCreate() throws InterruptedException
+    {
+        long beforeTime = System.currentTimeMillis();
+        Thread.sleep(1);
+        Preference p = Subject.doAs(_testSubject, new PrivilegedAction<Preference>()
+        {
+            @Override
+            public Preference run()
+            {
+                return PreferenceFactory.create(_testObject, createPreferenceAttributes(null,
+                                                                                        null,
+                                                                                        "X-PREF1",
+                                                                                        "myprefname",
+                                                                                        "myprefdescription",
+                                                                                        null,
+                                                                                        Collections.<String>emptySet(),
+                                                                                        Collections.<String, Object>emptyMap()));
+            }
+        });
+        Thread.sleep(1);
+        long afterTime = System.currentTimeMillis();
+        long updateTime = p.getLastUpdatedDate().getTime();
+
+        assertTrue(String.format("Update time is not greater then the time before creation: %d vs %d",
+                                 updateTime,
+                                 beforeTime), updateTime > beforeTime);
+        assertTrue(String.format("Update time is not less then the time after creation: %d vs %d",
+                                 updateTime,
+                                 afterTime), updateTime < afterTime);
+    }
+}

Added: qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/PreferenceTestHelper.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/PreferenceTestHelper.java?rev=1751929&view=auto
==============================================================================
--- qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/PreferenceTestHelper.java (added)
+++ qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/PreferenceTestHelper.java Fri Jul  8 15:52:31 2016
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.qpid.server.model.preferences;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+public class PreferenceTestHelper
+{
+    public static Map<String, Object> createPreferenceAttributes(UUID associatedObjectId,
+                                                                 UUID id,
+                                                                 String type,
+                                                                 String name,
+                                                                 String description,
+                                                                 String owner,
+                                                                 Set<String> visibilitySet,
+                                                                 Map<String, Object> preferenceValueAttributes)
+    {
+        Map<String, Object> preferenceAttributes = new HashMap<>();
+        preferenceAttributes.put(Preference.ASSOCIATED_OBJECT_ATTRIBUTE,
+                                 associatedObjectId == null ? null : associatedObjectId.toString());
+        preferenceAttributes.put(Preference.ID_ATTRIBUTE, id);
+        preferenceAttributes.put(Preference.TYPE_ATTRIBUTE, type);
+        preferenceAttributes.put(Preference.NAME_ATTRIBUTE, name);
+        preferenceAttributes.put(Preference.DESCRIPTION_ATTRIBUTE, description);
+        preferenceAttributes.put(Preference.OWNER_ATTRIBUTE, owner);
+        preferenceAttributes.put(Preference.VISIBILITY_LIST_ATTRIBUTE, visibilitySet);
+        preferenceAttributes.put(Preference.VALUE_ATTRIBUTE, preferenceValueAttributes);
+        return preferenceAttributes;
+    }
+}

Modified: qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/UserPreferencesTest.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/UserPreferencesTest.java?rev=1751929&r1=1751928&r2=1751929&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/UserPreferencesTest.java (original)
+++ qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/model/preferences/UserPreferencesTest.java Fri Jul  8 15:52:31 2016
@@ -27,8 +27,6 @@ import java.security.PrivilegedAction;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.TreeSet;
 import java.util.UUID;
@@ -66,9 +64,7 @@ public class UserPreferencesTest extends
         super.setUp();
         _configuredObject = mock(ConfiguredObject.class);
         _preferenceStore = mock(PreferenceStore.class);
-        _userPreferences = new UserPreferencesImpl(_configuredObject,
-                                                   new HashMap<UUID, Preference>(),
-                                                   new HashMap<String, List<Preference>>(), _preferenceStore);
+        _userPreferences = new UserPreferencesImpl(_preferenceStore, Collections.<Preference>emptyList());
         _groupPrincipal = new GroupPrincipal(MYGROUP);
         _owner = new AuthenticatedPrincipal(MYUSER);
         _subject = new Subject(true,



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message