use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class AbstractJDBCMessageStore method getStorableMessageMetaData.
private StorableMessageMetaData getStorableMessageMetaData(final long messageId, final InputStream stream) throws SQLException {
try {
int typeOrdinal = stream.read() & 0xff;
MessageMetaDataType type = MessageMetaDataTypeRegistry.fromOrdinal(typeOrdinal);
try (QpidByteBuffer buf = QpidByteBuffer.asQpidByteBuffer(stream)) {
return type.createMetaData(buf);
}
} catch (IOException | RuntimeException e) {
throw new StoreException("Failed to stream metadata for message with id " + messageId, e);
}
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class AbstractJDBCPreferenceStore method openAndLoad.
@Override
public Collection<PreferenceRecord> openAndLoad(final PreferenceStoreUpdater updater) throws StoreException {
if (!_storeState.compareAndSet(StoreState.CLOSED, StoreState.OPENING)) {
throw new IllegalStateException(String.format("PreferenceStore cannot be opened when in state '%s'", _storeState.get()));
}
try {
_storeState.set(StoreState.OPENED);
Collection<PreferenceRecord> records;
try (Connection connection = getConnection()) {
createVersionTable(connection);
createPreferencesTable(connection);
ModelVersion preferencesVersion = getPreferencesVersion(connection);
ModelVersion brokerModelVersion = ModelVersion.fromString(BrokerModel.MODEL_VERSION);
if (brokerModelVersion.lessThan(preferencesVersion)) {
throw new StoreException(String.format("Cannot downgrade preference store from '%s' to '%s'", preferencesVersion, brokerModelVersion));
}
records = getPreferenceRecords(connection);
if (preferencesVersion.lessThan(brokerModelVersion)) {
final Collection<UUID> ids = new HashSet<>();
for (PreferenceRecord record : records) {
ids.add(record.getId());
}
records = updater.updatePreferences(preferencesVersion.toString(), records);
removeAndAdd(ids, records, transactedConnection -> updateVersion(transactedConnection, brokerModelVersion.toString()));
}
}
return records;
} catch (SQLException e) {
_storeState.set(StoreState.ERRORED);
close();
throw new StoreException(e);
}
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class AbstractJDBCPreferenceStore method getPreferenceRecords.
private Collection<PreferenceRecord> getPreferenceRecords(final Connection connection) throws SQLException {
Collection<PreferenceRecord> records = new LinkedHashSet<>();
final ObjectMapper objectMapper = new ObjectMapper();
try (PreparedStatement stmt = connection.prepareStatement(String.format(SELECT_FROM_PREFERENCES, getPreferencesTableName()))) {
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
String id = rs.getString(1);
String attributes = getBlobAsString(rs, 2);
final PreferenceRecord preferenceRecord = new PreferenceRecordImpl(UUID.fromString(id), objectMapper.readValue(attributes, Map.class));
records.add(preferenceRecord);
}
} catch (IOException e) {
throw new StoreException("Error recovering persistent state: " + e.getMessage(), e);
}
}
return records;
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class JsonFilePreferenceStore method openAndLoad.
@Override
public synchronized Collection<PreferenceRecord> openAndLoad(final PreferenceStoreUpdater updater) throws StoreException {
if (_storeState != StoreState.CLOSED) {
throw new IllegalStateException(String.format("PreferenceStore cannot be opened when in state '%s'", _storeState));
}
try {
setup(DEFAULT_FILE_NAME, _storePath, _posixFilePermissions, Collections.singletonMap("version", BrokerModel.MODEL_VERSION));
StoreContent storeContent;
try {
storeContent = _objectMapper.readValue(getConfigFile(), StoreContent.class);
} catch (IOException e) {
throw new StoreException("Failed to read preferences from store", e);
}
ModelVersion storedVersion = ModelVersion.fromString(storeContent.getVersion());
ModelVersion currentVersion = new ModelVersion(BrokerModel.MODEL_MAJOR_VERSION, BrokerModel.MODEL_MINOR_VERSION);
if (currentVersion.lessThan(storedVersion)) {
throw new IllegalStateException(String.format("Cannot downgrade preference store storedVersion from '%s' to '%s'", currentVersion.toString(), BrokerModel.MODEL_VERSION));
}
Collection<PreferenceRecord> records = Arrays.<PreferenceRecord>asList(storeContent.getPreferences());
if (storedVersion.lessThan(currentVersion)) {
records = updater.updatePreferences(storedVersion.toString(), records);
storeContent.setVersion(BrokerModel.MODEL_VERSION);
storeContent.setPreferences(records.toArray(new StoredPreferenceRecord[records.size()]));
save(storeContent);
}
for (StoredPreferenceRecord preferenceRecord : storeContent.getPreferences()) {
_recordMap.put(preferenceRecord.getId(), preferenceRecord);
}
_storeState = StoreState.OPENED;
return records;
} catch (Exception e) {
_storeState = StoreState.ERRORED;
close();
throw e;
}
}
use of org.apache.qpid.server.store.StoreException in project qpid-broker-j by apache.
the class AbstractBDBMessageStore method removeXid.
private void removeXid(Transaction txn, long format, byte[] globalId, byte[] branchId) throws StoreException {
DatabaseEntry key = new DatabaseEntry();
Xid xid = new Xid(format, globalId, branchId);
XidBinding keyBinding = XidBinding.getInstance();
keyBinding.objectToEntry(xid, key);
try {
OperationStatus status = getXidDb().delete(txn, key);
if (status == OperationStatus.NOTFOUND) {
throw new StoreException("Unable to find xid");
} else if (status != OperationStatus.SUCCESS) {
throw new StoreException("Unable to remove xid");
}
} catch (RuntimeException e) {
getLogger().error("Failed to remove xid in transaction " + txn, e);
throw getEnvironmentFacade().handleDatabaseException("Error accessing database while removing xid: " + e.getMessage(), e);
}
}
Aggregations