use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class JCloudsMapEntityStoreMixin method get.
@Override
public Reader get(EntityReference entityReference) throws EntityStoreException {
Blob blob = storeContext.getBlobStore().getBlob(container, entityReference.identity());
if (blob == null) {
throw new EntityNotFoundException(entityReference);
}
Payload payload = blob.getPayload();
if (payload == null) {
throw new EntityNotFoundException(entityReference);
}
InputStream input = null;
try {
input = payload.openStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Inputs.byteBuffer(input, 4096).transferTo(Outputs.byteBuffer(baos));
return new StringReader(baos.toString("UTF-8"));
} catch (IOException ex) {
throw new EntityStoreException("Unable to read entity state for: " + entityReference, ex);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException ignored) {
}
}
}
}
use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class JCloudsMapEntityStoreMixin method activateService.
@Override
public void activateService() throws Exception {
configuration.refresh();
String provider = configuration.get().provider().get();
String identifier = configuration.get().identifier().get();
String credentials = configuration.get().credential().get();
Map<String, String> properties = configuration.get().properties().get();
container = configuration.get().container().get();
if (provider != null) {
checkArgument(contains(allKeys, provider), "provider %s not in supported list: %s", provider, allKeys);
} else {
provider = "transient";
}
if (container == null) {
container = "qi4j-entities";
}
storeContext = ContextBuilder.newBuilder(provider).credentials(identifier, credentials).overrides(asProperties(properties)).buildView(BlobStoreContext.class);
BlobStore blobStore = storeContext.getBlobStore();
if (!blobStore.containerExists(container)) {
if (!blobStore.createContainerInLocation(null, container)) {
throw new EntityStoreException("Unable to create JClouds Blob Container, cannot continue.");
} else {
LOGGER.debug("Created new container: {}", container);
}
}
LOGGER.info("Activated using {} cloud provider [id:{}]", provider, identifier);
}
use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class JdbmEntityStoreMixin method get.
@ReadLock
@Override
public Reader get(EntityReference entityReference) throws EntityStoreException {
try {
Long stateIndex = getStateIndex(entityReference.identity());
if (stateIndex == null) {
throw new EntityNotFoundException(entityReference);
}
byte[] serializedState = (byte[]) recordManager.fetch(stateIndex, serializer);
if (serializedState == null) {
throw new EntityNotFoundException(entityReference);
}
return new StringReader(new String(serializedState, "UTF-8"));
} catch (IOException e) {
throw new EntityStoreException(e);
}
}
use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class JdbmEntityStoreMixin method applyChanges.
@WriteLock
@Override
public void applyChanges(MapChanges changes) throws IOException {
try {
changes.visitMap(new MapChanger() {
@Override
public Writer newEntity(final EntityReference ref, EntityDescriptor descriptor) throws IOException {
return new StringWriter(1000) {
@Override
public void close() throws IOException {
super.close();
byte[] stateArray = toString().getBytes("UTF-8");
long stateIndex = recordManager.insert(stateArray, serializer);
String indexKey = ref.toString();
index.insert(indexKey.getBytes("UTF-8"), stateIndex, false);
}
};
}
@Override
public Writer updateEntity(final EntityReference ref, EntityDescriptor descriptor) throws IOException {
return new StringWriter(1000) {
@Override
public void close() throws IOException {
super.close();
Long stateIndex = getStateIndex(ref.toString());
byte[] stateArray = toString().getBytes("UTF-8");
recordManager.update(stateIndex, stateArray, serializer);
}
};
}
@Override
public void removeEntity(EntityReference ref, EntityDescriptor descriptor) throws EntityNotFoundException {
try {
Long stateIndex = getStateIndex(ref.toString());
recordManager.delete(stateIndex);
index.remove(ref.toString().getBytes("UTF-8"));
} catch (IOException e) {
throw new EntityStoreException(e);
}
}
});
recordManager.commit();
} catch (Exception e) {
e.printStackTrace();
recordManager.rollback();
if (e instanceof IOException) {
throw (IOException) e;
} else if (e instanceof EntityStoreException) {
throw (EntityStoreException) e;
} else {
throw new IOException(e);
}
}
}
use of org.qi4j.spi.entitystore.EntityStoreException in project qi4j-sdk by Qi4j.
the class DatabaseSQLServiceCoreMixin method startDatabase.
@Override
public void startDatabase() throws Exception {
Connection connection = getConnection();
String schema = this.getConfiguredSchemaName(SQLs.DEFAULT_SCHEMA_NAME);
if (schema == null) {
throw new EntityStoreException("Schema name must not be null.");
} else {
state.schemaName().set(schema);
state.vendor().set(this.descriptor.metaInfo(SQLVendor.class));
sqlStrings.init();
if (!spi.schemaExists(connection)) {
LOGGER.debug("Database Schema '{}' NOT found!", schema);
Statement stmt = null;
try {
stmt = connection.createStatement();
for (String sql : sqlStrings.buildSQLForSchemaCreation()) {
stmt.execute(sql);
}
} finally {
SQLUtil.closeQuietly(stmt);
}
LOGGER.debug("Database Schema '{}' created", schema);
}
if (!spi.tableExists(connection)) {
Statement stmt = null;
try {
stmt = connection.createStatement();
for (String sql : sqlStrings.buildSQLForTableCreation()) {
stmt.execute(sql);
}
for (String sql : sqlStrings.buildSQLForIndexCreation()) {
stmt.execute(sql);
}
} finally {
SQLUtil.closeQuietly(stmt);
}
LOGGER.trace("Table {} created", SQLs.TABLE_NAME);
}
connection.setAutoCommit(false);
}
SQLUtil.closeQuietly(connection);
}
Aggregations