Example usage for org.springframework.core.io.support ResourcePatternUtils isUrl

List of usage examples for org.springframework.core.io.support ResourcePatternUtils isUrl

Introduction

In this page you can find the example usage for org.springframework.core.io.support ResourcePatternUtils isUrl.

Prototype

public static boolean isUrl(@Nullable String resourceLocation) 

Source Link

Document

Return whether the given resource location is a URL: either a special "classpath" or "classpath*" pseudo URL or a standard URL.

Usage

From source file:com.taobao.itest.listener.ResourceLocationProcessingUtil.java

public static String[] modifyLocations(Class<?> clazz, String... locations) {
    String[] modifiedLocations = new String[locations.length];
    for (int i = 0; i < locations.length; i++) {
        String path = locations[i];
        if (path.startsWith("/")) {
            modifiedLocations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path;
        } else if (!ResourcePatternUtils.isUrl(path)) {
            modifiedLocations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + "/"
                    + StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + "/" + path);
        } else {//w  w w  .  j  ava  2 s.  c om
            modifiedLocations[i] = StringUtils.cleanPath(path);
        }
    }
    return modifiedLocations;
}

From source file:com.trigonic.utils.spring.beans.ImportHelper.java

private static boolean isAbsoluteLocation(String location) {
    boolean absoluteLocation = false;
    try {/*from w ww . j  ava  2  s  . co  m*/
        absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute();
    } catch (URISyntaxException ex) {
        // cannot convert to an URI, considering the location relative unless it has the "classpath*:" prefix
    }
    return absoluteLocation;
}

From source file:com.excilys.ebi.utils.spring.log.logback.test.LogbackConfigurerTestExecutionListener.java

/**
 * Generate a modified version of the supplied location and returns it.
 * <p>/*from   w w  w .  j a  va 2s.c o m*/
 * A plain path, e.g. &quot;context.xml&quot;, will be treated as a
 * classpath resource from the same package in which the specified class is
 * defined. A path starting with a slash is treated as a fully qualified
 * class path location, e.g.: &quot;/com/example/whatever/foo.xml&quot;. A
 * path which references a URL (e.g., a path prefixed with
 * {@link ResourceUtils#CLASSPATH_URL_PREFIX classpath:},
 * {@link ResourceUtils#FILE_URL_PREFIX file:}, <code>http:</code>, etc.)
 * will be added to the results unchanged.
 * <p>
 * Subclasses can override this method to implement a different
 * <em>location modification</em> strategy.
 * 
 * @param clazz
 *            the class with which the locations are associated
 * @param locations
 *            the resource location to be modified
 * @return the modified application context resource location
 */
protected String modifyLocation(Class<?> clazz, String location) {
    String modifiedLocation = null;
    if (location.startsWith("/")) {
        modifiedLocation = ResourceUtils.CLASSPATH_URL_PREFIX + location;
    } else if (!ResourcePatternUtils.isUrl(location)) {
        modifiedLocation = ResourceUtils.CLASSPATH_URL_PREFIX
                + StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + "/" + location);
    } else {
        modifiedLocation = StringUtils.cleanPath(location);
    }
    return modifiedLocation;
}

From source file:org.springframework.beans.factory.access.SingletonBeanFactoryLocator.java

/**
 * Returns an instance which uses the the specified selector, as the name of the
 * definition file(s). In the case of a name with a Spring 'classpath*:' prefix,
 * or with no prefix, which is treated the same, the current thread context
 * ClassLoader's {@code getResources} method will be called with this value
 * to get all resources having that name. These resources will then be combined to
 * form a definition. In the case where the name uses a Spring 'classpath:' prefix,
 * or a standard URL prefix, then only one resource file will be loaded as the
 * definition.//from   w w w.j  a v  a2  s  . c om
 * @param selector the name of the resource(s) which will be read and
 * combined to form the definition for the BeanFactoryLocator instance.
 * Any such files must form a valid BeanFactory definition.
 * @return the corresponding BeanFactoryLocator instance
 * @throws BeansException in case of factory loading failure
 */
public static BeanFactoryLocator getInstance(String selector) throws BeansException {
    String resourceLocation = selector;
    if (resourceLocation == null) {
        resourceLocation = DEFAULT_RESOURCE_LOCATION;
    }

    // For backwards compatibility, we prepend 'classpath*:' to the selector name if there
    // is no other prefix (i.e. classpath*:, classpath:, or some URL prefix.
    if (!ResourcePatternUtils.isUrl(resourceLocation)) {
        resourceLocation = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resourceLocation;
    }

    synchronized (instances) {
        if (logger.isTraceEnabled()) {
            logger.trace("SingletonBeanFactoryLocator.getInstance(): instances.hashCode=" + instances.hashCode()
                    + ", instances=" + instances);
        }
        BeanFactoryLocator bfl = instances.get(resourceLocation);
        if (bfl == null) {
            bfl = new SingletonBeanFactoryLocator(resourceLocation);
            instances.put(resourceLocation, bfl);
        }
        return bfl;
    }
}

From source file:org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.java

/**
 * Parse an "import" element and load the bean definitions
 * from the given resource into the bean factory.
 *///from  w  w w.j ava  2s. c o m
protected void importBeanDefinitionResource(Element ele) {
    String location = ele.getAttribute(RESOURCE_ATTRIBUTE);
    if (!StringUtils.hasText(location)) {
        getReaderContext().error("Resource location must not be empty", ele);
        return;
    }

    // Resolve system properties: e.g. "${user.dir}"
    location = getReaderContext().getEnvironment().resolveRequiredPlaceholders(location);

    Set<Resource> actualResources = new LinkedHashSet<>(4);

    // Discover whether the location is an absolute or relative URI
    boolean absoluteLocation = false;
    try {
        absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute();
    } catch (URISyntaxException ex) {
        // cannot convert to an URI, considering the location relative
        // unless it is the well-known Spring prefix "classpath*:"
    }

    // Absolute or relative?
    if (absoluteLocation) {
        try {
            int importCount = getReaderContext().getReader().loadBeanDefinitions(location, actualResources);
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "Imported " + importCount + " bean definitions from URL location [" + location + "]");
            }
        } catch (BeanDefinitionStoreException ex) {
            getReaderContext().error("Failed to import bean definitions from URL location [" + location + "]",
                    ele, ex);
        }
    } else {
        // No URL -> considering resource location as relative to the current file.
        try {
            int importCount;
            Resource relativeResource = getReaderContext().getResource().createRelative(location);
            if (relativeResource.exists()) {
                importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource);
                actualResources.add(relativeResource);
            } else {
                String baseLocation = getReaderContext().getResource().getURL().toString();
                importCount = getReaderContext().getReader().loadBeanDefinitions(
                        StringUtils.applyRelativePath(baseLocation, location), actualResources);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Imported " + importCount + " bean definitions from relative location [" + location
                        + "]");
            }
        } catch (IOException ex) {
            getReaderContext().error("Failed to resolve current resource location", ele, ex);
        } catch (BeanDefinitionStoreException ex) {
            getReaderContext().error(
                    "Failed to import bean definitions from relative location [" + location + "]", ele, ex);
        }
    }
    Resource[] actResArray = actualResources.toArray(new Resource[actualResources.size()]);
    getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele));
}

From source file:org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.java

/**
 * Parse an "import" element and load the bean definitions
 * from the given resource into the bean factory.
 *///from w  w  w  . j a  v a2 s  .com
protected void importBeanDefinitionResource(Element ele) throws BeanDefinitionStoreException {
    String location = ele.getAttribute(RESOURCE_ATTRIBUTE);
    // Resolve system properties: e.g. "${user.dir}"
    location = SystemPropertyUtils.resolvePlaceholders(location);

    if (ResourcePatternUtils.isUrl(location)) {
        int importCount = getBeanDefinitionReader().loadBeanDefinitions(location);
        if (logger.isDebugEnabled()) {
            logger.debug("Imported " + importCount + " bean definitions from URL location [" + location + "]");
        }
    } else {
        // No URL -> considering resource location as relative to the current file.
        try {
            Resource relativeResource = getResource().createRelative(location);
            int importCount = getBeanDefinitionReader().loadBeanDefinitions(relativeResource);
            if (logger.isDebugEnabled()) {
                logger.debug("Imported " + importCount + " bean definitions from relative location [" + location
                        + "]");
            }
        } catch (IOException ex) {
            throw new BeanDefinitionStoreException(
                    "Invalid relative resource location [" + location + "] to import bean definitions from",
                    ex);
        }
    }
}