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.kuali.kra.protocol.drools.util.DroolsRuleHandler.java

private KieContainer getRuleBase(String rulesFile) {
    final DefaultResourceLoader resourceLoader = new DefaultResourceLoader(
            ClassLoaderUtils.getDefaultClassLoader());
    final Resource resource = resourceLoader.getResource(rulesFile);
    try {/*from  w  ww  . j a  v a 2  s  .c  o m*/
        final KieServices kieServices = KieServices.Factory.get();
        final KieFileSystem kfs = kieServices.newKieFileSystem();
        kfs.write(ResourceFactory.newUrlResource(resource.getURL()));

        final KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();

        final Results results = kieBuilder.getResults();
        if (results.hasMessages(Message.Level.INFO)) {
            LOG.info(results.getMessages(Message.Level.INFO));
        }
        if (results.hasMessages(Message.Level.WARNING)) {
            LOG.warn(results.getMessages(Message.Level.WARNING));
        }
        if (results.hasMessages(Message.Level.ERROR)) {
            throw new RuntimeException(results.getMessages(Message.Level.ERROR).toString());
        }

        return kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.kuali.rice.krad.datadictionary.ReloadingDataDictionary.java

/**
 * After dictionary has been loaded, determine the source files and add them
 * to the monitor//from   w w w.ja  va2s  .co  m
 * 
 * @see org.kuali.rice.krad.datadictionary.DataDictionary#parseDataDictionaryConfigurationFiles(boolean)
 */
@Override
public void parseDataDictionaryConfigurationFiles(boolean allowConcurrentValidation) {
    ConfigurationService configurationService = KRADServiceLocator.getKualiConfigurationService();

    // class directory part of the path that should be replaced
    String classesDir = configurationService.getPropertyValueAsString(CLASS_DIR_CONFIG_PARM);

    // source directory where dictionary files are found
    String sourceDir = configurationService.getPropertyValueAsString(SOURCE_DIR_CONFIG_PARM);

    // interval to poll for changes in milliseconds
    int reloadInterval = Integer.parseInt(configurationService.getPropertyValueAsString(INTERVAL_CONFIG_PARM));

    FileMonitor dictionaryFileMonitor = new FileMonitor(reloadInterval);

    dictionaryUrlMonitor = new URLMonitor(reloadInterval);
    dictionaryUrlMonitor.addListener(this);

    // need to copy the configFileLocations list here because it gets
    // cleared out after processing by super
    List<String> configLocations = new ArrayList<String>(configFileLocations);

    super.parseDataDictionaryConfigurationFiles(allowConcurrentValidation);
    for (String configLocation : configLocations) {
        Resource classFileResource = getFileResource(configLocation);
        try {
            if (classFileResource.getURI().toString().startsWith("jar:")) {
                LOG.debug("Monitoring dictionary file at URI: " + classFileResource.getURI().toString());
                dictionaryUrlMonitor.addURI(classFileResource.getURL());
            } else {
                String filePathClassesDir = classFileResource.getFile().getAbsolutePath();
                String sourceFilePath = StringUtils.replace(filePathClassesDir, classesDir, sourceDir);
                File dictionaryFile = new File(filePathClassesDir);
                if (dictionaryFile.exists()) {
                    LOG.debug("Monitoring dictionary file: " + dictionaryFile.getName());
                    dictionaryFileMonitor.addFile(dictionaryFile);
                }
            }
        } catch (Exception e) {
            LOG.info("Exception in picking up dictionary file for monitoring:  " + e.getMessage(), e);
        }
    }

    // add the dictionary as a listener for file changes
    dictionaryFileMonitor.addListener(this);
}

From source file:org.kuali.rice.krad.devtools.datadictionary.ReloadingDataDictionary.java

/**
 * After dictionary has been loaded, determine the source files and add them
 * to the monitor/*from w w  w. j a  v  a  2 s . com*/
 *
 * @see org.kuali.rice.krad.datadictionary.DataDictionary#parseDataDictionaryConfigurationFiles(boolean)
 */
@Override
public void parseDataDictionaryConfigurationFiles(boolean allowConcurrentValidation) {
    ConfigurationService configurationService = CoreApiServiceLocator.getKualiConfigurationService();

    // class directory part of the path that should be replaced
    String classesDir = configurationService.getPropertyValueAsString(CLASS_DIR_CONFIG_PARM);

    // source directory where dictionary files are found
    String sourceDir = configurationService.getPropertyValueAsString(SOURCE_DIR_CONFIG_PARM);

    // interval to poll for changes in milliseconds
    int reloadInterval = Integer.parseInt(configurationService.getPropertyValueAsString(INTERVAL_CONFIG_PARM));

    dictionaryFileMonitor = new FileMonitor(reloadInterval);
    dictionaryFileMonitor.addListener(this);

    dictionaryUrlMonitor = new URLMonitor(reloadInterval);
    dictionaryUrlMonitor.addListener(this);

    super.parseDataDictionaryConfigurationFiles(allowConcurrentValidation);

    // need to hold mappings of file/url to namespace so we can correctly add beans to the associated
    // namespace when reloading the resource
    fileToNamespaceMapping = new HashMap<String, String>();
    urlToNamespaceMapping = new HashMap<String, String>();

    // add listener for each dictionary file
    for (Map.Entry<String, List<String>> moduleDictionary : moduleDictionaryFiles.entrySet()) {
        String namespace = moduleDictionary.getKey();
        List<String> configLocations = moduleDictionary.getValue();

        for (String configLocation : configLocations) {
            Resource classFileResource = getFileResource(configLocation);

            try {
                if (classFileResource.getURI().toString().startsWith("jar:")) {
                    LOG.trace("Monitoring dictionary file at URI: " + classFileResource.getURI().toString());

                    dictionaryUrlMonitor.addURI(classFileResource.getURL());
                    urlToNamespaceMapping.put(classFileResource.getURL().toString(), namespace);
                } else {
                    String filePathClassesDir = classFileResource.getFile().getAbsolutePath();
                    String sourceFilePath = StringUtils.replace(filePathClassesDir, classesDir, sourceDir);

                    File dictionaryFile = new File(filePathClassesDir);
                    if (dictionaryFile.exists()) {
                        LOG.trace("Monitoring dictionary file: " + dictionaryFile.getName());

                        dictionaryFileMonitor.addFile(dictionaryFile);
                        fileToNamespaceMapping.put(dictionaryFile.getAbsolutePath(), namespace);
                    }
                }
            } catch (Exception e) {
                LOG.info("Exception in picking up dictionary file for monitoring:  " + e.getMessage(), e);
            }
        }
    }
}

From source file:org.mifos.framework.util.ConfigurationLocator.java

/**
 * Will not throw an exception if the file is not found. This method may be
 * used to find files in cases where we don't care if the file cannot be
 * found./*www  .  j  ava2 s.  co  m*/
 * @throws IOException
 */
@SuppressWarnings("PMD")
// TODO It may be clearer if this returned an URI or URL instead of a String?
public String getCustomFilePath(String filename) throws IOException {
    String returnValue = filename;
    LOGGER.info("Checking existance of : " + filename);
    Resource configFile = getResource(filename);
    if (configFile != null && configFile.exists()) {
        returnValue = configFile.getURL().toExternalForm();
        LOGGER.info("Custom configuration file exists : " + returnValue);
    }
    return returnValue;
}

From source file:org.mifos.reports.MifosViewerServletContextListener.java

private void copyFromClassPathToDirectory(String directoryToScan, File rootDirectory) throws IOException {
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource[] resources = resolver.getResources(LOCATION_PATTERN);
    LOGGER.info("Found " + resources.length + " Resources on " + LOCATION_PATTERN);
    for (Resource resource : resources) {
        if (resource.exists() & resource.isReadable() && resource.contentLength() > 0) {
            URL url = resource.getURL();
            String urlString = url.toExternalForm();
            String targetName = urlString.substring(urlString.indexOf(directoryToScan));
            File destination = new File(rootDirectory, targetName);
            FileUtils.copyURLToFile(url, destination);
            LOGGER.info("Copied " + url + " to " + destination.getAbsolutePath());
        } else {//from w  w w  .  j  ava2  s .  co  m
            LOGGER.debug("Did not copy, seems to be directory: " + resource.getDescription());
        }
    }
}

From source file:org.mitre.mpf.wfm.util.MpfPropertiesConfigurationBuilder.java

private FileBasedConfigurationBuilder<PropertiesConfiguration> createFileBasedConfigurationBuilder(
        Resource resource) {

    URL url;//from w w w . j a v a  2  s  . c o m
    try {
        url = resource.getURL();
    } catch (IOException e) {
        throw new IllegalStateException("Cannot get URL from " + resource + ".", e);
    }

    FileBasedConfigurationBuilder<PropertiesConfiguration> fileBasedConfigBuilder = new FileBasedConfigurationBuilder<>(
            PropertiesConfiguration.class);

    Parameters configBuilderParameters = new Parameters();
    fileBasedConfigBuilder.configure(configBuilderParameters.fileBased().setURL(url)
            .setListDelimiterHandler(new DefaultListDelimiterHandler(',')));

    return fileBasedConfigBuilder;
}

From source file:org.ojbc.web.portal.services.XslFormattersTest.java

private SAXSource createSourceAndSetSystemId(Resource inputStream) {
    try {/*from w  w  w.  jav  a2 s  .c  o  m*/
        InputSource inputSource = new InputSource(inputStream.getInputStream());
        inputSource.setEncoding(CharEncoding.UTF_8);
        inputSource.setSystemId(inputStream.getURL().toExternalForm());
        return new SAXSource(inputSource);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.openehealth.ipf.commons.xml.XsdValidator.java

public Source schemaSource(String resource) throws IOException {
    Resource r = resourceLoader.getResource(resource);
    if (r != null) {
        if (r.getURL() != null) {
            return new StreamSource(r.getInputStream(), r.getURL().toExternalForm());
        } else {/*  w  w w .java  2s .  c  o  m*/
            return new StreamSource(r.getInputStream());
        }
    } else {
        throw new IllegalArgumentException("Schema not specified properly");
    }
}

From source file:org.slc.sli.ingestion.parser.impl.EdfiRecordParserImpl.java

private static Schema initializeSchema(Resource schemaResource) throws XmlParseException {
    Schema schema;// ww  w.  j av  a 2s.c  om
    try {
        schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(schemaResource.getURL());
    } catch (SAXException e) {
        throw new XmlParseException("Exception while initializing XSD schema", e);
    } catch (IOException e) {
        throw new XmlParseException("Exception while accessing XSD schema file", e);
    }
    return schema;
}

From source file:org.slc.sli.ingestion.parser.impl.EdfiRecordParserImpl2.java

public static void parse(InputStream input, Resource schemaResource, TypeProvider typeProvider,
        RecordVisitor visitor) throws SAXException, IOException, XmlParseException {

    EdfiRecordParserImpl2 parser = new EdfiRecordParserImpl2();

    parser.addVisitor(visitor);/*  w w  w .  j ava 2 s .co  m*/
    parser.typeProvider = typeProvider;

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    Schema schema = schemaFactory.newSchema(schemaResource.getURL());

    parser.parseAndValidate(input, schema);
}