Search in sources :

Example 1 with SystemReporter

use of org.apache.felix.ipojo.manipulator.reporter.SystemReporter in project felix by apache.

the class IPOJOURLHandler method openConnection.

/**
 * Opens a connection using the ipojo url handler.
 * This methods parses the URL and manipulate the given bundle.
 *
 * @param url the url.
 * @return the URL connection on the manipulated bundle
 * @throws java.io.IOException occurs when the bundle cannot be either downloaded, or manipulated or
 *                             installed correctly.
 * @see org.osgi.service.url.AbstractURLStreamHandlerService#openConnection(java.net.URL)
 */
public URLConnection openConnection(URL url) throws IOException {
    logger.log(LOG_DEBUG, format("Processing URL %s", url));
    String full = removeScheme(url);
    // Now full is like : URL!URL or URL
    String[] urls = full.split("!");
    URL bundleURL = null;
    URL metadataURL = null;
    if (urls.length == 1) {
        // URL form
        bundleURL = new URL(urls[0]);
    } else if (urls.length == 2) {
        // URL!URL form
        bundleURL = new URL(urls[0]);
        metadataURL = new URL(urls[1]);
    } else {
        throw new MalformedURLException("The iPOJO url is not formatted correctly, ipojo:bundle_url[!metadata_url] expected");
    }
    logger.log(LOG_DEBUG, format("Extracted URL %s", url));
    // Dump the referenced bundle on disk
    File original = File.createTempFile("original-", ".jar", m_temp);
    dump(bundleURL.openStream(), original);
    JarFile jf = new JarFile(original);
    File metadata = null;
    if (metadataURL != null) {
        metadata = File.createTempFile("ipojo-", ".xml", m_temp);
        dump(metadataURL, metadata);
    } else {
        // Check that the metadata are in the jar file
        metadata = findMetadata(jf);
    }
    Reporter reporter = new SystemReporter();
    File out = File.createTempFile("ipojo-", ".jar", m_temp);
    ResourceStore store = new BundleAwareJarFileResourceStore(jf, out, m_context);
    CompositeMetadataProvider composite = new CompositeMetadataProvider(reporter);
    if (metadata != null) {
        FileMetadataProvider provider = new FileMetadataProvider(metadata, reporter);
        composite.addMetadataProvider(provider);
    }
    ClassLoader classloader = new BridgeClassLoader(original, m_context);
    // Pojoization
    Pojoization pojoizator = new Pojoization(createModuleProvider());
    try {
        pojoizator.pojoization(store, composite, createVisitor(store, reporter), classloader);
    } catch (Exception e) {
        if (!pojoizator.getErrors().isEmpty()) {
            throw new IOException("Errors occurred during the manipulation : " + pojoizator.getErrors(), e);
        }
        e.printStackTrace();
        throw new IOException("Cannot manipulate the Url: " + url, e);
    }
    if (!pojoizator.getErrors().isEmpty()) {
        throw new IOException("Errors occurred during the manipulation : " + pojoizator.getErrors());
    }
    if (!pojoizator.getWarnings().isEmpty()) {
        logger.log(LOG_WARNING, format("Warnings occurred during the manipulation %s", pojoizator.getWarnings()));
    }
    logger.log(LOG_DEBUG, format("Manipulation done %s", out.exists()));
    // Cleanup
    if (metadata != null) {
        metadata.delete();
    }
    original.delete();
    out.deleteOnExit();
    // Returns the URL Connection
    return out.toURI().toURL().openConnection();
}
Also used : MalformedURLException(java.net.MalformedURLException) SystemReporter(org.apache.felix.ipojo.manipulator.reporter.SystemReporter) Reporter(org.apache.felix.ipojo.manipulator.Reporter) ResourceStore(org.apache.felix.ipojo.manipulator.ResourceStore) CompositeMetadataProvider(org.apache.felix.ipojo.manipulator.metadata.CompositeMetadataProvider) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Pojoization(org.apache.felix.ipojo.manipulator.Pojoization) FileMetadataProvider(org.apache.felix.ipojo.manipulator.metadata.FileMetadataProvider) SystemReporter(org.apache.felix.ipojo.manipulator.reporter.SystemReporter) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 JarFile (java.util.jar.JarFile)1 Pojoization (org.apache.felix.ipojo.manipulator.Pojoization)1 Reporter (org.apache.felix.ipojo.manipulator.Reporter)1 ResourceStore (org.apache.felix.ipojo.manipulator.ResourceStore)1 CompositeMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.CompositeMetadataProvider)1 FileMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.FileMetadataProvider)1 SystemReporter (org.apache.felix.ipojo.manipulator.reporter.SystemReporter)1