Search in sources :

Example 41 with JarInputStream

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

the class BlueprintDeploymentListenerTest method testCustomManifest.

public void testCustomManifest() throws Exception {
    File f = File.createTempFile("smx", ".jar");
    try {
        OutputStream os = new FileOutputStream(f);
        BlueprintTransformer.transform(getClass().getClassLoader().getResource("test.xml"), os);
        os.close();
        InputStream is = new FileInputStream(f);
        JarInputStream jar = new JarInputStream(is);
        jar.getManifest().write(System.err);
        is.close();
    } finally {
        f.delete();
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 42 with JarInputStream

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

the class Kar method extract.

/**
     * Extract a kar from a given URI into a repository dir and resource dir
     * and populate shouldInstallFeatures and featureRepos
     *
     * @param repoDir directory to write the repository contents of the kar to
     * @param resourceDir directory to write the resource contents of the kar to
     */
public void extract(File repoDir, File resourceDir) {
    InputStream is = null;
    JarInputStream zipIs = null;
    FeatureDetector featureDetector = new FeatureDetector();
    this.featureRepos = new ArrayList<>();
    this.shouldInstallFeatures = true;
    try {
        is = karUri.toURL().openStream();
        repoDir.mkdirs();
        if (!repoDir.isDirectory()) {
            throw new RuntimeException("The KAR file " + karUri + " is already installed");
        }
        LOGGER.debug("Uncompress the KAR file {} into directory {}", karUri, repoDir);
        zipIs = new JarInputStream(is);
        boolean scanForRepos = true;
        Manifest manifest = zipIs.getManifest();
        if (manifest != null) {
            Attributes attr = manifest.getMainAttributes();
            String featureStartSt = (String) attr.get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_START));
            if ("false".equals(featureStartSt)) {
                shouldInstallFeatures = false;
            }
            String featureReposAttr = (String) attr.get(new Attributes.Name(MANIFEST_ATTR_KARAF_FEATURE_REPOS));
            if (featureReposAttr != null) {
                featureRepos.add(new URI(featureReposAttr));
                scanForRepos = false;
            }
        }
        ZipEntry entry = zipIs.getNextEntry();
        while (entry != null) {
            if (entry.getName().startsWith("repository/")) {
                String path = entry.getName().substring("repository/".length());
                File destFile = new File(repoDir, path);
                extract(zipIs, entry, destFile);
                if (scanForRepos && featureDetector.isFeaturesRepository(destFile)) {
                    Map map = new HashMap<>();
                    String uri = Parser.pathToMaven(path, map);
                    if (map.get("classifier") != null && ((String) map.get("classifier")).equalsIgnoreCase("features"))
                        featureRepos.add(URI.create(uri));
                    else
                        featureRepos.add(destFile.toURI());
                }
            }
            if (entry.getName().startsWith("resources/")) {
                String path = entry.getName().substring("resources/".length());
                File destFile = new File(resourceDir, path);
                extract(zipIs, entry, destFile);
            }
            entry = zipIs.getNextEntry();
        }
    } catch (Exception e) {
        throw new RuntimeException("Error extracting kar file " + karUri + " into dir " + repoDir + ": " + e.getMessage(), e);
    } finally {
        closeStream(zipIs);
        closeStream(is);
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) HashMap(java.util.HashMap) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest) URI(java.net.URI) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 43 with JarInputStream

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

the class EncryptableConfigAdminPropertyPlaceholderTest method getBundleDescriptor.

private BundleDescriptor getBundleDescriptor(String path, TinyBundle bundle) throws Exception {
    File file = new File(path);
    FileOutputStream fos = new FileOutputStream(file);
    StreamUtils.copy(bundle.build(), fos);
    fos.close();
    JarInputStream jis = new JarInputStream(new FileInputStream(file));
    Map<String, String> headers = new HashMap<>();
    for (Map.Entry entry : jis.getManifest().getMainAttributes().entrySet()) {
        headers.put(entry.getKey().toString(), entry.getValue().toString());
    }
    return new BundleDescriptor(getClass().getClassLoader(), "jar:" + file.toURI().toString() + "!/", headers);
}
Also used : BundleDescriptor(org.apache.felix.connect.launch.BundleDescriptor) JarInputStream(java.util.jar.JarInputStream)

Example 44 with JarInputStream

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

the class EncryptablePropertyPlaceholderTest method getBundleDescriptor.

private BundleDescriptor getBundleDescriptor(String path, TinyBundle bundle) throws Exception {
    File file = new File(path);
    FileOutputStream fos = new FileOutputStream(file);
    StreamUtils.copy(bundle.build(), fos);
    fos.close();
    JarInputStream jis = new JarInputStream(new FileInputStream(file));
    Map<String, String> headers = new HashMap<>();
    for (Map.Entry entry : jis.getManifest().getMainAttributes().entrySet()) {
        headers.put(entry.getKey().toString(), entry.getValue().toString());
    }
    return new BundleDescriptor(getClass().getClassLoader(), "jar:" + file.toURI().toString() + "!/", headers);
}
Also used : BundleDescriptor(org.apache.felix.connect.launch.BundleDescriptor) JarInputStream(java.util.jar.JarInputStream) HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream)

Example 45 with JarInputStream

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

the class FileUtil method downloadSource.

public static void downloadSource(PrintStream out, PrintStream err, URL srcURL, String dirStr, boolean extract) {
    // Get the file name from the URL.
    String fileName = (srcURL.getFile().lastIndexOf('/') > 0) ? srcURL.getFile().substring(srcURL.getFile().lastIndexOf('/') + 1) : srcURL.getFile();
    try {
        out.println("Connecting...");
        File dir = new File(dirStr);
        if (!dir.exists()) {
            err.println("Destination directory does not exist.");
        }
        File file = new File(dir, fileName);
        OutputStream os = new FileOutputStream(file);
        URLConnection conn = srcURL.openConnection();
        int total = conn.getContentLength();
        InputStream is = conn.getInputStream();
        if (total > 0) {
            out.println("Downloading " + fileName + " ( " + total + " bytes ).");
        } else {
            out.println("Downloading " + fileName + ".");
        }
        byte[] buffer = new byte[4096];
        int count = 0;
        for (int len = is.read(buffer); len > 0; len = is.read(buffer)) {
            count += len;
            os.write(buffer, 0, len);
        }
        os.close();
        is.close();
        if (extract) {
            JarInputStream jis = new JarInputStream(new FileInputStream(file));
            out.println("Extracting...");
            unjar(jis, dir);
            jis.close();
            file.delete();
        }
    } catch (Exception ex) {
        err.println(ex);
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) URLConnection(java.net.URLConnection) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

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