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

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

Introduction

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

Prototype

URI getURI() throws IOException;

Source Link

Document

Return a URI handle for this resource.

Usage

From source file:nz.co.senanque.parser.InputStreamParserSource.java

public InputStreamParserSource(Resource resource, int bufferSize) throws IOException {
    this(resource.getInputStream(), resource.getURI().toString(), bufferSize);
}

From source file:com.github.mjeanroy.springmvc.view.mustache.configuration.MustacheTemplateLoaderFactoryBeanTest.java

@Test
public void classpath_resource_loader_should_load_resource_from_classpath() throws Exception {
    Resource resource = classpathResourceLoader.getResource("/templates/foo.template.html");
    assertThat(resource).isNotNull();/*from  w w w.j a  v a  2s  .  c  o  m*/
    assertThat(resource.exists()).isTrue();
    assertThat(resource.getURI().toString()).startsWith("file:/")
            .endsWith("/target/test-classes/templates/foo.template.html");
}

From source file:com.github.mjeanroy.springmvc.view.mustache.configuration.MustacheTemplateLoaderFactoryBeanTest.java

@Test
public void classpath_resource_loader_should_load_resource_from_classpath_and_remove_prefix() throws Exception {
    Resource resource = classpathResourceLoader.getResource("classpath:/templates/foo.template.html");
    assertThat(resource).isNotNull();/*ww w. ja  v  a  2  s. c  o m*/
    assertThat(resource.exists()).isTrue();
    assertThat(resource.getURI().toString()).startsWith("file:/")
            .endsWith("/target/test-classes/templates/foo.template.html");
}

From source file:org.powertac.samplebroker.core.BrokerPropertiesService.java

/**
 * Loads the properties from classpath, default config file,
 * and user-specified config file, just in case it's not already been
 * loaded. This is done when properties are first requested, to ensure
 * that the logger has been initialized. Because the CompositeConfiguration
 * treats its config sources in FIFO order, this should be called <i>after</i>
 * any user-specified config is loaded./*w w w  .j  a  v a 2 s.co  m*/
 */
void lazyInit() {
    // only do this once
    if (initialized)
        return;
    initialized = true;

    // find and load the default properties file
    log.debug("lazyInit");
    try {
        File defaultProps = new File("broker.properties");
        log.info("adding " + defaultProps.getName());
        config.addConfiguration(new PropertiesConfiguration(defaultProps));
    } catch (Exception e1) {
        log.warn("broker.properties not found: " + e1.toString());
    }

    // set up the classpath props
    try {
        Resource[] xmlResources = context.getResources("classpath*:config/properties.xml");
        for (Resource xml : xmlResources) {
            if (validXmlResource(xml)) {
                log.info("loading config from " + xml.getURI());
                XMLConfiguration xconfig = new XMLConfiguration();
                xconfig.load(xml.getInputStream());
                config.addConfiguration(xconfig);
            }
        }
        Resource[] propResources = context.getResources("classpath*:config/*.properties");
        for (Resource prop : propResources) {
            if (validPropResource(prop)) {
                if (null == prop) {
                    log.error("Null resource");
                }
                log.info("loading config from " + prop.getURI());
                PropertiesConfiguration pconfig = new PropertiesConfiguration();
                pconfig.load(prop.getInputStream());
                config.addConfiguration(pconfig);
            }
        }
    } catch (ConfigurationException e) {
        log.error("Problem loading configuration: " + e.toString());
    } catch (Exception e) {
        log.error("Error loading configuration: " + e.toString());
    }

    // set up the configurator
    configurator.setConfiguration(config);
}

From source file:org.powertac.server.ServerPropertiesService.java

private boolean validXmlResource(Resource xml) {
    log.debug("resource class: " + xml.getClass().getName());
    try {/* w w  w . ja  v  a2s.c om*/
        String path = xml.getURI().toString();
        for (String regex : excludedPaths) {
            if (path.matches(regex)) {
                log.debug("invalid path " + path);
                return false;
            }
        }
        return true;
    } catch (IOException e) {
        log.error("Should not happen: " + e.toString());
        return false;
    }
}

From source file:com.flipkart.phantom.runtime.impl.spring.web.HandlerConfigController.java

@RequestMapping(value = { "**/modifyConfig/**" }, method = RequestMethod.GET)
public String modifyConfig(ModelMap model, HttpServletRequest request,
        @ModelAttribute("handlerName") String handlerName,
        @RequestParam(defaultValue = "") String XMLFileContents,
        @RequestParam(defaultValue = "0") String identifier) {
    model.addAttribute("taskHandlers", this.configService.getAllHandlers());
    model.addAttribute("servletPath", request.getContextPath());
    Resource handlerFile = this.configService.getHandlerConfig(handlerName);
    model.addAttribute("XMLFileContents", ConfigFileUtils.getContents(handlerFile));
    try {/*from  w w w.  j  a  v a  2s .com*/
        model.addAttribute("XMLFileName", handlerFile.getURI());
    } catch (IOException e) {
        model.addAttribute("XMLFileName", "File not found");
    }
    return "modifyConfig";
}

From source file:com.flipkart.phantom.runtime.impl.spring.web.HandlerConfigController.java

@RequestMapping(value = { "/deploy/**" }, method = RequestMethod.POST)
public String deployModifiedConfig(ModelMap model, HttpServletRequest request,
        @ModelAttribute("handlerName") String handlerName,
        @RequestParam(defaultValue = "") String XMLFileContents,
        @RequestParam(defaultValue = "0") String identifier) {
    //Save the file
    XMLFileContents = XMLFileContents.trim();
    if (identifier.equals("Save")) {
        try {// w ww.  jav a  2 s  .c  o m
            this.configService.modifyHandlerConfig(handlerName,
                    new ByteArrayResource(XMLFileContents.getBytes()));
        } catch (Exception e) { //Loading failed
            model.addAttribute("XMLFileError", "Unable to deploy file");
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));
            model.addAttribute("LoadingError", errors.toString());
            if (errors.toString() == null) {
                model.addAttribute("LoadingError", "Unexpected error");
            }
            model.addAttribute("XMLFileContents",
                    ConfigFileUtils.getContents(this.configService.getHandlerConfig(handlerName)));
            return "modifyConfig";
        }
    }
    //Loading success
    model.addAttribute("SuccessMessage", "Successfully Deployed the new Handler Configuration");
    model.addAttribute("taskHandlers", this.configService.getAllHandlers());
    Resource handlerFile = this.configService.getHandlerConfig(handlerName);
    model.addAttribute("XMLFileContents", ConfigFileUtils.getContents(handlerFile));
    try {
        model.addAttribute("XMLFileName", handlerFile.getURI());
    } catch (IOException e) {
        model.addAttribute("XMLFileName", "File not found");
    }
    return "viewConfig";
}

From source file:org.powertac.server.ServerPropertiesService.java

/**
 * Loads the properties from classpath, default config file,
 * and user-specified config file, just in case it's not already been
 * loaded. This is done when properties are first requested, to ensure
 * that the logger has been initialized. Because the CompositeConfiguration
 * treats its config sources in FIFO order, this should be called <i>after</i>
 * any user-specified config is loaded.// w w  w  .  jav  a2 s.  c  o  m
 */
void lazyInit() {
    // only do this once
    if (initialized)
        return;
    initialized = true;

    // find and load the default properties file
    log.info("lazyInit");
    try {
        File defaultProps = new File("config/server.properties");
        if (defaultProps.canRead()) {
            log.debug("adding " + defaultProps.getName());
            config.addConfiguration(new PropertiesConfiguration(defaultProps));
        }
    } catch (Exception e1) {
        log.warn("config/server.properties not found: " + e1.toString());
    }

    // set up the classpath props
    try {
        Resource[] xmlResources = context.getResources("classpath*:config/properties.xml");
        for (Resource xml : xmlResources) {
            if (validXmlResource(xml)) {
                log.info("loading config from " + xml.getURI());
                XMLConfiguration xconfig = new XMLConfiguration();
                xconfig.load(xml.getInputStream());
                config.addConfiguration(xconfig);
            }
        }
        Resource[] propResources = context.getResources("classpath*:config/*.properties");
        for (Resource prop : propResources) {
            if (validPropResource(prop)) {
                log.info("loading config from " + prop.getURI());
                PropertiesConfiguration pconfig = new PropertiesConfiguration();
                pconfig.load(prop.getInputStream());
                config.addConfiguration(pconfig);
            }
        }
    } catch (ConfigurationException e) {
        log.error("Error loading configuration: " + e.toString());
    } catch (Exception e) {
        log.error("Error loading configuration: " + e.toString());
    }

    // set up the configurator
    configurator.setConfiguration(config);
}

From source file:com.sinosoft.one.mvc.scanning.MvcScanner.java

public List<ResourceRef> getJarOrClassesFolderResources(String[] scope) throws IOException {
    if (logger.isInfoEnabled()) {
        logger.info("[findFiles] start to found classes folders " + "or mvcd jar files by scope:"
                + Arrays.toString(scope));
    }/*  ww  w .  j av  a  2s .co m*/
    List<ResourceRef> resources;
    if (scope == null) {
        resources = new LinkedList<ResourceRef>();
        if (logger.isDebugEnabled()) {
            logger.debug("[findFiles] call 'classesFolder'");
        }
        resources.addAll(getClassesFolderResources());
        //
        if (logger.isDebugEnabled()) {
            logger.debug("[findFiles] exits from 'classesFolder'");
            logger.debug("[findFiles] call 'jarFile'");
        }
        resources.addAll(getJarResources());
        if (logger.isDebugEnabled()) {
            logger.debug("[findFiles] exits from 'jarFile'");
        }
    } else if (scope.length == 0) {
        return new ArrayList<ResourceRef>();
    } else {
        resources = new LinkedList<ResourceRef>();
        for (String scopeEntry : scope) {
            String packagePath = scopeEntry.replace('.', '/');
            Resource[] packageResources = resourcePatternResolver.getResources("classpath*:" + packagePath);
            for (Resource pkgResource : packageResources) {
                String uri = pkgResource.getURI().toString();
                uri = StringUtils.removeEnd(uri, "/");
                packagePath = StringUtils.removeEnd(packagePath, "/");
                uri = StringUtils.removeEnd(uri, packagePath);
                int beginIndex = uri.lastIndexOf("file:");
                if (beginIndex == -1) {
                    beginIndex = 0;
                } else {
                    beginIndex += "file:".length();
                }
                int endIndex = uri.lastIndexOf('!');
                if (endIndex == -1) {
                    endIndex = uri.length();
                }
                String path = uri.substring(beginIndex, endIndex);
                Resource folder = new FileSystemResource(path);
                ResourceRef ref = ResourceRef.toResourceRef(folder);
                if (!resources.contains(ref)) {
                    resources.add(ref);
                    if (logger.isDebugEnabled()) {
                        logger.debug(
                                "[findFiles] found classes folders " + "or mvcd jar files by scope:" + ref);
                    }
                }
            }
        }
    }
    //
    if (logger.isInfoEnabled()) {
        logger.info("[findFiles] found " + resources.size() + " classes folders " + "or mvcd jar files : "
                + resources);
    }

    return resources;
}

From source file:com.gzj.tulip.load.RoseScanner.java

public List<ResourceRef> getJarOrClassesFolderResources(String[] scope) throws IOException {
    if (logger.isInfoEnabled()) {
        logger.info("[findFiles] start to found classes folders " + "or rosed jar files by scope:"
                + Arrays.toString(scope));
    }/*from  w w  w . j  a  v  a 2 s.  c  om*/
    List<ResourceRef> resources;
    if (scope == null) {
        resources = new LinkedList<ResourceRef>();
        if (logger.isDebugEnabled()) {
            logger.debug("[findFiles] call 'classesFolder'");
        }
        resources.addAll(getClassesFolderResources());
        //
        if (logger.isDebugEnabled()) {
            logger.debug("[findFiles] exits from 'classesFolder'");
            logger.debug("[findFiles] call 'jarFile'");
        }
        resources.addAll(getJarResources());
        if (logger.isDebugEnabled()) {
            logger.debug("[findFiles] exits from 'jarFile'");
        }
    } else if (scope.length == 0) {
        return new ArrayList<ResourceRef>();
    } else {
        resources = new LinkedList<ResourceRef>();
        for (String scopeEntry : scope) {
            String packagePath = scopeEntry.replace('.', '/');
            Resource[] packageResources = resourcePatternResolver.getResources("classpath*:" + packagePath);
            for (Resource pkgResource : packageResources) {
                String uri = pkgResource.getURI().toString();
                uri = StringUtils.removeEnd(uri, "/");
                packagePath = StringUtils.removeEnd(packagePath, "/");
                uri = StringUtils.removeEnd(uri, packagePath);
                int beginIndex = uri.lastIndexOf("file:");
                if (beginIndex == -1) {
                    beginIndex = 0;
                } else {
                    beginIndex += "file:".length();
                }
                int endIndex = uri.lastIndexOf('!');
                if (endIndex == -1) {
                    endIndex = uri.length();
                }
                String path = uri.substring(beginIndex, endIndex);
                Resource folder = new FileSystemResource(path);
                ResourceRef ref = ResourceRef.toResourceRef(folder);
                if (!resources.contains(ref)) {
                    resources.add(ref);
                    if (logger.isDebugEnabled()) {
                        logger.debug(
                                "[findFiles] found classes folders " + "or rosed jar files by scope:" + ref);
                    }
                }
            }
        }
    }
    //
    if (logger.isInfoEnabled()) {
        logger.info("[findFiles] found " + resources.size() + " classes folders " + "or rosed jar files : "
                + resources);
    }

    return resources;
}