Search in sources :

Example 6 with UrlResource

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

the class ClassLoaderFilesResourcePatternResolver method getAdditionalResources.

private List<Resource> getAdditionalResources(String locationPattern) throws MalformedURLException {
    List<Resource> additionalResources = new ArrayList<>();
    String trimmedLocationPattern = trimLocationPattern(locationPattern);
    for (SourceFolder sourceFolder : this.classLoaderFiles.getSourceFolders()) {
        for (Entry<String, ClassLoaderFile> entry : sourceFolder.getFilesEntrySet()) {
            String name = entry.getKey();
            ClassLoaderFile file = entry.getValue();
            if (file.getKind() == Kind.ADDED && this.antPathMatcher.match(trimmedLocationPattern, name)) {
                URL url = new URL("reloaded", null, -1, "/" + name, new ClassLoaderFileURLStreamHandler(file));
                UrlResource resource = new UrlResource(url);
                additionalResources.add(resource);
            }
        }
    }
    return additionalResources;
}
Also used : SourceFolder(org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles.SourceFolder) ClassLoaderFileURLStreamHandler(org.springframework.boot.devtools.restart.classloader.ClassLoaderFileURLStreamHandler) UrlResource(org.springframework.core.io.UrlResource) ArrayList(java.util.ArrayList) UrlResource(org.springframework.core.io.UrlResource) ServletContextResource(org.springframework.web.context.support.ServletContextResource) Resource(org.springframework.core.io.Resource) AbstractResource(org.springframework.core.io.AbstractResource) ClassLoaderFile(org.springframework.boot.devtools.restart.classloader.ClassLoaderFile) URL(java.net.URL)

Example 7 with UrlResource

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

the class BeanWrapperGenericsTests method testGenericList.

@Test
public void testGenericList() throws MalformedURLException {
    GenericBean<?> gb = new GenericBean<>();
    BeanWrapper bw = new BeanWrapperImpl(gb);
    List<String> input = new ArrayList<>();
    input.add("http://localhost:8080");
    input.add("http://localhost:9090");
    bw.setPropertyValue("resourceList", input);
    assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0));
    assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1));
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ArrayList(java.util.ArrayList) GenericBean(org.springframework.tests.sample.beans.GenericBean) Test(org.junit.Test)

Example 8 with UrlResource

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

the class SpringFactoriesLoader method loadFactoryNames.

/**
	 * Load the fully qualified class names of factory implementations of the
	 * given type from {@value #FACTORIES_RESOURCE_LOCATION}, using the given
	 * class loader.
	 * @param factoryClass the interface or abstract class representing the factory
	 * @param classLoader the ClassLoader to use for loading resources; can be
	 * {@code null} to use the default
	 * @see #loadFactories
	 * @throws IllegalArgumentException if an error occurs while loading factory names
	 */
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
    String factoryClassName = factoryClass.getName();
    try {
        Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
        List<String> result = new ArrayList<>();
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
            String factoryClassNames = properties.getProperty(factoryClassName);
            result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
        }
        return result;
    } catch (IOException ex) {
        throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() + "] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex);
    }
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL)

Example 9 with UrlResource

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

the class CachingMetadataReaderLeakTests method testSignificantLoad.

@Test
public void testSignificantLoad() throws Exception {
    Assume.group(TestGroup.LONG_RUNNING);
    // the biggest public class in the JDK (>60k)
    URL url = getClass().getResource("/java/awt/Component.class");
    assertThat(url, notNullValue());
    // look at a LOT of items
    for (int i = 0; i < ITEMS_TO_LOAD; i++) {
        Resource resource = new UrlResource(url) {

            @Override
            public boolean equals(Object obj) {
                return (obj == this);
            }

            @Override
            public int hashCode() {
                return System.identityHashCode(this);
            }
        };
        MetadataReader reader = mrf.getMetadataReader(resource);
        assertThat(reader, notNullValue());
    }
// useful for profiling to take snapshots
// System.in.read();
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) URL(java.net.URL) Test(org.junit.Test)

Example 10 with UrlResource

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

the class PathResourceResolverTests method checkRelativeLocation.

// SPR-12624
@Test
public void checkRelativeLocation() throws Exception {
    String locationUrl = new UrlResource(getClass().getResource("./test/")).getURL().toExternalForm();
    Resource location = new UrlResource(locationUrl.replace("/springframework", "/../org/springframework"));
    List<Resource> locations = singletonList(location);
    assertNotNull(this.resolver.resolveResource(null, "main.css", locations, null).block(Duration.ofMillis(5000)));
}
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) Test(org.junit.Test)

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