Search in sources :

Example 1 with BundleLoadingException

use of org.motechproject.server.ex.BundleLoadingException in project motech by motech.

the class JspBundleLoader method loadBundle.

@SuppressWarnings("unchecked")
@Override
public void loadBundle(Bundle bundle) throws BundleLoadingException {
    // we want to build and unpack jar files in application temporary directory
    // if we found jsp file then we will copy it to destination directory
    File tempRoot = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
    File destRoot = new File(servletContext.getRealPath("/"));
    try {
        if (tempRoot != null) {
            tempDir = new File(tempRoot, String.valueOf(bundle.getBundleId()));
            destDir = new File(destRoot, String.valueOf(bundle.getBundleId()));
            // search for jars in bundle
            Enumeration<URL> jars = bundle.findEntries("/", "*.jar", true);
            if (jars != null) {
                while (jars.hasMoreElements()) {
                    URL jarUrl = jars.nextElement();
                    // build jar file
                    File tempJarFile = new File(tempDir, jarUrl.getFile());
                    // This is why this step must be synchronized.
                    synchronized (JspBundleLoader.class) {
                        FileUtils.copyURLToFile(jarUrl, tempJarFile);
                    }
                    JarFile jarFile = new JarFile(tempJarFile);
                    searchForJspFilesInJarFile(jarFile);
                    tempJarFile.delete();
                }
            }
            tempDir.delete();
            // Search for *.jsp files in bundle
            Enumeration<URL> jsps = bundle.findEntries("/webapp", "*.jsp", true);
            if (jsps != null) {
                while (jsps.hasMoreElements()) {
                    URL jspUrl = jsps.nextElement();
                    File destFile = new File(destDir, jspUrl.getFile());
                    FileUtils.copyURLToFile(jspUrl, destFile);
                    LOGGER.debug("Loaded " + jspUrl.getFile() + " from [" + bundle.getLocation() + "]");
                }
            }
            // Search for *.properties files in bundle
            for (String path : Arrays.asList("/webapp/resources/messages", "/webapp/messages", "/webapp/messages")) {
                loadBundleMessageFilesFromBundle(bundle, destRoot, path);
            }
        }
    } catch (IOException e) {
        throw new BundleLoadingException("IO Error when laoding bundle " + bundle, e);
    } catch (InvocationTargetException | NoSuchMethodException | NoSuchFieldException | IllegalAccessException e) {
        throw new BundleLoadingException(e);
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException) BundleLoadingException(org.motechproject.server.ex.BundleLoadingException) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 2 with BundleLoadingException

use of org.motechproject.server.ex.BundleLoadingException in project motech by motech.

the class OsgiFrameworkService method init.

public void init(BootstrapConfig bootstrapConfig) {
    try (InputStream is = getClass().getResourceAsStream("/osgi.properties")) {
        Properties properties = readOSGiProperties(bootstrapConfig, is);
        this.setOsgiFramework(new Felix(properties));
    } catch (IOException e) {
        throw new OsgiException("Cannot read OSGi properties", e);
    }
    try {
        LOGGER.info("Initializing OSGi framework");
        ServletContext servletContext = ((WebApplicationContext) applicationContext).getServletContext();
        osgiFramework.init();
        BundleContext bundleContext = osgiFramework.getBundleContext();
        // This is mandatory for Felix http servlet bridge
        servletContext.setAttribute(BundleContext.class.getName(), bundleContext);
        if (bootstrapConfig != null) {
            LOGGER.info("Installing all available bundles");
            installAllBundles(servletContext, bundleContext);
            registerBundleLoaderExecutor();
        }
        platformStatusProxy = new PlatformStatusProxy(bundleContext);
        LOGGER.info("OSGi framework initialization finished");
    } catch (BundleLoadingException e) {
        throw new OsgiException("Failed to start the OSGi framework, unable to load bundles", e);
    } catch (BundleException e) {
        throw new OsgiException("Failed to start the OSGi framework, error processing bundles", e);
    } catch (IOException e) {
        throw new OsgiException("Failed to start the OSGi framework, IO Error", e);
    }
}
Also used : Felix(org.apache.felix.framework.Felix) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) BundleLoadingException(org.motechproject.server.ex.BundleLoadingException) OsgiException(org.eclipse.gemini.blueprint.OsgiException) ServletContext(javax.servlet.ServletContext) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) Properties(java.util.Properties) WebApplicationContext(org.springframework.web.context.WebApplicationContext) BundleContext(org.osgi.framework.BundleContext)

Example 3 with BundleLoadingException

use of org.motechproject.server.ex.BundleLoadingException in project motech by motech.

the class OsgiFrameworkService method installAllBundles.

private void installAllBundles(ServletContext servletContext, BundleContext bundleContext) throws IOException, BundleLoadingException {
    for (URL url : findBundles(servletContext)) {
        LOGGER.debug("Installing bundle [" + url + "]");
        try {
            Bundle bundle = bundleContext.installBundle(url.toExternalForm());
            bundleLocationMapping.put(bundle.getBundleId() + ".0", bundle.getLocation());
        } catch (BundleException e) {
            throw new BundleLoadingException("Failed to install bundle from " + url, e);
        }
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleLoadingException(org.motechproject.server.ex.BundleLoadingException) BundleException(org.osgi.framework.BundleException) URL(java.net.URL)

Aggregations

BundleLoadingException (org.motechproject.server.ex.BundleLoadingException)3 IOException (java.io.IOException)2 URL (java.net.URL)2 BundleException (org.osgi.framework.BundleException)2 File (java.io.File)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Properties (java.util.Properties)1 JarFile (java.util.jar.JarFile)1 JarInputStream (java.util.jar.JarInputStream)1 ServletContext (javax.servlet.ServletContext)1 Felix (org.apache.felix.framework.Felix)1 OsgiException (org.eclipse.gemini.blueprint.OsgiException)1 Bundle (org.osgi.framework.Bundle)1 BundleContext (org.osgi.framework.BundleContext)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1