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

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

Introduction

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

Prototype

boolean exists();

Source Link

Document

Determine whether this resource actually exists in physical form.

Usage

From source file:com.google.api.ads.adwords.awreporting.AwReporting.java

/**
 * Initialize the application context, adding the properties configuration file depending on the
 * specified path.//ww w .  j a  v a2 s . com
 *
 * @param propertiesPath the path to the file.
 * @param forceOnFileProcessor true if the processor will be created to run "on file"
 * @return the resource loaded from the properties file.
 * @throws IOException error opening the properties file.
 */
private static Properties initApplicationContextAndProperties(String propertiesPath,
        boolean forceOnFileProcessor) throws IOException {

    Resource resource = new ClassPathResource(propertiesPath);
    if (!resource.exists()) {
        resource = new FileSystemResource(propertiesPath);
    }
    LOGGER.trace("Innitializing Spring application context.");
    DynamicPropertyPlaceholderConfigurer.setDynamicResource(resource);
    Properties properties = PropertiesLoaderUtils.loadProperties(resource);

    // Selecting the XMLs to choose the Spring Beans to load.
    List<String> listOfClassPathXml = Lists.newArrayList();

    // Choose the DB type to use based properties file, default to MYSQL
    String dbType = (String) properties.get(AW_REPORT_MODEL_DB_TYPE);
    DataBaseType sqldbType = null;
    if (DataBaseType.MONGODB.name().equals(dbType)) {
        LOGGER.info("Using MONGO DB configuration properties.");
        listOfClassPathXml.add("classpath:aw-report-mongodb-beans.xml");
    } else {
        if (DataBaseType.MSSQL.name().equals(dbType)) {
            sqldbType = DataBaseType.MSSQL;
            LOGGER.info("Using MSSQL DB configuration properties.");
        } else {
            // default to MYSQL
            sqldbType = DataBaseType.MYSQL;
            LOGGER.info("Using MYSQL DB configuration properties.");
        }
        LOGGER.warn("Updating database schema, this could take a few minutes ...");
        listOfClassPathXml.add("classpath:aw-report-sql-beans.xml");
        LOGGER.warn("Done.");
    }

    // Choose the Processor type to use based properties file
    String processorType = (String) properties.get(AW_REPORT_PROCESSOR_TYPE);
    if (!forceOnFileProcessor && ProcessorType.ONMEMORY.name().equals(processorType)) {
        LOGGER.info("Using ONMEMORY Processor.");
        listOfClassPathXml.add("classpath:aw-report-processor-beans-onmemory.xml");
    } else {
        LOGGER.info("Using ONFILE Processor.");
        listOfClassPathXml.add("classpath:aw-report-processor-beans-onfile.xml");
    }

    appCtx = new ClassPathXmlApplicationContext();
    if (sqldbType != null) {
        appCtx.getEnvironment().setActiveProfiles(sqldbType.name());
    }

    appCtx.setConfigLocations(listOfClassPathXml.toArray(new String[listOfClassPathXml.size()]));
    appCtx.refresh();

    return properties;
}

From source file:fr.mby.saml2.sp.opensaml.helper.OpenSamlHelper.java

/**
 * Build a metadata provider if a metadata resource was provided.
 * //from w w  w.ja va 2s . co m
 * @param metadata the metadata resource
 * @return the metadata provider
 * @throws MetadataProviderException
 * @throws XMLParserException
 */
public static MetadataProvider buildMetadataProvider(final Resource metadata)
        throws MetadataProviderException, XMLParserException {
    ResourceBackedMetadataProvider metatdataProvider = null;

    if ((metadata != null) && metadata.exists()) {
        org.opensaml.util.resource.Resource resource = new SpringResourceWrapper(metadata);

        metatdataProvider = new ResourceBackedMetadataProvider(new Timer(), resource);
        StaticBasicParserPool parserPool = new StaticBasicParserPool();
        parserPool.initialize();
        metatdataProvider.setParserPool(parserPool);
        metatdataProvider.initialize();
    }

    return metatdataProvider;
}

From source file:com.google.api.ads.adwords.awreporting.server.appengine.RestServer.java

/**
 * Initialize the application context, adding the properties configuration file depending on the
 * specified path.//from  w  w w. j av a  2  s  .  c  om
 */
protected synchronized static void initApplicationContextAndProperties() {

    // Resister all Model Objects in the ObjectifyService
    ObjectifyService.factory().getTranslators().add(new BigDecimalLongTranslatorFactory(1000000000));

    ObjectifyService.register(UserToken.class);

    // AwReporting Server model objects
    ObjectifyService.register(Account.class);
    ObjectifyService.register(HtmlTemplate.class);

    // AwReporting model objects
    ObjectifyService.register(AuthMcc.class);
    ObjectifyService.register(ReportAccount.class);
    ObjectifyService.register(ReportAd.class);
    ObjectifyService.register(ReportAdExtension.class);
    ObjectifyService.register(ReportAdGroup.class);
    ObjectifyService.register(ReportBudget.class);
    ObjectifyService.register(ReportCampaign.class);
    ObjectifyService.register(ReportCampaignNegativeKeyword.class);
    ObjectifyService.register(ReportCriteriaPerformance.class);
    ObjectifyService.register(ReportDestinationUrl.class);
    ObjectifyService.register(ReportKeyword.class);
    ObjectifyService.register(ReportPlaceholderFeedItem.class);
    ObjectifyService.register(ReportUrl.class);

    try {
        Resource resource = new ClassPathResource(PROPERTIES_FILE);
        if (!resource.exists()) {
            resource = new FileSystemResource(PROPERTIES_FILE);
        }
        DynamicPropertyPlaceholderConfigurer.setDynamicResource(resource);

        properties = PropertiesLoaderUtils.loadProperties(resource);

    } catch (IOException e) {
        LOGGER.severe("Error reading properties file " + e.getMessage());
    }

    // Loading AppEngine DB type by default (ignoring DB type from properties file)
    appCtx = new ClassPathXmlApplicationContext("classpath:aw-reporting-appengine-beans.xml");
    persister = appCtx.getBean(EntityPersister.class);
    authenticator = appCtx.getBean(AppEngineOAuth2Authenticator.class);
    webAuthenticator = appCtx.getBean(WebAuthenticator.class);
}

From source file:com.seer.datacruncher.utils.generic.CommonUtils.java

public static File getResourceFile(String path) throws IOException {
    ApplicationContext context = AppContext.getApplicationContext();
    Resource r = context.getResource(path);
    if (r.exists()) {
        return r.getFile();
    } else {/*from w  w  w .  j a  v  a  2  s.co  m*/
        return new File(AppContext.getApplicationContext().getResource("/").getFile().getAbsolutePath() + path);
    }
}

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

public static void setWaveMakerHome(Resource wmHome) throws FileAccessException {
    Assert.isInstanceOf(FileSystemResource.class, wmHome, "Expected a FileSystemResource");

    try {//  w  ww  .j a  v a2 s .  c o  m
        ConfigurationStore.setVersionedPreference(LocalStudioConfiguration.class, WMHOME_KEY,
                wmHome.getFile().getCanonicalPath());
    } catch (IOException ex) {
        throw new WMRuntimeException(ex);
    }

    if (!wmHome.exists()) {
        ((FileSystemResource) wmHome).getFile().mkdirs();
    }
}

From source file:com.bluexml.side.framework.alfresco.commons.configurations.PropertiesConfiguration.java

protected void loadResource(Resource r) {
    if (r.exists()) {
        Properties properties = new Properties();
        try {//from  w ww  . j  a v  a 2s. c  om
            properties.load(r.getInputStream());
            logger.info("Load Properties form Resource " + r);
        } catch (IOException e) {
            logger.error("Unexpected error loading property file \"" + r.getFilename() + "\" ", e);
        }
        if (isValidePropertiesResource(properties)) {
            for (Object property : properties.keySet()) {
                String key = (String) property;
                String value = (String) properties.getProperty(key);
                dictionary.put(key, value);
            }
        }
    } else {
        logger.info("Resource do not exists :" + r.getDescription());
    }
}

From source file:org.obiba.onyx.engine.ActionDefinitionReader.java

@SuppressWarnings("unchecked")
public List<ActionDefinition> read() throws IOException {
    List<ActionDefinition> definitions = new ArrayList<ActionDefinition>();
    for (int i = 0; i < this.resources.length; i++) {
        Resource resource = this.resources[i];
        if (resource.exists()) {
            definitions.addAll((List<ActionDefinition>) xstream.fromXML(resource.getInputStream()));
        }/*  w  w w. ja  v a 2  s .  c o  m*/
    }
    return definitions;
}

From source file:org.eclipse.gemini.blueprint.test.internal.util.FileSystemStorageTest.java

public void testResourceForTempFile() throws Exception {
    Resource res = storage.getResource();
    assertTrue(res.exists());
    File tempFile = res.getFile();
    assertTrue(tempFile.exists());//www. ja va  2  s .c  om
    assertTrue(tempFile.canRead());
    assertTrue(tempFile.canWrite());
}

From source file:wsdl.ResourceToStringConverter.java

public void setResource(Resource resource) {
    this.resource = resource;
    System.out.println(resource.exists());
}

From source file:org.apache.uima.ruta.resource.RutaResourceLoader.java

public Resource getResource(String location) {
    Resource resource = this.wrapped.getResource(location);
    if (!resource.exists()) {
        resource = fallback.getResource(location);
    }/*from w w  w .j  a v  a  2s.  co  m*/
    return resource;
}