Search in sources :

Example 51 with Manifest

use of java.util.jar.Manifest in project maven-plugins by apache.

the class ProjectAnalyzer method extractManifest.

private Manifest extractManifest(File file) throws IOException {
    Manifest manifest;
    if (file.isFile()) {
        JarFile jarFile = null;
        try {
            jarFile = new JarFile(file);
            manifest = jarFile.getManifest();
        } finally {
            jarFile.close();
        }
    } else if (new File(file, "META-INF/MANIFEST.MF").exists()) {
        manifest = new Manifest(new FileInputStream(new File(file, "META-INF/MANIFEST.MF")));
    } else {
        manifest = null;
    }
    return manifest;
}
Also used : Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 52 with Manifest

use of java.util.jar.Manifest in project karaf by apache.

the class SpringTransformer method transform.

public static void transform(URL url, OutputStream os) throws Exception {
    // Build dom document
    Document doc = parse(url);
    // Heuristicly retrieve name and version
    String name = getPath(url);
    int idx = name.lastIndexOf('/');
    if (idx >= 0) {
        name = name.substring(idx + 1);
    }
    String[] str = DeployerUtils.extractNameVersionType(name);
    // Create manifest
    Manifest m = new Manifest();
    m.getMainAttributes().putValue("Manifest-Version", "2");
    m.getMainAttributes().putValue(Constants.BUNDLE_MANIFESTVERSION, "2");
    m.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, str[0]);
    m.getMainAttributes().putValue(Constants.BUNDLE_VERSION, str[1]);
    m.getMainAttributes().putValue("Spring-Context", "*;publish-context:=false;create-asynchronously:=true");
    String importPkgs = getImportPackages(analyze(new DOMSource(doc)));
    if (importPkgs != null && importPkgs.length() > 0) {
        m.getMainAttributes().putValue(Constants.IMPORT_PACKAGE, importPkgs);
    }
    m.getMainAttributes().putValue(Constants.DYNAMICIMPORT_PACKAGE, "*");
    // Extract manifest entries from the DOM
    NodeList l = doc.getElementsByTagName("manifest");
    if (l != null) {
        for (int i = 0; i < l.getLength(); i++) {
            Element e = (Element) l.item(i);
            String text = e.getTextContent();
            Properties props = new Properties();
            props.load(new ByteArrayInputStream(text.trim().getBytes()));
            Enumeration<?> en = props.propertyNames();
            while (en.hasMoreElements()) {
                String k = (String) en.nextElement();
                String v = props.getProperty(k);
                m.getMainAttributes().putValue(k, v);
            }
            e.getParentNode().removeChild(e);
        }
    }
    // get original last modification date
    long lastModified = getLastModified(url);
    JarOutputStream out = new JarOutputStream(os);
    ZipEntry e = new ZipEntry(JarFile.MANIFEST_NAME);
    e.setTime(lastModified);
    out.putNextEntry(e);
    m.write(out);
    out.closeEntry();
    e = new ZipEntry("META-INF/");
    e.setTime(lastModified);
    out.putNextEntry(e);
    e = new ZipEntry("META-INF/spring/");
    e.setTime(lastModified);
    out.putNextEntry(e);
    out.closeEntry();
    e = new ZipEntry("META-INF/spring/" + name);
    e.setTime(lastModified);
    out.putNextEntry(e);
    // Copy the new DOM
    XmlUtils.transform(new DOMSource(doc), new StreamResult(out));
    out.closeEntry();
    out.close();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) Document(org.w3c.dom.Document) Manifest(java.util.jar.Manifest) Properties(java.util.Properties) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 53 with Manifest

use of java.util.jar.Manifest in project intellij-community by JetBrains.

the class JdkUtil method setClasspathJarParams.

private static boolean setClasspathJarParams(SimpleJavaParameters javaParameters, GeneralCommandLine commandLine, ParametersList vmParametersList, Class commandLineWrapper) throws CantRunException {
    boolean dynamicVMOptions = javaParameters.isDynamicVMOptions() && useDynamicVMOptions();
    boolean dynamicParameters = javaParameters.isDynamicParameters() && useDynamicParameters();
    try {
        final Manifest manifest = new Manifest();
        manifest.getMainAttributes().putValue("Created-By", ApplicationNamesInfo.getInstance().getFullProductName());
        if (dynamicVMOptions) {
            List<String> properties = new ArrayList<>();
            for (String param : vmParametersList.getList()) {
                if (isUserDefinedProperty(param)) {
                    properties.add(param);
                } else {
                    commandLine.addParameter(param);
                }
            }
            manifest.getMainAttributes().putValue("VM-Options", ParametersListUtil.join(properties));
        } else {
            commandLine.addParameters(vmParametersList.getList());
        }
        if (dynamicParameters) {
            manifest.getMainAttributes().putValue("Program-Parameters", ParametersListUtil.join(javaParameters.getProgramParametersList().getList()));
        }
        final boolean notEscape = vmParametersList.hasParameter(PROPERTY_DO_NOT_ESCAPE_CLASSPATH_URL);
        final List<String> classPathList = javaParameters.getClassPath().getPathList();
        final File classpathJarFile = CommandLineWrapperUtil.createClasspathJarFile(manifest, classPathList, notEscape);
        getFilesToDeleteUserData(commandLine).add(classpathJarFile);
        final String jarFile = classpathJarFile.getAbsolutePath();
        commandLine.addParameter("-classpath");
        if (dynamicVMOptions || dynamicParameters) {
            commandLine.addParameter(PathUtil.getJarPathForClass(commandLineWrapper) + File.pathSeparator + jarFile);
            appendEncoding(javaParameters, commandLine, vmParametersList);
            commandLine.addParameter(commandLineWrapper.getName());
            commandLine.addParameter(jarFile);
        } else {
            commandLine.addParameters(jarFile);
            appendEncoding(javaParameters, commandLine, vmParametersList);
        }
    } catch (IOException e) {
        throwUnableToCreateTempFile(e);
    }
    return dynamicParameters;
}
Also used : ArrayList(java.util.ArrayList) Manifest(java.util.jar.Manifest) VirtualFile(com.intellij.openapi.vfs.VirtualFile) JarFile(java.util.jar.JarFile)

Example 54 with Manifest

use of java.util.jar.Manifest in project sling by apache.

the class BSNRenamer method processManifest.

protected Manifest processManifest(Manifest inputMF) {
    Attributes inputAttrs = inputMF.getMainAttributes();
    String orgBSN = inputAttrs.getValue(BUNDLE_SYMBOLIC_NAME);
    Manifest newMF = new Manifest(inputMF);
    Attributes outputAttrs = newMF.getMainAttributes();
    outputAttrs.putValue(BUNDLE_SYMBOLIC_NAME, newBSN);
    outputAttrs.putValue(X_ORIG_BSN, orgBSN);
    return newMF;
}
Also used : Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest)

Example 55 with Manifest

use of java.util.jar.Manifest in project sling by apache.

the class BundleFileProcessorTest method testBSNRenaming.

@Test
public void testBSNRenaming() throws IOException {
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    // Just take any bundle from the maven deps as an example...
    File originalFile = getMavenArtifactFile(getMavenRepoRoot(), "com.google.guava", "guava", "15.0");
    File generatedFile = new BSNRenamer(originalFile, tempDir, "org.acme.baklava.guava").process();
    try {
        compareJarContents(originalFile, generatedFile);
        JarFile jfOrg = null;
        JarFile jfNew = null;
        try {
            jfOrg = new JarFile(originalFile);
            jfNew = new JarFile(generatedFile);
            Manifest mfOrg = jfOrg.getManifest();
            Manifest mfNew = jfNew.getManifest();
            Attributes orgAttrs = mfOrg.getMainAttributes();
            Attributes newAttrs = mfNew.getMainAttributes();
            for (Object key : orgAttrs.keySet()) {
                String orgVal = orgAttrs.getValue(key.toString());
                String newVal = newAttrs.getValue(key.toString());
                if ("Bundle-SymbolicName".equals(key.toString())) {
                    assertEquals("Should have recorded the original Bundle-SymbolicName", orgVal, newAttrs.getValue("X-Original-Bundle-SymbolicName"));
                    assertEquals("org.acme.baklava.guava", newVal);
                } else {
                    assertEquals("Different keys: " + key, orgVal, newVal);
                }
            }
        } finally {
            closeQuietly(jfOrg);
            closeQuietly(jfNew);
        }
    } finally {
        assertTrue("Unable to delete temporary file", generatedFile.delete());
    }
}
Also used : Attributes(java.util.jar.Attributes) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Aggregations

Manifest (java.util.jar.Manifest)1226 Attributes (java.util.jar.Attributes)392 File (java.io.File)391 IOException (java.io.IOException)336 JarFile (java.util.jar.JarFile)231 InputStream (java.io.InputStream)184 URL (java.net.URL)177 JarOutputStream (java.util.jar.JarOutputStream)145 FileOutputStream (java.io.FileOutputStream)131 Test (org.junit.Test)129 FileInputStream (java.io.FileInputStream)119 Jar (aQute.bnd.osgi.Jar)105 JarInputStream (java.util.jar.JarInputStream)104 Builder (aQute.bnd.osgi.Builder)99 ZipEntry (java.util.zip.ZipEntry)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)96 JarEntry (java.util.jar.JarEntry)96 ByteArrayInputStream (java.io.ByteArrayInputStream)93 ArrayList (java.util.ArrayList)83 Map (java.util.Map)83