Example usage for java.util.jar JarFile JarFile

List of usage examples for java.util.jar JarFile JarFile

Introduction

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

Prototype

public JarFile(File file) throws IOException 

Source Link

Document

Creates a new JarFile to read from the specified File object.

Usage

From source file:org.apache.servicemix.jbi.deployer.handler.JBIDeploymentListener.java

/**
 * Check if the file is a recognized JBI artifact that needs to be
 * processed.//from   ww w.ja  va2 s.  c o m
 *
 * @param artifact the file to check
 * @return <code>true</code> is the file is a JBI artifact that
 *         should be transformed into an OSGi bundle.
 */
public boolean canHandle(File artifact) {
    try {
        // Accept jars and zips
        if (!artifact.getName().endsWith(".zip") && !artifact.getName().endsWith(".jar")) {
            return false;
        }
        JarFile jar = new JarFile(artifact);
        JarEntry entry = jar.getJarEntry(DescriptorFactory.DESCRIPTOR_FILE);
        // Only handle JBI artifacts
        if (entry == null) {
            return false;
        }
        // Only handle non OSGi bundles
        Manifest m = jar.getManifest();
        if (m != null && m.getMainAttributes().getValue(new Attributes.Name("Bundle-SymbolicName")) != null
                && m.getMainAttributes().getValue(new Attributes.Name("Bundle-Version")) != null) {
            return false;
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:com.navercorp.pinpoint.bootstrap.java9.module.JarFileAnalyzerTest.java

@Test
public void jarFileToURI() throws IOException {
    URL url = CodeSourceUtils.getCodeLocation(Logger.class);
    logger.debug("url:{}", url);

    JarFile jarFile = new JarFile(url.getFile());
    logger.debug("jarFile:{}", jarFile.getName());
    File file = new File(jarFile.getName());
    logger.debug("url1:{}", file.toURI());
}

From source file:net.sf.keystore_explorer.crypto.jcepolicy.JcePolicyUtil.java

/**
 * Get a JCE policy's crypto strength.//from w ww .  j  a  va 2 s . co  m
 *
 * @param jcePolicy
 *            JCE policy
 * @return Crypto strength
 * @throws CryptoException
 *             If there was a problem getting the crypto strength
 */
public static CryptoStrength getCryptoStrength(JcePolicy jcePolicy) throws CryptoException {
    try {
        File file = getJarFile(jcePolicy);

        // if there is no policy file at all, we assume that we are running under OpenJDK
        if (!file.exists()) {
            return UNLIMITED;
        }

        JarFile jar = new JarFile(file);

        Manifest jarManifest = jar.getManifest();
        String strength = jarManifest.getMainAttributes().getValue("Crypto-Strength");

        // workaround for IBM JDK: test for maximum key size
        if (strength == null) {
            return unlimitedStrengthTest();
        }

        if (strength.equals(LIMITED.manifestValue())) {
            return LIMITED;
        } else {
            return UNLIMITED;
        }
    } catch (IOException ex) {
        throw new CryptoException(
                MessageFormat.format(res.getString("NoGetCryptoStrength.exception.message"), jcePolicy), ex);
    }
}

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 w w .  j a  v a 2s.c om*/
    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:org.apache.bcel.BCELBenchmark.java

private JarFile getJarFile() throws IOException {
    String javaHome = System.getProperty("java.home");
    return new JarFile(javaHome + "/lib/rt.jar");
}

From source file:SerialVersionUID.java

/**
 * Build a TreeMap of the class name to ClassVersionInfo
 * //from   ww  w.java  2s .  c o  m
 * @param jar
 * @param classVersionMap
 *          TreeMap<String, ClassVersionInfo> for serializable classes
 * @param cl -
 *          the class loader to use
 * @throws IOException
 *           thrown if the jar cannot be opened
 */
static void generateJarSerialVersionUIDs(URL jar, TreeMap classVersionMap, ClassLoader cl, String pkgPrefix)
        throws IOException {
    String jarName = jar.getFile();
    JarFile jf = new JarFile(jarName);
    Enumeration entries = jf.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = (JarEntry) entries.nextElement();
        String name = entry.getName();
        if (name.endsWith(".class") && name.startsWith(pkgPrefix)) {
            name = name.substring(0, name.length() - 6);
            String classname = name.replace('/', '.');
            try {
                log.fine("Creating ClassVersionInfo for: " + classname);
                ClassVersionInfo cvi = new ClassVersionInfo(classname, cl);
                log.fine(cvi.toString());
                if (cvi.getSerialVersion() != 0) {
                    ClassVersionInfo prevCVI = (ClassVersionInfo) classVersionMap.put(classname, cvi);
                    if (prevCVI != null) {
                        if (prevCVI.getSerialVersion() != cvi.getSerialVersion()) {
                            log.severe("Found inconsistent classes, " + prevCVI + " != " + cvi + ", jar: "
                                    + jarName);
                        }
                    }
                    if (cvi.getHasExplicitSerialVersionUID() == false) {
                        log.warning("No explicit serialVersionUID: " + cvi);
                    }
                }
            } catch (OutOfMemoryError e) {
                log.log(Level.SEVERE, "Check the MaxPermSize", e);
            } catch (Throwable e) {
                log.log(Level.FINE, "While loading: " + name, e);
            }
        }
    }
    jf.close();
}

From source file:com.camnter.patch.utils.classref.ClassReferenceListBuilder.java

public void addRoots(String path) throws IOException {
    this.path = new Path(path);
    this.jarOfRoots = new JarFile(path);
}

From source file:com.gzj.tulip.load.ResourceRef.java

public static ResourceRef toResourceRef(Resource folder) throws IOException {
    ResourceRef rr = new ResourceRef(folder, null, null);
    String[] modifiers = null;//from  w  ww . j av a 2  s.  co  m
    Resource rosePropertiesResource = rr.getInnerResource("META-INF/rose.properties");
    if (rosePropertiesResource.exists()) {
        if (logger.isDebugEnabled()) {
            logger.debug("found rose.properties: " + rosePropertiesResource.getURI());
        }
        InputStream in = rosePropertiesResource.getInputStream();
        rr.properties.load(in);
        in.close();
        String attrValue = rr.properties.getProperty("rose");
        if (attrValue == null) {
            attrValue = rr.properties.getProperty("Rose");
        }
        if (attrValue != null) {
            modifiers = StringUtils.split(attrValue, ", ;\n\r\t");
            if (logger.isDebugEnabled()) {
                logger.debug("modifiers[by properties][" + rr.getResource().getURI() + "]="
                        + Arrays.toString(modifiers));
            }
        }
    }
    //
    if (modifiers == null) {
        if (!"jar".equals(rr.getProtocol())) {
            modifiers = new String[] { "**" };
            if (logger.isDebugEnabled()) {
                logger.debug("modifiers[by default][" + rr.getResource().getURI() + "]="
                        + Arrays.toString(modifiers));
            }
        } else {
            JarFile jarFile = new JarFile(rr.getResource().getFile());
            Manifest manifest = jarFile.getManifest();
            if (manifest != null) {
                Attributes attributes = manifest.getMainAttributes();
                String attrValue = attributes.getValue("rose");
                if (attrValue == null) {
                    attrValue = attributes.getValue("Rose");
                }
                if (attrValue != null) {
                    modifiers = StringUtils.split(attrValue, ", ;\n\r\t");
                    if (logger.isDebugEnabled()) {
                        logger.debug("modifiers[by manifest.mf][" + rr.getResource().getURI() + "]="
                                + Arrays.toString(modifiers));
                    }
                }
            }
        }
    }
    rr.setModifiers(modifiers);
    return rr;
}

From source file:com.sinosoft.one.mvc.scanning.ResourceRef.java

public static ResourceRef toResourceRef(Resource folder) throws IOException {
    ResourceRef rr = new ResourceRef(folder, null, null);
    String[] modifiers = null;/*from w ww .ja va 2  s .  c  o m*/
    Resource mvcPropertiesResource = rr.getInnerResource("META-INF/mvc.properties");
    if (mvcPropertiesResource.exists()) {
        if (logger.isDebugEnabled()) {
            logger.debug("found mvc.properties: " + mvcPropertiesResource.getURI());
        }
        InputStream in = mvcPropertiesResource.getInputStream();
        rr.properties.load(in);
        in.close();
        String attrValue = rr.properties.getProperty("mvc");
        if (attrValue == null) {
            attrValue = rr.properties.getProperty("Mvc");
        }
        if (attrValue != null) {
            modifiers = StringUtils.split(attrValue, ", ;\n\r\t");
            if (logger.isDebugEnabled()) {
                logger.debug("modifiers[by properties][" + rr.getResource().getURI() + "]="
                        + Arrays.toString(modifiers));
            }
        }
    }
    //
    if (modifiers == null) {
        if (!"jar".equals(rr.getProtocol())) {
            modifiers = new String[] { "**" };
            if (logger.isDebugEnabled()) {
                logger.debug("modifiers[by default][" + rr.getResource().getURI() + "]="
                        + Arrays.toString(modifiers));
            }
        } else {
            JarFile jarFile = new JarFile(rr.getResource().getFile());
            Manifest manifest = jarFile.getManifest();
            if (manifest != null) {
                Attributes attributes = manifest.getMainAttributes();
                String attrValue = attributes.getValue("mvc");
                if (attrValue == null) {
                    attrValue = attributes.getValue("Mvc");
                }
                if (attrValue != null) {
                    modifiers = StringUtils.split(attrValue, ", ;\n\r\t");
                    if (logger.isDebugEnabled()) {
                        logger.debug("modifiers[by manifest.mf][" + rr.getResource().getURI() + "]="
                                + Arrays.toString(modifiers));
                    }
                }
            }
        }
    }
    rr.setModifiers(modifiers);
    return rr;
}

From source file:nl.tue.gale.ae.config.GaleContextLoader.java

private void findInJar(ServletContext sc, String path, List<String> locations) {
    try {/* w  w w .j av  a2  s. com*/
        JarFile jar = new JarFile(sc.getRealPath(path));
        for (JarEntry entry : enumIterable(jar.entries())) {
            if (entry.getName().endsWith("-galeconfig.xml"))
                locations.add("classpath:" + entry.getName());
        }
        jar.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}