Search in sources :

Example 1 with Pojoization

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

the class PojoizationPlugin method manipulateComponents.

protected void manipulateComponents(BndReporter reporter, BndJarResourceStore store, CacheableMetadataProvider cache, ClassLoader classLoader) {
    Pojoization pojoization = new Pojoization(reporter);
    pojoization.disableAnnotationProcessing();
    if (m_useLocalSchemas) {
        pojoization.setUseLocalXSD();
    }
    pojoization.pojoization(store, cache, createVisitor(store, reporter), classLoader);
}
Also used : Pojoization(org.apache.felix.ipojo.manipulator.Pojoization)

Example 2 with Pojoization

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

the class IPojoc method manipulate.

private void manipulate() {
    m_pojoization = new Pojoization();
    m_pojoization.pojoization(m_input, m_output, m_metadata);
}
Also used : Pojoization(org.apache.felix.ipojo.manipulator.Pojoization)

Example 3 with Pojoization

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

the class PojoizationTest method testJarManipulationJava5.

public void testJarManipulationJava5() {
    Pojoization pojoization = new Pojoization();
    pojoization.setUseLocalXSD();
    File in = new File("target/test-classes/tests.manipulation.java5.jar");
    File out = new File("target/test-classes/tests.manipulation.java5-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 4 with Pojoization

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

the class IPojoTask method execute.

/**
 * Execute the Ant Task.
 *
 * @see org.apache.tools.ant.Task#execute()
 */
public void execute() {
    if (m_input == null && m_directory == null) {
        throw new BuildException("Neither input bundle nor directory specified");
    }
    if (m_input != null && !m_input.exists()) {
        throw new BuildException("The input bundle " + m_input.getAbsolutePath() + " does not exist");
    }
    if (m_directory != null && !m_directory.exists()) {
        throw new BuildException("The input directory " + m_directory.getAbsolutePath() + " does not exist");
    }
    if (m_directory != null && !m_directory.isDirectory()) {
        throw new BuildException("The input directory " + m_directory.getAbsolutePath() + " is not a directory");
    }
    if (m_input != null) {
        log("Input bundle file : " + m_input.getAbsolutePath());
    } else {
        log("Input directory : " + m_directory.getAbsolutePath());
    }
    if (m_manifest != null) {
        if (m_input != null) {
            throw new BuildException("The manifest location cannot be used when manipulating an existing bundle");
        }
        if (!m_manifest.exists()) {
            throw new BuildException("The manifest file " + m_manifest.getAbsolutePath() + " does not exist");
        }
    }
    // Get metadata file
    if (m_metadata == null) {
        m_metadata = new File("./metadata.xml");
        if (!m_metadata.exists()) {
            // Verify if annotations are ignored
            if (m_ignoreAnnotations) {
                log("No metadata file found & annotations ignored : nothing to do");
                return;
            } else {
                log("No metadata file found - trying to use only annotations");
                m_metadata = null;
            }
        } else {
            log("Metadata file : " + m_metadata.getAbsolutePath());
        }
    } else {
        // Metadata file is specified, check existence
        if (!m_metadata.exists()) {
            throw new BuildException("No metadata file found - the file " + m_metadata.getAbsolutePath() + " does not exist");
        } else {
            if (m_metadata.isDirectory()) {
                log("Metadata directory : " + m_metadata.getAbsolutePath());
            } else {
                log("Metadata file : " + m_metadata.getAbsolutePath());
            }
        }
    }
    initializeSaxDriver();
    log("Start manipulation");
    if (m_input != null) {
        // Prepare output file
        if (m_output == null) {
            m_output = new File("./_out.jar");
        }
        if (m_output.exists()) {
            boolean r = m_output.delete();
            if (!r) {
                throw new BuildException("The file " + m_output.getAbsolutePath() + " cannot be deleted");
            }
        }
    }
    AntReporter reporter = new AntReporter(getProject());
    Pojoization pojo = new Pojoization(reporter);
    if (m_ignoreAnnotations) {
        pojo.disableAnnotationProcessing();
    }
    if (!m_ignoreLocalXSD) {
        pojo.setUseLocalXSD();
    }
    Path classpath = getClasspath();
    classpath.addJavaRuntime();
    // Adding the input jar or directory
    if (m_classpath == null) {
        m_classpath = createClasspath();
    }
    Path element = m_classpath.createPath();
    if (m_input != null) {
        element.setLocation(m_input.getAbsoluteFile());
    } else if (m_directory != null) {
        element.setLocation(m_directory.getAbsoluteFile());
    }
    m_classpath.add(element);
    ClassLoader loader = getProject().createClassLoader(getClasspath());
    if (m_input != null) {
        pojo.pojoization(m_input, m_output, m_metadata, loader);
    } else {
        pojo.directoryPojoization(m_directory, m_metadata, m_manifest, loader);
    }
    for (int i = 0; i < reporter.getWarnings().size(); i++) {
        log((String) reporter.getWarnings().get(i), Project.MSG_WARN);
    }
    if (reporter.getErrors().size() > 0) {
        throw new BuildException((String) reporter.getErrors().get(0));
    }
    if (m_input != null) {
        String out;
        if (m_output.getName().equals("_out.jar")) {
            if (m_input.delete()) {
                if (!m_output.renameTo(m_input)) {
                    log("Cannot rename the output jar to " + m_input.getAbsolutePath(), Project.MSG_WARN);
                }
            } else {
                log("Cannot delete the input file : " + m_input.getAbsolutePath(), Project.MSG_WARN);
            }
            out = m_input.getAbsolutePath();
        } else {
            out = m_output.getAbsolutePath();
        }
        log("Bundle manipulation - SUCCESS");
        log("Output file : " + out);
    } else {
        log("Manipulation - SUCCESS");
        log("Output files : " + m_directory.getAbsolutePath());
        if (m_manifest != null) {
            log("Manifest : " + m_manifest.getAbsolutePath());
        }
    }
}
Also used : Path(org.apache.tools.ant.types.Path) Pojoization(org.apache.felix.ipojo.manipulator.Pojoization) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 5 with Pojoization

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

the class DirManipulationTest method testManifestLocationKept.

/**
 * Test case for FELIX-3466.
 *
 * Checks if directory manipulation, uses the supplied manifest file as
 * output.
 *
 * @throws IOException
 */
@Test
public void testManifestLocationKept() throws IOException {
    Pojoization pojoizator = new Pojoization();
    File tmpDir = null, manifestFile = null, testClass = null;
    // To obtain OS's temp directory.
    File tmpFile = File.createTempFile("pojodir", ".dir");
    String tmpPath = tmpFile.getAbsolutePath();
    tmpFile.delete();
    // Creating directory on temp location
    tmpDir = new File(tmpPath);
    tmpDir.mkdir();
    tmpDir.deleteOnExit();
    // Create manifest file under temp directory
    manifestFile = new File(tmpDir, "MANIFEST.MF");
    new FileOutputStream(manifestFile).write("Manifest-Version: 1.0\r\n".getBytes());
    manifestFile.deleteOnExit();
    // Just to ensure it is not deleted later from test classes.
    AnnotatedComponent safe;
    // Annotated Class File
    File annotedClassPackage = new File(tmpDir, "test");
    annotedClassPackage.deleteOnExit();
    annotedClassPackage.mkdir();
    testClass = new File(annotedClassPackage, "AnnotatedComponent.class");
    testClass.deleteOnExit();
    FileOutputStream os = new FileOutputStream(testClass);
    os.write(ManipulatorTest.getBytesFromFile(new File("target/test-classes/test/AnnotatedComponent.class")));
    os.close();
    // Issue directory manipulation
    pojoizator.directoryPojoization(tmpDir, null, manifestFile, null);
    // Check if supplied manifest file is altered in place
    BufferedReader fi = new BufferedReader(new FileReader(manifestFile));
    String manifestLine;
    while ((manifestLine = fi.readLine()) != null) {
        if (manifestLine.contains("iPOJO-Components")) {
            assertTrue(true);
            return;
        }
    }
    assertTrue("Directory Manipulation didn't use supplied manifest file as output", false);
}
Also used : AnnotatedComponent(test.AnnotatedComponent) Pojoization(org.apache.felix.ipojo.manipulator.Pojoization) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.Test)

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