Search in sources :

Example 1 with ContainerType

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();
}
Also used : QpidServiceLoader(org.apache.qpid.server.plugin.QpidServiceLoader) HashMap(java.util.HashMap) ContainerType(org.apache.qpid.server.model.ContainerType) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DynamicModel(org.apache.qpid.server.model.DynamicModel)

Example 2 with ContainerType

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);
    }
}
Also used : QpidServiceLoader(org.apache.qpid.server.plugin.QpidServiceLoader) ContainerType(org.apache.qpid.server.model.ContainerType) IOException(java.io.IOException) FileReader(java.io.FileReader) UUID(java.util.UUID) File(java.io.File) DynamicModel(org.apache.qpid.server.model.DynamicModel)

Aggregations

UUID (java.util.UUID)2 ContainerType (org.apache.qpid.server.model.ContainerType)2 DynamicModel (org.apache.qpid.server.model.DynamicModel)2 QpidServiceLoader (org.apache.qpid.server.plugin.QpidServiceLoader)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)1