Example usage for java.util.jar JarEntry getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the name of the entry.

Usage

From source file:com.liferay.ide.project.core.modules.ServiceWrapperCommand.java

private Map<String, String[]> _getDynamicServiceWrapper() throws IOException {
    PortalRuntime portalRuntime = (PortalRuntime) _server.getRuntime().loadAdapter(PortalRuntime.class, null);

    IPath bundleLibPath = portalRuntime.getAppServerLibGlobalDir();
    IPath bundleServerPath = portalRuntime.getAppServerDir();

    Map<String, String[]> map = new LinkedHashMap<>();

    List<File> libFiles;

    File portalkernelJar = null;//w  w  w.ja v a 2  s.  co  m

    try {
        libFiles = FileListing.getFileListing(new File(bundleLibPath.toOSString()));

        for (File lib : libFiles) {
            if (FileUtil.exists(lib) && lib.getName().endsWith("portal-kernel.jar")) {
                portalkernelJar = lib;

                break;
            }
        }

        libFiles = FileListing.getFileListing(new File(bundleServerPath.append("../osgi").toOSString()));

        libFiles.add(portalkernelJar);

        if (ListUtil.isNotEmpty(libFiles)) {
            for (File lib : libFiles) {
                if (lib.getName().endsWith(".lpkg")) {
                    try (JarFile jar = new JarFile(lib)) {
                        Enumeration<JarEntry> enu = jar.entries();

                        while (enu.hasMoreElements()) {
                            try {
                                JarEntry entry = enu.nextElement();

                                String name = entry.getName();

                                if (name.contains(".api-")) {
                                    JarEntry jarentry = jar.getJarEntry(name);

                                    try (InputStream inputStream = jar.getInputStream(jarentry);
                                            JarInputStream jarInputStream = new JarInputStream(inputStream)) {
                                        JarEntry nextJarEntry;

                                        while ((nextJarEntry = jarInputStream.getNextJarEntry()) != null) {
                                            String entryName = nextJarEntry.getName();

                                            _getServiceWrapperList(map, entryName, jarInputStream);
                                        }
                                    }
                                }
                            } catch (Exception e) {
                            }
                        }
                    }
                } else if (lib.getName().endsWith("api.jar") || lib.getName().equals("portal-kernel.jar")) {

                    try (JarFile jar = new JarFile(lib);
                            InputStream inputStream = Files.newInputStream(lib.toPath());
                            JarInputStream jarinput = new JarInputStream(inputStream)) {

                        Enumeration<JarEntry> enu = jar.entries();

                        while (enu.hasMoreElements()) {
                            JarEntry entry = enu.nextElement();

                            String name = entry.getName();

                            _getServiceWrapperList(map, name, jarinput);
                        }
                    } catch (IOException ioe) {
                    }
                }
            }
        }
    } catch (FileNotFoundException fnfe) {
    }

    return map;
}

From source file:org.batfish.common.plugin.PluginConsumer.java

private void loadPluginJar(Path path) {
    /*/*from  w  ww. java 2  s.  co  m*/
     * Adapted from
     * http://stackoverflow.com/questions/11016092/how-to-load-classes-at-
     * runtime-from-a-folder-or-jar Retrieved: 2016-08-31 Original Authors:
     * Kevin Crain http://stackoverflow.com/users/2688755/kevin-crain
     * Apfelsaft http://stackoverflow.com/users/1447641/apfelsaft License:
     * https://creativecommons.org/licenses/by-sa/3.0/
     */
    String pathString = path.toString();
    if (pathString.endsWith(".jar")) {
        try {
            URL[] urls = { new URL("jar:file:" + pathString + "!/") };
            URLClassLoader cl = URLClassLoader.newInstance(urls, _currentClassLoader);
            _currentClassLoader = cl;
            Thread.currentThread().setContextClassLoader(cl);
            JarFile jar = new JarFile(path.toFile());
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry element = entries.nextElement();
                String name = element.getName();
                if (element.isDirectory() || !name.endsWith(CLASS_EXTENSION)) {
                    continue;
                }
                String className = name.substring(0, name.length() - CLASS_EXTENSION.length()).replace("/",
                        ".");
                try {
                    cl.loadClass(className);
                    Class<?> pluginClass = Class.forName(className, true, cl);
                    if (!Plugin.class.isAssignableFrom(pluginClass)
                            || Modifier.isAbstract(pluginClass.getModifiers())) {
                        continue;
                    }
                    Constructor<?> pluginConstructor;
                    try {
                        pluginConstructor = pluginClass.getConstructor();
                    } catch (NoSuchMethodException | SecurityException e) {
                        throw new BatfishException(
                                "Could not find default constructor in plugin: '" + className + "'", e);
                    }
                    Object pluginObj;
                    try {
                        pluginObj = pluginConstructor.newInstance();
                    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                            | InvocationTargetException e) {
                        throw new BatfishException(
                                "Could not instantiate plugin '" + className + "' from constructor", e);
                    }
                    Plugin plugin = (Plugin) pluginObj;
                    plugin.initialize(this);

                } catch (ClassNotFoundException e) {
                    jar.close();
                    throw new BatfishException("Unexpected error loading classes from jar", e);
                }
            }
            jar.close();
        } catch (IOException e) {
            throw new BatfishException("Error loading plugin jar: '" + path.toString() + "'", e);
        }
    }
}

From source file:com.amalto.core.jobox.watch.JoboxListener.java

public void contextChanged(String jobFile, String context) {
    File entity = new File(jobFile);
    String sourcePath = jobFile;//from w w  w .  ja va  2 s .co  m
    int dotMark = jobFile.lastIndexOf("."); //$NON-NLS-1$
    int separateMark = jobFile.lastIndexOf(File.separatorChar);
    if (dotMark != -1) {
        sourcePath = System.getProperty("java.io.tmpdir") + File.separatorChar //$NON-NLS-1$
                + jobFile.substring(separateMark, dotMark);
    }
    try {
        JoboxUtil.extract(jobFile, System.getProperty("java.io.tmpdir") + File.separatorChar); //$NON-NLS-1$
    } catch (Exception e1) {
        LOGGER.error("Extraction exception occurred.", e1);
        return;
    }
    List<File> resultList = new ArrayList<File>();
    JoboxUtil.findFirstFile(null, new File(sourcePath), "classpath.jar", resultList); //$NON-NLS-1$
    if (!resultList.isEmpty()) {
        JarInputStream jarIn = null;
        JarOutputStream jarOut = null;
        try {
            JarFile jarFile = new JarFile(resultList.get(0));
            Manifest mf = jarFile.getManifest();
            jarIn = new JarInputStream(new FileInputStream(resultList.get(0)));
            Manifest newManifest = jarIn.getManifest();
            if (newManifest == null) {
                newManifest = new Manifest();
            }
            newManifest.getMainAttributes().putAll(mf.getMainAttributes());
            newManifest.getMainAttributes().putValue("activeContext", context); //$NON-NLS-1$
            jarOut = new JarOutputStream(new FileOutputStream(resultList.get(0)), newManifest);
            byte[] buf = new byte[4096];
            JarEntry entry;
            while ((entry = jarIn.getNextJarEntry()) != null) {
                if ("META-INF/MANIFEST.MF".equals(entry.getName())) { //$NON-NLS-1$
                    continue;
                }
                jarOut.putNextEntry(entry);
                int read;
                while ((read = jarIn.read(buf)) != -1) {
                    jarOut.write(buf, 0, read);
                }
                jarOut.closeEntry();
            }
        } catch (Exception e) {
            LOGGER.error("Extraction exception occurred.", e);
        } finally {
            IOUtils.closeQuietly(jarIn);
            IOUtils.closeQuietly(jarOut);
        }
        // re-zip file
        if (entity.getName().endsWith(".zip")) { //$NON-NLS-1$
            File sourceFile = new File(sourcePath);
            try {
                JoboxUtil.zip(sourceFile, jobFile);
            } catch (Exception e) {
                LOGGER.error("Zip exception occurred.", e);
            }
        }
    }
}

From source file:com.l2jserver.service.game.scripting.impl.ecj.EclipseCompilerScriptClassLoader.java

/**
 * AddsLibrary jar//from w  ww.j  av a2  s .  co m
 * 
 * @param file
 *            jar file to add
 * @throws IOException
 *             if any I/O error occur
 */
@Override
public void addLibrary(File file) throws IOException {
    URL fileURL = file.toURI().toURL();
    addURL(fileURL);

    JarFile jarFile = new JarFile(file);

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

        String name = entry.getName();
        if (name.endsWith(".class")) {
            name = name.substring(0, name.length() - 6);
            name = name.replace('/', '.');
            libraryClasses.add(name);
        }
    }

    jarFile.close();
}

From source file:org.spoutcraft.launcher.launch.MinecraftClassLoader.java

private void index(File file) throws IOException {
    JarFile jar = null;/*w ww.  ja  v a  2s  .  c  om*/
    try {
        jar = new JarFile(file);
        Enumeration<JarEntry> i = jar.entries();
        while (i.hasMoreElements()) {
            JarEntry entry = i.nextElement();
            if (entry.getName().endsWith(".class")) {
                String name = entry.getName();
                name = name.replace("/", ".").substring(0, name.length() - 6);
                classLocations.put(name, file);
            }
        }
    } catch (IOException e) {
        throw e;
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (IOException ignore) {
            }
        }
    }
}

From source file:com.krikelin.spotifysource.AppInstaller.java

protected void extractJar(String destDir, String jarFile) throws IOException {
    File destination = new File(destDir);
    java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
    java.util.Enumeration<JarEntry> enu = jar.entries();
    while (enu.hasMoreElements()) {
        try {//from  w w w  .j  a v  a  2s  .  co  m
            java.util.jar.JarEntry file = (java.util.jar.JarEntry) enu.nextElement();
            java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
            if (file.isDirectory()) { // if its a directory, create it
                f.mkdir();
                continue;
            }
            java.io.InputStream is = jar.getInputStream(file); // get the input stream
            if (!(destination.exists())) {
                destination.createNewFile();
            }

            java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
            while (is.available() > 0) { // write contents of 'is' to 'fos'
                fos.write(is.read());
            }
            fos.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:org.rhq.enterprise.server.plugin.pc.perspective.PerspectiveServerPluginManager.java

private void deployEmbeddedWars(ServerPluginEnvironment env) {
    String name = null;/*from   w  ww.  j  a  v  a2  s.c  o  m*/
    try {
        JarFile pluginJarFile = new JarFile(env.getPluginUrl().getFile());
        try {
            for (JarEntry entry : Collections.list(pluginJarFile.entries())) {
                name = entry.getName();
                if (name.toLowerCase().endsWith(".war")) {
                    deployWar(env, entry.getName(), pluginJarFile.getInputStream(entry));
                }
            }
        } finally {
            pluginJarFile.close();
        }
    } catch (Exception e) {
        Throwable t = (e instanceof MBeanException) ? e.getCause() : e;
        log.error("Failed to deploy " + env.getPluginKey().getPluginName() + "#" + name, t);
    }
}

From source file:com.izforge.izpack.installer.unpacker.Pack200FileUnpackerTest.java

/**
 * Returns a file from a jar as a byte array.
 *
 * @param file the jar file//from  w ww . j ava2s.co m
 * @param name the entry name
 * @return the file content
 * @throws IOException for any I/O error
 */
private byte[] getEntry(File file, String name) throws IOException {
    JarInputStream stream = new JarInputStream(new FileInputStream(file));
    try {
        JarEntry entry;
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        while ((entry = stream.getNextJarEntry()) != null) {
            if (entry.getName().endsWith(name)) {
                IOUtils.copy(stream, bytes);
                return bytes.toByteArray();
            }
        }
        fail("Entry not found: " + name);
    } finally {
        stream.close();
    }
    return null;
}

From source file:com.jayway.maven.plugins.android.phase04processclasses.DexMojo.java

private void unjar(JarFile jarFile, File outputDirectory) throws IOException {
    for (Enumeration en = jarFile.entries(); en.hasMoreElements();) {
        JarEntry entry = (JarEntry) en.nextElement();
        File entryFile = new File(outputDirectory, entry.getName());
        if (!entryFile.getParentFile().exists() && !entry.getName().startsWith("META-INF")) {
            entryFile.getParentFile().mkdirs();
        }//from w  ww  .ja v a 2s  .c o m
        if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
            final InputStream in = jarFile.getInputStream(entry);
            try {
                final OutputStream out = new FileOutputStream(entryFile);
                try {
                    IOUtil.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            } finally {
                IOUtils.closeQuietly(in);
            }
        }
    }
}

From source file:gov.nih.nci.restgen.util.JarHelper.java

/**
 * Given an InputStream on a jar file, unjars the contents into the given
 * directory.//from  ww w  .  j av  a2s . c om
 */
public void unjar(InputStream in, File destDir) throws IOException {
    BufferedOutputStream dest = null;
    JarInputStream jis = new JarInputStream(in);
    JarEntry entry;
    while ((entry = jis.getNextJarEntry()) != null) {
        if (entry.isDirectory()) {
            File dir = new File(destDir, entry.getName());
            dir.mkdir();
            if (entry.getTime() != -1)
                dir.setLastModified(entry.getTime());
            continue;
        }
        int count;
        byte data[] = new byte[BUFFER_SIZE];
        File destFile = new File(destDir, entry.getName());
        if (mVerbose) {
            //System.out.println("unjarring " + destFile + " from " + entry.getName());
        }
        FileOutputStream fos = new FileOutputStream(destFile);
        dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = jis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
        if (entry.getTime() != -1)
            destFile.setLastModified(entry.getTime());
    }
    jis.close();
}