Search in sources :

Example 36 with ConfiguredObjectRecord

use of org.apache.qpid.server.store.ConfiguredObjectRecord in project qpid-broker-j by apache.

the class ManagementModeStoreHandler method createCLIPortEntry.

private ConfiguredObjectRecord createCLIPortEntry(int port, Protocol protocol) {
    ConfiguredObjectRecord parent = findBroker();
    Map<String, Object> attributes = new HashMap<String, Object>();
    attributes.put(Port.PORT, port);
    attributes.put(Port.PROTOCOLS, Collections.singleton(protocol));
    attributes.put(Port.NAME, MANAGEMENT_MODE_PORT_PREFIX + protocol.name());
    attributes.put(Port.AUTHENTICATION_PROVIDER, BrokerImpl.MANAGEMENT_MODE_AUTHENTICATION);
    ConfiguredObjectRecord portEntry = new ConfiguredObjectRecordImpl(UUID.randomUUID(), PORT_TYPE, attributes, Collections.singletonMap(parent.getType(), parent.getId()));
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Add management mode port configuration " + portEntry + " for port " + port + " and protocol " + protocol);
    }
    return portEntry;
}
Also used : ConfiguredObjectRecordImpl(org.apache.qpid.server.store.ConfiguredObjectRecordImpl) HashMap(java.util.HashMap) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord)

Example 37 with ConfiguredObjectRecord

use of org.apache.qpid.server.store.ConfiguredObjectRecord in project qpid-broker-j by apache.

the class BDBConfigurationStore method doVisitAllConfiguredObjectRecords.

private Collection<? extends ConfiguredObjectRecord> doVisitAllConfiguredObjectRecords() {
    Map<UUID, BDBConfiguredObjectRecord> configuredObjects = new HashMap<>();
    try (Cursor objectsCursor = getConfiguredObjectsDb().openCursor(null, null)) {
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry value = new DatabaseEntry();
        while (objectsCursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS) {
            UUID id = UUIDTupleBinding.getInstance().entryToObject(key);
            BDBConfiguredObjectRecord configuredObject = (BDBConfiguredObjectRecord) new ConfiguredObjectBinding(id).entryToObject(value);
            configuredObjects.put(configuredObject.getId(), configuredObject);
        }
        // set parents
        try (Cursor hierarchyCursor = getConfiguredObjectHierarchyDb().openCursor(null, null)) {
            while (hierarchyCursor.getNext(key, value, LockMode.READ_UNCOMMITTED) == OperationStatus.SUCCESS) {
                HierarchyKey hk = HierarchyKeyBinding.getInstance().entryToObject(key);
                UUID parentId = UUIDTupleBinding.getInstance().entryToObject(value);
                BDBConfiguredObjectRecord child = configuredObjects.get(hk.getChildId());
                if (child != null) {
                    ConfiguredObjectRecord parent = configuredObjects.get(parentId);
                    if (parent != null) {
                        child.addParent(hk.getParentType(), parent);
                    }
                }
            }
        }
    }
    return configuredObjects.values();
}
Also used : ConfiguredObjectBinding(org.apache.qpid.server.store.berkeleydb.tuple.ConfiguredObjectBinding) HashMap(java.util.HashMap) HierarchyKey(org.apache.qpid.server.store.berkeleydb.entry.HierarchyKey) DatabaseEntry(com.sleepycat.je.DatabaseEntry) ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord) UUID(java.util.UUID) Cursor(com.sleepycat.je.Cursor)

Example 38 with ConfiguredObjectRecord

use of org.apache.qpid.server.store.ConfiguredObjectRecord in project qpid-broker-j by apache.

the class BDBConfigurationStore method openConfigurationStore.

@Override
public boolean openConfigurationStore(ConfiguredObjectRecordHandler handler, final ConfiguredObjectRecord... initialRecords) {
    changeState(CONFIGURED, OPEN);
    try {
        Collection<? extends ConfiguredObjectRecord> records = doVisitAllConfiguredObjectRecords();
        boolean empty = records.isEmpty();
        if (empty) {
            records = Arrays.asList(initialRecords);
            com.sleepycat.je.Transaction txn = null;
            try {
                txn = _environmentFacade.beginTransaction(null);
                for (ConfiguredObjectRecord record : records) {
                    update(true, record, txn);
                }
                txn.commit();
                txn = null;
            } catch (RuntimeException e) {
                throw _environmentFacade.handleDatabaseException("Error updating configuration details within the store: " + e, e);
            } finally {
                if (txn != null) {
                    abortTransactionSafely(txn, _environmentFacade);
                }
            }
        }
        for (ConfiguredObjectRecord record : records) {
            handler.handle(record);
        }
        return empty;
    } catch (RuntimeException e) {
        throw _environmentFacade.handleDatabaseException("Cannot visit configured object records", e);
    }
}
Also used : ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord) Transaction(com.sleepycat.je.Transaction)

Example 39 with ConfiguredObjectRecord

use of org.apache.qpid.server.store.ConfiguredObjectRecord in project qpid-broker-j by apache.

the class AbstractConfiguredObject method asObjectRecord.

@Override
public final ConfiguredObjectRecord asObjectRecord() {
    return new ConfiguredObjectRecord() {

        @Override
        public UUID getId() {
            return AbstractConfiguredObject.this.getId();
        }

        @Override
        public String getType() {
            return getCategoryClass().getSimpleName();
        }

        @Override
        public Map<String, Object> getAttributes() {
            return Subject.doAs(getSubjectWithAddedSystemRights(), new PrivilegedAction<Map<String, Object>>() {

                @Override
                public Map<String, Object> run() {
                    Map<String, Object> attributes = new LinkedHashMap<>();
                    Map<String, Object> actualAttributes = getActualAttributes();
                    for (ConfiguredObjectAttribute<?, ?> attr : _attributeTypes.values()) {
                        if (attr.isPersisted() && !ID.equals(attr.getName())) {
                            if (attr.isDerived()) {
                                Object value = getAttribute(attr.getName());
                                attributes.put(attr.getName(), toRecordedForm(attr, value));
                            } else if (actualAttributes.containsKey(attr.getName())) {
                                Object value = actualAttributes.get(attr.getName());
                                attributes.put(attr.getName(), toRecordedForm(attr, value));
                            }
                        }
                    }
                    return attributes;
                }
            });
        }

        public Object toRecordedForm(final ConfiguredObjectAttribute<?, ?> attr, Object value) {
            if (value instanceof ConfiguredObject) {
                value = ((ConfiguredObject) value).getId();
            }
            if (attr.isSecure() && _encrypter != null && value != null) {
                if (value instanceof Collection || value instanceof Map) {
                    ObjectMapper mapper = ConfiguredObjectJacksonModule.newObjectMapper(false);
                    try (StringWriter stringWriter = new StringWriter()) {
                        mapper.writeValue(stringWriter, value);
                        value = _encrypter.encrypt(stringWriter.toString());
                    } catch (IOException e) {
                        throw new IllegalConfigurationException("Failure when encrypting a secret value", e);
                    }
                } else {
                    value = _encrypter.encrypt(value.toString());
                }
            }
            return value;
        }

        @Override
        public Map<String, UUID> getParents() {
            Map<String, UUID> parents = new LinkedHashMap<>();
            Class<? extends ConfiguredObject> parentClass = getModel().getParentType(getCategoryClass());
            ConfiguredObject parent = (ConfiguredObject) getParent();
            if (parent != null) {
                parents.put(parentClass.getSimpleName(), parent.getId());
            }
            return parents;
        }

        @Override
        public String toString() {
            return AbstractConfiguredObject.this.getClass().getSimpleName() + "[name=" + getName() + ", categoryClass=" + getCategoryClass() + ", type=" + getType() + ", id=" + getId() + ", attributes=" + getAttributes() + "]";
        }
    };
}
Also used : IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 40 with ConfiguredObjectRecord

use of org.apache.qpid.server.store.ConfiguredObjectRecord in project qpid-broker-j by apache.

the class AbstractVirtualHost method importMessageStore.

@Override
public void importMessageStore(final String source) {
    try {
        final URL url = convertStringToURL(source);
        try (InputStream input = url.openStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(input);
            DataInputStream data = new DataInputStream(bufferedInputStream)) {
            final MessageStoreSerializer serializer = MessageStoreSerializer.FACTORY.newInstance(data);
            doSync(doOnConfigThread(new Task<ListenableFuture<Void>, IOException>() {

                @Override
                public ListenableFuture<Void> execute() throws IOException {
                    if (getState() != State.STOPPED) {
                        throw new IllegalArgumentException("The importMessageStore operation can only be called when the virtual host is stopped");
                    }
                    try {
                        _messageStore.openMessageStore(AbstractVirtualHost.this);
                        checkMessageStoreEmpty();
                        final Map<String, UUID> queueMap = new HashMap<>();
                        getDurableConfigurationStore().reload(new ConfiguredObjectRecordHandler() {

                            @Override
                            public void handle(final ConfiguredObjectRecord record) {
                                if (record.getType().equals(Queue.class.getSimpleName())) {
                                    queueMap.put((String) record.getAttributes().get(ConfiguredObject.NAME), record.getId());
                                }
                            }
                        });
                        serializer.deserialize(queueMap, _messageStore, data);
                    } finally {
                        _messageStore.closeMessageStore();
                    }
                    return Futures.immediateFuture(null);
                }

                @Override
                public String getObject() {
                    return AbstractVirtualHost.this.toString();
                }

                @Override
                public String getAction() {
                    return "importMessageStore";
                }

                @Override
                public String getArguments() {
                    if (url.getProtocol().equalsIgnoreCase("http") || url.getProtocol().equalsIgnoreCase("https") || url.getProtocol().equalsIgnoreCase("file")) {
                        return "source=" + source;
                    } else if (url.getProtocol().equalsIgnoreCase("data")) {
                        return "source=<data stream>";
                    } else {
                        return "source=<unknown source type>";
                    }
                }
            }));
        }
    } catch (IOException e) {
        throw new IllegalConfigurationException("Cannot convert '" + source + "' to a readable resource", e);
    }
}
Also used : Task(org.apache.qpid.server.configuration.updater.Task) StatisticsReportingTask(org.apache.qpid.server.stats.StatisticsReportingTask) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) URL(java.net.URL) BufferedInputStream(java.io.BufferedInputStream) ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord) MessageStoreSerializer(org.apache.qpid.server.store.serializer.MessageStoreSerializer) UUID(java.util.UUID) ConfiguredObjectRecordHandler(org.apache.qpid.server.store.handler.ConfiguredObjectRecordHandler)

Aggregations

ConfiguredObjectRecord (org.apache.qpid.server.store.ConfiguredObjectRecord)58 HashMap (java.util.HashMap)27 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)25 UUID (java.util.UUID)24 ConfiguredObjectRecordImpl (org.apache.qpid.server.store.ConfiguredObjectRecordImpl)14 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)13 ArrayList (java.util.ArrayList)12 ConfiguredObjectRecordHandler (org.apache.qpid.server.store.handler.ConfiguredObjectRecordHandler)8 LinkedHashMap (java.util.LinkedHashMap)6 IOException (java.io.IOException)5 Transaction (com.sleepycat.je.Transaction)4 Map (java.util.Map)4 DurableConfigurationStore (org.apache.qpid.server.store.DurableConfigurationStore)4 StoreException (org.apache.qpid.server.store.StoreException)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Answer (org.mockito.stubbing.Answer)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3