Search in sources :

Example 1 with CompositeMetadataProvider

use of org.apache.felix.ipojo.manipulator.metadata.CompositeMetadataProvider in project felix by apache.

the class PojoizationPlugin method analyzeJar.

public boolean analyzeJar(Analyzer analyzer) throws Exception {
    long start = System.currentTimeMillis();
    // Wraps the Bnd Reporter
    BndReporter reporter = new BndReporter(this.m_reporter);
    // Build ResourceStore
    BndJarResourceStore store = new BndJarResourceStore(analyzer, this.m_reporter);
    store.setIncludeEmbedComponents(m_includeEmbedBundles);
    CompositeMetadataProvider provider = buildMetadataProvider(analyzer, reporter, store);
    // Quick exit
    CacheableMetadataProvider cache = new CacheableMetadataProvider(provider);
    if (cache.getMetadatas().isEmpty() && !hasEmbedComponents(analyzer)) {
        return false;
    }
    // Compute the classpath to build the classloader.
    List<Jar> jars = analyzer.getClasspath();
    Jar jar = analyzer.getJar();
    Set<String> paths = new LinkedHashSet<String>();
    if (jar != null && jar.getSource() != null) {
        paths.add(jar.getSource().getAbsolutePath());
    }
    for (Jar j : jars) {
        if (j.getSource() != null) {
            paths.add(j.getSource().getAbsolutePath());
        }
    }
    Classpath cp = new Classpath(paths);
    manipulateComponents(reporter, store, cache, cp.createClassLoader());
    int nbComponents = findElements(cache.getMetadatas(), "component").size();
    int nbHandlers = findElements(cache.getMetadatas(), "handler").size();
    this.m_reporter.trace("iPOJO manipulation performed performed in %s ms (%d components, %d handlers).", (System.currentTimeMillis() - start), nbComponents, nbHandlers);
    // Return true if a new run should be performed after the analyze
    return false;
}
Also used : Classpath(org.apache.felix.ipojo.manipulator.util.Classpath) CompositeMetadataProvider(org.apache.felix.ipojo.manipulator.metadata.CompositeMetadataProvider) Jar(aQute.bnd.osgi.Jar) CacheableMetadataProvider(org.apache.felix.ipojo.manipulator.metadata.CacheableMetadataProvider)

Example 2 with CompositeMetadataProvider

use of org.apache.felix.ipojo.manipulator.metadata.CompositeMetadataProvider in project felix by apache.

the class PojoizationPlugin method buildMetadataProvider.

protected CompositeMetadataProvider buildMetadataProvider(Analyzer analyzer, BndReporter reporter, BndJarResourceStore store) {
    // Build MetadataProvider
    CompositeMetadataProvider provider = new CompositeMetadataProvider(reporter);
    File file = new File(m_metadata);
    if (file.exists()) {
        // Absolute file system resource
        FileMetadataProvider fmp = new FileMetadataProvider(file, reporter);
        fmp.setValidateUsingLocalSchemas(m_useLocalSchemas);
        provider.addMetadataProvider(fmp);
    } else {
        // In archive resource
        Resource resource = analyzer.getJar().getResource(m_metadata);
        if (resource != null) {
            ResourceMetadataProvider rmp = new ResourceMetadataProvider(resource, reporter);
            rmp.setValidateUsingLocalSchemas(m_useLocalSchemas);
            provider.addMetadataProvider(rmp);
        }
    }
    provider.addMetadataProvider(new AnnotationMetadataProvider(store, reporter));
    return provider;
}
Also used : FileMetadataProvider(org.apache.felix.ipojo.manipulator.metadata.FileMetadataProvider) Resource(aQute.bnd.osgi.Resource) CompositeMetadataProvider(org.apache.felix.ipojo.manipulator.metadata.CompositeMetadataProvider) AnnotationMetadataProvider(org.apache.felix.ipojo.manipulator.metadata.AnnotationMetadataProvider) File(java.io.File)

Example 3 with CompositeMetadataProvider

use of org.apache.felix.ipojo.manipulator.metadata.CompositeMetadataProvider 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

CompositeMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.CompositeMetadataProvider)3 File (java.io.File)2 FileMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.FileMetadataProvider)2 Jar (aQute.bnd.osgi.Jar)1 Resource (aQute.bnd.osgi.Resource)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 AnnotationMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.AnnotationMetadataProvider)1 CacheableMetadataProvider (org.apache.felix.ipojo.manipulator.metadata.CacheableMetadataProvider)1 SystemReporter (org.apache.felix.ipojo.manipulator.reporter.SystemReporter)1 Classpath (org.apache.felix.ipojo.manipulator.util.Classpath)1