Example usage for org.springframework.core.io ResourceLoader CLASSPATH_URL_PREFIX

List of usage examples for org.springframework.core.io ResourceLoader CLASSPATH_URL_PREFIX

Introduction

In this page you can find the example usage for org.springframework.core.io ResourceLoader CLASSPATH_URL_PREFIX.

Prototype

String CLASSPATH_URL_PREFIX

To view the source code for org.springframework.core.io ResourceLoader CLASSPATH_URL_PREFIX.

Click Source Link

Document

Pseudo URL prefix for loading from the class path: "classpath:".

Usage

From source file:spring.osgi.utils.OsgiResourceUtils.java

/**
 * Return the search type to be used for the give string based on the
 * prefix.//from   ww  w.  java 2 s.  c om
 *
 * @param path path
 * @return type
 */
public static int getSearchType(String path) {
    Assert.notNull(path);
    int type;
    String prefix = getPrefix(path);

    // no prefix is treated just like osgibundle:
    if (!StringUtils.hasText(prefix))
        type = PREFIX_TYPE_NOT_SPECIFIED;
    else if (prefix.startsWith(OsgiBundleResource.BUNDLE_URL_PREFIX))
        type = PREFIX_TYPE_BUNDLE_SPACE;
    else if (prefix.startsWith(OsgiBundleResource.BUNDLE_JAR_URL_PREFIX))
        type = PREFIX_TYPE_BUNDLE_JAR;
    else if (prefix.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX))
        type = PREFIX_TYPE_CLASS_SPACE;
    else if (prefix.startsWith(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX))
        type = PREFIX_TYPE_CLASS_ALL_SPACE;

    else
        type = PREFIX_TYPE_UNKNOWN;

    return type;
}

From source file:it.scoppelletti.sdk.schemaupdate.SpringResourceAccessor.java

/**
 * Restituisce una risorsa.//from   w ww .  jav a  2  s .co  m
 * 
 * @param  file Nome della risorsa.
 * @return      Risorsa.
 */
private Resource getResource(String file) {
    if (isClasspathResource(myParentFile) && !isClasspathResource(file)) {
        file = ResourceLoader.CLASSPATH_URL_PREFIX.concat(file);
    }

    return myResLoader.getResource(file);
}

From source file:it.scoppelletti.sdk.schemaupdate.SpringResourceAccessor.java

/**
 * Verifica se il percorso di una risorsa è relativo al
 * class-path.//from ww  w. java  2s  . c o  m
 * 
 * @param  path Percorso.
 * @return      Esito della verifica.
 */
private boolean isClasspathResource(String path) {
    return path.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX);
}

From source file:spring.osgi.io.OsgiBundleResource.java

/**
 * Constructs a new <code>OsgiBundleResource</code> instance.
 *
 * @param bundle OSGi bundle used by this resource
 * @param path   resource path inside the bundle.
 *//*from w  w w.  ja  va2s. c o  m*/
public OsgiBundleResource(Bundle bundle, String path) {
    Assert.notNull(bundle, "Bundle must not be null");
    this.bundle = bundle;

    // check path
    Assert.notNull(path, "Path must not be null");

    this.path = StringUtils.cleanPath(path);

    this.searchType = OsgiResourceUtils.getSearchType(this.path);

    switch (this.searchType) {
    case OsgiResourceUtils.PREFIX_TYPE_NOT_SPECIFIED:
        pathWithoutPrefix = path;
        break;
    case OsgiResourceUtils.PREFIX_TYPE_BUNDLE_SPACE:
        pathWithoutPrefix = path.substring(BUNDLE_URL_PREFIX.length());
        break;
    case OsgiResourceUtils.PREFIX_TYPE_BUNDLE_JAR:
        pathWithoutPrefix = path.substring(BUNDLE_JAR_URL_PREFIX.length());
        break;
    case OsgiResourceUtils.PREFIX_TYPE_CLASS_SPACE:
        pathWithoutPrefix = path.substring(ResourceLoader.CLASSPATH_URL_PREFIX.length());
        break;
    // prefix unknown so the path will be resolved outside the context
    default:
        pathWithoutPrefix = null;
    }
}

From source file:com.haulmont.cuba.core.sys.AbstractWebAppContextLoader.java

protected void initAppProperties(ServletContext sc) {
    // get properties from web.xml
    String appProperties = sc.getInitParameter(APP_PROPS_PARAM);
    if (appProperties != null) {
        StrTokenizer tokenizer = new StrTokenizer(appProperties);
        for (String str : tokenizer.getTokenArray()) {
            int i = str.indexOf("=");
            if (i < 0)
                continue;
            String name = StringUtils.substring(str, 0, i);
            String value = StringUtils.substring(str, i + 1);
            if (!StringUtils.isBlank(name)) {
                AppContext.setProperty(name, value);
            }/*from   ww  w  .  j  a v  a 2s .  co m*/
        }
    }

    // get properties from a set of app.properties files defined in web.xml
    String propsConfigName = getAppPropertiesConfig(sc);
    if (propsConfigName == null)
        throw new IllegalStateException(APP_PROPS_CONFIG_PARAM + " servlet context parameter not defined");

    final Properties properties = new Properties();

    DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
    StrTokenizer tokenizer = new StrTokenizer(propsConfigName);
    tokenizer.setQuoteChar('"');
    for (String str : tokenizer.getTokenArray()) {
        log.trace("Processing properties location: {}", str);
        str = StrSubstitutor.replaceSystemProperties(str);
        InputStream stream = null;
        try {
            if (ResourceUtils.isUrl(str) || str.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)) {
                Resource resource = resourceLoader.getResource(str);
                if (resource.exists())
                    stream = resource.getInputStream();
            } else {
                stream = sc.getResourceAsStream(str);
            }

            if (stream != null) {
                log.info("Loading app properties from {}", str);
                try (Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
                    properties.load(reader);
                }
            } else {
                log.trace("Resource {} not found, ignore it", str);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }

    for (Object key : properties.keySet()) {
        AppContext.setProperty((String) key, properties.getProperty((String) key).trim());
    }
}

From source file:org.broadleafcommerce.test.BroadleafGenericGroovyXmlWebContextLoader.java

/**
 * Configures web resources for the supplied web application context (WAC).
 *
 * <h4>Implementation Details</h4>
 *
 * <p>If the supplied WAC has no parent or its parent is not a WAC, the
 * supplied WAC will be configured as the Root WAC (see "<em>Root WAC
 * Configuration</em>" below)./*from  w ww  . j  a  v a2  s.c o m*/
 *
 * <p>Otherwise the context hierarchy of the supplied WAC will be traversed
 * to find the top-most WAC (i.e., the root); and the {@link ServletContext}
 * of the Root WAC will be set as the {@code ServletContext} for the supplied
 * WAC.
 *
 * <h4>Root WAC Configuration</h4>
 *
 * <ul>
 * <li>The resource base path is retrieved from the supplied
 * {@code WebMergedContextConfiguration}.</li>
 * <li>A {@link ResourceLoader} is instantiated for the {@link MockServletContext}:
 * if the resource base path is prefixed with "{@code classpath:/}", a
 * {@link DefaultResourceLoader} will be used; otherwise, a
 * {@link FileSystemResourceLoader} will be used.</li>
 * <li>A {@code MockServletContext} will be created using the resource base
 * path and resource loader.</li>
 * <li>The supplied {@link MergeXmlWebApplicationContext} is then stored in
 * the {@code MockServletContext} under the
 * {@link MergeXmlWebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} key.</li>
 * <li>Finally, the {@code MockServletContext} is set in the
 * {@code MergeXmlWebApplicationContext}.</li>
 *
 * @param context the merge xml web application context for which to configure the web
 * resources
 * @param webMergedConfig the merged context configuration to use to load the
 * merge xml web application context
 */
protected void configureWebResources(MergeXmlWebApplicationContext context,
        WebMergedContextConfiguration webMergedConfig) {

    ApplicationContext parent = context.getParent();

    // if the WAC has no parent or the parent is not a WAC, set the WAC as
    // the Root WAC:
    if (parent == null || (!(parent instanceof WebApplicationContext))) {
        String resourceBasePath = webMergedConfig.getResourceBasePath();
        ResourceLoader resourceLoader = resourceBasePath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)
                ? new DefaultResourceLoader()
                : new FileSystemResourceLoader();

        ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader);
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
        context.setServletContext(servletContext);
    } else {
        ServletContext servletContext = null;

        // find the Root WAC
        while (parent != null) {
            if (parent instanceof WebApplicationContext
                    && !(parent.getParent() instanceof WebApplicationContext)) {
                servletContext = ((WebApplicationContext) parent).getServletContext();
                break;
            }
            parent = parent.getParent();
        }
        Assert.state(servletContext != null,
                "Failed to find Root MergeXmlWebApplicationContext in the context hierarchy");
        context.setServletContext(servletContext);
    }
}

From source file:org.springframework.test.context.support.AbstractGenericWebContextLoader.java

/**
 * TODO [SPR-9864] Document configureWebResources().
 *///ww  w.j  a v a2 s  .c  o  m
protected void configureWebResources(GenericWebApplicationContext context,
        WebMergedContextConfiguration webMergedConfig) {

    String resourceBasePath = webMergedConfig.getResourceBasePath();
    ResourceLoader resourceLoader = resourceBasePath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)
            ? new DefaultResourceLoader()
            : new FileSystemResourceLoader();

    ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader);
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
    context.setServletContext(servletContext);
}

From source file:org.springframework.test.context.web.AbstractGenericWebContextLoader.java

/**
 * Configures web resources for the supplied web application context (WAC).
 * <h4>Implementation Details</h4>
 * <p>If the supplied WAC has no parent or its parent is not a WAC, the
 * supplied WAC will be configured as the Root WAC (see "<em>Root WAC
 * Configuration</em>" below)./*from   ww w  .jav a  2  s . co  m*/
 * <p>Otherwise the context hierarchy of the supplied WAC will be traversed
 * to find the top-most WAC (i.e., the root); and the {@link ServletContext}
 * of the Root WAC will be set as the {@code ServletContext} for the supplied
 * WAC.
 * <h4>Root WAC Configuration</h4>
 * <ul>
 * <li>The resource base path is retrieved from the supplied
 * {@code WebMergedContextConfiguration}.</li>
 * <li>A {@link ResourceLoader} is instantiated for the {@link MockServletContext}:
 * if the resource base path is prefixed with "{@code classpath:}", a
 * {@link DefaultResourceLoader} will be used; otherwise, a
 * {@link FileSystemResourceLoader} will be used.</li>
 * <li>A {@code MockServletContext} will be created using the resource base
 * path and resource loader.</li>
 * <li>The supplied {@link GenericWebApplicationContext} is then stored in
 * the {@code MockServletContext} under the
 * {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} key.</li>
 * <li>Finally, the {@code MockServletContext} is set in the
 * {@code WebApplicationContext}.</li>
 * @param context the web application context for which to configure the web resources
 * @param webMergedConfig the merged context configuration to use to load the web application context
 */
protected void configureWebResources(GenericWebApplicationContext context,
        WebMergedContextConfiguration webMergedConfig) {

    ApplicationContext parent = context.getParent();

    // If the WebApplicationContext has no parent or the parent is not a WebApplicationContext,
    // set the current context as the root WebApplicationContext:
    if (parent == null || (!(parent instanceof WebApplicationContext))) {
        String resourceBasePath = webMergedConfig.getResourceBasePath();
        ResourceLoader resourceLoader = (resourceBasePath.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)
                ? new DefaultResourceLoader()
                : new FileSystemResourceLoader());
        ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader);
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
        context.setServletContext(servletContext);
    } else {
        ServletContext servletContext = null;
        // Find the root WebApplicationContext
        while (parent != null) {
            if (parent instanceof WebApplicationContext
                    && !(parent.getParent() instanceof WebApplicationContext)) {
                servletContext = ((WebApplicationContext) parent).getServletContext();
                break;
            }
            parent = parent.getParent();
        }
        Assert.state(servletContext != null,
                "Failed to find root WebApplicationContext in the context hierarchy");
        context.setServletContext(servletContext);
    }
}