Search in sources :

Example 31 with JarInputStream

use of java.util.jar.JarInputStream in project aries by apache.

the class BundleContextMock method installBundle.

/**
   * Asks to install an OSGi bundle from the given location.
   * 
   * @param location the location of the bundle on the file system.
   * @return the installed bundle.
   * @throws BundleException
   */
public Bundle installBundle(String location) throws BundleException {
    try {
        URI uri = new URI(location.replaceAll(" ", "%20"));
        File baseDir = new File(uri);
        Manifest man = null;
        //check if it is a directory
        if (baseDir.isDirectory()) {
            man = new Manifest(new FileInputStream(new File(baseDir, "META-INF/MANIFEST.MF")));
        } else //if it isn't assume it is a jar file
        {
            InputStream is = new FileInputStream(baseDir);
            JarInputStream jis = new JarInputStream(is);
            man = jis.getManifest();
            jis.close();
            if (man == null) {
                throw new BundleException("Null manifest");
            }
        }
        return createBundle(man, location);
    } catch (IOException e) {
        throw new BundleException(e.getMessage(), e);
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        throw new BundleException(e.getMessage(), e);
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Manifest(java.util.jar.Manifest) URI(java.net.URI) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 32 with JarInputStream

use of java.util.jar.JarInputStream in project aries by apache.

the class Util method getClassLoaderViaBundleClassPath.

private static ClassLoader getClassLoaderViaBundleClassPath(Bundle b, URL url) {
    try {
        JarInputStream jis = null;
        try {
            jis = new JarInputStream(url.openStream());
            JarEntry je = null;
            while ((je = jis.getNextJarEntry()) != null) {
                String path = je.getName();
                if (path.endsWith(".class")) {
                    ClassLoader cl = getClassLoaderFromClassResource(b, path);
                    if (cl != null)
                        return cl;
                }
            }
        } finally {
            if (jis != null)
                jis.close();
        }
    } catch (IOException e) {
        BaseActivator.activator.log(LogService.LOG_ERROR, "Problem loading class from embedded jar file: " + url + " in bundle " + b.getSymbolicName(), e);
    }
    return null;
}
Also used : JarInputStream(java.util.jar.JarInputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry)

Example 33 with JarInputStream

use of java.util.jar.JarInputStream in project aries by apache.

the class JarCreator method createJarFromFile.

private static void createJarFromFile(String fileName, String version) throws Exception {
    JarOutputStream jos = null;
    FileOutputStream fos = null;
    ZipEntry entry = null;
    try {
        // let's get hold of jars on disk
        File jarFile = new File(fileName);
        JarInputStream jarInput = new JarInputStream(jarFile.toURL().openStream());
        Manifest manifest = jarInput.getManifest();
        //update manifest, Bundle-Version
        Attributes attr = manifest.getMainAttributes();
        attr.putValue(Constants.BUNDLE_VERSION, version);
        if (fileName.indexOf("helloIsolationRef") < 0) {
            attr.putValue(Constants.EXPORT_PACKAGE, "org.apache.aries.subsystem.example.helloIsolation;uses:=\"org.osgi.util.tracker,org.osgi.framework\";version=" + version);
        }
        int lastSlash = fileName.lastIndexOf("/");
        // trim the path
        fileName = fileName.substring(lastSlash + 1);
        int loc = fileName.indexOf("-");
        String jarFilePath = fileName.substring(0, loc + 1) + version + ".jar";
        if (fileName.indexOf("helloIsolationRef") < 0) {
            File directory = new File(System.getProperty("user.home") + "/.m2/repository/org/apache/aries/subsystem/example/org.apache.aries.subsystem.example.helloIsolation/" + version + "/");
            if (!directory.exists()) {
                directory.mkdir();
            }
            fos = new FileOutputStream(directory.getAbsolutePath() + "/" + jarFilePath);
        } else {
            File directory = new File(System.getProperty("user.home") + "/.m2/repository/org/apache/aries/subsystem/example/org.apache.aries.subsystem.example.helloIsolationRef/" + version + "/");
            if (!directory.exists()) {
                directory.mkdir();
            }
            fos = new FileOutputStream(directory.getAbsolutePath() + "/" + jarFilePath);
        }
        jos = new JarOutputStream(fos, manifest);
        //Copy across all entries from the original jar
        int val;
        while ((entry = jarInput.getNextEntry()) != null) {
            jos.putNextEntry(entry);
            byte[] buffer = new byte[4096];
            while ((val = jarInput.read(buffer)) != -1) jos.write(buffer, 0, val);
        }
        jos.closeEntry();
        jos.finish();
        System.out.println("finishing creating jar file: " + jarFilePath + " in local m2 repo");
    } finally {
        if (jos != null) {
            jos.close();
        }
        if (fos != null) {
            fos.close();
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Attributes(java.util.jar.Attributes) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) File(java.io.File)

Example 34 with JarInputStream

use of java.util.jar.JarInputStream in project zm-mailbox by Zimbra.

the class ZimletFile method initialize.

@SuppressWarnings("unchecked")
private void initialize(String name) throws IOException, ZimletException {
    if (!name.matches(ZimletUtil.ZIMLET_NAME_REGEX)) {
        //so there is no need to try and load a zimlet with a bad name 
        throw ZimletException.INVALID_ZIMLET_NAME();
    }
    if (name.endsWith(ZIP_SUFFIX)) {
        name = name.substring(0, name.length() - 4);
    }
    mDescFile = name + XML_SUFFIX;
    mEntries = new HashMap<String, ZimletEntry>();
    if (mBaseStream != null) {
        mCopy = ByteUtil.getContent(mBaseStream, 0);
        JarInputStream zis = new JarInputStream(new ByteArrayInputStream(mCopy));
        Manifest mf = zis.getManifest();
        if (mf != null) {
            mDescFile = mf.getMainAttributes().getValue("Zimlet-Description-File");
        }
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            if (entry.getSize() > 0)
                mEntries.put(entry.getName().toLowerCase(), new ZimletRawEntry(zis, entry.getName(), (int) entry.getSize()));
            zis.closeEntry();
            entry = zis.getNextEntry();
        }
        zis.close();
    } else if (mBase.isDirectory()) {
        addFileEntry(mBase, "");
    } else {
        JarFile jar = new JarFile(mBase);
        Manifest mf = jar.getManifest();
        if (mf != null) {
            mDescFile = mf.getMainAttributes().getValue("Zimlet-Description-File");
        }
        Enumeration entries = jar.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            if (entry.getSize() > 0)
                mEntries.put(entry.getName().toLowerCase(), new ZimletZipEntry(jar, entry));
        }
    }
    initZimletDescription();
    mZimletName = mDesc.getName();
}
Also used : Enumeration(java.util.Enumeration) JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipEntry(java.util.zip.ZipEntry) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile)

Example 35 with JarInputStream

use of java.util.jar.JarInputStream in project aries by apache.

the class WarToWabConverterImpl method generateManifest.

private void generateManifest() throws IOException {
    if (wabManifest != null) {
        // WAB manifest is already generated
        return;
    }
    JarInputStream jarInput = null;
    try {
        jarInput = new JarInputStream(input.getInputStream());
        Manifest manifest = jarInput.getManifest();
        if (isBundle(manifest)) {
            wabManifest = updateBundleManifest(manifest);
        } else {
            scanForDependencies(jarInput);
            // Add the new properties to the manifest byte stream
            wabManifest = updateManifest(manifest);
        }
    } finally {
        try {
            if (jarInput != null)
                jarInput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) IOException(java.io.IOException) Manifest(java.util.jar.Manifest)

Aggregations

JarInputStream (java.util.jar.JarInputStream)186 JarEntry (java.util.jar.JarEntry)83 IOException (java.io.IOException)73 FileInputStream (java.io.FileInputStream)67 Manifest (java.util.jar.Manifest)56 File (java.io.File)48 InputStream (java.io.InputStream)45 ZipEntry (java.util.zip.ZipEntry)34 JarOutputStream (java.util.jar.JarOutputStream)29 Test (org.junit.Test)29 FileOutputStream (java.io.FileOutputStream)26 ByteArrayInputStream (java.io.ByteArrayInputStream)24 URL (java.net.URL)20 ArrayList (java.util.ArrayList)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 OutputStream (java.io.OutputStream)14 JarFile (java.util.jar.JarFile)14 BufferedInputStream (java.io.BufferedInputStream)11 Attributes (java.util.jar.Attributes)11 HashSet (java.util.HashSet)9