Search in sources :

Example 31 with ConfiguredObject

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

the class AbstractJDBCConfigurationStore method insertConfiguredObject.

private void insertConfiguredObject(ConfiguredObjectRecord configuredObject, final Connection conn) throws StoreException {
    try {
        try (PreparedStatement stmt = conn.prepareStatement("SELECT object_type, attributes FROM " + getConfiguredObjectsTableName() + " where id = ?")) {
            stmt.setString(1, configuredObject.getId().toString());
            boolean exists;
            try (ResultSet rs = stmt.executeQuery()) {
                exists = rs.next();
            }
            // If we don't have any data in the result set then we can add this configured object
            if (!exists) {
                try (PreparedStatement insertStmt = conn.prepareStatement("INSERT INTO " + getConfiguredObjectsTableName() + " ( id, object_type, attributes) VALUES (?,?,?)")) {
                    insertStmt.setString(1, configuredObject.getId().toString());
                    insertStmt.setString(2, configuredObject.getType());
                    if (configuredObject.getAttributes() == null) {
                        insertStmt.setNull(3, Types.BLOB);
                    } else {
                        final Map<String, Object> attributes = configuredObject.getAttributes();
                        final ObjectMapper objectMapper = ConfiguredObjectJacksonModule.newObjectMapper(true);
                        byte[] attributesAsBytes = objectMapper.writeValueAsBytes(attributes);
                        ByteArrayInputStream bis = new ByteArrayInputStream(attributesAsBytes);
                        insertStmt.setBinaryStream(3, bis, attributesAsBytes.length);
                    }
                    insertStmt.execute();
                }
                writeHierarchy(configuredObject, conn);
            }
        }
    } catch (IOException | SQLException e) {
        throw new StoreException("Error inserting of configured object " + configuredObject + " into database: " + e.getMessage(), e);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) StoreException(org.apache.qpid.server.store.StoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 32 with ConfiguredObject

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

the class PreferencesRecoverer method setUserPreferences.

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 {
                Preference recoveredPreference = PreferenceFactory.fromAttributes(associatedObject, attributes);
                validateRecoveredPreference(recoveredPreference);
                recoveredPreferences.add(recoveredPreference);
            } 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(_executor, associatedObject, preferenceStore, recoveredPreferences));
    if (!(associatedObject instanceof PreferencesRoot)) {
        Collection<Class<? extends ConfiguredObject>> childrenCategories = associatedObject.getModel().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);
            }
        }
    }
}
Also used : UserPreferencesImpl(org.apache.qpid.server.model.preferences.UserPreferencesImpl) ArrayList(java.util.ArrayList) Preference(org.apache.qpid.server.model.preferences.Preference) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject)

Example 33 with ConfiguredObject

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

the class AbstractConfiguredObjectTest method XtestUnsuccessfulStateTransitionDoesNotInvokesListener.

// TODO - not sure if I want to keep the state transition methods on delete
public void XtestUnsuccessfulStateTransitionDoesNotInvokesListener() throws Exception {
    final IllegalStateTransitionException expectedException = new IllegalStateTransitionException("This test fails the state transition.");
    TestConfiguredObject parent = new TestConfiguredObject("parent") {

        @Override
        protected ListenableFuture<Void> doDelete() {
            throw expectedException;
        }
    };
    parent.create();
    final AtomicInteger callCounter = new AtomicInteger();
    parent.addChangeListener(new AbstractConfigurationChangeListener() {

        @Override
        public void stateChanged(final ConfiguredObject<?> object, final State old, final State state) {
            super.stateChanged(object, old, state);
            callCounter.incrementAndGet();
        }

        @Override
        public void attributeSet(ConfiguredObject<?> object, String attributeName, Object oldAttributeValue, Object newAttributeValue) {
            super.attributeSet(object, attributeName, oldAttributeValue, newAttributeValue);
            callCounter.incrementAndGet();
        }
    });
    try {
        parent.delete();
        fail("Exception not thrown.");
    } catch (RuntimeException e) {
        assertSame("State transition threw unexpected exception.", expectedException, e);
    }
    assertEquals(0, callCounter.get());
    assertEquals(State.ACTIVE, parent.getDesiredState());
    assertEquals(State.ACTIVE, parent.getState());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) State(org.apache.qpid.server.model.State) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) IllegalStateTransitionException(org.apache.qpid.server.model.IllegalStateTransitionException) AbstractConfigurationChangeListener(org.apache.qpid.server.model.AbstractConfigurationChangeListener)

Example 34 with ConfiguredObject

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

the class GenericRecoverer method resolveObjects.

private void resolveObjects(ConfiguredObject<?> parentObject, List<ConfiguredObjectRecord> records, final boolean isNew) {
    ConfiguredObjectFactory factory = parentObject.getObjectFactory();
    Map<UUID, ConfiguredObject<?>> resolvedObjects = new HashMap<UUID, ConfiguredObject<?>>();
    resolvedObjects.put(parentObject.getId(), parentObject);
    Collection<ConfiguredObjectRecord> recordsWithUnresolvedParents = new ArrayList<ConfiguredObjectRecord>(records);
    Collection<UnresolvedConfiguredObject<? extends ConfiguredObject>> recordsWithUnresolvedDependencies = new ArrayList<UnresolvedConfiguredObject<? extends ConfiguredObject>>();
    boolean updatesMade;
    do {
        updatesMade = false;
        Iterator<ConfiguredObjectRecord> iter = recordsWithUnresolvedParents.iterator();
        while (iter.hasNext()) {
            ConfiguredObjectRecord record = iter.next();
            Collection<ConfiguredObject<?>> parents = new ArrayList<ConfiguredObject<?>>();
            boolean foundParents = true;
            for (UUID parentId : record.getParents().values()) {
                if (!resolvedObjects.containsKey(parentId)) {
                    foundParents = false;
                    break;
                } else {
                    parents.add(resolvedObjects.get(parentId));
                }
            }
            if (parents.size() > 1) {
                throw new IllegalStateException(String.format("Unexpected number of parents %d for record %s ", parents.size(), record));
            }
            if (foundParents) {
                iter.remove();
                ConfiguredObject<?>[] parentArray = parents.toArray(new ConfiguredObject<?>[parents.size()]);
                UnresolvedConfiguredObject<? extends ConfiguredObject> recovered = factory.recover(record, parentArray[0]);
                Collection<ConfiguredObjectDependency<?>> dependencies = recovered.getUnresolvedDependencies();
                if (dependencies.isEmpty()) {
                    updatesMade = true;
                    ConfiguredObject<?> resolved = recovered.resolve();
                    if (!isNew) {
                        resolved.decryptSecrets();
                    }
                    resolvedObjects.put(resolved.getId(), resolved);
                } else {
                    recordsWithUnresolvedDependencies.add(recovered);
                }
            }
        }
        Iterator<UnresolvedConfiguredObject<? extends ConfiguredObject>> unresolvedIter = recordsWithUnresolvedDependencies.iterator();
        while (unresolvedIter.hasNext()) {
            UnresolvedConfiguredObject<? extends ConfiguredObject> unresolvedObject = unresolvedIter.next();
            Collection<ConfiguredObjectDependency<?>> dependencies = new ArrayList<ConfiguredObjectDependency<?>>(unresolvedObject.getUnresolvedDependencies());
            for (ConfiguredObjectDependency dependency : dependencies) {
                if (dependency instanceof ConfiguredObjectIdDependency) {
                    UUID id = ((ConfiguredObjectIdDependency) dependency).getId();
                    if (resolvedObjects.containsKey(id)) {
                        dependency.resolve(resolvedObjects.get(id));
                    }
                } else if (dependency instanceof ConfiguredObjectNameDependency) {
                    ConfiguredObject<?> dependentObject = null;
                    ConfiguredObject<?> parent = unresolvedObject.getParent();
                    dependentObject = parent.findConfiguredObject(dependency.getCategoryClass(), ((ConfiguredObjectNameDependency) dependency).getName());
                    if (dependentObject != null) {
                        dependency.resolve(dependentObject);
                    }
                } else {
                    throw new ServerScopedRuntimeException("Unknown dependency type " + dependency.getClass().getSimpleName());
                }
            }
            if (unresolvedObject.getUnresolvedDependencies().isEmpty()) {
                updatesMade = true;
                unresolvedIter.remove();
                ConfiguredObject<?> resolved = unresolvedObject.resolve();
                resolvedObjects.put(resolved.getId(), resolved);
            }
        }
    } while (updatesMade && !(recordsWithUnresolvedDependencies.isEmpty() && recordsWithUnresolvedParents.isEmpty()));
    if (!recordsWithUnresolvedDependencies.isEmpty()) {
        throw new IllegalArgumentException("Cannot resolve some objects: " + recordsWithUnresolvedDependencies);
    }
    if (!recordsWithUnresolvedParents.isEmpty()) {
        throw new IllegalArgumentException("Cannot resolve object because their parents cannot be found" + recordsWithUnresolvedParents);
    }
}
Also used : ConfiguredObjectFactory(org.apache.qpid.server.model.ConfiguredObjectFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) UUID(java.util.UUID)

Example 35 with ConfiguredObject

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

the class StoreConfigurationChangeListenerTest method testStateChanged.

public void testStateChanged() {
    notifyBrokerStarted();
    UUID id = UUID.randomUUID();
    ConfiguredObject object = mock(VirtualHost.class);
    when(object.isDurable()).thenReturn(true);
    when(object.getId()).thenReturn(id);
    ConfiguredObjectRecord record = mock(ConfiguredObjectRecord.class);
    when(object.asObjectRecord()).thenReturn(record);
    _listener.stateChanged(object, State.ACTIVE, State.DELETED);
    verify(_store).remove(record);
}
Also used : ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord) UUID(java.util.UUID)

Aggregations

ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)91 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)28 List (java.util.List)23 AbstractConfiguredObject (org.apache.qpid.server.model.AbstractConfiguredObject)22 LinkedHashMap (java.util.LinkedHashMap)20 UUID (java.util.UUID)20 Map (java.util.Map)18 AbstractConfigurationChangeListener (org.apache.qpid.server.model.AbstractConfigurationChangeListener)12 ManagedObject (org.apache.qpid.server.model.ManagedObject)11 Date (java.util.Date)7 TreeMap (java.util.TreeMap)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 InternalMessageHeader (org.apache.qpid.server.message.internal.InternalMessageHeader)6 State (org.apache.qpid.server.model.State)6 Collection (java.util.Collection)5 ConfiguredObjectFinder (org.apache.qpid.server.model.ConfiguredObjectFinder)5 Model (org.apache.qpid.server.model.Model)5 Preference (org.apache.qpid.server.model.preferences.Preference)5 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)4