Java Code Examples for org.springframework.core.io.support.ResourcePatternUtils#isUrl()

The following examples show how to use org.springframework.core.io.support.ResourcePatternUtils#isUrl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContextSingletonBeanFactoryLocator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an instance which uses 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's context class
 * loader'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.
 * @param selector the location 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 ApplicationContext 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("ContextSingletonBeanFactoryLocator.getInstance(): instances.hashCode=" +
					instances.hashCode() + ", instances=" + instances);
		}
		BeanFactoryLocator bfl = instances.get(resourceLocation);
		if (bfl == null) {
			bfl = new ContextSingletonBeanFactoryLocator(resourceLocation);
			instances.put(resourceLocation, bfl);
		}
		return bfl;
	}
}
 
Example 2
Source File: SingletonBeanFactoryLocator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an instance which uses 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.
 * @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;
	}
}
 
Example 3
Source File: SingletonBeanFactoryLocator.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * 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.
 * @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;
	}
}
 
Example 4
Source File: ContextSingletonBeanFactoryLocator.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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's context class
 * loader'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.
 * @param selector the location 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 ApplicationContext 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("ContextSingletonBeanFactoryLocator.getInstance(): instances.hashCode=" +
					instances.hashCode() + ", instances=" + instances);
		}
		BeanFactoryLocator bfl = instances.get(resourceLocation);
		if (bfl == null) {
			bfl = new ContextSingletonBeanFactoryLocator(resourceLocation);
			instances.put(resourceLocation, bfl);
		}
		return bfl;
	}
}
 
Example 5
Source File: AbstractSingleSpringContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Subclasses can override this method to return the locations of their
 * config files.
 * <p>A plain path will be treated as class path location, e.g.:
 * "org/springframework/whatever/foo.xml". Note however that you may prefix
 * path locations with standard Spring resource prefixes. Therefore, a
 * config location path prefixed with "classpath:" with behave the same as a
 * plain path, but a config location such as
 * "file:/some/path/path/location/appContext.xml" will be treated as a
 * filesystem location.
 * <p>The default implementation builds config locations for the config paths
 * specified through {@link #getConfigPaths()}.
 * @return an array of config locations
 * @see #getConfigPaths()
 * @see org.springframework.core.io.ResourceLoader#getResource(String)
 */
protected final String[] getConfigLocations() {
	String[] paths = getConfigPaths();
	String[] convertedPaths = new String[paths.length];
	for (int i = 0; i < paths.length; i++) {
		String path = paths[i];
		if (path.startsWith(SLASH)) {
			convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path;
		}
		else if (!ResourcePatternUtils.isUrl(path)) {
			convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + SLASH
					+ StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(getClass()) + SLASH + path);
		}
		else {
			convertedPaths[i] = StringUtils.cleanPath(path);
		}
	}
	return convertedPaths;
}
 
Example 6
Source File: SingletonBeanFactoryLocator.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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.
 * @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;
	}
}
 
Example 7
Source File: TestContextResourceUtils.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Convert the supplied paths to classpath resource paths.
 *
 * <p>For each of the supplied paths:
 * <ul>
 * <li>A plain path &mdash; for example, {@code "context.xml"} &mdash; will
 * be treated as a classpath resource that is relative to the package in
 * which the specified class is defined.
 * <li>A path starting with a slash will be treated as an absolute path
 * within the classpath, for example: {@code "/org/example/schema.sql"}.
 * <li>A path which is prefixed with a URL protocol (e.g.,
 * {@link ResourceUtils#CLASSPATH_URL_PREFIX classpath:},
 * {@link ResourceUtils#FILE_URL_PREFIX file:}, {@code http:}, etc.) will be
 * {@link StringUtils#cleanPath cleaned} but otherwise unmodified.
 * </ul>
 * @param clazz the class with which the paths are associated
 * @param paths the paths to be converted
 * @return a new array of converted resource paths
 * @see #convertToResources
 */
public static String[] convertToClasspathResourcePaths(Class<?> clazz, String... paths) {
	String[] convertedPaths = new String[paths.length];
	for (int i = 0; i < paths.length; i++) {
		String path = paths[i];
		if (path.startsWith(SLASH)) {
			convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path;
		}
		else if (!ResourcePatternUtils.isUrl(path)) {
			convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + SLASH +
					StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + SLASH + path);
		}
		else {
			convertedPaths[i] = StringUtils.cleanPath(path);
		}
	}
	return convertedPaths;
}
 
Example 8
Source File: TestContextResourceUtils.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Convert the supplied paths to classpath resource paths.
 *
 * <p>For each of the supplied paths:
 * <ul>
 * <li>A plain path &mdash; for example, {@code "context.xml"} &mdash; will
 * be treated as a classpath resource that is relative to the package in
 * which the specified class is defined.
 * <li>A path starting with a slash will be treated as an absolute path
 * within the classpath, for example: {@code "/org/example/schema.sql"}.
 * <li>A path which is prefixed with a URL protocol (e.g.,
 * {@link ResourceUtils#CLASSPATH_URL_PREFIX classpath:},
 * {@link ResourceUtils#FILE_URL_PREFIX file:}, {@code http:}, etc.) will be
 * {@link StringUtils#cleanPath cleaned} but otherwise unmodified.
 * </ul>
 * @param clazz the class with which the paths are associated
 * @param paths the paths to be converted
 * @return a new array of converted resource paths
 * @see #convertToResources
 */
public static String[] convertToClasspathResourcePaths(Class<?> clazz, String... paths) {
	String[] convertedPaths = new String[paths.length];
	for (int i = 0; i < paths.length; i++) {
		String path = paths[i];
		if (path.startsWith(SLASH)) {
			convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path;
		}
		else if (!ResourcePatternUtils.isUrl(path)) {
			convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + SLASH +
					StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + SLASH + path);
		}
		else {
			convertedPaths[i] = StringUtils.cleanPath(path);
		}
	}
	return convertedPaths;
}
 
Example 9
Source File: TestContextResourceUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Convert the supplied paths to classpath resource paths.
 *
 * <p>For each of the supplied paths:
 * <ul>
 * <li>A plain path &mdash; for example, {@code "context.xml"} &mdash; will
 * be treated as a classpath resource that is relative to the package in
 * which the specified class is defined.
 * <li>A path starting with a slash will be treated as an absolute path
 * within the classpath, for example: {@code "/org/example/schema.sql"}.
 * <li>A path which is prefixed with a URL protocol (e.g.,
 * {@link ResourceUtils#CLASSPATH_URL_PREFIX classpath:},
 * {@link ResourceUtils#FILE_URL_PREFIX file:}, {@code http:}, etc.) will be
 * {@link StringUtils#cleanPath cleaned} but otherwise unmodified.
 *
 * @param clazz the class with which the paths are associated
 * @param paths the paths to be converted
 * @return a new array of converted resource paths
 * @see #convertToResources
 */
public static String[] convertToClasspathResourcePaths(Class<?> clazz, String... paths) {
	String[] convertedPaths = new String[paths.length];
	for (int i = 0; i < paths.length; i++) {
		String path = paths[i];
		if (path.startsWith(SLASH)) {
			convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path;
		}
		else if (!ResourcePatternUtils.isUrl(path)) {
			convertedPaths[i] = ResourceUtils.CLASSPATH_URL_PREFIX + SLASH
					+ StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + SLASH + path);
		}
		else {
			convertedPaths[i] = StringUtils.cleanPath(path);
		}
	}
	return convertedPaths;
}