Search in sources :

Example 6 with JarEntry

use of java.util.jar.JarEntry in project hbase by apache.

the class TestClassFinder method packageAndLoadJar.

/**
   * Makes a jar out of some class files. Unfortunately it's very tedious.
   * @param filesInJar Files created via compileTestClass.
   * @return path to the resulting jar file.
   */
private static String packageAndLoadJar(FileAndPath... filesInJar) throws Exception {
    // First, write the bogus jar file.
    String path = basePath + "jar" + jarCounter.incrementAndGet() + ".jar";
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    FileOutputStream fos = new FileOutputStream(path);
    JarOutputStream jarOutputStream = new JarOutputStream(fos, manifest);
    // Directory entries for all packages have to be added explicitly for
    // resources to be findable via ClassLoader. Directory entries must end
    // with "/"; the initial one is expected to, also.
    Set<String> pathsInJar = new HashSet<>();
    for (FileAndPath fileAndPath : filesInJar) {
        String pathToAdd = fileAndPath.path;
        while (pathsInJar.add(pathToAdd)) {
            int ix = pathToAdd.lastIndexOf('/', pathToAdd.length() - 2);
            if (ix < 0) {
                break;
            }
            pathToAdd = pathToAdd.substring(0, ix);
        }
    }
    for (String pathInJar : pathsInJar) {
        jarOutputStream.putNextEntry(new JarEntry(pathInJar));
        jarOutputStream.closeEntry();
    }
    for (FileAndPath fileAndPath : filesInJar) {
        File file = fileAndPath.file;
        jarOutputStream.putNextEntry(new JarEntry(fileAndPath.path + file.getName()));
        byte[] allBytes = new byte[(int) file.length()];
        FileInputStream fis = new FileInputStream(file);
        fis.read(allBytes);
        fis.close();
        jarOutputStream.write(allBytes);
        jarOutputStream.closeEntry();
    }
    jarOutputStream.close();
    fos.close();
    // Add the file to classpath.
    File jarFile = new File(path);
    assertTrue(jarFile.exists());
    URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
    method.setAccessible(true);
    method.invoke(urlClassLoader, new Object[] { jarFile.toURI().toURL() });
    return jarFile.getAbsolutePath();
}
Also used : JarOutputStream(java.util.jar.JarOutputStream) Method(java.lang.reflect.Method) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) URLClassLoader(java.net.URLClassLoader) File(java.io.File) HashSet(java.util.HashSet)

Example 7 with JarEntry

use of java.util.jar.JarEntry in project hive by apache.

the class ClassNameCompleter method getClassNames.

public static String[] getClassNames() throws IOException {
    Set urls = new HashSet();
    for (ClassLoader loader = Thread.currentThread().getContextClassLoader(); loader != null; loader = loader.getParent()) {
        if (!(loader instanceof URLClassLoader)) {
            continue;
        }
        urls.addAll(Arrays.asList(((URLClassLoader) loader).getURLs()));
    }
    // Now add the URL that holds java.lang.String. This is because
    // some JVMs do not report the core classes jar in the list of
    // class loaders.
    Class[] systemClasses = new Class[] { String.class, javax.swing.JFrame.class };
    for (int i = 0; i < systemClasses.length; i++) {
        URL classURL = systemClasses[i].getResource("/" + systemClasses[i].getName().replace('.', '/') + clazzFileNameExtension);
        if (classURL != null) {
            URLConnection uc = classURL.openConnection();
            if (uc instanceof JarURLConnection) {
                urls.add(((JarURLConnection) uc).getJarFileURL());
            }
        }
    }
    Set classes = new HashSet();
    for (Iterator i = urls.iterator(); i.hasNext(); ) {
        URL url = (URL) i.next();
        try {
            File file = new File(url.getFile());
            if (file.isDirectory()) {
                Set files = getClassFiles(file.getAbsolutePath(), new HashSet(), file, new int[] { 200 });
                classes.addAll(files);
                continue;
            }
            if (!isJarFile(file)) {
                continue;
            }
            JarFile jf = new JarFile(file);
            for (Enumeration e = jf.entries(); e.hasMoreElements(); ) {
                JarEntry entry = (JarEntry) e.nextElement();
                if (entry == null) {
                    continue;
                }
                String name = entry.getName();
                if (isClazzFile(name)) {
                    /* only use class file */
                    classes.add(name);
                } else if (isJarFile(name)) {
                    classes.addAll(getClassNamesFromJar(name));
                } else {
                    continue;
                }
            }
        } catch (IOException e) {
            throw new IOException(String.format("Error reading classpath entry: %s", url), e);
        }
    }
    // now filter classes by changing "/" to "." and trimming the
    // trailing ".class"
    Set classNames = new TreeSet();
    for (Iterator i = classes.iterator(); i.hasNext(); ) {
        String name = (String) i.next();
        classNames.add(name.replace('/', '.').substring(0, name.length() - 6));
    }
    return (String[]) classNames.toArray(new String[classNames.size()]);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Enumeration(java.util.Enumeration) JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) TreeSet(java.util.TreeSet) URLClassLoader(java.net.URLClassLoader) Iterator(java.util.Iterator) URLClassLoader(java.net.URLClassLoader) JarFile(java.util.jar.JarFile) File(java.io.File) HashSet(java.util.HashSet)

Example 8 with JarEntry

use of java.util.jar.JarEntry in project tomcat by apache.

the class JspCompilationContext method getLastModified.

public Long getLastModified(String resource, Jar tagJar) {
    long result = -1;
    URLConnection uc = null;
    try {
        if (tagJar != null) {
            if (resource.startsWith("/")) {
                resource = resource.substring(1);
            }
            result = tagJar.getLastModified(resource);
        } else {
            URL jspUrl = getResource(resource);
            if (jspUrl == null) {
                incrementRemoved();
                return Long.valueOf(result);
            }
            uc = jspUrl.openConnection();
            if (uc instanceof JarURLConnection) {
                JarEntry jarEntry = ((JarURLConnection) uc).getJarEntry();
                if (jarEntry != null) {
                    result = jarEntry.getTime();
                } else {
                    result = uc.getLastModified();
                }
            } else {
                result = uc.getLastModified();
            }
        }
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.debug(Localizer.getMessage("jsp.error.lastModified", getJspFile()), e);
        }
        result = -1;
    } finally {
        if (uc != null) {
            try {
                uc.getInputStream().close();
            } catch (IOException e) {
                if (log.isDebugEnabled()) {
                    log.debug(Localizer.getMessage("jsp.error.lastModified", getJspFile()), e);
                }
                result = -1;
            }
        }
    }
    return Long.valueOf(result);
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) URL(java.net.URL)

Example 9 with JarEntry

use of java.util.jar.JarEntry in project tomcat by apache.

the class JarWarResource method getJarInputStreamWrapper.

@Override
protected JarInputStreamWrapper getJarInputStreamWrapper() {
    JarFile warFile = null;
    JarInputStream jarIs = null;
    JarEntry entry = null;
    try {
        warFile = getArchiveResourceSet().openJarFile();
        JarEntry jarFileInWar = warFile.getJarEntry(archivePath);
        InputStream isInWar = warFile.getInputStream(jarFileInWar);
        jarIs = new JarInputStream(isInWar);
        entry = jarIs.getNextJarEntry();
        while (entry != null && !entry.getName().equals(getResource().getName())) {
            entry = jarIs.getNextJarEntry();
        }
        if (entry == null) {
            return null;
        }
        return new JarInputStreamWrapper(entry, jarIs);
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("jarResource.getInputStreamFail", getResource().getName(), getBaseUrl()), e);
        }
        return null;
    } finally {
        if (entry == null) {
            if (jarIs != null) {
                try {
                    jarIs.close();
                } catch (IOException ioe) {
                // Ignore
                }
            }
            if (warFile != null) {
                getArchiveResourceSet().closeJarFile();
            }
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 10 with JarEntry

use of java.util.jar.JarEntry in project storm by apache.

the class Utils method extractDirFromJarImpl.

public void extractDirFromJarImpl(String jarpath, String dir, File destdir) {
    try (JarFile jarFile = new JarFile(jarpath)) {
        Enumeration<JarEntry> jarEnums = jarFile.entries();
        while (jarEnums.hasMoreElements()) {
            JarEntry entry = jarEnums.nextElement();
            if (!entry.isDirectory() && entry.getName().startsWith(dir)) {
                File aFile = new File(destdir, entry.getName());
                aFile.getParentFile().mkdirs();
                try (FileOutputStream out = new FileOutputStream(aFile);
                    InputStream in = jarFile.getInputStream(entry)) {
                    IOUtils.copy(in, out);
                }
            }
        }
    } catch (IOException e) {
        LOG.info("Could not extract {} from {}", dir, jarpath);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile)

Aggregations

JarEntry (java.util.jar.JarEntry)506 JarFile (java.util.jar.JarFile)238 File (java.io.File)188 IOException (java.io.IOException)167 InputStream (java.io.InputStream)116 JarOutputStream (java.util.jar.JarOutputStream)102 FileOutputStream (java.io.FileOutputStream)96 FileInputStream (java.io.FileInputStream)79 JarInputStream (java.util.jar.JarInputStream)76 URL (java.net.URL)70 ArrayList (java.util.ArrayList)55 Manifest (java.util.jar.Manifest)50 JarURLConnection (java.net.JarURLConnection)42 Test (org.junit.Test)34 ZipFile (java.util.zip.ZipFile)30 ZipEntry (java.util.zip.ZipEntry)29 OutputStream (java.io.OutputStream)26 HashSet (java.util.HashSet)26 BufferedInputStream (java.io.BufferedInputStream)21 ByteArrayInputStream (java.io.ByteArrayInputStream)20