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:org.javaan.bytecode.JarFileLoader.java

private void processEntry(String path, String fileName, JarFile jar, List<Type> classes, JarEntry entry)
        throws IOException {
    if (!entry.isDirectory()) {
        String name = entry.getName();
        boolean isClass = name.endsWith(".class");
        boolean isLibrary = name.endsWith(".jar") || name.endsWith(".war") || name.endsWith(".ear");
        if (isClass) {
            ClassParser parser = new ClassParser(fileName, entry.getName());
            JavaClass javaClass = parser.parse();
            String filePath = path + File.pathSeparator + javaClass.getFileName();
            Type type = Type.create(javaClass, filePath);
            classes.add(type);//ww w  . j a va  2s  .c o  m
        } else if (isLibrary) {
            InputStream input = jar.getInputStream(entry);
            try {
                processJar(path + File.pathSeparator + entry.getName(), input, classes);
            } finally {
                input.close();
            }
        }
    }
}

From source file:com.xiovr.unibot.plugin.impl.PluginLoaderImpl.java

private Properties getPluginProps(File file) throws IOException {
    Properties result = null;// w  ww  . j  a  v  a 2s  .  c o m
    JarFile jar = new JarFile(file);
    Enumeration<JarEntry> entries = jar.entries();

    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        if (entry.getName().equals(PROPERTY_FILE_NAME)) {
            // That's it! Load props
            InputStream is = null;
            try {
                is = jar.getInputStream(entry);
                result = new Properties();
                result.load(is);
            } finally {
                if (is != null)
                    is.close();
            }
        }
    }
    jar.close();
    return result;
}

From source file:org.b3log.latke.servlet.RequestProcessors.java

/**
 * Scans classpath (lib directory) to discover request processor classes.
 *//*from   w w w .j a  v a 2s.  c o  m*/
private static void discoverFromLibDir() {
    final String webRoot = AbstractServletListener.getWebRoot();
    final File libDir = new File(
            webRoot + File.separator + "WEB-INF" + File.separator + "lib" + File.separator);
    @SuppressWarnings("unchecked")
    final Collection<File> files = FileUtils.listFiles(libDir, new String[] { "jar" }, true);

    final ClassLoader classLoader = RequestProcessors.class.getClassLoader();

    try {
        for (final File file : files) {
            if (file.getName().contains("appengine-api") || file.getName().startsWith("freemarker")
                    || file.getName().startsWith("javassist") || file.getName().startsWith("commons")
                    || file.getName().startsWith("mail") || file.getName().startsWith("activation")
                    || file.getName().startsWith("slf4j") || file.getName().startsWith("bonecp")
                    || file.getName().startsWith("jsoup") || file.getName().startsWith("guava")
                    || file.getName().startsWith("markdown") || file.getName().startsWith("mysql")
                    || file.getName().startsWith("c3p0")) {
                // Just skips some known dependencies hardly....
                LOGGER.log(Level.INFO, "Skipped request processing discovery[jarName={0}]", file.getName());

                continue;
            }

            final JarFile jarFile = new JarFile(file.getPath());
            final Enumeration<JarEntry> entries = jarFile.entries();

            while (entries.hasMoreElements()) {
                final JarEntry jarEntry = entries.nextElement();
                final String classFileName = jarEntry.getName();

                if (classFileName.contains("$") // Skips inner class
                        || !classFileName.endsWith(".class")) {
                    continue;
                }

                final DataInputStream classInputStream = new DataInputStream(jarFile.getInputStream(jarEntry));

                final ClassFile classFile = new ClassFile(classInputStream);
                final AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) classFile
                        .getAttribute(AnnotationsAttribute.visibleTag);

                if (null == annotationsAttribute) {
                    continue;
                }

                for (Annotation annotation : annotationsAttribute.getAnnotations()) {
                    if ((annotation.getTypeName()).equals(RequestProcessor.class.getName())) {
                        // Found a request processor class, loads it
                        final String className = classFile.getName();
                        final Class<?> clz = classLoader.loadClass(className);

                        LOGGER.log(Level.FINER, "Found a request processor[className={0}]", className);
                        final Method[] declaredMethods = clz.getDeclaredMethods();

                        for (int i = 0; i < declaredMethods.length; i++) {
                            final Method mthd = declaredMethods[i];
                            final RequestProcessing requestProcessingMethodAnn = mthd
                                    .getAnnotation(RequestProcessing.class);

                            if (null == requestProcessingMethodAnn) {
                                continue;
                            }

                            addProcessorMethod(requestProcessingMethodAnn, clz, mthd);
                        }
                    }
                }
            }
        }
    } catch (final Exception e) {
        LOGGER.log(Level.SEVERE, "Scans classpath (lib directory) failed", e);
    }
}

From source file:com.photon.maven.plugins.android.standalonemojos.UnpackMojo.java

private File unpackClasses() throws MojoExecutionException {
    File outputDirectory = new File(project.getBuild().getDirectory(), "android-classes");
    if (lazyLibraryUnpack && outputDirectory.exists())
        getLog().info("skip library unpacking due to lazyLibraryUnpack policy");
    else {//from  w  ww  .  j a v a  2 s.  c  o  m
        for (Artifact artifact : getRelevantCompileArtifacts()) {

            if (artifact.getFile().isDirectory()) {
                try {
                    FileUtils.copyDirectory(artifact.getFile(), outputDirectory);
                } catch (IOException e) {
                    throw new MojoExecutionException(
                            "IOException while copying " + artifact.getFile().getAbsolutePath() + " into "
                                    + outputDirectory.getAbsolutePath(),
                            e);
                }
            } else {
                try {
                    JarHelper.unjar(new JarFile(artifact.getFile()), outputDirectory,
                            new JarHelper.UnjarListener() {
                                @Override
                                public boolean include(JarEntry jarEntry) {
                                    return !jarEntry.getName().startsWith("META-INF")
                                            && jarEntry.getName().endsWith(".class");
                                }
                            });
                } catch (IOException e) {
                    throw new MojoExecutionException(
                            "IOException while unjarring " + artifact.getFile().getAbsolutePath() + " into "
                                    + outputDirectory.getAbsolutePath(),
                            e);
                }
            }

        }
    }

    try {
        File sourceDirectory = new File(project.getBuild().getDirectory(), "classes");
        if (!sourceDirectory.exists()) {
            sourceDirectory.mkdirs();
        }
        FileUtils.copyDirectory(sourceDirectory, outputDirectory);
    } catch (IOException e) {
        throw new MojoExecutionException("IOException while copying " + sourceDirectory.getAbsolutePath()
                + " into " + outputDirectory.getAbsolutePath(), e);
    }
    return outputDirectory;
}

From source file:org.commonjava.web.test.fixture.JarKnockouts.java

public void rewriteJar(final File source, final File targetDir) throws IOException {
    targetDir.mkdirs();/*from w  ww  . jav a 2s.c  o m*/
    final File target = new File(targetDir, source.getName());

    JarFile in = null;
    JarOutputStream out = null;
    try {
        in = new JarFile(source);

        final BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(target));
        out = new JarOutputStream(fos, in.getManifest());

        final Enumeration<JarEntry> entries = in.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            if (!knockout(entry.getName())) {
                final InputStream stream = in.getInputStream(entry);
                out.putNextEntry(entry);
                copy(stream, out);
                out.closeEntry();
            }
        }
    } finally {
        closeQuietly(out);
        if (in != null) {
            try {
                in.close();
            } catch (final IOException e) {
            }
        }
    }
}

From source file:abs.backend.erlang.ErlApp.java

private void copyJarDirectory(JarFile jarFile, String inname, String outname) throws IOException {
    InputStream is = null;/*from w w w  .  j  ava 2  s. com*/
    for (JarEntry entry : Collections.list(jarFile.entries())) {
        if (entry.getName().startsWith(inname)) {
            String relFilename = entry.getName().substring(inname.length());
            if (!entry.isDirectory()) {
                is = jarFile.getInputStream(entry);
                ByteStreams.copy(is, Files.newOutputStreamSupplier(new File(outname, relFilename)));
            } else {
                new File(outname, relFilename).mkdirs();
            }
        }
    }
    is.close();
}

From source file:javadepchecker.Main.java

public void processJar(JarFile jar) throws IOException {
    for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
        JarEntry entry = e.nextElement();
        String name = entry.getName();
        if (!entry.isDirectory() && name.endsWith(".class")) {
            this.current.add(name);
            InputStream stream = jar.getInputStream(entry);
            ClassParser parser = new ClassParser(stream, name);
            JavaClass jclass = parser.parse();
            this.pool = jclass.getConstantPool();
            new DescendingVisitor(jclass, this).visitConstantPool(this.pool);
        }/*w w w. ja v a  2 s . c  o m*/
    }
}

From source file:i18nplugin.TranslationNode.java

/**
 * Create Tree//w  w  w  .  ja v  a2  s  .co  m
 * @param string Name of Tree-Node
 * @param file Jar-File with Properties
 */
public TranslationNode(String string, File file) {
    super(string);

    try {
        JarFile jarfile = new JarFile(file);

        Enumeration<JarEntry> entries = jarfile.entries();

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

            if (entry.getName().endsWith(".properties")) {
                String name = entry.getName();

                name = name.substring(0, name.length() - 11);

                if (name.indexOf('/') >= 0) {
                    String dir = StringUtils.substringBeforeLast(name, "/");

                    if (dir.contains("/")) {
                        dir = StringUtils.substringAfterLast(dir, "/");
                    }

                    name = StringUtils.substringAfterLast(name, "/");

                    if (name.equals(dir)) {
                        addEntry(jarfile, entry);
                    }
                }
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:org.massyframework.modules.utils.SunJdkModuleExporter.java

/**
 * ?SunJdk??/*from   w  w  w .j  av a 2 s.c o  m*/
 * 
 * @param fileName
 * @return
 * @throws IOException
 */
protected List<String> getSunJdkPackageNames(String rtJar) throws IOException {
    Set<String> packageNames = new HashSet<String>();
    JarFile file = null;
    try {
        file = new JarFile(rtJar);
        Enumeration<JarEntry> em = file.entries();
        while (em.hasMoreElements()) {
            JarEntry entry = em.nextElement();
            if (!entry.isDirectory()) {
                String name = entry.getName();
                if (name.endsWith(".class")) {
                    int index = StringUtils.lastIndexOf(name, "/");
                    packageNames.add(StringUtils.substring(name, 0, index));
                }
            }
        }
    } finally {
        IOUtils.closeStream(file);
    }
    List<String> result = new ArrayList<String>(packageNames);
    Collections.sort(result);
    return result;
}

From source file:com.googlecode.arit.systest.Application.java

public File getExplodedWAR() {
    if (explodedWAR == null) {
        File explodedDir = new File(tmpDir, "exploded");
        explodedDir.mkdir();/*from w  ww  .  jav a2s  . co  m*/
        String file = url.getFile();
        explodedWAR = new File(explodedDir, file.substring(file.lastIndexOf('/')));
        try {
            InputStream in = url.openStream();
            try {
                JarInputStream jar = new JarInputStream(in);
                JarEntry jarEntry;
                while ((jarEntry = jar.getNextJarEntry()) != null) {
                    File dest = new File(explodedWAR, jarEntry.getName());
                    if (jarEntry.isDirectory()) {
                        dest.mkdir();
                    } else {
                        dest.getParentFile().mkdirs();
                        OutputStream out = new FileOutputStream(dest);
                        try {
                            IOUtils.copy(jar, out);
                        } finally {
                            out.close();
                        }
                    }
                }
            } finally {
                in.close();
            }
        } catch (IOException ex) {
            throw new SystestException("Failed to explode WAR", ex);
        }
    }
    return explodedWAR;
}