Example usage for org.springframework.core.io ContextResource getURL

List of usage examples for org.springframework.core.io ContextResource getURL

Introduction

In this page you can find the example usage for org.springframework.core.io ContextResource getURL.

Prototype

URL getURL() throws IOException;

Source Link

Document

Return a URL handle for this resource.

Usage

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

/**
 * Searches the bundle classpath (Bundle-Classpath) entries for the given
 * pattern./* w  w w.  j  a v  a 2  s.c o m*/
 *
 * @param bundle  bundle
 * @param pattern pattern
 * @return paths
 * @throws java.io.IOException
 */
private Collection<String> findBundleClassPathMatchingPaths(Bundle bundle, String pattern) throws IOException {
    // list of strings pointing to the matching resources
    List<String> list = new ArrayList<>(4);

    boolean trace = logger.isTraceEnabled();
    if (trace)
        logger.trace("Analyzing " + Constants.BUNDLE_CLASSPATH + " entries for bundle [" + bundle.getBundleId()
                + "|" + bundle.getSymbolicName() + "]");
    // see if there is a bundle class-path defined
    String[] entries = OsgiHeaderUtils.getBundleClassPath(bundle);

    if (trace)
        logger.trace(
                "Found " + Constants.BUNDLE_CLASSPATH + " entries " + ObjectUtils.nullSafeToString(entries));

    // 1. if so, look at the entries
    for (String entry : entries) {
        // make sure to exclude the default entry
        if (!entry.equals(BUNDLE_DEFAULT_CP)) {

            // 2. locate resource first from the bundle space (since it might not exist)
            OsgiBundleResource entryResource = new OsgiBundleResource(bundle, entry);
            // call the internal method to avoid catching an exception
            URL url = null;
            ContextResource res = entryResource.getResourceFromBundleSpace(entry);
            if (res != null) {
                url = res.getURL();
            }

            if (trace)
                logger.trace("Classpath entry [" + entry + "] resolves to [" + url + "]");
            // we've got a valid entry so let's parse it
            if (url != null) {
                String cpEntryPath = url.getPath();
                // is it a jar ?
                if (entry.endsWith(JAR_EXTENSION))
                    findBundleClassPathMatchingJarEntries(list, url, pattern);
                // no, so it must be a folder
                else
                    findBundleClassPathMatchingFolders(list, bundle, cpEntryPath, pattern);
            }
        }
    }

    return list;
}

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

/**
 * Locates the resource in the underlying bundle based on the prefix, if it
 * exists. Note that the location happens per call since due to the dynamic
 * nature of OSGi, the classpath of the bundle (among others) can change
 * during a bundle lifecycle (depending on its imports).
 *
 * @return URL to this resource/*  w w w  . ja  va 2s. c  o  m*/
 * @throws java.io.IOException if the resource cannot be resolved as URL, i.e. if
 *                             the resource is not available as descriptor
 * @see org.osgi.framework.Bundle#getEntry(String)
 * @see org.osgi.framework.Bundle#getResource(String)
 */
public URL getURL() throws IOException {
    ContextResource res = null;
    URL url = null;

    switch (searchType) {
    // same as bundle space but with a different string
    case OsgiResourceUtils.PREFIX_TYPE_NOT_SPECIFIED:
        res = getResourceFromBundleSpace(pathWithoutPrefix);
        break;
    case OsgiResourceUtils.PREFIX_TYPE_BUNDLE_SPACE:
        res = getResourceFromBundleSpace(pathWithoutPrefix);
        break;
    case OsgiResourceUtils.PREFIX_TYPE_BUNDLE_JAR:
        url = getResourceFromBundleJar(pathWithoutPrefix);
        break;
    case OsgiResourceUtils.PREFIX_TYPE_CLASS_SPACE:
        url = getResourceFromBundleClasspath(pathWithoutPrefix);
        break;
    // fallback
    default:
        // just try to convert it to an URL
        url = new URL(path);
        break;
    }

    if (res != null) {
        url = res.getURL();
    }

    if (url == null) {
        throw new FileNotFoundException(
                getDescription() + " cannot be resolved to URL because it does not exist");
    }

    return url;
}

From source file:org.eclipse.gemini.blueprint.io.OsgiBundleResourcePatternResolver.java

/**
 * Searches the bundle classpath (Bundle-Classpath) entries for the given pattern.
 * //from   w ww .j a v a2  s  . co  m
 * @param bundle
 * @param pattern
 * @return
 * @throws IOException
 */
private Collection<String> findBundleClassPathMatchingPaths(Bundle bundle, String pattern) throws IOException {
    // list of strings pointing to the matching resources
    List<String> list = new ArrayList<String>(4);

    boolean trace = logger.isTraceEnabled();
    if (trace)
        logger.trace("Analyzing " + Constants.BUNDLE_CLASSPATH + " entries for bundle [" + bundle.getBundleId()
                + "|" + bundle.getSymbolicName() + "]");
    // see if there is a bundle class-path defined
    String[] entries = OsgiHeaderUtils.getBundleClassPath(bundle);

    if (trace)
        logger.trace(
                "Found " + Constants.BUNDLE_CLASSPATH + " entries " + ObjectUtils.nullSafeToString(entries));

    // 1. if so, look at the entries
    for (int i = 0; i < entries.length; i++) {
        String entry = entries[i];

        // make sure to exclude the default entry
        if (!entry.equals(BUNDLE_DEFAULT_CP)) {

            // 2. locate resource first from the bundle space (since it might not exist)
            OsgiBundleResource entryResource = new OsgiBundleResource(bundle, entry);
            // call the internal method to avoid catching an exception
            URL url = null;
            ContextResource res = entryResource.getResourceFromBundleSpace(entry);
            if (res != null) {
                url = res.getURL();
            }

            if (trace)
                logger.trace("Classpath entry [" + entry + "] resolves to [" + url + "]");
            // we've got a valid entry so let's parse it
            if (url != null) {
                String cpEntryPath = url.getPath();
                // is it a jar ?
                if (entry.endsWith(JAR_EXTENSION))
                    findBundleClassPathMatchingJarEntries(list, url, pattern);
                // no, so it must be a folder
                else
                    findBundleClassPathMatchingFolders(list, bundle, cpEntryPath, pattern);
            }
        }
    }

    return list;
}