use of org.apache.qpid.server.model.ContainerType in project qpid-broker-j by apache.
the class ConfiguredObjectRecordConverter method readFromJson.
public Collection<ConfiguredObjectRecord> readFromJson(Class<? extends ConfiguredObject> rootClass, ConfiguredObject<?> parent, Reader reader) throws IOException {
Map<UUID, ConfiguredObjectRecord> objectsById = new HashMap<>();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
Map data = objectMapper.readValue(reader, Map.class);
if (!data.isEmpty()) {
if (rootClass == null && parent instanceof DynamicModel) {
String defaultContainerType = ((DynamicModel) parent).getDefaultContainerType();
String containerTypeName = defaultContainerType;
if (data.get(ConfiguredObject.TYPE) instanceof String) {
containerTypeName = data.get(ConfiguredObject.TYPE).toString();
}
QpidServiceLoader loader = new QpidServiceLoader();
Map<String, ContainerType> instancesByType = loader.getInstancesByType(ContainerType.class);
final ContainerType<?> containerType = instancesByType.get(containerTypeName);
if (containerType != null) {
_model = containerType.getModel();
rootClass = containerType.getCategoryClass();
} else {
// fall back to default container type
final ContainerType<?> defaultContainerTypeInstance = instancesByType.get(defaultContainerType);
if (defaultContainerTypeInstance != null) {
_model = defaultContainerTypeInstance.getModel();
rootClass = defaultContainerTypeInstance.getCategoryClass();
} else {
throw new IllegalConfigurationException(String.format("Cannot identify container type for '%s'", containerType));
}
}
}
Collection<NameToIdResolver> unresolved = loadChild(rootClass, data, parent.getCategoryClass(), parent.getId(), objectsById);
_rootClass = rootClass;
Iterator<NameToIdResolver> iterator = unresolved.iterator();
while (iterator.hasNext()) {
if (iterator.next().resolve(objectsById)) {
iterator.remove();
}
}
if (!unresolved.isEmpty()) {
throw new IllegalArgumentException("Initial configuration has unresolved references");
}
}
return objectsById.values();
}
use of org.apache.qpid.server.model.ContainerType in project qpid-broker-j by apache.
the class JsonFileConfigStore method load.
protected boolean load(final ConfiguredObjectRecord... initialRecords) {
final File configFile = getConfigFile();
try {
LOGGER.debug("Loading file {}", configFile.getCanonicalPath());
boolean updated = false;
Collection<ConfiguredObjectRecord> records = Collections.emptyList();
ConfiguredObjectRecordConverter configuredObjectRecordConverter = new ConfiguredObjectRecordConverter(_parent.getModel());
records = configuredObjectRecordConverter.readFromJson(_rootClass, _parent, new FileReader(configFile));
if (_rootClass == null) {
_rootClass = configuredObjectRecordConverter.getRootClass();
_classNameMapping = generateClassNameMap(configuredObjectRecordConverter.getModel(), _rootClass);
}
if (records.isEmpty()) {
LOGGER.debug("File contains no records - using initial configuration");
records = Arrays.asList(initialRecords);
updated = true;
if (_rootClass == null) {
String containerTypeName = ((DynamicModel) _parent).getDefaultContainerType();
ConfiguredObjectRecord rootRecord = null;
for (ConfiguredObjectRecord record : records) {
if (record.getParents() == null || record.getParents().isEmpty()) {
rootRecord = record;
break;
}
}
if (rootRecord != null && rootRecord.getAttributes().get(ConfiguredObject.TYPE) instanceof String) {
containerTypeName = rootRecord.getAttributes().get(ConfiguredObject.TYPE).toString();
}
QpidServiceLoader loader = new QpidServiceLoader();
final ContainerType<?> containerType = loader.getInstancesByType(ContainerType.class).get(containerTypeName);
if (containerType != null) {
_rootClass = containerType.getCategoryClass();
_classNameMapping = generateClassNameMap(containerType.getModel(), containerType.getCategoryClass());
}
}
}
for (ConfiguredObjectRecord record : records) {
LOGGER.debug("Loading record (Category: {} \t Name: {} \t ID: {}", record.getType(), record.getAttributes().get("name"), record.getId());
_objectsById.put(record.getId(), record);
List<UUID> idsForType = _idsByType.get(record.getType());
if (idsForType == null) {
idsForType = new ArrayList<>();
_idsByType.put(record.getType(), idsForType);
}
if (idsForType.contains(record.getId())) {
throw new IllegalArgumentException("Duplicate id for record " + record);
}
idsForType.add(record.getId());
}
if (updated) {
save();
}
return updated;
} catch (IOException e) {
throw new StoreException("Cannot construct configuration from the configuration file " + configFile, e);
}
}
Aggregations