Search in sources :

Example 1 with MdsException

use of org.motechproject.mds.exception.MdsException in project motech by motech.

the class EntitiesBundleMonitor method start.

/**
 * Starts or updates the entities bundle from the given {@code File}.
 *
 * @param src         file that points to an entities bundle jar
 * @param startBundle {@code true} if the generated bundle should start;
 *                    otherwise {@code false}.
 * @see org.motechproject.mds.service.JarGeneratorService
 */
public void start(File src, boolean startBundle) {
    LOGGER.debug("Starting bundle from: {}", src);
    try (InputStream stream = new FileInputStream(src)) {
        Bundle entitiesBundle = getEntitiesBundle();
        if (entitiesBundle == null) {
            LOGGER.info("Entities bundle does not exist");
            install(stream);
        } else if (Bundle.INSTALLED == entitiesBundle.getState()) {
            LOGGER.info("Entities bundle exists and it is installed");
            update(stream);
        } else {
            LOGGER.info("Entities bundle exists and it is resolved");
            stopEntitiesBundle();
            update(stream);
        }
        if (startBundle) {
            start();
        }
    } catch (IOException e) {
        throw new MdsException("Unable to read temporary entities bundle", e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Bundle(org.osgi.framework.Bundle) MdsException(org.motechproject.mds.exception.MdsException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 2 with MdsException

use of org.motechproject.mds.exception.MdsException in project motech by motech.

the class EntitiesBundleMonitor method install.

private void install(InputStream stream) {
    LOGGER.info("Installing the entities bundle");
    try {
        bundleContext.installBundle(bundleLocation(), stream);
    } catch (BundleException e) {
        throw new MdsException("Unable to install the entities bundle", e);
    }
    waitUntil(new Condition() {

        @Override
        public boolean await() {
            return !bundleInstalled;
        }
    }, "installed");
    LOGGER.info("Installed the entities bundle");
}
Also used : MdsException(org.motechproject.mds.exception.MdsException) BundleException(org.osgi.framework.BundleException)

Example 3 with MdsException

use of org.motechproject.mds.exception.MdsException in project motech by motech.

the class StateManagerUtil method setTransactionVersion.

/**
 * Sets the given transaction version to the instance state manager.
 *
 * @param instance the instance from which state manager will be retrieved
 * @param version the transaction version
 * @param versionFieldName the name of the version field
 */
public static void setTransactionVersion(Object instance, Object version, String versionFieldName) {
    try {
        StateManagerImpl stateManager = getStateManager(instance);
        stateManager.setVersion(version);
        AbstractClassMetaData cmd = stateManager.getClassMetaData();
        int fieldPosition = cmd.getAbsolutePositionOfMember(versionFieldName);
        boolean[] dirtyFields = getDirtyFields(stateManager);
        // we must mark version field as non dirty
        dirtyFields[fieldPosition] = false;
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new MdsException("Cannot set proper transaction version", e);
    }
}
Also used : MdsException(org.motechproject.mds.exception.MdsException) StateManagerImpl(org.datanucleus.state.StateManagerImpl) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 4 with MdsException

use of org.motechproject.mds.exception.MdsException in project motech by motech.

the class MdsBundleWatcher method processAnnotationScanningResults.

private void processAnnotationScanningResults(MDSProcessorOutput output) {
    Map<String, Long> entityIdMappings = new HashMap<>();
    Set<String> newEntities = new HashSet<>();
    Bundle bundle = output.getBundle();
    try {
        migrationService.processBundle(bundle);
    } catch (IOException e) {
        LOGGER.error("An error occurred while copying the migrations from bundle: {}", bundle.getSymbolicName(), e);
    }
    try {
        editableLookupsLoader.addEditableLookups(output, bundle);
    } catch (MdsException e) {
        LOGGER.error("Unable to read JSON defined lookups from bundle: {}", bundle, e);
    }
    for (EntityProcessorOutput result : output.getEntityProcessorOutputs()) {
        EntityDto processedEntity = result.getEntityProcessingResult();
        EntityDto entity = entityService.getEntityByClassName(processedEntity.getClassName());
        if (entity == null) {
            entity = entityService.createEntity(processedEntity);
            newEntities.add(entity.getClassName());
        }
        entityIdMappings.put(entity.getClassName(), entity.getId());
        entityService.updateRestOptions(entity.getId(), result.getRestProcessingResult());
        entityService.updateTracking(entity.getId(), result.getTrackingProcessingResult());
        entityService.addFields(entity, result.getFieldProcessingResult());
        entityService.addFilterableFields(entity, result.getUiFilterableProcessingResult());
        entityService.addDisplayedFields(entity, result.getUiDisplayableProcessingResult());
        entityService.updateSecurityOptions(entity.getId(), processedEntity.getSecurityMode(), processedEntity.getSecurityMembers(), processedEntity.getReadOnlySecurityMode(), processedEntity.getReadOnlySecurityMembers());
        entityService.updateMaxFetchDepth(entity.getId(), processedEntity.getMaxFetchDepth());
        entityService.addNonEditableFields(entity, result.getNonEditableProcessingResult());
    }
    for (Map.Entry<String, List<LookupDto>> entry : output.getLookupProcessorOutputs().entrySet()) {
        String entityClassName = entry.getKey();
        Long entityId = entityIdMappings.get(entityClassName);
        if (schemaComparator.lookupsDiffer(entityId, entry.getValue())) {
            entityService.addLookups(entityId, entry.getValue());
            if (!newEntities.contains(entityClassName)) {
                entityService.incrementVersion(entityId);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) EntityDto(org.motechproject.mds.dto.EntityDto) EntityProcessorOutput(org.motechproject.mds.annotations.internal.EntityProcessorOutput) MdsException(org.motechproject.mds.exception.MdsException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 5 with MdsException

use of org.motechproject.mds.exception.MdsException in project motech by motech.

the class EntitiesBundleMonitor method start.

/**
 * Starts the entities bundle and waits until it will be started and its context will be
 * initialized.
 */
public void start() {
    LOGGER.info("Starting the entities bundle");
    try {
        Bundle entitiesBundle = getEntitiesBundle();
        if (entitiesBundle != null && entitiesBundle.getState() != Bundle.STARTING && entitiesBundle.getState() != Bundle.ACTIVE) {
            entitiesBundle.start();
        } else {
            LOGGER.warn("No entities bundle to start");
            return;
        }
    } catch (BundleException e) {
        if (e.getType() == BundleException.RESOLVE_ERROR) {
            throw new MdsEntityWireException(e);
        } else {
            throw new MdsException("Unable to start the entities bundle", e);
        }
    }
    waitUntil(new Condition() {

        @Override
        public boolean await() {
            return !bundleStarted;
        }
    }, "started");
    LOGGER.info("Started the entities bundle");
    waitForEntitiesContext();
}
Also used : Bundle(org.osgi.framework.Bundle) MdsEntityWireException(org.motechproject.mds.exception.init.MdsEntityWireException) MdsException(org.motechproject.mds.exception.MdsException) BundleException(org.osgi.framework.BundleException)

Aggregations

MdsException (org.motechproject.mds.exception.MdsException)10 Bundle (org.osgi.framework.Bundle)6 BundleException (org.osgi.framework.BundleException)5 IOException (java.io.IOException)3 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Extension (javax.jdo.annotations.Extension)1 Version (javax.jdo.annotations.Version)1 VersionMetadata (javax.jdo.metadata.VersionMetadata)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 StateManagerImpl (org.datanucleus.state.StateManagerImpl)1 EntityProcessorOutput (org.motechproject.mds.annotations.internal.EntityProcessorOutput)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 MdsEntityWireException (org.motechproject.mds.exception.init.MdsEntityWireException)1