Search in sources :

Example 31 with ConfiguredObjectRecord

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

the class TestBrokerConfiguration method addObjectConfiguration.

public UUID addObjectConfiguration(final Class<? extends ConfiguredObject> parentCategory, final String parentName, Class<? extends ConfiguredObject> type, Map<String, Object> attributes) {
    UUID id = UUIDGenerator.generateRandomUUID();
    ConfiguredObjectRecord entry = new ConfiguredObjectRecordImpl(id, type.getSimpleName(), attributes, Collections.singletonMap(parentCategory.getSimpleName(), findObject(parentCategory, parentName).getId()));
    _store.update(true, entry);
    return id;
}
Also used : ConfiguredObjectRecordImpl(org.apache.qpid.server.store.ConfiguredObjectRecordImpl) ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord) UUID(java.util.UUID)

Example 32 with ConfiguredObjectRecord

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

the class AbstractJDBCConfigurationStore method upgradeFromV7.

private void upgradeFromV7(ConfiguredObject<?> parent) throws SQLException {
    @SuppressWarnings("serial") Map<String, String> defaultExchanges = new HashMap<String, String>() {

        {
            put("amq.direct", "direct");
            put("amq.topic", "topic");
            put("amq.fanout", "fanout");
            put("amq.match", "headers");
        }
    };
    Connection connection = newConnection();
    try {
        String virtualHostName = parent.getName();
        UUID virtualHostId = UUIDGenerator.generateVhostUUID(virtualHostName);
        String stringifiedConfigVersion = "0." + DEFAULT_CONFIG_VERSION;
        boolean tableExists = tableExists(getConfigurationVersionTableName(), connection);
        if (tableExists) {
            int configVersion = getConfigVersion(connection);
            getLogger().debug("Upgrader read existing config version {}", configVersion);
            stringifiedConfigVersion = "0." + configVersion;
        }
        Map<String, Object> virtualHostAttributes = new HashMap<String, Object>();
        virtualHostAttributes.put("modelVersion", stringifiedConfigVersion);
        virtualHostAttributes.put("name", virtualHostName);
        ConfiguredObjectRecord virtualHostRecord = new ConfiguredObjectRecordImpl(virtualHostId, "VirtualHost", virtualHostAttributes);
        insertConfiguredObject(virtualHostRecord, connection);
        getLogger().debug("Upgrader created VirtualHost configuration entry with config version {}", stringifiedConfigVersion);
        Map<UUID, Map<String, Object>> bindingsToUpdate = new HashMap<UUID, Map<String, Object>>();
        List<UUID> others = new ArrayList<UUID>();
        final ObjectMapper objectMapper = ConfiguredObjectJacksonModule.newObjectMapper(true);
        PreparedStatement stmt = connection.prepareStatement("SELECT id, object_type, attributes FROM " + getConfiguredObjectsTableName());
        try {
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    UUID id = UUID.fromString(rs.getString(1));
                    String objectType = rs.getString(2);
                    if ("VirtualHost".equals(objectType)) {
                        continue;
                    }
                    Map<String, Object> attributes = objectMapper.readValue(getBlobAsString(rs, 3), Map.class);
                    if (objectType.endsWith("Binding")) {
                        bindingsToUpdate.put(id, attributes);
                    } else {
                        if (objectType.equals("Exchange")) {
                            defaultExchanges.remove((String) attributes.get("name"));
                        }
                        others.add(id);
                    }
                }
            } catch (IOException e) {
                throw new StoreException("Error recovering persistent state: " + e.getMessage(), e);
            }
        } finally {
            stmt.close();
        }
        stmt = connection.prepareStatement("INSERT INTO " + getConfiguredObjectHierarchyTableName() + " ( child_id, parent_type, parent_id) VALUES (?,?,?)");
        try {
            for (UUID id : others) {
                stmt.setString(1, id.toString());
                stmt.setString(2, "VirtualHost");
                stmt.setString(3, virtualHostId.toString());
                stmt.execute();
            }
            for (Map.Entry<UUID, Map<String, Object>> bindingEntry : bindingsToUpdate.entrySet()) {
                stmt.setString(1, bindingEntry.getKey().toString());
                stmt.setString(2, "Queue");
                stmt.setString(3, bindingEntry.getValue().remove("queue").toString());
                stmt.execute();
                stmt.setString(1, bindingEntry.getKey().toString());
                stmt.setString(2, "Exchange");
                stmt.setString(3, bindingEntry.getValue().remove("exchange").toString());
                stmt.execute();
            }
        } finally {
            stmt.close();
        }
        for (Map.Entry<String, String> defaultExchangeEntry : defaultExchanges.entrySet()) {
            UUID id = UUIDGenerator.generateExchangeUUID(defaultExchangeEntry.getKey(), virtualHostName);
            Map<String, Object> exchangeAttributes = new HashMap<String, Object>();
            exchangeAttributes.put("name", defaultExchangeEntry.getKey());
            exchangeAttributes.put("type", defaultExchangeEntry.getValue());
            exchangeAttributes.put("lifetimePolicy", "PERMANENT");
            Map<String, UUID> parents = Collections.singletonMap("VirtualHost", virtualHostRecord.getId());
            ConfiguredObjectRecord exchangeRecord = new org.apache.qpid.server.store.ConfiguredObjectRecordImpl(id, "Exchange", exchangeAttributes, parents);
            insertConfiguredObject(exchangeRecord, connection);
        }
        stmt = connection.prepareStatement("UPDATE " + getConfiguredObjectsTableName() + " set object_type =?, attributes = ? where id = ?");
        try {
            for (Map.Entry<UUID, Map<String, Object>> bindingEntry : bindingsToUpdate.entrySet()) {
                stmt.setString(1, "Binding");
                byte[] attributesAsBytes = objectMapper.writeValueAsBytes(bindingEntry.getValue());
                ByteArrayInputStream bis = new ByteArrayInputStream(attributesAsBytes);
                stmt.setBinaryStream(2, bis, attributesAsBytes.length);
                stmt.setString(3, bindingEntry.getKey().toString());
                stmt.execute();
            }
        } catch (IOException e) {
            throw new StoreException("Error recovering persistent state: " + e.getMessage(), e);
        } finally {
            stmt.close();
        }
        if (tableExists) {
            dropConfigVersionTable(connection);
        }
        connection.commit();
    } catch (SQLException e) {
        try {
            connection.rollback();
        } catch (SQLException re) {
        }
        throw e;
    } finally {
        connection.close();
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) UUID(java.util.UUID) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) StoreException(org.apache.qpid.server.store.StoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 33 with ConfiguredObjectRecord

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

the class ManagementModeStoreHandler method quiesceEntries.

private Map<UUID, Object> quiesceEntries(final SystemConfig<?> options, List<ConfiguredObjectRecord> records) {
    final Map<UUID, Object> quiescedEntries = new HashMap<UUID, Object>();
    final int managementModeHttpPortOverride = options.getManagementModeHttpPortOverride();
    for (ConfiguredObjectRecord entry : records) {
        String entryType = entry.getType();
        Map<String, Object> attributes = entry.getAttributes();
        boolean quiesce = false;
        if (VIRTUAL_HOST_TYPE.equals(entryType) && options.isManagementModeQuiesceVirtualHosts()) {
            quiesce = true;
        } else if (PORT_TYPE.equals(entryType)) {
            if (attributes == null) {
                throw new IllegalConfigurationException("Port attributes are not set in " + entry);
            }
            Set<Protocol> protocols = getPortProtocolsAttribute(attributes);
            if (protocols == null) {
                quiesce = true;
            } else {
                for (Protocol protocol : protocols) {
                    switch(protocol) {
                        case HTTP:
                            quiesce = managementModeHttpPortOverride > 0;
                            break;
                        default:
                            quiesce = true;
                    }
                }
            }
        }
        if (quiesce) {
            LOGGER.debug("Management mode quiescing entry {}", entry);
            // save original state
            quiescedEntries.put(entry.getId(), attributes.get(ATTRIBUTE_STATE));
        }
    }
    return quiescedEntries;
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord) UUID(java.util.UUID) Protocol(org.apache.qpid.server.model.Protocol)

Example 34 with ConfiguredObjectRecord

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

the class ManagementModeStoreHandler method createPortsFromCommandLineOptions.

private Map<UUID, ConfiguredObjectRecord> createPortsFromCommandLineOptions(SystemConfig<?> options) {
    int managementModeHttpPortOverride = options.getManagementModeHttpPortOverride();
    if (managementModeHttpPortOverride < 0) {
        throw new IllegalConfigurationException("Invalid http port is specified: " + managementModeHttpPortOverride);
    }
    Map<UUID, ConfiguredObjectRecord> cliEntries = new HashMap<UUID, ConfiguredObjectRecord>();
    if (managementModeHttpPortOverride != 0) {
        ConfiguredObjectRecord entry = createCLIPortEntry(managementModeHttpPortOverride, Protocol.HTTP);
        cliEntries.put(entry.getId(), entry);
    }
    return cliEntries;
}
Also used : HashMap(java.util.HashMap) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord) UUID(java.util.UUID)

Example 35 with ConfiguredObjectRecord

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

the class ManagementModeStoreHandler method recoverRecords.

public void recoverRecords(final List<ConfiguredObjectRecord> records) {
    boolean b = _systemConfig.getManagementModeHttpPortOverride() > 0;
    for (ConfiguredObjectRecord object : records) {
        String entryType = object.getType();
        Map<String, Object> attributes = object.getAttributes();
        boolean quiesce = false;
        if (VIRTUAL_HOST_TYPE.equals(entryType) && _systemConfig.isManagementModeQuiesceVirtualHosts()) {
            quiesce = true;
        } else if (PORT_TYPE.equals(entryType)) {
            if (attributes == null) {
                throw new IllegalConfigurationException("Port attributes are not set in " + object);
            }
            Set<Protocol> protocols = getPortProtocolsAttribute(attributes);
            if (protocols == null) {
                quiesce = true;
            } else {
                for (Protocol protocol : protocols) {
                    switch(protocol) {
                        case HTTP:
                            quiesce = b;
                            break;
                        default:
                            quiesce = true;
                    }
                }
            }
        }
        if (quiesce) {
            LOGGER.debug("Management mode quiescing entry {}", object);
            // save original state
            _quiescedEntriesOriginalState.put(object.getId(), attributes.get(ATTRIBUTE_STATE));
            Map<String, Object> modifiedAttributes = new HashMap<String, Object>(attributes);
            modifiedAttributes.put(ATTRIBUTE_STATE, State.QUIESCED);
            ConfiguredObjectRecord record = new ConfiguredObjectRecordImpl(object.getId(), object.getType(), modifiedAttributes, object.getParents());
            _records.put(record.getId(), record);
        } else {
            _records.put(object.getId(), object);
        }
    }
}
Also used : ConfiguredObjectRecordImpl(org.apache.qpid.server.store.ConfiguredObjectRecordImpl) Set(java.util.Set) HashMap(java.util.HashMap) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObjectRecord(org.apache.qpid.server.store.ConfiguredObjectRecord) Protocol(org.apache.qpid.server.model.Protocol)

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