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);
}
}
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);
}
}
}
}
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());
}
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);
}
}
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);
}
Aggregations