Example usage for java.util.jar JarFile getJarEntry

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

Introduction

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

Prototype

public JarEntry getJarEntry(String name) 

Source Link

Document

Returns the JarEntry for the given base entry name or null if not found.

Usage

From source file:org.ebayopensource.turmeric.eclipse.utils.io.IOUtil.java

/**
 * In the normal jar URLs usage in Windows Java puts a lock on it. And the
 * SOA Tool Handler will create a non locking URL by setting the caching
 * off. There is obviously a performance compromise made here by disabling
 * the cache.// w  w w.  j av  a 2 s. co m
 *
 * @param jarFileUrl the jar file url
 * @param jarEntryPath the jar entry path
 * @return the non locking url
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static URL getNonLockingURL(URL jarFileUrl, String jarEntryPath) throws IOException {
    File file = FileUtils.toFile(jarFileUrl);
    JarFile jarFile;
    jarFile = new JarFile(file);
    JarEntry jarEntry = jarFile.getJarEntry(jarEntryPath);
    if (jarEntry != null) {
        SOAToolFileUrlHandler handler = new SOAToolFileUrlHandler(jarFile, jarEntry);
        URL retUrl = new URL("jar", "", -1,
                new File(jarFile.getName()).toURI().toURL() + "!/" + jarEntry.getName(), handler);
        handler.setExpectedUrl(retUrl);
        return retUrl;

    }
    return null;
}

From source file:org.ebayopensource.turmeric.eclipse.utils.wsdl.WSDLUtil.java

/**
 * Reading wsdl from the provided jar file.
 * The format for documetn base URI would be "jar:file:[JAR_FILE_LOCATION]!/[WSDL_JAR_ENTRY_PATH]"
 *
 * @param file the file/*from   ww w . j  av a 2  s.c o m*/
 * @param jarEntryLocation the jar entry location
 * @return The instance of WSDL definition, or null is could not read it.
 * @throws WSDLException the wSDL exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static Definition readWSDLFromJarFile(final File file, final String jarEntryLocation)
        throws WSDLException, IOException {
    InputStream wsdlStream = null;
    if (file.exists() && file.canRead()) {
        final JarFile jarFile = new JarFile(file);
        final JarEntry jarEntry = jarFile.getJarEntry(jarEntryLocation);
        if (jarEntry != null) {
            // found the wsdl file
            wsdlStream = jarFile.getInputStream(jarEntry);
            return WSDLUtil.readWSDL(StringUtil.toString(URL_PREFIX_JAR_FILE, file.getAbsolutePath(),
                    JAR_FILE_SEPARATOR, jarEntryLocation), wsdlStream);
        }
    }
    return null;
}

From source file:org.ebayopensource.turmeric.plugins.maven.resources.ResourceLocator.java

private URI findFileResource(File path, String pathref) {
    if (path.isFile()) {
        // Assume its an archive.
        String extn = FilenameUtils.getExtension(path.getName());
        if (StringUtils.isBlank(extn)) {
            log.debug("No extension found for file: " + path);
            return null;
        }//from w ww.j a va 2 s  . c  om

        extn = extn.toLowerCase();
        if ("jar".equals(extn)) {
            log.debug("looking inside of jar file: " + path);
            JarFile jar = null;
            try {
                jar = new JarFile(path);
                JarEntry entry = jar.getJarEntry(pathref);
                if (entry == null) {
                    log.debug("JarEntry not found: " + pathref);
                    return null;
                }

                String uripath = String.format("jar:%s!/%s", path.toURI(), entry.getName());
                try {
                    return new URI(uripath);
                } catch (URISyntaxException e) {
                    log.debug("Unable to create URI reference: " + path, e);
                    return null;
                }
            } catch (JarException e) {
                log.debug("Unable to open archive: " + path, e);
                return null;
            } catch (IOException e) {
                log.debug("Unable to open archive: " + path, e);
                return null;
            } finally {
                if (jar != null) {
                    try {
                        jar.close();
                    } catch (IOException e) {
                        log.debug("Unable to close jar: " + path, e);
                    }
                }
            }
        }

        log.debug("Unsupported archive file: " + path);
        return null;
    }

    if (path.isDirectory()) {
        File testFile = new File(path, FilenameUtils.separatorsToSystem(pathref));
        if (testFile.exists() && testFile.isFile()) {
            return testFile.toURI();
        }

        log.debug("Not found in directory: " + testFile);
        return null;
    }

    log.debug("Unable to handle non-file, non-directory " + File.class.getName() + " objects: " + path);
    return null;
}

From source file:org.ebayopensource.turmeric.tools.codegen.util.CodeGenClassLoader.java

public URL findResource(String resourceName) {
    for (URL url : m_jarURLs) {

        JarFile jarFile = null;
        try {/*from  w w w.  j  a v a  2 s  . c  o m*/
            File file = CodeGenUtil.urlToFile(url);
            jarFile = new JarFile(file);
            JarEntry jarEntry = jarFile.getJarEntry(resourceName);
            if (jarEntry == null) {
                continue; // Skip, not part of this jar.
            }
            // Java supports "jar:" url references natively.
            return new URL(String.format("jar:%s!/%s", file.toURI().toASCIIString(), jarEntry.getName()));
        } catch (IOException e) {
            e.printStackTrace(); // KEEPME
        } catch (Exception e) {
            /* ignore */
        } finally {
            CodeGenUtil.closeQuietly(jarFile);
        }

    }
    return super.findResource(resourceName);
}

From source file:org.ebayopensource.turmeric.tools.library.builders.CodeGenTypeLibraryGenerator.java

private static void deleteTheGeneratedDependentTypesForV3(String dependentJarPath,
        TypeLibraryCodeGenContext codeGenCtx) {

    if (TypeLibraryUtilities.isEmptyString(dependentJarPath))
        return;//w w w.ja  v a2 s .co m

    String[] dependentLibrariesJarPaths = dependentJarPath.split(DEPENDENT_JARS_DELIMITER);

    if (dependentLibrariesJarPaths.length == 0)
        return;

    //identify the possible java types to be deleted
    List<String> javaTypesToDelete = new ArrayList<String>();
    //Need to check if ObjectFactory is being deleted.
    //One ObjectFactory per library.Need to get a set of all namespaces being referred.
    Set<String> dependentLibsNamespace = new HashSet<String>();

    for (String currDepLibraryJar : dependentLibrariesJarPaths) {
        String currLibraryName = getLibraryNameFromJarFilePath(currDepLibraryJar);
        //for referred libraries  the TypeInformation.xml from the related jar . this jar won't be available in classpath and hence has to be mnaually processed
        String typeInformationFileRelativePath = TypeLibraryConstants.META_INF_FOLDER + File.separator
                + currLibraryName + File.separator + TypeLibraryConstants.TYPE_INFORMATION_FILE_NAME;

        File theJarFile = new File(currDepLibraryJar);
        if (!theJarFile.exists())
            continue;

        JarFile jarFile = null;
        try {
            jarFile = new JarFile(currDepLibraryJar);
            JarEntry entry = jarFile.getJarEntry(typeInformationFileRelativePath);
            if (entry == null)
                entry = jarFile.getJarEntry(typeInformationFileRelativePath.replace("\\", "/"));
            if (entry == null) {
                getLogger().log(Level.WARNING,
                        "Could not find the TypeInformation.xml file for the dependent library represented by the jar : "
                                + currDepLibraryJar);
                continue;
            }
            InputStream inputStream = jarFile.getInputStream(entry);

            if (inputStream != null) {

                TypeLibraryType typeLibraryType = JAXB.unmarshal(inputStream, TypeLibraryType.class);
                if (typeLibraryType != null) {
                    dependentLibsNamespace.add(typeLibraryType.getLibraryNamespace());
                    for (TypeInformationType typeInformationType : typeLibraryType.getType())
                        javaTypesToDelete.add(typeInformationType.getJavaTypeName());
                }

            }

        } catch (IOException e) {
            getLogger().log(Level.WARNING,
                    "Exception while parsing the TypeInformation.xml of jar " + currDepLibraryJar, e);
        }
    }

    deleteJavaTypes(javaTypesToDelete, codeGenCtx);
    findPackageForObjectFactoriesAndDelete(codeGenCtx, dependentLibsNamespace);
}

From source file:org.echocat.nodoodle.classloading.FileClassLoader.java

@Override
protected synchronized Class<?> findClass(final String name) throws ClassNotFoundException {
    ensureNotClosed();//from  w ww . j  ava 2  s  .  c  om
    try {
        return doPrivileged(new PrivilegedExceptionAction<Class<?>>() {
            @Override
            public Class<?> run() throws Exception {
                Class<?> result;
                result = null;
                final String path = name.replace('.', '/').concat(".class");
                final Iterator<File> i = _directories.iterator();
                while (result == null && i.hasNext()) {
                    final File directory = i.next();
                    final File file = new File(directory, path);
                    if (file.isFile()) {
                        result = defineClass(name, new DirectoryResource(directory, file));
                    }
                }
                if (result == null) {
                    final Iterator<JarFile> j = _jarFiles.iterator();
                    while (result == null && j.hasNext()) {
                        final JarFile jarFile = j.next();
                        final JarEntry jarEntry = jarFile.getJarEntry(path);
                        if (jarEntry != null) {
                            result = defineClass(name, new JarResource(jarFile, jarEntry));
                        }
                    }
                }
                if (result == null) {
                    throw new ClassNotFoundException(name);
                }
                return result;
            }
        }, _acc);
    } catch (PrivilegedActionException e) {
        final Exception exception = e.getException();
        if (exception instanceof ClassNotFoundException) {
            throw (ClassNotFoundException) exception;
        } else {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.echocat.nodoodle.classloading.FileClassLoader.java

@Override
protected synchronized URL findResource(final String name) {
    ensureNotClosed();//from w  ww . j  a v  a  2 s. co m
    return doPrivileged(new PrivilegedAction<URL>() {
        @Override
        public URL run() {
            URL result = null;
            final Iterator<File> i = _directories.iterator();
            while (result == null && i.hasNext()) {
                final File directory = i.next();
                final File file = new File(directory, name);
                if (file.isFile()) {
                    result = new DirectoryResource(directory, file).getResourceUrl();
                }
            }
            if (result == null) {
                final Iterator<JarFile> j = _jarFiles.iterator();
                while (result == null && j.hasNext()) {
                    final JarFile jarFile = j.next();
                    final JarEntry jarEntry = jarFile.getJarEntry(name);
                    if (jarEntry != null) {
                        result = new JarResource(jarFile, jarEntry).getResourceUrl();
                    }
                }
            }
            return result;
        }
    }, _acc);
}

From source file:org.echocat.nodoodle.classloading.FileClassLoader.java

@Override
protected synchronized Enumeration<URL> findResources(final String name) throws IOException {
    ensureNotClosed();/*from  w  w  w.ja  v a 2 s .  c om*/
    final Iterable<URL> iterable = doPrivileged(new PrivilegedAction<Iterable<URL>>() {
        @Override
        public Iterable<URL> run() {
            final Collection<URL> result = new ArrayList<URL>();
            for (File directory : _directories) {
                final File file = new File(directory, name);
                if (file.isFile()) {
                    result.add(new DirectoryResource(directory, file).getResourceUrl());
                }
            }
            for (JarFile jarFile : _jarFiles) {
                final JarEntry jarEntry = jarFile.getJarEntry(name);
                if (jarEntry != null) {
                    result.add(new JarResource(jarFile, jarEntry).getResourceUrl());
                }
            }
            return result;
        }
    }, _acc);
    //noinspection unchecked
    return new IteratorEnumeration(iterable.iterator());
}

From source file:org.gofleet.context.GoClassLoader.java

private void findModulesInPath(ArrayList<File> res, String module_suffix, final java.lang.String path) {
    try {//from   w  w w .java2s  .  c o  m
        LOG.trace("findModulesInPath(" + path + ")");
        final java.io.File object = new java.io.File(path);

        if (object.isDirectory()) {
            LOG.trace("Directory found: " + object.getAbsolutePath());
            for (java.lang.String entry : object.list()) {
                final java.io.File thing = new java.io.File(
                        object.getCanonicalPath() + System.getProperty("file.separator") + entry);
                if (thing.isFile() && thing.getName().endsWith(module_suffix))
                    res.add(object);
                else
                    findModulesInPath(res, module_suffix, thing.getCanonicalPath());
            }
        } else if (object.isFile() && object.getName().endsWith(module_suffix)) {
            LOG.info("Module found: " + object.getAbsolutePath());
            res.add(object);
        } else if (object.isFile() && object.getName().endsWith(".jar")) {
            LOG.debug("Jar found: " + object.getAbsolutePath());
            FileInputStream fis = new FileInputStream(object);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry zipEntry;
            while ((zipEntry = zis.getNextEntry()) != null) {
                if (zipEntry.getName().endsWith(module_suffix)) {

                    JarFile jarFile = new JarFile(object);

                    JarEntry entry = jarFile.getJarEntry(zipEntry.getName());

                    InputStream inputStream = jarFile.getInputStream(entry);

                    File f = File.createTempFile("gofleet-", ".module");
                    f.deleteOnExit();
                    OutputStream out = new FileOutputStream(f);
                    byte buf[] = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buf)) > 0)
                        out.write(buf, 0, len);
                    out.close();
                    inputStream.close();

                    LOG.info("Module found: " + zipEntry.getName());
                    res.add(f);
                }
            }
            zis.close();
            fis.close();
        }
    } catch (Throwable t) {
        LOG.error(t, t);
    }
}

From source file:org.gofleet.context.GoClassLoader.java

private void findFileInPath(ArrayList<String> res, String cadena, final java.lang.String path) {
    try {//from   w  w  w  . j  a  va  2 s.  com
        LOG.trace("findFileInPath(" + path + ")");
        final java.io.File object = new java.io.File(path);

        if (object.isDirectory()) {
            LOG.trace("Directory found: " + object.getAbsolutePath());
            for (java.lang.String entry : object.list()) {
                final java.io.File thing = new java.io.File(
                        object.getCanonicalPath() + System.getProperty("file.separator") + entry);
                if (thing.isFile() && thing.getName().contains(cadena))
                    res.add(object.getCanonicalPath());
                else
                    findFileInPath(res, cadena, thing.getCanonicalPath());
            }
        } else if (object.isFile() && object.getName().contains(cadena)) {
            LOG.debug("File found: " + object.getAbsolutePath());
            res.add(object.getCanonicalPath());
        } else if (object.isFile() && object.getName().endsWith(".jar")) {
            LOG.debug("Jar found: " + object.getAbsolutePath());
            FileInputStream fis = new FileInputStream(object);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry zipEntry;
            while ((zipEntry = zis.getNextEntry()) != null) {
                if (zipEntry.getName().contains(cadena)) {

                    JarFile jarFile = new JarFile(object);

                    JarEntry entry = jarFile.getJarEntry(zipEntry.getName());

                    InputStream inputStream = jarFile.getInputStream(entry);

                    System.out.println(object.getAbsolutePath() + "/" + zipEntry.getName());

                    File f = File.createTempFile(zipEntry.getName(), "");
                    f.deleteOnExit();
                    OutputStream out = new FileOutputStream(f);
                    byte buf[] = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buf)) > 0)
                        out.write(buf, 0, len);
                    out.close();
                    inputStream.close();

                    LOG.debug("File found: " + zipEntry.getName());
                    res.add(f.getCanonicalPath());
                }
            }
            zis.close();
            fis.close();
        }
    } catch (Throwable t) {
        LOG.error(t, t);
    }
}