Search in sources :

Example 6 with JarFile

use of java.util.jar.JarFile in project enumerable by hraberg.

the class LambdaCompiler method unjar.

File unjar(JarFile jarFile) throws IOException {
    InputStream in = null;
    OutputStream out = null;
    File tempDir = new File(getProperty("java.io.tmpdir"), new File(jarFile.getName()).getName() + "-" + currentTimeMillis());
    ensureDirCreated(tempDir);
    try {
        Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            File file = new File(tempDir, jarEntry.getName());
            if (jarEntry.isDirectory()) {
                file.mkdir();
            } else {
                file.getParentFile().mkdirs();
                in = jarFile.getInputStream(jarEntry);
                out = new FileOutputStream(file);
                int read;
                while ((read = in.read(buffer)) != -1) out.write(buffer, 0, read);
                out.flush();
            }
        }
        return tempDir;
    } finally {
        if (out != null)
            out.close();
        if (in != null)
            in.close();
        jarFile.close();
    }
}
Also used : JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile)

Example 7 with JarFile

use of java.util.jar.JarFile 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 8 with JarFile

use of java.util.jar.JarFile 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 9 with JarFile

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

the class BookmarksPlugin method getPluginJar.

/**
     * Returns the plugin JAR for the plugin of the provided name.
     *
     * @param pluginName the name of the plugin (cannot be null or empty).
     * @return The plugin JAR file, or null when not found.
     */
private static JarFile getPluginJar(final String pluginName) throws IOException {
    File pluginDir = new File(JiveGlobals.getHomeDirectory(), "plugins");
    File[] jars = pluginDir.listFiles(new FileFilter() {

        public boolean accept(File pathname) {
            return pathname.getName().equalsIgnoreCase(pluginName + ".jar");
        }
    });
    final File jar;
    if (jars.length > 0) {
        return new JarFile(jars[0]);
    } else {
        return null;
    }
}
Also used : JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile)

Example 10 with JarFile

use of java.util.jar.JarFile in project flink by apache.

the class JarListHandler method handleJsonRequest.

@Override
public String handleJsonRequest(Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager) throws Exception {
    try {
        StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
        gen.writeStartObject();
        gen.writeStringField("address", queryParams.get(RuntimeMonitorHandler.WEB_MONITOR_ADDRESS_KEY));
        gen.writeArrayFieldStart("files");
        File[] list = jarDir.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".jar");
            }
        });
        for (File f : list) {
            // separate the uuid and the name parts.
            String id = f.getName();
            int startIndex = id.indexOf("_");
            if (startIndex < 0) {
                continue;
            }
            String name = id.substring(startIndex + 1);
            if (name.length() < 5 || !name.endsWith(".jar")) {
                continue;
            }
            gen.writeStartObject();
            gen.writeStringField("id", id);
            gen.writeStringField("name", name);
            gen.writeNumberField("uploaded", f.lastModified());
            gen.writeArrayFieldStart("entry");
            String[] classes = new String[0];
            try {
                JarFile jar = new JarFile(f);
                Manifest manifest = jar.getManifest();
                String assemblerClass = null;
                if (manifest != null) {
                    assemblerClass = manifest.getMainAttributes().getValue(PackagedProgram.MANIFEST_ATTRIBUTE_ASSEMBLER_CLASS);
                    if (assemblerClass == null) {
                        assemblerClass = manifest.getMainAttributes().getValue(PackagedProgram.MANIFEST_ATTRIBUTE_MAIN_CLASS);
                    }
                }
                if (assemblerClass != null) {
                    classes = assemblerClass.split(",");
                }
            } catch (IOException ignored) {
            // we simply show no entries here
            }
            // show every entry class that can be loaded later on.
            for (String clazz : classes) {
                clazz = clazz.trim();
                PackagedProgram program = null;
                try {
                    program = new PackagedProgram(f, clazz, new String[0]);
                } catch (Exception ignored) {
                // ignore jar files which throw an error upon creating a PackagedProgram
                }
                if (program != null) {
                    gen.writeStartObject();
                    gen.writeStringField("name", clazz);
                    String desc = program.getDescription();
                    gen.writeStringField("description", desc == null ? "No description provided" : desc);
                    gen.writeEndObject();
                }
            }
            gen.writeEndArray();
            gen.writeEndObject();
        }
        gen.writeEndArray();
        gen.writeEndObject();
        gen.close();
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException("Failed to fetch jar list: " + e.getMessage(), e);
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) IOException(java.io.IOException) FilenameFilter(java.io.FilenameFilter) PackagedProgram(org.apache.flink.client.program.PackagedProgram) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

JarFile (java.util.jar.JarFile)1366 File (java.io.File)720 JarEntry (java.util.jar.JarEntry)616 IOException (java.io.IOException)587 URL (java.net.URL)272 InputStream (java.io.InputStream)271 ZipEntry (java.util.zip.ZipEntry)203 Manifest (java.util.jar.Manifest)186 ArrayList (java.util.ArrayList)158 Test (org.junit.Test)131 JarURLConnection (java.net.JarURLConnection)123 FileOutputStream (java.io.FileOutputStream)122 ZipFile (java.util.zip.ZipFile)121 FileInputStream (java.io.FileInputStream)111 Attributes (java.util.jar.Attributes)110 MalformedURLException (java.net.MalformedURLException)94 Enumeration (java.util.Enumeration)67 JarOutputStream (java.util.jar.JarOutputStream)65 HashSet (java.util.HashSet)58 URLClassLoader (java.net.URLClassLoader)55