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

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

Introduction

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

Prototype

Resource createRelative(String relativePath) throws IOException;

Source Link

Document

Create a resource relative to this resource.

Usage

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

private static void importRelativeResource(XmlBeanDefinitionReader reader, Resource sourceResource,
        String location, Set<Resource> actualResources) {
    int importCount = 0;
    try {/*from ww  w.ja v a2s .c o m*/
        Resource relativeResource = sourceResource.createRelative(location);
        if (relativeResource.exists()) {
            importCount = reader.loadBeanDefinitions(relativeResource);
            actualResources.add(relativeResource);
        }
    } catch (IOException e) {
        throw new BeanDefinitionStoreException("Could not resolve current location [" + location + "]", e);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Imported " + importCount + " bean definitions from relative location [" + location + "]");
    }
}

From source file:org.biopax.validator.api.ValidatorUtils.java

/**
 * Gets the validator's home directory path.
 * /*from  w w  w. java2 s  .  c o m*/
 * @return
 * @throws IOException 
 */
public static String getHomeDir() throws IOException {
    Resource r = new FileSystemResource(ResourceUtils.getFile("classpath:"));
    return r.createRelative("..").getFile().getCanonicalPath();
}

From source file:com.scf.core.context.app.cfg.module.ModuleConfigHandler.java

/**
 *
 * @param classPackage//from  www .java 2 s .co  m
 * @param marges
 */
private static void loadModuleConfigs(String classPackage, Properties marges) {
    Resource[] resources = ClassScanner.scan(classPackage, "config.properties");
    for (Resource resource : resources) {
        try {
            if (!resource.exists()) {
                continue;
            }
            Properties props = new Properties();
            // ?file??
            String moduleName = resource.createRelative("/").getFilename();
            PropertiesLoaderUtils.fillProperties(props, new EncodedResource(resource, "utf-8"));
            ConfigParams cp = loadModuleConfig(marges, moduleName, props);
            _logger.info("Loaded module config for " + moduleName + " has " + cp.getParams().keySet().size()
                    + " properties.");
            map.put(moduleName, cp);
        } catch (IOException ex) {
            _logger.warn("Can not load module config for " + resource, ex);
        }
    }
}

From source file:org.paxml.util.PaxmlUtils.java

/**
 * Get resource from path.//www. j a va2 s  .c  om
 * 
 * @param path
 *            if path has prefix, the path will be directly used; if not,
 *            the path will be treated as a relative path based on the
 *            "base" resource parameter.
 * @param base
 *            the base resource when path has no prefix. If null given and
 *            base resource has no prefix, then the path is assumed to be a
 *            file system resource if it exists, and if it doesn't exist, it
 *            will be assumed as a classpath resource.
 * @return the Spring resource.
 */
public static Resource getResource(String path, Resource base) {
    final String filePrefix = "file:";
    final String classpathPrefix = "classpath:";

    if (!path.startsWith(filePrefix) && !path.startsWith(classpathPrefix)) {
        if (base == null) {
            File file = getFile(path);
            if (file.isFile()) {
                path = filePrefix + file.getAbsolutePath();
            } else {
                // assume to be a file
                path = classpathPrefix + path;
            }
        } else {
            try {
                path = base.createRelative(path).getURI().toString();
            } catch (IOException e) {
                throw new PaxmlParseException("Cannot create relative path '" + path + "' from base resource: "
                        + base + ", because: " + e.getMessage());
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Taking resource from computed path: " + path);
    }
    return new DefaultResourceLoader().getResource(path);

}

From source file:org.codehaus.griffon.plugins.XmlPluginDescriptorReader.java

public GriffonPluginInfo readPluginInfo(Resource pluginDescriptor) {
    try {//w  w w.j ava 2s .  c  om
        Resource pluginXml = pluginDescriptor.createRelative("plugin.xml");
        if (pluginXml.exists()) {
            return new PluginInfo(pluginXml, pluginSettings);
        }

    } catch (IOException e) {
        // ignore
    }
    return null;
}

From source file:org.wallride.service.MediaService.java

public Media createMedia(MultipartFile file) {
    Media media = new Media();
    media.setMimeType(file.getContentType());
    media.setOriginalName(file.getOriginalFilename());
    media = mediaRepository.saveAndFlush(media);

    //      Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
    try {//from   w w  w.  j  a v  a2  s  .  c  o m
        Resource prefix = resourceLoader.getResource(wallRideProperties.getMediaLocation());
        Resource resource = prefix.createRelative(media.getId());
        //         AmazonS3ResourceUtils.writeMultipartFile(file, resource);
        ExtendedResourceUtils.write(resource, file);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return media;
}

From source file:org.geomajas.gwt.server.mvc.ResourceSerializationPolicyLocator.java

@Override
public SerializationPolicy loadPolicy(HttpServletRequest request, String moduleBaseURL, String strongName) {

    SerializationPolicy serializationPolicy = null;
    String serializationPolicyFilePath = SerializationPolicyLoader.getSerializationPolicyFileName(strongName);

    for (Resource directory : policyRoots) {
        Resource policy;/*from  w  ww .j a  va2 s. co  m*/
        try {
            policy = directory.createRelative(serializationPolicyFilePath);
            if (policy.exists()) {
                // Open the RPC resource file and read its contents.
                InputStream is = policy.getInputStream();
                try {
                    if (is != null) {
                        try {
                            serializationPolicy = SerializationPolicyLoader.loadFromStream(is, null);
                            break;
                        } catch (ParseException e) {
                            log.error("Failed to parse the policy file '" + policy + "'", e);
                        } catch (IOException e) {
                            log.error("Could not read the policy file '" + policy + "'", e);
                        }
                    } else {
                        // existing spring resources should not return null, log anyways
                        log.error("Unexpected null stream from the policy file resource '" + policy + "'");
                    }
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            // Ignore this error
                        }
                    }
                }
            }
        } catch (IOException e1) {
            log.debug("Could not create the policy resource '" + serializationPolicyFilePath
                    + "' from parent resource " + directory, e1);
        }
    }
    if (serializationPolicy == null) {
        String message = "The serialization policy file '" + serializationPolicyFilePath
                + "' was not found; did you forget to include it in this deployment?";
        log.error(message);
    }
    return serializationPolicy;
}

From source file:de.perdian.apps.dashboard.mvc.portal.PortalController.java

private Resource computeDashboardFileResource(ServletWebRequest webRequest) throws IOException {
    String dashboardFileLocation = this.computeDashboardFileLocation(webRequest);
    Resource dashboardParentResource = this.getResourceLoader()
            .getResource(PortalConstants.DASHBOARD_RESOURCE_BASE);
    Resource dashboardFileResource = dashboardParentResource.createRelative(dashboardFileLocation);
    if (dashboardFileResource != null && dashboardFileResource.exists()) {
        return dashboardFileResource;
    } else {//from  www  .jav  a 2  s . co  m
        throw new DashboardException("Cannot find requested dashboard file at: " + dashboardFileLocation);
    }
}

From source file:de.codecentric.boot.admin.web.servlet.resource.PreferMinifiedFilteringResourceResolver.java

private Resource findMinified(Resource resource) {
    try {/*  w w  w.j a  v  a 2  s  .co m*/
        String basename = StringUtils.stripFilenameExtension(resource.getFilename());
        String extension = StringUtils.getFilenameExtension(resource.getFilename());
        Resource minified = resource.createRelative(basename + extensionPrefix + '.' + extension);
        if (minified.exists()) {
            return minified;
        }
    } catch (IOException ex) {
        logger.trace("No minified resource for [" + resource.getFilename() + "]", ex);
    }
    return null;
}

From source file:com.wavemaker.tools.project.CloudFoundryStudioConfigurationTest.java

@Test
public void testGetStudioWebAppRootFile() throws Exception {
    GridFSStudioFileSystem sf = new GridFSStudioFileSystem(this.mongoFactory);
    sf.setServletContext(this.servletContext);
    Resource studioWebAppRootFile = sf.getStudioWebAppRoot();
    assertTrue(studioWebAppRootFile.exists());
    Resource studioWebInfFile = studioWebAppRootFile.createRelative("WEB-INF/");
    assertTrue(studioWebInfFile.exists());
    Resource webXmlFile = studioWebInfFile.createRelative(ProjectConstants.WEB_XML);
    assertTrue(webXmlFile.exists());/*from   ww w  .j  a  v a  2 s.  c o  m*/
}