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

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

Introduction

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

Prototype

URL getURL() throws IOException;

Source Link

Document

Return a URL handle for this resource.

Usage

From source file:org.activiti.rest.osgi.OsgiClassPathStore.java

private List<String> matchDocumentPaths(String pattern) throws IOException {
    Resource[] resources = getDocumentResources(pattern);
    List<String> documentPaths = new ArrayList<String>(resources.length);
    for (Resource resource : resources) {
        String documentPath = toDocumentPath(resource.getURL().toExternalForm());
        documentPaths.add(documentPath);
    }/*from  w ww .  ja va2 s . c  o  m*/
    return documentPaths;
}

From source file:org.activiti.rest.osgi.OsgiClassPathStore.java

private Resource[] getDocumentResources(String locationPattern) throws IOException {
    String resourcePath = toResourcePath(locationPattern);

    Resource[] resources = resolver.getResources("classpath*:" + resourcePath);
    ArrayList<Resource> list = new ArrayList<Resource>(resources.length);
    for (Resource resource : resources) {
        // only keep documents, not directories
        if (!resource.getURL().toExternalForm().endsWith("/")) {
            list.add(resource);/*from   www.  j  av  a 2 s  . co  m*/
        }
    }

    return list.toArray(new Resource[list.size()]);
}

From source file:org.activiti.rest.osgi.OsgiClassPathStore.java

/**
 * The only change is to resolver instantiation
 *///from www  .  j a va2  s. c o  m
@Override
public void init() {
    // wrap the application context resource resolver with our own
    this.resolver = new OsgiClassPathStoreResourceResolver(applicationContext);

    // check if there are any resources that live under this path
    // this is valid for read-only classpaths (class files + JAR file contents)
    try {
        Resource[] resources = resolver.getResources("classpath*:" + classPath + "/**/*");
        if (resources.length != 0) {
            exists = true;
        } else {
            resources = resolver.getResources("classpath*:" + classPath + "/*");
            if (resources.length != 0) {
                exists = true;
            }
        }

        // NOTE: Locate root of web script store
        // NOTE: Following awkward approach is used to mirror lookup of web scripts within store.  This
        //       ensures root paths match.
        try {
            // Process each root resource - there may be several as the classpath* could match
            // multiple location that each contain the configured path.
            Resource rootResource = null;
            resources = applicationContext.getResources("classpath*:" + classPath + "*");
            List<String> storeDirList = new ArrayList<String>(resources.length);
            for (Resource resource : resources) {
                String externalForm = resource.getURL().toExternalForm();
                if (externalForm.endsWith(classPath) || externalForm.endsWith(classPath + "/")) {
                    // we've found the right resource, let's now bind using string constructor
                    // so that Spring 3 will correctly create relative paths
                    String directoryPath = resource.getFile().getAbsolutePath();
                    if (resource.getFile().isDirectory() && !directoryPath.endsWith("/")) {
                        directoryPath += "/";
                    }
                    if (new FileSystemResource(directoryPath).exists()) {
                        // retrieve file system directory
                        storeDirList.add(resource.getFile().toURI().toURL().toExternalForm());
                    }
                }
            }
            this.storeDirs = storeDirList.toArray(new String[storeDirList.size()]);
        } catch (IOException ioErr) {
            // unable to resolve a storeDir - this is expected for certain protocols such as "vfszip"
            // it is not critical and those protocols don't require it during path resolution later
            if (logger.isDebugEnabled())
                logger.debug("Unable to resolve storeDir for base path " + classPath);
        }
    } catch (IOException ioe) {
        throw new WebScriptException("Failed to initialise Web Script Store classpath: " + classPath, ioe);
    }

    if (!exists && mustExist) {
        throw new WebScriptException(
                "Web Script Store classpath:" + classPath + " must exist; it was not found");
    }
}

From source file:org.activiti.rest.osgi.OsgiClassPathStoreResourceResolver.java

protected Resource resolveRootDirResource(Resource original) throws IOException {
    Resource resolved = super.resolveRootDirResource(original);
    if (resolved == original) {
        // Equinox/Felix specific hack
        try {/*from   w  w w .  j  av  a2  s.  co m*/
            URL url = original.getURL();
            URLConnection con = url.openConnection();
            try {
                Method mth = con.getClass().getMethod("getLocalURL");
                mth.setAccessible(true);
                URL localUrl = (URL) mth.invoke(con);
                if (localUrl != null) {
                    return new UrlResource(localUrl);
                }
            } catch (NoSuchMethodException t) {
                Object targetModule = getField(con, "m_targetModule");
                int classPathIdx = (Integer) getField(con, "m_classPathIdx");
                Object content;
                if (classPathIdx == 0) {
                    content = getField(targetModule, "m_content");
                } else {
                    Object[] contentPath = (Object[]) getField(targetModule, "m_contentPath");
                    content = contentPath[classPathIdx - 1];
                }
                URL localUrl = getContentUrl(content, url.getPath());
                return new UrlResource(localUrl);
            }
        } catch (Throwable t) {
            logger.debug("Could not resolve url");
        }
    }
    return original;
}

From source file:org.alfresco.repo.activities.feed.local.LocalFeedTaskProcessor.java

private List<String> getPaths(String pattern, String classPath) throws IOException {
    Resource[] resources = resolver.getResources(pattern);
    List<String> documentPaths = new ArrayList<String>(resources.length);
    for (Resource resource : resources) {
        String resourcePath = resource.getURL().toExternalForm();

        int idx = resourcePath.lastIndexOf(classPath);
        if (idx != -1) {
            String documentPath = resourcePath.substring(idx);
            documentPath = documentPath.replace('\\', '/');
            if (logger.isTraceEnabled()) {
                logger.trace("Item resource path: " + resourcePath + " , item path: " + documentPath);
            }//from   www .  j  a v  a2 s  .  com
            documentPaths.add(documentPath);
        }
    }
    return documentPaths;
}

From source file:org.alfresco.repo.admin.Log4JHierarchyInit.java

private void importLogSettings(Method method, String springUrl) {
    Resource[] resources = null;//from  ww  w .jav  a 2 s. c o m

    try {
        resources = resolver.getResources(springUrl);
    } catch (Exception e) {
        logger.warn("Failed to find additional Logger configuration: " + springUrl);
    }

    // Read each resource
    for (Resource resource : resources) {
        try {
            URL url = resource.getURL();
            method.invoke(null, url);
        } catch (Throwable e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to add extra Logger configuration: \n" + "   URL:   " + springUrl + "\n"
                        + "   Error: " + e.getMessage(), e);
            }
        }
    }
}

From source file:org.apache.cloudstack.spring.module.model.impl.DefaultModuleDefinition.java

protected void checkNameMatchesSelf() throws IOException {
    String expectedLocation = ModuleLocationUtils.getModuleLocation(baseDir, name);
    Resource self = resolver.getResource(expectedLocation);

    if (!self.exists()) {
        throw new IOException("Resource [" + location() + "] is expected to exist at [" + expectedLocation
                + "] please ensure the name property is correct");
    }/*from   w w  w  . j a  v a 2s  . c  o  m*/

    String moduleUrl = moduleProperties.getURL().toExternalForm();
    String selfUrl = self.getURL().toExternalForm();

    if (!moduleUrl.equals(selfUrl)) {
        throw new IOException("Resource [" + location() + "] and [" + self.getURL()
                + "] do not appear to be the same resource, "
                + "please ensure the name property is correct or that the " + "module is not defined twice");
    }
}

From source file:org.apache.cocoon.core.container.spring.avalon.ConfigurationReader.java

protected String getUrl(Resource rsrc) throws IOException {
    if (rsrc instanceof SourceResource) {
        return ((SourceResource) rsrc).getUrlString();
    } else {//from ww  w.j  a va 2 s. co m
        return rsrc.getURL().toExternalForm();
    }
}

From source file:org.apache.cocoon.core.container.spring.avalon.ConfigurationReader.java

protected void convertSitemap(String sitemapLocation) throws Exception {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Reading sitemap from " + sitemapLocation);
    }// w ww  .ja  v  a  2 s. c om
    final Resource root = this.resolver.getResource(getUrl(sitemapLocation, null));
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Resolved sitemap: " + root.getURL());
    }
    final DefaultConfigurationBuilder b = new DefaultConfigurationBuilder(true);

    final Configuration config = b.build(getInputSource(root));
    // validate sitemap.xmap
    if (!"sitemap".equals(config.getName())) {
        throw new ConfigurationException("Invalid sitemap\n" + config);
    }

    final Configuration completeConfig = SitemapHelper.createSitemapConfiguration(config);
    if (completeConfig != null) {
        convert(completeConfig, null, getUrl(root));
    }
}

From source file:org.apache.cocoon.core.xml.impl.DefaultEntityResolver.java

/**
 * Parse a catalog/*from w  ww .j  a  va2s  .c  o m*/
 */
protected void parseCatalog(String uri) {
    if (this.getLogger().isDebugEnabled()) {
        this.getLogger().debug("Additional Catalog is " + uri);
    }

    final Resource resource = this.resourceLoader.getResource(uri);
    try {
        this.catalogResolver.getCatalog().parseCatalog(this.correctUri(resource.getURL().toExternalForm()));
    } catch (Exception e) {
        this.getLogger().warn("Could not get Catalog file. Trying again: " + uri, e);

        // try it again
        try {
            this.catalogResolver.getCatalog().parseCatalog("text/plain", resource.getInputStream());
        } catch (Exception ex) {
            this.getLogger().warn("Could not get Catalog file: " + uri, ex);
        }
    }
}