Search in sources :

Example 6 with Pojoization

use of org.apache.felix.ipojo.manipulator.Pojoization in project felix by apache.

the class PojoizationTest method testJarManipulation.

public void testJarManipulation() {
    Pojoization pojoization = new Pojoization();
    pojoization.setUseLocalXSD();
    File in = new File("target/test-classes/tests.manipulation-no-annotations.jar");
    File out = new File("target/test-classes/tests.manipulation-no-annotations-manipulated.jar");
    out.delete();
    File metadata = new File("target/test-classes/metadata.xml");
    pojoization.pojoization(in, out, metadata, null);
    Assert.assertTrue(out.exists());
}
Also used : Pojoization(org.apache.felix.ipojo.manipulator.Pojoization) File(java.io.File)

Example 7 with Pojoization

use of org.apache.felix.ipojo.manipulator.Pojoization in project felix by apache.

the class PojoizationTest method testManipulationWithAnnotations.

public void testManipulationWithAnnotations() {
    Pojoization pojoization = new Pojoization();
    pojoization.setUseLocalXSD();
    File in = new File("target/test-classes/tests.manipulator-annotations.jar");
    File out = new File("target/test-classes/tests.manipulation-annotations-manipulated.jar");
    out.delete();
    pojoization.pojoization(in, out, (File) null, null);
    Assert.assertTrue(out.exists());
}
Also used : Pojoization(org.apache.felix.ipojo.manipulator.Pojoization) File(java.io.File)

Example 8 with Pojoization

use of org.apache.felix.ipojo.manipulator.Pojoization in project felix by apache.

the class ManipulatorMojo method execute.

/**
 * Execute method : this method launches the pojoization.
 *
 * @throws MojoExecutionException : an exception occurs during the manipulation.
 * @see org.apache.maven.plugin.AbstractMojo#execute()
 */
public void execute() throws MojoExecutionException {
    // ignore project types not supported, useful when the plugin is configured in the parent pom
    if (!this.m_supportedProjectTypes.contains(m_project.getArtifact().getType())) {
        this.getLog().debug("Ignoring project " + m_project.getArtifact() + " : type " + m_project.getArtifact().getType() + " is not supported by iPOJO plugin, supported types are " + this.m_supportedProjectTypes);
        return;
    }
    initializeSaxDriver();
    getLog().info("Start bundle manipulation");
    // Get metadata
    // Check if metadata are contained in the configuration
    // Metadata File or directory containing the metadata files.
    File metadata = null;
    // Use if contained in the configuration
    InputStream is = null;
    // Create the ClassPath and classloader.
    Set<Artifact> artifacts = m_project.getArtifacts();
    Set<String> urls = new LinkedHashSet<String>();
    File classes = new File(m_project.getBasedir(), "target/classes");
    if (classes.isDirectory()) {
        urls.add(classes.getAbsolutePath());
    }
    for (Artifact artifact : artifacts) {
        File file = artifact.getFile();
        if (file != null && file.isFile()) {
            urls.add(file.getAbsolutePath());
        }
    }
    getLog().debug("Compute classpath: " + urls);
    Classpath classpath = new Classpath(urls);
    if (isXML()) {
        is = new ByteArrayInputStream(m_metadata.getBytes());
    } else {
        // first check if ./src/main/ipojo exists, if so look into it.
        if (m_metadata == null) {
            File m = new File(m_project.getBasedir(), "src/main/ipojo");
            if (m.isDirectory()) {
                metadata = m;
                getLog().info("Metadata directory : " + metadata.getAbsolutePath());
            } else {
                // Else check target/classes/metadata.xml
                File meta = new File(m_outputDirectory + File.separator + "metadata.xml");
                if (!meta.exists()) {
                    // If it still does not exist, try ./metadata.xml
                    meta = new File(m_project.getBasedir() + File.separator + "metadata.xml");
                }
                if (meta.exists()) {
                    metadata = meta;
                    getLog().info("Metadata file : " + metadata.getAbsolutePath());
                }
            // No metadata.
            }
        } else {
            // metadata path set.
            File m = new File(m_project.getBasedir(), m_metadata);
            if (!m.exists()) {
                throw new MojoExecutionException("The metadata file does not exist : " + m.getAbsolutePath());
            }
            metadata = m;
            if (m.isDirectory()) {
                getLog().info("Metadata directory : " + metadata.getAbsolutePath());
            } else {
                getLog().info("Metadata file : " + metadata.getAbsolutePath());
            }
        }
        if (metadata == null) {
            // Verify if annotations are ignored
            if (m_ignoreAnnotations) {
                getLog().info("No metadata file found - ignoring annotations");
                return;
            } else {
                getLog().info("No metadata file found - trying to use only annotations");
            }
        }
    }
    // Get input bundle, we use the already create artifact.
    File in = null;
    if (m_inputClassifier == null) {
        in = m_project.getArtifact().getFile();
        getLog().info("Input Bundle File : " + in.getAbsolutePath());
        if (!in.exists()) {
            throw new MojoExecutionException("The specified bundle file does not exist : " + in.getAbsolutePath());
        }
    } else {
        // Look from attached artifacts.
        @SuppressWarnings("unchecked") List<Artifact> attached = m_project.getAttachedArtifacts();
        for (int i = 0; in == null && attached != null && i < attached.size(); i++) {
            Artifact artifact = attached.get(i);
            if (artifact.hasClassifier() && m_inputClassifier.equals(artifact.getClassifier())) {
                in = artifact.getFile();
            }
        }
        if (in == null) {
            throw new MojoExecutionException("Cannot find the file to manipulate, " + "no attached artifact with classifier " + m_inputClassifier);
        }
        getLog().info("Input Bundle File : " + in.getAbsolutePath());
        if (!in.exists()) {
            throw new MojoExecutionException("The specified bundle file does not exist : " + in.getAbsolutePath());
        }
    }
    File out = new File(m_buildDirectory + File.separator + "_out.jar");
    Reporter reporter = new MavenReporter(getLog());
    Pojoization pojo = new Pojoization(reporter);
    if (m_ignoreAnnotations) {
        pojo.disableAnnotationProcessing();
    }
    if (!m_ignoreEmbeddedXSD) {
        pojo.setUseLocalXSD();
    }
    // Executes the pojoization.
    if (is == null) {
        if (metadata == null) {
            // No metadata.
            // Only annotations
            pojo.pojoization(in, out, (File) null, classpath.createClassLoader());
        } else {
            // Metadata set
            pojo.pojoization(in, out, metadata, classpath.createClassLoader());
        }
    } else {
        // In-Pom metadata.
        pojo.pojoization(in, out, is, classpath.createClassLoader());
    }
    for (int i = 0; i < reporter.getWarnings().size(); i++) {
        getLog().warn((String) reporter.getWarnings().get(i));
    }
    if (reporter.getErrors().size() > 0) {
        throw new MojoExecutionException((String) reporter.getErrors().get(0));
    }
    if (m_classifier != null) {
        // The user want to attach the resulting jar
        // Do not delete in File
        m_helper.attachArtifact(m_project, "jar", m_classifier, out);
    } else {
        // Usual behavior
        if (in.delete()) {
            if (!out.renameTo(in)) {
                getLog().warn("Cannot rename the manipulated jar file");
            }
        } else {
            getLog().warn("Cannot delete the input jar file");
        }
    }
    getLog().info("Bundle manipulation - SUCCESS");
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Reporter(org.apache.felix.ipojo.manipulator.Reporter) Artifact(org.apache.maven.artifact.Artifact) Pojoization(org.apache.felix.ipojo.manipulator.Pojoization) ByteArrayInputStream(java.io.ByteArrayInputStream) Classpath(org.apache.felix.ipojo.manipulator.util.Classpath) File(java.io.File)

Example 9 with Pojoization

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

Pojoization (org.apache.felix.ipojo.manipulator.Pojoization)9 File (java.io.File)7 Reporter (org.apache.felix.ipojo.manipulator.Reporter)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 LinkedHashSet (java.util.LinkedHashSet)1 JarFile (java.util.jar.JarFile)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 Classpath (org.apache.felix.ipojo.manipulator.util.Classpath)1 Artifact (org.apache.maven.artifact.Artifact)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1