Example usage for java.util.jar JarFile getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the path name of the ZIP file.

Usage

From source file:org.hyperic.snmp.MIBTree.java

public boolean parse(JarFile jar, String[] accept) throws IOException {
    AcceptFilter filter = new AcceptFilter(accept);

    for (Enumeration e = jar.entries(); e.hasMoreElements();) {
        JarEntry entry = (JarEntry) e.nextElement();

        if (entry.isDirectory()) {
            continue;
        }/* w  ww.  j av  a2 s.com*/

        if (!entry.getName().startsWith("mibs/")) {
            continue;
        }

        String name = entry.getName().substring(5);

        if (!filter.accept(name)) {
            continue;
        }

        if (hasParsedFile(new File(name))) {
            continue;
        }

        String where = jar.getName() + "!" + entry.getName();

        parse(where, jar.getInputStream(entry));
    }

    return true;
}

From source file:org.openmrs.module.ModuleUtil.java

/**
 * Load resource from a jar inside a jar.
 * /*from   w w  w  .j a va2 s.  c  o m*/
 * @param outerJarFile jar file that contains a jar file
 * @param innerJarFileLocation inner jar file location relative to the outer jar
 * @param resource path to a resource relative to the inner jar
 * @return resource from the inner jar as an input stream or <code>null</code> if resource cannot be loaded
 */
private static InputStream getResourceFromInnerJar(JarFile outerJarFile, String innerJarFileLocation,
        String resource) {
    File tempFile = null;
    FileOutputStream tempOut = null;
    JarFile innerJarFile = null;
    InputStream innerInputStream = null;
    try {
        tempFile = File.createTempFile("tempFile", "jar");
        tempOut = new FileOutputStream(tempFile);
        ZipEntry innerJarFileEntry = outerJarFile.getEntry(innerJarFileLocation);
        if (innerJarFileEntry != null) {
            IOUtils.copy(outerJarFile.getInputStream(innerJarFileEntry), tempOut);
            innerJarFile = new JarFile(tempFile);
            ZipEntry targetEntry = innerJarFile.getEntry(resource);
            if (targetEntry != null) {
                // clone InputStream to make it work after the innerJarFile is closed
                innerInputStream = innerJarFile.getInputStream(targetEntry);
                byte[] byteArray = IOUtils.toByteArray(innerInputStream);
                return new ByteArrayInputStream(byteArray);
            }
        }
    } catch (IOException e) {
        log.error("Unable to get '" + resource + "' from '" + innerJarFileLocation + "' of '"
                + outerJarFile.getName() + "'", e);
    } finally {
        IOUtils.closeQuietly(tempOut);
        IOUtils.closeQuietly(innerInputStream);

        // close inner jar file before attempting to delete temporary file
        try {
            if (innerJarFile != null) {
                innerJarFile.close();
            }
        } catch (IOException e) {
            log.warn("Unable to close inner jarfile: " + innerJarFile, e);
        }

        // delete temporary file
        if (tempFile != null && !tempFile.delete()) {
            log.warn("Could not delete temporary jarfile: " + tempFile);
        }
    }
    return null;
}

From source file:org.netbeans.nbbuild.MakeJnlp2.java

/**
 * returns version of jar file depending on manifest.mf or explicitly specified
 * @param jar/*from   w w w.jav a2 s .  c  o  m*/
 * @return
 * @throws IOException
 */
private String getJarVersion(JarFile jar) throws IOException {
    String version = jar.getManifest().getMainAttributes().getValue("OpenIDE-Module-Specification-Version");
    if (version == null) {
        version = jar.getManifest().getMainAttributes().getValue("Specification-Version");
    }
    if (version == null && jarVersions != null) {
        version = jarVersions.get(jar.getName());
    }
    return version;
}

From source file:UnpackedJarFile.java

public NestedJarFile(JarFile jarFile, String path) throws IOException {
    super(DeploymentUtil.DUMMY_JAR_FILE);

    // verify that the jar actually contains that path
    JarEntry targetEntry = jarFile.getJarEntry(path + "/");
    if (targetEntry == null) {
        targetEntry = jarFile.getJarEntry(path);
        if (targetEntry == null) {
            throw new IOException("Jar entry does not exist: jarFile=" + jarFile.getName() + ", path=" + path);
        }/* w w w . jav  a 2 s.  com*/
    }

    if (targetEntry.isDirectory()) {
        if (targetEntry instanceof UnpackedJarEntry) {
            //unpacked nested module inside unpacked ear
            File targetFile = ((UnpackedJarEntry) targetEntry).getFile();
            baseJar = new UnpackedJarFile(targetFile);
            basePath = "";
        } else {
            baseJar = jarFile;
            if (!path.endsWith("/")) {
                path += "/";
            }
            basePath = path;
        }
    } else {
        if (targetEntry instanceof UnpackedJarEntry) {
            // for unpacked jars we don't need to copy the jar file
            // out to a temp directory, since it is already available
            // as a raw file
            File targetFile = ((UnpackedJarEntry) targetEntry).getFile();
            baseJar = new JarFile(targetFile);
            basePath = "";
        } else {
            tempFile = DeploymentUtil.toFile(jarFile, targetEntry.getName());
            baseJar = new JarFile(tempFile);
            basePath = "";
        }
    }
}

From source file:org.jasig.portal.plugin.deployer.AbstractExtractingEarDeployer.java

/**
 * Gets the EAR descriptor from the {@link JarFile}.
 * //w w w.  j a  va2s.co  m
 * @param earFile The EAR to get the descriptor from.
 * @return The descriptor DOM for the EAR.
 * @throws IOException If there is any problem reading the descriptor from the EAR.
 */
protected Document getDescriptorDom(final JarFile earFile) throws MojoFailureException {
    final ZipEntry descriptorEntry = earFile.getEntry(DESCRIPTOR_PATH);
    if (descriptorEntry == null) {
        throw new IllegalArgumentException(
                "JarFile '" + earFile + "' does not contain a descriptor at '" + DESCRIPTOR_PATH + "'");
    }

    InputStream descriptorStream = null;
    try {
        descriptorStream = earFile.getInputStream(descriptorEntry);

        final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

        final DocumentBuilder docBuilder;
        try {
            docBuilder = docBuilderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException pce) {
            throw new RuntimeException("Failed to create DocumentBuilder to parse EAR descriptor.", pce);
        }

        docBuilder.setEntityResolver(new ClasspathEntityResolver(this.getLogger()));

        final Document descriptorDom;
        try {
            descriptorDom = docBuilder.parse(descriptorStream);
            return descriptorDom;
        } catch (SAXException e) {
            throw new MojoFailureException(
                    "Failed to parse descriptor '" + DESCRIPTOR_PATH + "' from EAR '" + earFile.getName() + "'",
                    e);
        }
    } catch (IOException e) {
        throw new MojoFailureException(
                "Failed to read descriptor '" + DESCRIPTOR_PATH + "' from EAR '" + earFile.getName() + "'", e);
    } finally {
        IOUtils.closeQuietly(descriptorStream);
    }
}

From source file:org.openmrs.module.ModuleFactory.java

/**
 * Execute all unrun changeSets in liquibase.xml for the given module
 * /*from w  w  w .  jav a2  s . c  om*/
 * @param module the module being executed on
 */
private static void runLiquibase(Module module) {
    JarFile jarFile = null;
    boolean liquibaseFileExists = false;

    try {
        try {
            jarFile = new JarFile(module.getFile());
        } catch (IOException e) {
            throw new ModuleException("Unable to get jar file", module.getName(), e);
        }

        //check whether module has a liquibase.xml
        InputStream inStream = null;
        ZipEntry entry = null;
        try {
            inStream = ModuleUtil.getResourceFromApi(jarFile, module.getModuleId(), module.getVersion(),
                    MODULE_CHANGELOG_FILENAME);
            if (inStream == null) {
                // Try the old way. Loading from the root of the omod
                entry = jarFile.getEntry(MODULE_CHANGELOG_FILENAME);
            }
            liquibaseFileExists = (inStream != null) || (entry != null);
        } finally {
            IOUtils.closeQuietly(inStream);
        }
    } finally {
        try {
            if (jarFile != null) {
                jarFile.close();
            }
        } catch (IOException e) {
            log.warn("Unable to close jarfile: " + jarFile.getName());
        }
    }

    if (liquibaseFileExists) {
        try {
            // run liquibase.xml by Liquibase API
            DatabaseUpdater.executeChangelog(MODULE_CHANGELOG_FILENAME, null, null, null,
                    getModuleClassLoader(module));
        } catch (InputRequiredException ire) {
            // the user would be stepped through the questions returned here.
            throw new ModuleException("Input during database updates is not yet implemented.", module.getName(),
                    ire);
        } catch (DatabaseUpdateException e) {
            throw new ModuleException("Unable to update data model using liquibase.xml.", module.getName(), e);
        } catch (Exception e) {
            throw new ModuleException("Unable to update data model using liquibase.xml.", module.getName(), e);
        }
    }
}

From source file:net.dries007.coremod.Module.java

/**
 * This method gets all the dependencies, ASM classes and ATs from the files associated with this module.
 *//*from  w  ww .  j  a va  2s. c o m*/
public void parceJarFiles() {
    for (ModuleFile mFile : this.files) {
        try {
            JarFile jar = new JarFile(mFile.file);
            Manifest mf = jar.getManifest();
            if (mf != null) {
                for (Entry<Object, Object> attribute : mf.getMainAttributes().entrySet()) {
                    attributes.put(attribute.getKey().toString(), attribute.getValue().toString());
                }
                /**
                 * Reading NORMAL libs from the modules' manifest files.
                 * We want: Space sperated pairs of filename:sha1
                 */
                if (Data.hasKey(Data.LIBKEY_NORMAL, Data.LIBURL_NORMAL)) {
                    String libs = mf.getMainAttributes().getValue(Data.get(Data.LIBKEY_NORMAL));
                    if (libs != null)
                        for (String lib : libs.split(" ")) {
                            DefaultDependency dependency = new DefaultDependency(lib);
                            dependecies.add(dependency);
                        }
                }

                /**
                 * Reading MAVEN libs from the modules' manifest files.
                 * We want: the maven name
                 */
                if (Data.hasKey(Data.LIBKEY_MAVEN, Data.LIBURL_MAVEN)) {
                    String libs = mf.getMainAttributes().getValue(Data.get(Data.LIBKEY_MAVEN));
                    if (libs != null)
                        for (String lib : libs.split(" ")) {
                            MavenDependency dependency = new MavenDependency(this, lib);
                            dependecies.add(dependency);
                            dependecies.addAll(Coremod.getDependencies(dependency));
                        }
                }

                /*
                 * Reading ASM classes from the modules' manifest files
                 */
                if (Data.hasKey(Data.CLASSKEY_ASM)) {
                    String asmclasses = mf.getMainAttributes().getValue(Data.get(Data.CLASSKEY_ASM));
                    if (asmclasses != null)
                        for (String asmclass : asmclasses.split(" ")) {
                            this.ASMClasses.add(asmclass);
                            System.out.println("[" + Data.NAME + "] Added ASM class (" + asmclass
                                    + ") for module file " + jar.getName());
                        }
                }

                /*
                 * Reading AT Files from the modules' manifest files
                 */
                if (Data.hasKey(Data.FILEKEY_TA)) {
                    String ats = mf.getMainAttributes().getValue(Data.FILEKEY_TA);
                    if (ats != null)
                        for (String at : ats.split(" ")) {
                            this.ATFiles.add(at);
                            System.out.println("[" + Data.NAME + "] Added AccessTransformer (" + at
                                    + ") for module file " + jar.getName());
                        }
                }
            }
            jar.close();
        } catch (final MalformedURLException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.EngineServiceImpl.java

private Object getJarFileNames(List jarFiles) {
    List jarFileNames = new ArrayList(jarFiles.size());
    for (Iterator it = jarFiles.iterator(); it.hasNext();) {
        JarFile jar = (JarFile) ((CacheObject) it.next()).getObject();
        jarFileNames.add(jar.getName());
    }/*from   ww w .  j a v a2  s.  com*/
    return jarFileNames;
}

From source file:org.openmrs.module.SqlDiffFileParser.java

/**
 * Get the diff map. Return a sorted map&lt;version, sql statements&gt;
 *
 * @return SortedMap&lt;String, String&gt;
 * @throws ModuleException//from   www .  jav  a 2  s.com
 */
public static SortedMap<String, String> getSqlDiffs(Module module) throws ModuleException {
    if (module == null) {
        throw new ModuleException("Module cannot be null");
    }

    SortedMap<String, String> map = new TreeMap<String, String>(new VersionComparator());

    InputStream diffStream = null;

    // get the diff stream
    JarFile jarfile = null;
    try {
        try {
            jarfile = new JarFile(module.getFile());
        } catch (IOException e) {
            throw new ModuleException("Unable to get jar file", module.getName(), e);
        }

        diffStream = ModuleUtil.getResourceFromApi(jarfile, module.getModuleId(), module.getVersion(),
                SQLDIFF_CHANGELOG_FILENAME);
        if (diffStream == null) {
            // Try the old way. Loading from the root of the omod
            ZipEntry diffEntry = jarfile.getEntry(SQLDIFF_CHANGELOG_FILENAME);
            if (diffEntry == null) {
                log.debug("No sqldiff.xml found for module: " + module.getName());
                return map;
            } else {
                try {
                    diffStream = jarfile.getInputStream(diffEntry);
                } catch (IOException e) {
                    throw new ModuleException("Unable to get sql diff file stream", module.getName(), e);
                }
            }
        }

        try {
            // turn the diff stream into an xml document
            Document diffDoc = null;
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                db.setEntityResolver(new EntityResolver() {

                    @Override
                    public InputSource resolveEntity(String publicId, String systemId)
                            throws SAXException, IOException {
                        // When asked to resolve external entities (such as a DTD) we return an InputSource
                        // with no data at the end, causing the parser to ignore the DTD.
                        return new InputSource(new StringReader(""));
                    }
                });
                diffDoc = db.parse(diffStream);
            } catch (Exception e) {
                throw new ModuleException("Error parsing diff sqldiff.xml file", module.getName(), e);
            }

            Element rootNode = diffDoc.getDocumentElement();

            String diffVersion = rootNode.getAttribute("version");

            if (!validConfigVersions().contains(diffVersion)) {
                throw new ModuleException("Invalid config version: " + diffVersion, module.getModuleId());
            }

            NodeList diffNodes = getDiffNodes(rootNode, diffVersion);

            if (diffNodes != null && diffNodes.getLength() > 0) {
                int i = 0;
                while (i < diffNodes.getLength()) {
                    Element el = (Element) diffNodes.item(i++);
                    String version = getElement(el, diffVersion, "version");
                    String sql = getElement(el, diffVersion, "sql");
                    map.put(version, sql);
                }
            }
        } catch (ModuleException e) {
            if (diffStream != null) {
                try {
                    diffStream.close();
                } catch (IOException io) {
                    log.error("Error while closing config stream for module: " + module.getModuleId(), io);
                }
            }

            // rethrow the moduleException
            throw e;
        }

    } finally {
        try {
            if (jarfile != null) {
                jarfile.close();
            }
        } catch (IOException e) {
            log.warn("Unable to close jarfile: " + jarfile.getName());
        }
    }
    return map;
}

From source file:com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.EngineServiceImpl.java

protected void dispose(JarFile jarFile) {
    try {//from w w  w  . j  a  v  a2 s . c  o m
        jarFile.close();
    } catch (IOException e) {
        log.warn("Unable to close jar file \"" + jarFile.getName() + "\"", e);
    }
    File file = new File(jarFile.getName());
    if (file.exists() && !file.delete()) {
        log.warn("Unable to delete jar file \"" + jarFile.getName() + "\"");
    }
}