Example usage for java.util.jar JarEntry isDirectory

List of usage examples for java.util.jar JarEntry isDirectory

Introduction

In this page you can find the example usage for java.util.jar JarEntry isDirectory.

Prototype

public boolean isDirectory() 

Source Link

Document

Returns true if this is a directory entry.

Usage

From source file:io.vertx.lang.js.JSVerticleFactory.java

private boolean hasNodeModules(URL url) {
    JarEntry modules = null;
    try {//from w  ww .  j av a  2 s  .co  m
        modules = ClasspathFileResolver.getJarEntry(url, "node_modules");
    } catch (IOException ex) {
        log.warn(ex.toString());
        return false;
    }
    return modules != null && (modules.isDirectory() || modules.getSize() == 0);
}

From source file:com.tobedevoured.solrsail.SolrConfig.java

/**
 * Install Solr Config t the local file system by extracting from the
 * SolrSail jar//from w ww.ja  va 2 s .  c  o m
 * 
 * @param jar File
 * @throws IOException
 */
public void installFromJar(File jar) throws IOException {
    logger.info("Installing config from Jar to {}", this.getSolrHome());
    logger.debug("Opening Jar {}", jar.toString());

    JarFile jarFile = new JarFile(jar);

    for (Enumeration<JarEntry> enumeration = jarFile.entries(); enumeration.hasMoreElements();) {
        JarEntry entry = enumeration.nextElement();

        if (!entry.getName().equals("solr/") && entry.getName().startsWith("solr/")) {
            StringBuilder dest = new StringBuilder(getSolrHome()).append(File.separator)
                    .append(entry.getName().replaceFirst("solr/", ""));

            File file = new File(dest.toString());

            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                if (file.getParentFile() != null) {
                    file.getParentFile().mkdirs();
                }

                logger.debug("Copying {} to {}", entry.getName(), dest.toString());

                InputStream input = jarFile.getInputStream(entry);
                Writer writer = new FileWriter(file.getAbsoluteFile());
                IOUtils.copy(input, writer);

                input.close();

                writer.close();
            }
        }
    }
}

From source file:org.springframework.boot.loader.tools.JarWriter.java

private long getNestedLibraryTime(File file) {
    try {//  w w  w .j  av  a 2  s .co  m
        try (JarFile jarFile = new JarFile(file)) {
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (!entry.isDirectory()) {
                    return entry.getTime();
                }
            }
        }
    } catch (Exception ex) {
        // Ignore and just use the source file timestamp
    }
    return file.lastModified();
}

From source file:com.azurenight.maven.TroposphereMojo.java

private Collection<File> extractAllFiles(File outputDirectory, ZipFile ja, Enumeration<JarEntry> en)
        throws MojoExecutionException {
    List<File> files = new ArrayList<File>();
    while (en.hasMoreElements()) {
        JarEntry el = en.nextElement();
        if (!el.isDirectory()) {
            File destFile = new File(outputDirectory, el.getName());
            if (OVERRIDE || !destFile.exists()) {
                destFile.getParentFile().mkdirs();
                try {
                    FileOutputStream fo = new FileOutputStream(destFile);
                    IOUtils.copy(ja.getInputStream(el), fo);
                    fo.close();//from   ww w.  ja  va  2  s .co m
                } catch (IOException e) {
                    throw new MojoExecutionException(
                            "extracting " + el.getName() + " from jython artifact jar failed", e);
                }
            }
            files.add(destFile);
        }
    }
    return files;
}

From source file:org.amanzi.awe.scripting.utils.ScriptUtils.java

/**
 * @param pluginName/*w  w w . j a  v a2 s .  co  m*/
 * @param loadPath
 * @throws ScriptingException
 */
private void makePluginLoadName(final String pluginName, final List<String> loadPath)
        throws ScriptingException {
    if (StringUtils.isEmpty(pluginName)) {
        LOGGER.warn("Plugin name is empty");
        return;
    }
    String pluginPath = getPluginRoot(pluginName);
    if (StringUtils.isEmpty(pluginPath)) {
        LOGGER.warn("Plugin not found");
        return;
    }
    // loadPath.add(pluginPath);
    JarFile jarFile = null;
    if (pluginPath.startsWith(PREFIX_FILE) && pluginPath.endsWith(POSTFIX_JAR)) {
        String path = prepareJarPath(pluginPath);
        try {
            jarFile = new JarFile(path);
        } catch (IOException e) {
            LOGGER.error("can't find jar file", e);
        }
    }
    if (jarFile == null) {
        File rootFolder = new File(pluginPath + JRUBY_PLUGI_LIB);
        if (rootFolder.exists()) {
            for (File file : rootFolder.listFiles()) {
                if (file.isDirectory()) {
                    loadPath.add(file.getAbsolutePath());
                }
            }
        }

    } else {
        JarEntry entry;
        for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) {
            entry = entries.nextElement();
            LOGGER.info(entry.getName());
            if (entry.isDirectory() && entry.getName().contains(JRUBY_PLUGI_LIB)) {
                loadPath.add(entry.getName().substring(0, entry.getName().length() - 1));
                LOGGER.info("initialized with jar entry " + entry.getName());
            }
        }
    }

    loadPath.add(JRUBY_PLUGI_LIB);
}

From source file:org.doctester.rendermachine.RenderMachineImpl.java

private void unzipFromJar(String classpathLocation, String destinationDirectory) {

    try {//from   ww w  .  j av a 2 s.  c  o m

        URL url = this.getClass().getClassLoader().getResource(classpathLocation);

        JarURLConnection urlcon = (JarURLConnection) (url.openConnection());
        try (JarFile jar = urlcon.getJarFile();) {
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry jarEntry = entries.nextElement();

                if (jarEntry.isDirectory()) {

                    new File(destinationDirectory + File.separator + jarEntry.getName()).mkdirs();

                } else {

                    ByteStreams.copy(jar.getInputStream(jarEntry), new FileOutputStream(
                            new File(destinationDirectory + File.separator + jarEntry.getName())));

                }

            }
        }

    } catch (IOException e) {
        logger.error("An error occurred while copying from webjars archive to site directory", e);
    }

}

From source file:com.technophobia.substeps.report.DefaultExecutionReportBuilder.java

public void copyJarResourcesRecursively(final File destination, final JarURLConnection jarConnection)
        throws IOException {
    final JarFile jarFile = jarConnection.getJarFile();
    for (final JarEntry entry : Collections.list(jarFile.entries())) {
        if (entry.getName().startsWith(jarConnection.getEntryName())) {
            final String fileName = StringUtils.removeStart(entry.getName(), jarConnection.getEntryName());
            if (!entry.isDirectory()) {
                InputStream entryInputStream = null;
                try {
                    entryInputStream = jarFile.getInputStream(entry);
                    FileUtils.copyInputStreamToFile(entryInputStream, new File(destination, fileName));
                } finally {
                    IOUtils.closeQuietly(entryInputStream);
                }//w ww . j a  v a 2s .c  o m
            } else {
                new File(destination, fileName).mkdirs();
            }
        }
    }
}

From source file:com.netflix.nicobar.core.persistence.JarArchiveRepository.java

@Override
public void insertArchive(JarScriptArchive jarScriptArchive) throws IOException {
    Objects.requireNonNull(jarScriptArchive, "jarScriptArchive");
    ScriptModuleSpec moduleSpec = jarScriptArchive.getModuleSpec();
    ModuleId moduleId = moduleSpec.getModuleId();
    Path moduleJarPath = getModuleJarPath(moduleId);
    Files.deleteIfExists(moduleJarPath);
    JarFile sourceJarFile;// w w w .  j a v a  2  s  .c  o  m
    try {
        sourceJarFile = new JarFile(jarScriptArchive.getRootUrl().toURI().getPath());
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
    JarOutputStream destJarFile = new JarOutputStream(new FileOutputStream(moduleJarPath.toFile()));
    try {
        String moduleSpecFileName = moduleSpecSerializer.getModuleSpecFileName();
        Enumeration<JarEntry> sourceEntries = sourceJarFile.entries();
        while (sourceEntries.hasMoreElements()) {
            JarEntry sourceEntry = sourceEntries.nextElement();
            if (sourceEntry.getName().equals(moduleSpecFileName)) {
                // avoid double entry for module spec
                continue;
            }
            destJarFile.putNextEntry(sourceEntry);
            if (!sourceEntry.isDirectory()) {
                InputStream inputStream = sourceJarFile.getInputStream(sourceEntry);
                IOUtils.copy(inputStream, destJarFile);
                IOUtils.closeQuietly(inputStream);
            }
            destJarFile.closeEntry();
        }
        // write the module spec
        String serialized = moduleSpecSerializer.serialize(moduleSpec);
        JarEntry moduleSpecEntry = new JarEntry(moduleSpecSerializer.getModuleSpecFileName());
        destJarFile.putNextEntry(moduleSpecEntry);
        IOUtils.write(serialized, destJarFile);
        destJarFile.closeEntry();
    } finally {
        IOUtils.closeQuietly(sourceJarFile);
        IOUtils.closeQuietly(destJarFile);
    }
    // update the timestamp on the jar file to indicate that the module has been updated
    Files.setLastModifiedTime(moduleJarPath, FileTime.fromMillis(jarScriptArchive.getCreateTime()));
}

From source file:org.drools.guvnor.server.contenthandler.ModelContentHandler.java

private Set<String> getImportsFromJar(AssetItem assetItem) throws IOException {

    Set<String> imports = new HashSet<String>();
    Map<String, String> nonCollidingImports = new HashMap<String, String>();
    String assetPackageName = assetItem.getModuleName();

    //Setup class-loader to check for class visibility
    JarInputStream cljis = new JarInputStream(assetItem.getBinaryContentAttachment());
    List<JarInputStream> jarInputStreams = new ArrayList<JarInputStream>();
    jarInputStreams.add(cljis);//from   w  w  w. j  av  a 2 s .  c  om
    ClassLoaderBuilder clb = new ClassLoaderBuilder(jarInputStreams);
    ClassLoader cl = clb.buildClassLoader();

    //Reset stream to read classes
    JarInputStream jis = new JarInputStream(assetItem.getBinaryContentAttachment());
    JarEntry entry = null;

    //Get Class names from JAR, only the first occurrence of a given Class leaf name will be inserted. Thus 
    //"org.apache.commons.lang.NumberUtils" will be imported but "org.apache.commons.lang.math.NumberUtils"
    //will not, assuming it follows later in the JAR structure.
    while ((entry = jis.getNextJarEntry()) != null) {
        if (!entry.isDirectory()) {
            if (entry.getName().endsWith(".class") && !entry.getName().endsWith("package-info.class")) {
                final String fullyQualifiedName = convertPathToName(entry.getName());
                final String fullyQualifiedClassName = convertPathToClassName(entry.getName());
                if (isClassVisible(cl, fullyQualifiedClassName, assetPackageName)) {
                    String leafName = getLeafName(fullyQualifiedName);
                    if (!nonCollidingImports.containsKey(leafName)) {
                        nonCollidingImports.put(leafName, fullyQualifiedName);
                    }
                }
            }
        }
    }

    //Build list of imports
    for (String value : nonCollidingImports.values()) {
        String line = "import " + value;
        imports.add(line);
    }

    return imports;
}

From source file:org.apache.storm.utils.Utils.java

public static Map<String, Object> getConfigFromClasspath(List<String> cp, Map<String, Object> conf)
        throws IOException {
    if (cp == null || cp.isEmpty()) {
        return conf;
    }//w w  w  .  ja  va2 s  .  c  o m
    Yaml yaml = new Yaml(new SafeConstructor());
    Map<String, Object> defaultsConf = null;
    Map<String, Object> stormConf = null;
    for (String part : cp) {
        File f = new File(part);
        if (f.isDirectory()) {
            if (defaultsConf == null) {
                defaultsConf = readConfIgnoreNotFound(yaml, new File(f, "defaults.yaml"));
            }

            if (stormConf == null) {
                stormConf = readConfIgnoreNotFound(yaml, new File(f, "storm.yaml"));
            }
        } else {
            //Lets assume it is a jar file
            try (JarFile jarFile = new JarFile(f)) {
                Enumeration<JarEntry> jarEnums = jarFile.entries();
                while (jarEnums.hasMoreElements()) {
                    JarEntry entry = jarEnums.nextElement();
                    if (!entry.isDirectory()) {
                        if (defaultsConf == null && entry.getName().equals("defaults.yaml")) {
                            try (InputStream in = jarFile.getInputStream(entry)) {
                                defaultsConf = (Map<String, Object>) yaml.load(new InputStreamReader(in));
                            }
                        }

                        if (stormConf == null && entry.getName().equals("storm.yaml")) {
                            try (InputStream in = jarFile.getInputStream(entry)) {
                                stormConf = (Map<String, Object>) yaml.load(new InputStreamReader(in));
                            }
                        }
                    }
                }
            }
        }
    }
    if (stormConf != null) {
        defaultsConf.putAll(stormConf);
    }
    return defaultsConf;
}