Search in sources :

Example 66 with ConfiguredObject

use of org.apache.qpid.server.model.ConfiguredObject in project qpid-broker-j by apache.

the class AbstractConfiguredObjectTest method testSetAttributesFiresListener.

@Test
public void testSetAttributesFiresListener() {
    final String objectName = "listenerFiring";
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(ConfiguredObject.NAME, objectName);
    attributes.put(TestSingleton.STRING_VALUE, "first");
    final TestSingleton object = _model.getObjectFactory().create(TestSingleton.class, attributes, null);
    final AtomicInteger listenerCount = new AtomicInteger();
    final LinkedHashMap<String, String> updates = new LinkedHashMap<>();
    object.addChangeListener(new AbstractConfigurationChangeListener() {

        @Override
        public void attributeSet(final ConfiguredObject<?> object, final String attributeName, final Object oldAttributeValue, final Object newAttributeValue) {
            listenerCount.incrementAndGet();
            String delta = String.valueOf(oldAttributeValue) + "=>" + String.valueOf(newAttributeValue);
            updates.put(attributeName, delta);
        }
    });
    // Set updated value (should cause listener to fire)
    object.setAttributes(Collections.singletonMap(TestSingleton.STRING_VALUE, "second"));
    assertEquals((long) 1, (long) listenerCount.get());
    String delta = updates.remove(TestSingleton.STRING_VALUE);
    assertEquals("first=>second", delta);
    // Set unchanged value (should not cause listener to fire)
    object.setAttributes(Collections.singletonMap(TestSingleton.STRING_VALUE, "second"));
    assertEquals((long) 1, (long) listenerCount.get());
    // Set value to null (should cause listener to fire)
    object.setAttributes(Collections.singletonMap(TestSingleton.STRING_VALUE, null));
    assertEquals((long) 2, (long) listenerCount.get());
    delta = updates.remove(TestSingleton.STRING_VALUE);
    assertEquals("second=>null", delta);
    // Set to null again (should not cause listener to fire)
    object.setAttributes(Collections.singletonMap(TestSingleton.STRING_VALUE, null));
    assertEquals((long) 2, (long) listenerCount.get());
    // Set updated value (should cause listener to fire)
    object.setAttributes(Collections.singletonMap(TestSingleton.STRING_VALUE, "third"));
    assertEquals((long) 3, (long) listenerCount.get());
    delta = updates.remove(TestSingleton.STRING_VALUE);
    assertEquals("null=>third", delta);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) LinkedHashMap(java.util.LinkedHashMap) AbstractConfigurationChangeListener(org.apache.qpid.server.model.AbstractConfigurationChangeListener) Test(org.junit.Test)

Example 67 with ConfiguredObject

use of org.apache.qpid.server.model.ConfiguredObject in project qpid-broker-j by apache.

the class AbstractConfiguredObjectTest method testSetAttributesInterpolateValues.

@Test
public void testSetAttributesInterpolateValues() {
    setTestSystemProperty("foo1", "myValue1");
    setTestSystemProperty("foo2", "myValue2");
    setTestSystemProperty("foo3", null);
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(ConfiguredObject.NAME, getTestName());
    attributes.put(TestSingleton.STRING_VALUE, "${foo1}");
    final TestSingleton object = _model.getObjectFactory().create(TestSingleton.class, attributes, null);
    final AtomicInteger listenerCount = new AtomicInteger();
    object.addChangeListener(new AbstractConfigurationChangeListener() {

        @Override
        public void attributeSet(final ConfiguredObject<?> object, final String attributeName, final Object oldAttributeValue, final Object newAttributeValue) {
            listenerCount.incrementAndGet();
        }
    });
    assertEquals("myValue1", object.getStringValue());
    assertEquals("${foo1}", object.getActualAttributes().get(TestSingleton.STRING_VALUE));
    // Update the actual value ${foo1} => ${foo2}
    object.setAttributes(Collections.singletonMap(TestSingleton.STRING_VALUE, "${foo2}"));
    assertEquals((long) 1, (long) listenerCount.get());
    assertEquals("myValue2", object.getStringValue());
    assertEquals("${foo2}", object.getActualAttributes().get(TestSingleton.STRING_VALUE));
    // No change
    object.setAttributes(Collections.singletonMap(TestSingleton.STRING_VALUE, "${foo2}"));
    assertEquals((long) 1, (long) listenerCount.get());
    // Update the actual value ${foo2} => ${foo3} (which doesn't have a value)
    object.setAttributes(Collections.singletonMap(TestSingleton.STRING_VALUE, "${foo3}"));
    assertEquals((long) 2, (long) listenerCount.get());
    assertEquals("${foo3}", object.getStringValue());
    assertEquals("${foo3}", object.getActualAttributes().get(TestSingleton.STRING_VALUE));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) AbstractConfigurationChangeListener(org.apache.qpid.server.model.AbstractConfigurationChangeListener) Test(org.junit.Test)

Example 68 with ConfiguredObject

use of org.apache.qpid.server.model.ConfiguredObject in project qpid-broker-j by apache.

the class AbstractConfiguredObjectTest method testSuccessfulDeletion.

@Test
public void testSuccessfulDeletion() throws Exception {
    TestConfiguredObject configuredObject = new TestConfiguredObject("configuredObject");
    configuredObject.create();
    final List<ChangeEvent> events = new ArrayList<>();
    configuredObject.addChangeListener(new AbstractConfigurationChangeListener() {

        @Override
        public void attributeSet(ConfiguredObject<?> object, String attributeName, Object oldAttributeValue, Object newAttributeValue) {
            events.add(new ChangeEvent(EventType.ATTRIBUTE_SET, object, attributeName, oldAttributeValue, newAttributeValue));
        }

        @Override
        public void stateChanged(ConfiguredObject<?> object, State oldState, State newState) {
            events.add(new ChangeEvent(EventType.STATE_CHANGED, object, ConfiguredObject.DESIRED_STATE, oldState, newState));
        }
    });
    configuredObject.delete();
    assertEquals((long) 2, (long) events.size());
    assertEquals(State.DELETED, configuredObject.getDesiredState());
    assertEquals(State.DELETED, configuredObject.getState());
    assertEquals("Unexpected events number", (long) 2, (long) events.size());
    ChangeEvent event0 = events.get(0);
    ChangeEvent event1 = events.get(1);
    assertEquals("Unexpected first event: " + event0, new ChangeEvent(EventType.STATE_CHANGED, configuredObject, Exchange.DESIRED_STATE, State.ACTIVE, State.DELETED), event0);
    assertEquals("Unexpected second event: " + event1, new ChangeEvent(EventType.ATTRIBUTE_SET, configuredObject, Exchange.DESIRED_STATE, State.ACTIVE, State.DELETED), event1);
}
Also used : State(org.apache.qpid.server.model.State) ArrayList(java.util.ArrayList) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfigurationChangeListener(org.apache.qpid.server.model.AbstractConfigurationChangeListener) Test(org.junit.Test)

Example 69 with ConfiguredObject

use of org.apache.qpid.server.model.ConfiguredObject in project qpid-broker-j by apache.

the class FormattingStatisticsResolverTest method setUp.

@Before
public void setUp() throws Exception {
    final ConfiguredObject<?> object = mock(ConfiguredObject.class);
    final Map<String, Object> statisticsMap = Maps.newHashMap();
    statisticsMap.put(LARGEST_POSITIVE_VALUE_STAT_NAME, (1024L * 1024L) + 1L);
    statisticsMap.put(LARGER_POSITIVE_VALUE_STAT_NAME, 1025L);
    statisticsMap.put(POSITIVE_VALUE_STAT_NAME, 10L);
    statisticsMap.put(NEGATIVE_VALUE_STAT_NAME, -1L);
    statisticsMap.put(SMALLER_NEGATIVE_VALUE_STAT_NAME, -1025L);
    statisticsMap.put(SMALLEST_NEGATIVE_VALUE_STAT_NAME, (-1024L * 1024L) - 1L);
    statisticsMap.put(ZERO_VALUE_STAT_NAME, 0L);
    statisticsMap.put(EPOCH_DATE_STAT_NAME, new Date(0L));
    when(object.getStatistics()).thenReturn(statisticsMap);
    _resolver = new FormattingStatisticsResolver(object);
}
Also used : ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) Date(java.util.Date) Before(org.junit.Before)

Example 70 with ConfiguredObject

use of org.apache.qpid.server.model.ConfiguredObject in project qpid-broker-j by apache.

the class ManagementNode method generateAttributeNames.

private List<String> generateAttributeNames(String entityType) {
    Set<String> attrNameSet = new HashSet<>();
    List<String> attributeNames = new ArrayList<>();
    for (String standardAttribute : Arrays.asList(IDENTITY_ATTRIBUTE, TYPE_ATTRIBUTE, QPID_TYPE, OBJECT_PATH)) {
        attrNameSet.add(standardAttribute);
        attributeNames.add(standardAttribute);
    }
    final ConfiguredObjectTypeRegistry typeRegistry = _model.getTypeRegistry();
    List<Class<? extends ConfiguredObject>> classes = new ArrayList<>();
    if (entityType != null && !entityType.trim().equals("")) {
        Class<? extends ConfiguredObject> clazz = _managedTypes.get(entityType);
        if (clazz != null) {
            classes.add(clazz);
            if (ConfiguredObjectTypeRegistry.getCategory(clazz) == clazz) {
                classes.addAll(_model.getTypeRegistry().getTypeSpecialisations(clazz));
            }
        }
    } else {
        for (Class<? extends ConfiguredObject> clazz : _managedCategories) {
            classes.add(clazz);
            classes.addAll(_model.getTypeRegistry().getTypeSpecialisations(clazz));
        }
    }
    for (Class<? extends ConfiguredObject> clazz : classes) {
        for (String name : typeRegistry.getAttributeNames(clazz)) {
            if (!ConfiguredObject.ID.equals(name) && attrNameSet.add(name)) {
                attributeNames.add(name);
            }
        }
        final Class<? extends ConfiguredObject> category = ConfiguredObjectTypeRegistry.getCategory(clazz);
        if (category != _managedObject.getCategoryClass() && !isSyntheticChildClass(category)) {
            Class<? extends ConfiguredObject> parentType = _model.getParentType(category);
            if (parentType != _managedObject.getCategoryClass()) {
                attributeNames.add(parentType.getSimpleName().toLowerCase());
            }
        }
    }
    return attributeNames;
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) ConfiguredObjectTypeRegistry(org.apache.qpid.server.model.ConfiguredObjectTypeRegistry) HashSet(java.util.HashSet)

Aggregations

ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)117 ArrayList (java.util.ArrayList)43 HashMap (java.util.HashMap)35 Test (org.junit.Test)33 Map (java.util.Map)29 List (java.util.List)27 LinkedHashMap (java.util.LinkedHashMap)25 UUID (java.util.UUID)21 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)21 AbstractConfigurationChangeListener (org.apache.qpid.server.model.AbstractConfigurationChangeListener)15 Collection (java.util.Collection)12 LegacyConfiguredObject (org.apache.qpid.server.management.plugin.controller.LegacyConfiguredObject)12 ConfiguredObjectFinder (org.apache.qpid.server.model.ConfiguredObjectFinder)12 ManagedObject (org.apache.qpid.server.model.ManagedObject)11 State (org.apache.qpid.server.model.State)10 Date (java.util.Date)7 TreeMap (java.util.TreeMap)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 GenericLegacyConfiguredObject (org.apache.qpid.server.management.plugin.controller.GenericLegacyConfiguredObject)6 InternalMessageHeader (org.apache.qpid.server.message.internal.InternalMessageHeader)6