Search in sources :

Example 41 with UrlResource

use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.

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.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<>(16);
    for (Resource rootDirResource : rootDirResources) {
        rootDirResource = resolveRootDirResource(rootDirResource);
        URL rootDirURL = rootDirResource.getURL();
        if (equinoxResolveMethod != null) {
            if (rootDirURL.getProtocol().startsWith("bundle")) {
                rootDirURL = (URL) ReflectionUtils.invokeMethod(equinoxResolveMethod, null, rootDirURL);
                rootDirResource = new UrlResource(rootDirURL);
            }
        }
        if (rootDirURL.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
            result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirURL, subPattern, getPathMatcher()));
        } else if (ResourceUtils.isJarURL(rootDirURL) || isJarResource(rootDirResource)) {
            result.addAll(doFindPathMatchingJarResources(rootDirResource, rootDirURL, 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) UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) VfsResource(org.springframework.core.io.VfsResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) URL(java.net.URL)

Example 42 with UrlResource

use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.

the class PathMatchingResourcePatternResolver method addClassPathManifestEntries.

/**
	 * Determine jar file references from the "java.class.path." manifest property and add them
	 * to the given set of resources in the form of pointers to the root of the jar file content.
	 * @param result the set of resources to add jar roots to
	 * @since 4.3
	 */
protected void addClassPathManifestEntries(Set<Resource> result) {
    try {
        String javaClassPathProperty = System.getProperty("java.class.path");
        for (String path : StringUtils.delimitedListToStringArray(javaClassPathProperty, System.getProperty("path.separator"))) {
            try {
                File file = new File(path);
                UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX + ResourceUtils.FILE_URL_PREFIX + file.getAbsolutePath() + ResourceUtils.JAR_URL_SEPARATOR);
                if (jarResource.exists()) {
                    result.add(jarResource);
                }
            } catch (MalformedURLException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Cannot search for matching files underneath [" + path + "] because it cannot be converted to a valid 'jar:' URL: " + ex.getMessage());
                }
            }
        }
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to evaluate 'java.class.path' manifest entries: " + ex);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) UrlResource(org.springframework.core.io.UrlResource) JarFile(java.util.jar.JarFile) File(java.io.File) URISyntaxException(java.net.URISyntaxException) ZipException(java.util.zip.ZipException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 43 with UrlResource

use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.

the class PersistenceXmlParsingTests method testPersistenceUnitRootUrlWithJar.

@Test
public void testPersistenceUnitRootUrlWithJar() throws Exception {
    ClassPathResource archive = new ClassPathResource("/org/springframework/orm/jpa/jpa-archive.jar");
    String newRoot = "jar:" + archive.getURL().toExternalForm() + "!/META-INF/persist.xml";
    Resource insideArchive = new UrlResource(newRoot);
    // make sure the location actually exists
    assertTrue(insideArchive.exists());
    URL url = PersistenceUnitReader.determinePersistenceUnitRootUrl(insideArchive);
    assertTrue("the archive location should have been returned", archive.getURL().sameFile(url));
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) URL(java.net.URL) Test(org.junit.Test)

Example 44 with UrlResource

use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.

the class ServletContextResourcePatternResolver method doRetrieveMatchingJarEntries.

/**
	 * Extract entries from the given jar by pattern.
	 * @param jarFilePath the path to the jar file
	 * @param entryPattern the pattern for jar entries to match
	 * @param result the Set of matching Resources to add to
	 */
private void doRetrieveMatchingJarEntries(String jarFilePath, String entryPattern, Set<Resource> result) {
    if (logger.isDebugEnabled()) {
        logger.debug("Searching jar file [" + jarFilePath + "] for entries matching [" + entryPattern + "]");
    }
    try {
        JarFile jarFile = new JarFile(jarFilePath);
        try {
            for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
                JarEntry entry = entries.nextElement();
                String entryPath = entry.getName();
                if (getPathMatcher().match(entryPattern, entryPath)) {
                    result.add(new UrlResource(ResourceUtils.URL_PROTOCOL_JAR, ResourceUtils.FILE_URL_PREFIX + jarFilePath + ResourceUtils.JAR_URL_SEPARATOR + entryPath));
                }
            }
        } finally {
            jarFile.close();
        }
    } catch (IOException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Cannot search for matching resources in jar file [" + jarFilePath + "] because the jar cannot be opened through the file system", ex);
        }
    }
}
Also used : UrlResource(org.springframework.core.io.UrlResource) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 45 with UrlResource

use of org.springframework.core.io.UrlResource in project spring-framework by spring-projects.

the class CandidateComponentsIndexLoader method doLoadIndex.

private static CandidateComponentsIndex doLoadIndex(ClassLoader classLoader) {
    if (shouldIgnoreIndex) {
        return null;
    }
    try {
        Enumeration<URL> urls = classLoader.getResources(COMPONENTS_RESOURCE_LOCATION);
        if (!urls.hasMoreElements()) {
            return null;
        }
        List<Properties> result = new ArrayList<>();
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
            result.add(properties);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Loaded " + result.size() + "] index(es)");
        }
        int totalCount = result.stream().mapToInt(Properties::size).sum();
        return (totalCount > 0 ? new CandidateComponentsIndex(result) : null);
    } catch (IOException ex) {
        throw new IllegalStateException("Unable to load indexes from location [" + COMPONENTS_RESOURCE_LOCATION + "]", ex);
    }
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) SpringProperties(org.springframework.core.SpringProperties) URL(java.net.URL)

Aggregations

UrlResource (org.springframework.core.io.UrlResource)56 Test (org.junit.Test)31 Resource (org.springframework.core.io.Resource)26 URL (java.net.URL)17 Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)10 ArrayList (java.util.ArrayList)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 GenericBean (org.springframework.tests.sample.beans.GenericBean)9 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)7 IOException (java.io.IOException)6 InputStream (java.io.InputStream)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 JUnitDNSServer (org.opennms.core.test.dns.annotations.JUnitDNSServer)5 BeansException (org.springframework.beans.BeansException)5 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 MalformedURLException (java.net.MalformedURLException)4 JAXBContext (javax.xml.bind.JAXBContext)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 File (java.io.File)3