Search in sources :

Example 11 with EntityStoreException

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) {
            }
        }
    }
}
Also used : Blob(org.jclouds.blobstore.domain.Blob) InputStream(java.io.InputStream) StringReader(java.io.StringReader) Payload(org.jclouds.io.Payload) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Example 12 with EntityStoreException

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);
}
Also used : BlobStoreContext(org.jclouds.blobstore.BlobStoreContext) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) BlobStore(org.jclouds.blobstore.BlobStore)

Example 13 with EntityStoreException

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);
    }
}
Also used : StringReader(java.io.StringReader) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) IOException(java.io.IOException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) ReadLock(org.qi4j.library.locking.ReadLock)

Example 14 with EntityStoreException

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);
        }
    }
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) StringWriter(java.io.StringWriter) EntityReference(org.qi4j.api.entity.EntityReference) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) WriteLock(org.qi4j.library.locking.WriteLock)

Example 15 with EntityStoreException

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);
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) SQLVendor(org.sql.generation.api.vendor.SQLVendor) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException)

Aggregations

EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)35 JSONException (org.json.JSONException)16 JSONObject (org.json.JSONObject)13 EntityReference (org.qi4j.api.entity.EntityReference)9 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)9 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)8 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)6 Map (java.util.Map)6 JSONArray (org.json.JSONArray)6 QualifiedName (org.qi4j.api.common.QualifiedName)6 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)5 DefaultEntityState (org.qi4j.spi.entitystore.helpers.DefaultEntityState)5 StringReader (java.io.StringReader)4 Connection (java.sql.Connection)4 Module (org.qi4j.api.structure.Module)4