Search in sources :

Example 1 with Resource

use of org.hotswap.agent.util.spring.io.resource.Resource in project HotswapAgent by HotswapProjects.

the class PathMatchingResourcePatternResolver method doFindPathMatchingJarResources.

/**
 * Find all resources in jar files that match the given location pattern via
 * the Ant-style PathMatcher.
 *
 * @param rootDirResource
 *            the root directory as Resource
 * @param subPattern
 *            the sub pattern to match (below the root directory)
 * @return a mutable Set of matching Resource instances
 * @throws IOException
 *             in case of I/O errors
 * @see java.net.JarURLConnection
 * @see org.hotswap.agent.util.spring.path.springframework.util.PathMatcher
 */
protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, String subPattern) throws IOException {
    URLConnection con = rootDirResource.getURL().openConnection();
    JarFile jarFile;
    String jarFileUrl;
    String rootEntryPath;
    boolean newJarFile = false;
    if (con instanceof JarURLConnection) {
        // Should usually be the case for traditional JAR files.
        JarURLConnection jarCon = (JarURLConnection) con;
        ResourceUtils.useCachesIfNecessary(jarCon);
        jarFile = jarCon.getJarFile();
        jarFileUrl = jarCon.getJarFileURL().toExternalForm();
        JarEntry jarEntry = jarCon.getJarEntry();
        rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
    } else {
        // No JarURLConnection -> need to resort to URL file parsing.
        // We'll assume URLs of the format "jar:path!/entry", with the
        // protocol
        // being arbitrary as long as following the entry format.
        // We'll also handle paths with and without leading "file:" prefix.
        String urlFile = rootDirResource.getURL().getFile();
        try {
            int separatorIndex = urlFile.indexOf(ResourceUtils.JAR_URL_SEPARATOR);
            if (separatorIndex != -1) {
                jarFileUrl = urlFile.substring(0, separatorIndex);
                rootEntryPath = urlFile.substring(separatorIndex + ResourceUtils.JAR_URL_SEPARATOR.length());
                jarFile = getJarFile(jarFileUrl);
            } else {
                jarFile = new JarFile(urlFile);
                jarFileUrl = urlFile;
                rootEntryPath = "";
            }
            newJarFile = true;
        } catch (ZipException ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Skipping invalid jar classpath entry [" + urlFile + "]");
            }
            return Collections.emptySet();
        }
    }
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Looking for matching resources in jar file [" + jarFileUrl + "]");
        }
        if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) {
            // Root entry path must end with slash to allow for proper
            // matching.
            // The Sun JRE does not return a slash here, but BEA JRockit
            // does.
            rootEntryPath = rootEntryPath + "/";
        }
        Set<Resource> result = new LinkedHashSet<Resource>(8);
        for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
            JarEntry entry = entries.nextElement();
            String entryPath = entry.getName();
            if (entryPath.startsWith(rootEntryPath)) {
                String relativePath = entryPath.substring(rootEntryPath.length());
                if (getPathMatcher().match(subPattern, relativePath)) {
                    result.add(rootDirResource.createRelative(relativePath));
                }
            }
        }
        return result;
    } finally {
        // not from JarURLConnection, which might cache the file reference.
        if (newJarFile) {
            jarFile.close();
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JarURLConnection(java.net.JarURLConnection) VfsResource(org.hotswap.agent.util.spring.io.resource.VfsResource) FileSystemResource(org.hotswap.agent.util.spring.io.resource.FileSystemResource) UrlResource(org.hotswap.agent.util.spring.io.resource.UrlResource) Resource(org.hotswap.agent.util.spring.io.resource.Resource) ZipException(java.util.zip.ZipException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 2 with Resource

use of org.hotswap.agent.util.spring.io.resource.Resource in project HotswapAgent by HotswapProjects.

the class DeploymentInfo method fromClassLoader.

/**
 * Load the deployment info for this classloader.
 *
 * @param classloader
 *            the ClassLoader
 * @return the deployment info
 */
public static DeploymentInfo fromClassLoader(ClassLoader classloader) {
    ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(classloader);
        Set<MavenInfo> maven = new LinkedHashSet<>();
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(classloader);
        try {
            Enumeration<URL> urls = classloader.getResources("META-INF/maven/");
            while (urls.hasMoreElements()) {
                URL u = urls.nextElement();
                try {
                    Resource[] resources = resolver.getResources(u.toExternalForm() + "**/pom.properties");
                    if (resources != null) {
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("META-INF/maven/**/pom.properties FOUND:{}", Arrays.toString(resources));
                        }
                        for (Resource resource : resources) {
                            MavenInfo m = getMavenInfo(resource);
                            if (m != null) {
                                maven.add(m);
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            LOGGER.debug("Error trying to find maven properties", e);
        }
        return new DeploymentInfo(maven, getManifest(classloader));
    } finally {
        Thread.currentThread().setContextClassLoader(oldContextClassLoader);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Resource(org.hotswap.agent.util.spring.io.resource.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.hotswap.agent.util.spring.path.PathMatchingResourcePatternResolver) URL(java.net.URL) IOException(java.io.IOException)

Example 3 with Resource

use of org.hotswap.agent.util.spring.io.resource.Resource in project HotswapAgent by HotswapProjects.

the class DeploymentInfo method getManifest.

/**
 * Gets the manifest Info.
 *
 * @param classloader
 *            the ClassLoader
 * @return the manifest
 */
public static Set<ManifestInfo> getManifest(ClassLoader classloader) {
    Set<ManifestInfo> manifests = new LinkedHashSet<>();
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(classloader);
    try {
        Enumeration<URL> urls = classloader.getResources("META-INF/MANIFEST.MF");
        while (urls.hasMoreElements()) {
            URL u = urls.nextElement();
            try {
                Resource[] resources = resolver.getResources(u.toExternalForm());
                if (resources != null) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("META-INF/MANIFEST.MF FOUND:\n" + Arrays.toString(resources));
                    }
                    for (Resource resource : resources) {
                        ManifestInfo m = getManifest(resource);
                        if (m != null) {
                            manifests.add(m);
                        }
                    }
                }
            } catch (Exception e) {
                LOGGER.debug("Error trying to get manifest entries", e);
            }
        }
    } catch (IOException e) {
        LOGGER.debug("Error trying to get manifest entries", e);
    }
    return manifests;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Resource(org.hotswap.agent.util.spring.io.resource.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.hotswap.agent.util.spring.path.PathMatchingResourcePatternResolver) URL(java.net.URL) IOException(java.io.IOException)

Example 4 with Resource

use of org.hotswap.agent.util.spring.io.resource.Resource in project HotswapAgent by HotswapProjects.

the class PathMatchingResourcePatternResolver method findPathMatchingResources.

/**
 * Find all resources that match the given location pattern via the
 * Ant-style PathMatcher. Supports resources in jar files and zip files and
 * in the file system.
 *
 * @param locationPattern
 *            the location pattern to match
 * @return the result as Resource array
 * @throws IOException
 *             in case of I/O errors
 * @see #doFindPathMatchingJarResources
 * @see #doFindPathMatchingFileResources
 * @see org.hotswap.agent.util.spring.path.springframework.util.PathMatcher
 */
protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {
    String rootDirPath = determineRootDir(locationPattern);
    String subPattern = locationPattern.substring(rootDirPath.length());
    Resource[] rootDirResources = getResources(rootDirPath);
    Set<Resource> result = new LinkedHashSet<Resource>(16);
    for (Resource rootDirResource : rootDirResources) {
        rootDirResource = resolveRootDirResource(rootDirResource);
        if (rootDirResource.getURL().getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
            result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirResource, subPattern, getPathMatcher()));
        } else if (isJarResource(rootDirResource)) {
            result.addAll(doFindPathMatchingJarResources(rootDirResource, subPattern));
        } else {
            result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern));
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result);
    }
    return result.toArray(new Resource[result.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) VfsResource(org.hotswap.agent.util.spring.io.resource.VfsResource) FileSystemResource(org.hotswap.agent.util.spring.io.resource.FileSystemResource) UrlResource(org.hotswap.agent.util.spring.io.resource.UrlResource) Resource(org.hotswap.agent.util.spring.io.resource.Resource)

Example 5 with Resource

use of org.hotswap.agent.util.spring.io.resource.Resource in project HotswapAgent by HotswapProjects.

the class PathMatchingResourcePatternResolver method doFindAllClassPathResources.

/**
 * Find all class location resources with the given path via the
 * ClassLoader. Called by {@link #findAllClassPathResources(String)}.
 *
 * @param path
 *            the absolute path within the classpath (never a leading slash)
 * @return a mutable Set of matching Resource instances
 */
protected Set<Resource> doFindAllClassPathResources(String path) throws IOException {
    Set<Resource> result = new LinkedHashSet<Resource>(16);
    ClassLoader cl = getClassLoader();
    Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path));
    while (resourceUrls.hasMoreElements()) {
        URL url = resourceUrls.nextElement();
        result.add(convertClassLoaderURL(url));
    }
    if ("".equals(path)) {
        // The above result is likely to be incomplete, i.e. only containing
        // file system references.
        // We need to have pointers to each of the jar files on the
        // classpath as well...
        addAllClassLoaderJarRoots(cl, result);
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) VfsResource(org.hotswap.agent.util.spring.io.resource.VfsResource) FileSystemResource(org.hotswap.agent.util.spring.io.resource.FileSystemResource) UrlResource(org.hotswap.agent.util.spring.io.resource.UrlResource) Resource(org.hotswap.agent.util.spring.io.resource.Resource) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)6 Resource (org.hotswap.agent.util.spring.io.resource.Resource)6 FileSystemResource (org.hotswap.agent.util.spring.io.resource.FileSystemResource)4 UrlResource (org.hotswap.agent.util.spring.io.resource.UrlResource)4 VfsResource (org.hotswap.agent.util.spring.io.resource.VfsResource)4 URL (java.net.URL)3 IOException (java.io.IOException)2 JarFile (java.util.jar.JarFile)2 PathMatchingResourcePatternResolver (org.hotswap.agent.util.spring.path.PathMatchingResourcePatternResolver)2 File (java.io.File)1 JarURLConnection (java.net.JarURLConnection)1 URLClassLoader (java.net.URLClassLoader)1 URLConnection (java.net.URLConnection)1 JarEntry (java.util.jar.JarEntry)1 ZipException (java.util.zip.ZipException)1