Search in sources :

Example 1 with JarInformation

use of org.motechproject.admin.bundles.JarInformation in project motech by motech.

the class ModuleAdminServiceImpl method getJarInformations.

private JarInformation getJarInformations(File bundleFile) throws IOException {
    JarInformation jarInformation = new JarInformation(bundleFile);
    jarInformation.readPOMInformation(bundleFile);
    if (!isValidPluginBundle(jarInformation)) {
        LOGGER.warn(jarInformation.getFilename() + " is not allowed to install as add-on");
        return null;
    }
    return jarInformation;
}
Also used : JarInformation(org.motechproject.admin.bundles.JarInformation)

Example 2 with JarInformation

use of org.motechproject.admin.bundles.JarInformation in project motech by motech.

the class ModuleAdminServiceImpl method installBundleFromFile.

private Bundle installBundleFromFile(File bundleFile, boolean updateExistingBundle, boolean installInBundlesDirectory) throws IOException, BundleException, DependencyResolutionException {
    LOGGER.info("Starting installation process for: {}", bundleFile);
    JarInformation jarInformation = getJarInformations(bundleFile);
    if (jarInformation == null || jarInformation.isMotechPlatformBundle()) {
        LOGGER.info("Skipping {}", bundleFile);
        return null;
    }
    Bundle bundle = findMatchingBundle(jarInformation);
    try (InputStream bundleInputStream = FileUtils.openInputStream(bundleFile)) {
        if (bundle == null) {
            LOGGER.info("Installing new bundle: {}", bundleFile);
            if (installInBundlesDirectory) {
                // install in the bundles directory
                LOGGER.debug("Installing {} in the bundle directory", bundleFile);
                final File installedBundleFile = bundleDirectoryManager.saveBundleStreamToFile(bundleFile.getName(), bundleInputStream);
                final String bundleFileLocationAsURL = installedBundleFile.toURI().toURL().toExternalForm();
                bundle = bundleContext.installBundle(bundleFileLocationAsURL);
            } else {
                // install from the provided location
                LOGGER.debug("Installing bundle from file: {}", bundleFile);
                final String bundleFileLocationAsURL = bundleFile.toURI().toURL().toExternalForm();
                bundle = bundleContext.installBundle(bundleFileLocationAsURL);
            }
        } else if (updateExistingBundle) {
            // either install from the file provided or install in the bundles directory
            final File installedBundleFile = (installInBundlesDirectory) ? bundleDirectoryManager.saveBundleStreamToFile(bundleFile.getName(), bundleInputStream) : bundleFile;
            LOGGER.info("Updating bundle " + bundle.getSymbolicName() + "|" + bundle.getVersion());
            if (bundle.getState() == Bundle.ACTIVE) {
                bundle.stop();
            }
            try (InputStream installedInputStream = FileUtils.openInputStream(installedBundleFile)) {
                bundle.update(installedInputStream);
            }
        }
    }
    return bundle;
}
Also used : Bundle(org.osgi.framework.Bundle) InputStream(java.io.InputStream) JarInformation(org.motechproject.admin.bundles.JarInformation) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 3 with JarInformation

use of org.motechproject.admin.bundles.JarInformation 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

JarInformation (org.motechproject.admin.bundles.JarInformation)3 Bundle (org.osgi.framework.Bundle)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 BundleInformation (org.motechproject.admin.bundles.BundleInformation)1 ExtendedBundleInformation (org.motechproject.admin.bundles.ExtendedBundleInformation)1 MotechException (org.motechproject.commons.api.MotechException)1 BundleException (org.osgi.framework.BundleException)1 RepositoryException (org.sonatype.aether.RepositoryException)1 Dependency (org.sonatype.aether.graph.Dependency)1 ArtifactResult (org.sonatype.aether.resolution.ArtifactResult)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1