Search in sources :

Example 6 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class RestTestUtil method jsonMatcher.

/**
 * Returns a JSON matcher that will match the given response against the expected one.
 * The json files will be compared without respecting the order in which the variables appear in.
 * This matcher was written with Spring MVC Testing in mind, but can be potentially used in other cases.
 * @param expected the expected JSON output, as string
 * @return an instance of a matcher that will perform the match
 * @throws MotechException if we failed to parse the expected or actual string values into JSON
 */
public static Matcher<String> jsonMatcher(final String expected) {
    return new BaseMatcher<String>() {

        @Override
        public boolean matches(Object argument) {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                String actual = (String) argument;
                JsonNode expectedTree = objectMapper.readTree(expected);
                JsonNode actualTree = objectMapper.readTree(actual);
                return expectedTree.equals(actualTree);
            } catch (IOException e) {
                throw new MotechException("Json parsing failure", e);
            }
        }

        @Override
        public void describeTo(Description description) {
            description.appendText(expected);
        }
    };
}
Also used : MotechException(org.motechproject.commons.api.MotechException) Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) JsonNode(org.codehaus.jackson.JsonNode) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 7 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class ConfigurationServiceImpl method savePlatformSettings.

@Override
@Transactional
public void savePlatformSettings(Properties settings) {
    SettingsRecord dbSettings = getSettings();
    dbSettings.setPlatformInitialized(true);
    dbSettings.setLastRun(DateTime.now());
    dbSettings.updateFromProperties(settings);
    dbSettings.removeDefaults(defaultConfig);
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        dbSettings.setConfigFileChecksum(new String(digest.digest(MapUtils.toProperties(dbSettings.asProperties()).toString().getBytes())));
    } catch (NoSuchAlgorithmException e) {
        throw new MotechException("MD5 algorithm not available", e);
    }
    addOrUpdateSettings(dbSettings);
}
Also used : MotechException(org.motechproject.commons.api.MotechException) SettingsRecord(org.motechproject.config.domain.SettingsRecord) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class SettingsFacade method registerAllRawConfig.

/**
 * Registers all raw configurations to the configuration service.
 */
protected void registerAllRawConfig() {
    if (configurationService != null) {
        for (Map.Entry<String, Resource> entry : rawConfig.entrySet()) {
            String filename = entry.getKey();
            Resource resource = entry.getValue();
            if (!configurationService.rawConfigExists(getBundleSymbolicName(), filename)) {
                // register new config with the platform
                try {
                    InputStream is = resource.getInputStream();
                    configurationService.saveRawConfig(getBundleSymbolicName(), getBundleVersion(), filename, is);
                } catch (IOException e) {
                    throw new MotechException("Can't save raw config " + filename, e);
                }
            }
        }
        rawConfigRegistered = true;
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) InputStream(java.io.InputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class SettingsFacade method setConfigFiles.

public void setConfigFiles(List<Resource> resources) {
    for (Resource configFile : resources) {
        try (InputStream inputStream = configFile.getInputStream()) {
            Properties props = new Properties();
            props.load(inputStream);
            config.put(getResourceFileName(configFile), props);
            defaultConfig.put(getResourceFileName(configFile), props);
        } catch (IOException e) {
            throw new MotechException("Cant load config file " + configFile.getFilename(), e);
        }
    }
    registerAllProperties();
}
Also used : MotechException(org.motechproject.commons.api.MotechException) InputStream(java.io.InputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) Properties(java.util.Properties)

Example 10 with MotechException

use of org.motechproject.commons.api.MotechException in project motech by motech.

the class ModuleAdminServiceImpl method installWithDependenciesFromFile.

private BundleInformation installWithDependenciesFromFile(File bundleFile, boolean startBundle) throws IOException {
    JarInformation jarInformation = getJarInformations(bundleFile);
    List<Bundle> bundlesInstalled = new ArrayList<>();
    BundleInformation bundleInformation = null;
    if (jarInformation == null) {
        throw new IOException("Unable to read bundleFile " + bundleFile.getAbsolutePath());
    }
    try {
        List<ArtifactResult> artifactResults = new LinkedList<>();
        bundleWatcherSuspensionService.suspendBundleProcessing();
        for (Dependency dependency : jarInformation.getPomInformation().getDependencies()) {
            artifactResults.addAll(resolveDependencies(dependency, jarInformation.getPomInformation()));
        }
        artifactResults = removeDuplicatedArtifacts(artifactResults);
        bundlesInstalled = installBundlesFromArtifacts(artifactResults);
        final Bundle requestedModule = installBundleFromFile(bundleFile, true, false);
        if (requestedModule != null) {
            if (!isFragmentBundle(requestedModule) && startBundle) {
                requestedModule.start();
            }
            bundlesInstalled.add(requestedModule);
            bundleInformation = null;
        } else {
            bundleInformation = new BundleInformation(null);
        }
    } catch (BundleException | RepositoryException e) {
        throw new MotechException("Error while installing bundle and dependencies.", e);
    } finally {
        bundleWatcherSuspensionService.restoreBundleProcessing();
    }
    // start bundles after all bundles installed to avoid any dependency resolution problems.
    startBundles(bundlesInstalled, startBundle);
    return bundleInformation;
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) RepositoryException(org.sonatype.aether.RepositoryException) IOException(java.io.IOException) Dependency(org.sonatype.aether.graph.Dependency) JarInformation(org.motechproject.admin.bundles.JarInformation) LinkedList(java.util.LinkedList) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) MotechException(org.motechproject.commons.api.MotechException) BundleInformation(org.motechproject.admin.bundles.BundleInformation) ExtendedBundleInformation(org.motechproject.admin.bundles.ExtendedBundleInformation) BundleException(org.osgi.framework.BundleException)

Aggregations

MotechException (org.motechproject.commons.api.MotechException)33 IOException (java.io.IOException)25 InputStream (java.io.InputStream)9 Properties (java.util.Properties)8 ArrayList (java.util.ArrayList)6 MessageDigest (java.security.MessageDigest)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HashMap (java.util.HashMap)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 Map (java.util.Map)3 ObjectName (javax.management.ObjectName)3 SettingsRecord (org.motechproject.config.domain.SettingsRecord)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 DigestInputStream (java.security.DigestInputStream)2 QueueViewMBean (org.apache.activemq.broker.jmx.QueueViewMBean)2 TaskHandlerException (org.motechproject.tasks.exception.TaskHandlerException)2 Bundle (org.osgi.framework.Bundle)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Resource (org.springframework.core.io.Resource)2