Search in sources :

Example 81 with JarEntry

use of java.util.jar.JarEntry in project semantic-versioning by jeluard.

the class JarDiff method loadClasses.

/**
     * Load all the classes from the specified URL and store information
     * about them in the specified map.
     * This currently only works for jar files, <b>not</b> directories
     * which contain classes in subdirectories or in the current directory.
     *
     * @param infoMap the map to store the ClassInfo in.
     * @param file the jarfile to load classes from.
     * @throws IOException if there is an IOException reading info about a
     *                     class.
     */
private void loadClasses(Map infoMap, File file) throws DiffException {
    try {
        JarFile jar = new JarFile(file);
        Enumeration e = jar.entries();
        while (e.hasMoreElements()) {
            JarEntry entry = (JarEntry) e.nextElement();
            String name = entry.getName();
            if (!entry.isDirectory() && name.endsWith(".class")) {
                ClassReader reader = new ClassReader(jar.getInputStream(entry));
                ClassInfo ci = loadClassInfo(reader);
                infoMap.put(ci.getName(), ci);
            }
        }
    } catch (IOException ioe) {
        throw new DiffException(ioe);
    }
}
Also used : ClassReader(org.objectweb.asm.ClassReader) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 82 with JarEntry

use of java.util.jar.JarEntry in project rest.li by linkedin.

the class TestUtil method createSchemaJar.

public static void createSchemaJar(String jarFileName, Map<String, String> fileToSchemaMap, boolean debug) throws IOException {
    if (debug)
        out.println("creating " + jarFileName);
    FileOutputStream jarFileStream = new FileOutputStream(jarFileName);
    JarOutputStream jarStream = new JarOutputStream(jarFileStream, new Manifest());
    for (Map.Entry<String, String> entry : fileToSchemaMap.entrySet()) {
        String key = entry.getKey();
        // JARs use resource separator as the file separator
        String filename = "pegasus" + key.replace(File.separatorChar, '/');
        if (debug)
            out.println("  adding " + filename);
        JarEntry jarEntry = new JarEntry(filename);
        jarStream.putNextEntry(jarEntry);
        jarStream.write(entry.getValue().getBytes(Data.UTF_8_CHARSET));
    }
    jarStream.close();
    jarFileStream.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 83 with JarEntry

use of java.util.jar.JarEntry in project rest.li by linkedin.

the class FileFormatDataSchemaParser method parseSources.

public DataSchemaParser.ParseResult parseSources(String[] sources) throws IOException {
    final DataSchemaParser.ParseResult result = new DataSchemaParser.ParseResult();
    try {
        for (String source : sources) {
            final File sourceFile = new File(source);
            if (sourceFile.exists()) {
                if (sourceFile.isDirectory()) {
                    final FileUtil.FileExtensionFilter filter = new FileUtil.FileExtensionFilter(_schemaParserFactory.getLanguageExtension());
                    final List<File> sourceFilesInDirectory = FileUtil.listFiles(sourceFile, filter);
                    for (File f : sourceFilesInDirectory) {
                        parseFile(f, result);
                        result.getSourceFiles().add(f);
                    }
                } else {
                    if (sourceFile.getName().endsWith(".jar")) {
                        final JarFile jarFile = new JarFile(sourceFile);
                        final Enumeration<JarEntry> entries = jarFile.entries();
                        while (entries.hasMoreElements()) {
                            final JarEntry entry = entries.nextElement();
                            if (!entry.isDirectory() && entry.getName().endsWith(_schemaParserFactory.getLanguageExtension())) {
                                parseJarEntry(jarFile, entry, result);
                            }
                        }
                    } else {
                        parseFile(sourceFile, result);
                    }
                    result.getSourceFiles().add(sourceFile);
                }
            } else {
                final StringBuilder errorMessage = new StringBuilder();
                final DataSchema schema = _schemaResolver.findDataSchema(source, errorMessage);
                if (schema == null) {
                    result._messageBuilder.append("File cannot be opened or schema name cannot be resolved: ").append(source).append("\n");
                }
                if (errorMessage.length() > 0) {
                    result._messageBuilder.append(errorMessage.toString());
                }
            }
        }
        if (result._messageBuilder.length() > 0) {
            throw new IOException(result.getMessage());
        }
        for (Map.Entry<String, DataSchemaLocation> entry : _schemaResolver.nameToDataSchemaLocations().entrySet()) {
            final DataSchema schema = _schemaResolver.bindings().get(entry.getKey());
            result.getSchemaAndLocations().put(schema, entry.getValue());
        }
        return result;
    } catch (RuntimeException e) {
        if (result._messageBuilder.length() > 0) {
            e = new RuntimeException("Unexpected " + e.getClass().getSimpleName() + " encountered.\n" + "This may be caused by the following parsing or processing errors:\n" + result.getMessage(), e);
        }
        throw e;
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) InJarFileDataSchemaLocation(com.linkedin.data.schema.resolver.InJarFileDataSchemaLocation) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) FileDataSchemaLocation(com.linkedin.data.schema.resolver.FileDataSchemaLocation) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) JarFile(java.util.jar.JarFile) File(java.io.File) FileUtil(com.linkedin.util.FileUtil) Map(java.util.Map)

Example 84 with JarEntry

use of java.util.jar.JarEntry in project Openfire by igniterealtime.

the class ModuleLoader method loadNonBridgeModule.

private void loadNonBridgeModule(String dirEntry) throws IOException {
    JarFile jarFile = new JarFile(dirEntry);
    Enumeration entries = jarFile.entries();
    if (entries == null) {
        Logger.println("No entries in jarFile:  " + dirEntry);
        return;
    }
    while (entries.hasMoreElements()) {
        JarEntry jarEntry = (JarEntry) entries.nextElement();
        String className = jarEntry.getName();
        int ix;
        if ((ix = className.indexOf(".class")) < 0) {
            if (Logger.logLevel >= Logger.LOG_INFO) {
                Logger.println("Skipping non-class entry in jarFile:  " + className);
            }
            continue;
        }
        className = className.replaceAll(".class", "");
        className = className.replaceAll("/", ".");
        try {
            if (Logger.logLevel >= Logger.LOG_INFO) {
                Logger.println("Looking for class '" + className + "'");
            }
            // load the class
            loadClass(className);
        } catch (ClassNotFoundException e) {
            Logger.println("ClassNotFoundException:  '" + className + "'");
        }
    }
}
Also used : Enumeration(java.util.Enumeration) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 85 with JarEntry

use of java.util.jar.JarEntry in project jersey by jersey.

the class JarUtils method createJarFile.

public static File createJarFile(final String name, final Suffix s, final String base, final Map<String, String> entries) throws IOException {
    final File tempJar = File.createTempFile(name, "." + s);
    tempJar.deleteOnExit();
    final JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(tempJar)), new Manifest());
    final Set<String> usedSegments = new HashSet<String>();
    for (final Map.Entry<String, String> entry : entries.entrySet()) {
        for (final String path : getPaths(entry.getValue())) {
            if (usedSegments.contains(path)) {
                continue;
            }
            usedSegments.add(path);
            final JarEntry e = new JarEntry(path);
            jos.putNextEntry(e);
            jos.closeEntry();
        }
        final JarEntry e = new JarEntry(entry.getValue());
        jos.putNextEntry(e);
        final InputStream f = new BufferedInputStream(new FileInputStream(base + entry.getKey()));
        final byte[] buf = new byte[1024];
        int read = 1024;
        while ((read = f.read(buf, 0, read)) != -1) {
            jos.write(buf, 0, read);
        }
        jos.closeEntry();
    }
    jos.close();
    return tempJar;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

JarEntry (java.util.jar.JarEntry)594 JarFile (java.util.jar.JarFile)290 File (java.io.File)217 IOException (java.io.IOException)187 InputStream (java.io.InputStream)134 JarOutputStream (java.util.jar.JarOutputStream)112 FileOutputStream (java.io.FileOutputStream)109 FileInputStream (java.io.FileInputStream)92 URL (java.net.URL)87 JarInputStream (java.util.jar.JarInputStream)87 ArrayList (java.util.ArrayList)67 Manifest (java.util.jar.Manifest)58 JarURLConnection (java.net.JarURLConnection)53 Test (org.junit.Test)39 HashSet (java.util.HashSet)31 ZipEntry (java.util.zip.ZipEntry)31 ZipFile (java.util.zip.ZipFile)30 OutputStream (java.io.OutputStream)29 BufferedInputStream (java.io.BufferedInputStream)26 Enumeration (java.util.Enumeration)26