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

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

Introduction

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

Prototype

File getFile() throws IOException;

Source Link

Document

Return a File handle for this resource.

Usage

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/* w  w w . ja va  2s  . c  o  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  www . j  a va 2 s  .  c  o  m*/
 *
 * @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.lexgrid.loader.writer.NoClosingRootTagStaxEventItemWriter.java

@Override
public void setResource(Resource resource) {
    try {/*w ww  .  j a  v  a 2s .  c o m*/
        this.xmlFile = resource.getFile();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    super.setResource(resource);
}

From source file:org.mifos.platform.questionnaire.domain.ppi.PPISurveyLocatorImpl.java

@Override
public List<String> getAllPPISurveyFiles() {
    try {//from   www  . j a v a  2 s.c o  m
        Resource resource = this.resourceLoader.getResource(resolvePath());
        return getPPISurveyFiles(resource.getFile());
    } catch (IOException e) {
        throw new SystemException(FETCH_PPI_XMLS_FAILED, e);
    }
}

From source file:org.mifos.platform.questionnaire.domain.ppi.PPISurveyLocatorImpl.java

@Override
public String getPPIUploadFileForCountry(String country) {
    try {/*w w w  . j  ava2  s . c  om*/
        String fileName = getPPIXmlFileName(country);
        Resource resource = this.resourceLoader.getResource(resolvePath());
        return getPPIFilePath(fileName, resource.getFile());
    } catch (IOException e) {
        throw new SystemException(FETCH_PPI_COUNTRY_XML_FAILED, e);
    }
}

From source file:org.mitre.mpf.wfm.pipeline.PipelineManager.java

public Tuple<Boolean, String> save(String type) {
    Resource resource;
    AlgorithmDefinitionCollection algorithmDefinitionCollection = null;
    ActionDefinitionCollection actionDefinitionCollection = null;
    TaskDefinitionCollection taskDefinitionCollection = null;
    PipelineDefinitionCollection pipelineDefinitionCollection = null;

    switch (type) {
    case "algorithm":
        algorithmDefinitionCollection = new AlgorithmDefinitionCollection();
        for (Entry<String, AlgorithmDefinition> entry : algorithms.entrySet()) {
            algorithmDefinitionCollection.getAlgorithms().add(entry.getValue());
        }//w  w  w .  ja  v  a2s .c  o m
        resource = propertiesUtil.getAlgorithmDefinitions();
        break;
    case "action":
        //loop through the map
        actionDefinitionCollection = new ActionDefinitionCollection();
        for (Entry<String, ActionDefinition> entry : actions.entrySet()) {
            actionDefinitionCollection.getActionDefinitions().add(entry.getValue());
        }
        resource = propertiesUtil.getActionDefinitions();
        break;
    case "task":
        taskDefinitionCollection = new TaskDefinitionCollection();
        for (Entry<String, TaskDefinition> entry : tasks.entrySet()) {
            taskDefinitionCollection.getTasks().add(entry.getValue());
        }
        resource = propertiesUtil.getTaskDefinitions();
        break;
    case "pipeline":
        pipelineDefinitionCollection = new PipelineDefinitionCollection();
        for (Entry<String, PipelineDefinition> entry : pipelines.entrySet()) {
            pipelineDefinitionCollection.getPipelines().add(entry.getValue());
        }
        resource = propertiesUtil.getPipelineDefinitions();
        break;
    default:
        return new Tuple<>(false, "Unable to save object type: " + type);
    }

    try (Writer outputWriter = Files.newBufferedWriter(resource.getFile().toPath())) {
        switch (type) {
        case "algorithm":
            xStream.toXML(algorithmDefinitionCollection, outputWriter);
            break;
        case "action":
            xStream.toXML(actionDefinitionCollection, outputWriter);
            break;
        case "task":
            xStream.toXML(taskDefinitionCollection, outputWriter);
            break;
        case "pipeline":
            xStream.toXML(pipelineDefinitionCollection, outputWriter);
            break;
        default:
            return new Tuple<>(false, "Unable to save object type: " + type);
        }
        return new Tuple<>(true, null);
    } catch (IOException e) {
        log.error("Failed to write to xml with message: {}", e.getMessage());
    }
    return new Tuple<>(false, "error reading the xml file of type: " + type);
}

From source file:org.nimbustools.messaging.gt4_0_elastic.v2008_05_05.general.defaults.DefaultElasticPersistence.java

public DefaultElasticPersistence(Resource dbResource) throws IOException {
    if (dbResource == null) {
        throw new IllegalArgumentException("diskStoreResource may not be null");
    }/* w w w. ja va  2  s .c om*/

    final String dbPath = dbResource.getFile().getAbsolutePath();

    //don't know how to feed this in with Spring and still have it fixup the
    // $NIMBUS_HOME in path
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.sqlite.JDBC");
    ds.setUrl("jdbc:sqlite://" + dbPath);

    this.dataSource = ds;
}

From source file:org.nimbustools.messaging.query.security.FileUserDetailsService.java

public FileUserDetailsService(Resource fileResource) throws IOException {
    if (fileResource == null) {
        throw new IllegalArgumentException("fileResource may not be null");
    }/*from   w  ww  .ja v a 2  s. c o  m*/

    mapByID = new HashMap<String, QueryUser>();
    mapByDN = new HashMap<String, QueryUser>();

    load(fileResource.getFile());
}

From source file:org.olat.core.util.i18n.I18nManager.java

/**
 * Search in all packages on the source patch for packages that contain an
 * _i18n directory that can be used to store olatcore localization files
 * /*  w  w w  .j a  v a  2s.c  om*/
 * @return set of bundles that contain olatcore i18n compatible localization
 *         files
 */
List<String> searchForBundleNamesContainingI18nFiles() {
    List<String> foundBundles;
    // 1) First search on normal source path of application
    String srcPath = null;
    File applicationDir = I18nModule.getTransToolApplicationLanguagesSrcDir();
    if (applicationDir != null) {
        srcPath = applicationDir.getAbsolutePath();
    } else {
        // Fall back to compiled classes
        srcPath = WebappHelper.getBuildOutputFolderRoot();
    }
    I18nDirectoriesVisitor srcVisitor = new I18nDirectoriesVisitor(srcPath);
    FileUtils.visitRecursively(new File(srcPath), srcVisitor);
    foundBundles = srcVisitor.getBundlesContainingI18nFiles();
    // 3) For jUnit tests, add also the I18n test dir
    if (Settings.isJUnitTest()) {
        Resource testres = new ClassPathResource("olat.local.properties");
        String jUnitSrcPath = null;
        try {
            jUnitSrcPath = testres.getFile().getAbsolutePath();
        } catch (IOException e) {
            throw new StartupException(
                    "Could not find classpath resource for: test-classes/olat.local.property ", e);
        }

        I18nDirectoriesVisitor juniSrcVisitor = new I18nDirectoriesVisitor(jUnitSrcPath);
        FileUtils.visitRecursively(new File(jUnitSrcPath), juniSrcVisitor);
        foundBundles.addAll(juniSrcVisitor.getBundlesContainingI18nFiles());
    }
    // Sort alphabetically
    Collections.sort(foundBundles);
    return foundBundles;
}

From source file:org.openlmis.web.view.pdf.requisition.RequisitionCellFactory.java

public static PdfPCell imageCell() throws BadElementException, IOException {
    Resource resource = new ClassPathResource(OK_IMAGE);
    Image image = Image.getInstance(resource.getFile().getAbsolutePath());
    PdfPCell cell = new PdfPCell(image);
    cell.setPadding(CELL_PADDING);/*from  w  w w . j av a 2s  .c  o  m*/
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    return cell;
}