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.sinosoft.one.mvc.scanning.ResourceRef.java

public static ResourceRef toResourceRef(Resource folder) throws IOException {
    ResourceRef rr = new ResourceRef(folder, null, null);
    String[] modifiers = null;//  ww w  . j  av a2 s.c o  m
    Resource mvcPropertiesResource = rr.getInnerResource("META-INF/mvc.properties");
    if (mvcPropertiesResource.exists()) {
        if (logger.isDebugEnabled()) {
            logger.debug("found mvc.properties: " + mvcPropertiesResource.getURI());
        }
        InputStream in = mvcPropertiesResource.getInputStream();
        rr.properties.load(in);
        in.close();
        String attrValue = rr.properties.getProperty("mvc");
        if (attrValue == null) {
            attrValue = rr.properties.getProperty("Mvc");
        }
        if (attrValue != null) {
            modifiers = StringUtils.split(attrValue, ", ;\n\r\t");
            if (logger.isDebugEnabled()) {
                logger.debug("modifiers[by properties][" + rr.getResource().getURI() + "]="
                        + Arrays.toString(modifiers));
            }
        }
    }
    //
    if (modifiers == null) {
        if (!"jar".equals(rr.getProtocol())) {
            modifiers = new String[] { "**" };
            if (logger.isDebugEnabled()) {
                logger.debug("modifiers[by default][" + rr.getResource().getURI() + "]="
                        + Arrays.toString(modifiers));
            }
        } else {
            JarFile jarFile = new JarFile(rr.getResource().getFile());
            Manifest manifest = jarFile.getManifest();
            if (manifest != null) {
                Attributes attributes = manifest.getMainAttributes();
                String attrValue = attributes.getValue("mvc");
                if (attrValue == null) {
                    attrValue = attributes.getValue("Mvc");
                }
                if (attrValue != null) {
                    modifiers = StringUtils.split(attrValue, ", ;\n\r\t");
                    if (logger.isDebugEnabled()) {
                        logger.debug("modifiers[by manifest.mf][" + rr.getResource().getURI() + "]="
                                + Arrays.toString(modifiers));
                    }
                }
            }
        }
    }
    rr.setModifiers(modifiers);
    return rr;
}

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

public static ResourceRef toResourceRef(Resource folder) throws IOException {
    ResourceRef rr = new ResourceRef(folder, null, null);
    String[] modifiers = null;/*from  w  ww .j a v a  2 s  .  c o m*/
    Resource rosePropertiesResource = rr.getInnerResource("META-INF/rose.properties");
    if (rosePropertiesResource.exists()) {
        if (logger.isDebugEnabled()) {
            logger.debug("found rose.properties: " + rosePropertiesResource.getURI());
        }
        InputStream in = rosePropertiesResource.getInputStream();
        rr.properties.load(in);
        in.close();
        String attrValue = rr.properties.getProperty("rose");
        if (attrValue == null) {
            attrValue = rr.properties.getProperty("Rose");
        }
        if (attrValue != null) {
            modifiers = StringUtils.split(attrValue, ", ;\n\r\t");
            if (logger.isDebugEnabled()) {
                logger.debug("modifiers[by properties][" + rr.getResource().getURI() + "]="
                        + Arrays.toString(modifiers));
            }
        }
    }
    //
    if (modifiers == null) {
        if (!"jar".equals(rr.getProtocol())) {
            modifiers = new String[] { "**" };
            if (logger.isDebugEnabled()) {
                logger.debug("modifiers[by default][" + rr.getResource().getURI() + "]="
                        + Arrays.toString(modifiers));
            }
        } else {
            JarFile jarFile = new JarFile(rr.getResource().getFile());
            Manifest manifest = jarFile.getManifest();
            if (manifest != null) {
                Attributes attributes = manifest.getMainAttributes();
                String attrValue = attributes.getValue("rose");
                if (attrValue == null) {
                    attrValue = attributes.getValue("Rose");
                }
                if (attrValue != null) {
                    modifiers = StringUtils.split(attrValue, ", ;\n\r\t");
                    if (logger.isDebugEnabled()) {
                        logger.debug("modifiers[by manifest.mf][" + rr.getResource().getURI() + "]="
                                + Arrays.toString(modifiers));
                    }
                }
            }
        }
    }
    rr.setModifiers(modifiers);
    return rr;
}

From source file:grails.plugin.searchable.internal.SearchableUtils.java

private static Map loadMetadata() {
    Resource r = new ClassPathResource(PROJECT_META_FILE);
    if (r.exists()) {
        return loadMetadata(r);
    }/*from   w w w . j a  v  a 2  s.co  m*/
    String basedir = System.getProperty("base.dir");
    if (basedir != null) {
        r = new FileSystemResource(new File(basedir, PROJECT_META_FILE));
        if (r.exists()) {
            return loadMetadata(r);
        }
    }
    return null;
}

From source file:org.dspace.servicemanager.spring.ResourceFinder.java

private static Resource findResource(String path) {
    Resource r;
    String envPath = getEnvironmentPath() + path;
    r = new FileSystemResource(envPath);
    if (!r.exists()) {
        // try the relative path next
        String relPath = getRelativePath() + path;
        r = new FileSystemResource(relPath);
        if (!r.exists()) {
            // now try the classloaders
            ClassLoader cl = ResourceFinder.class.getClassLoader();
            r = new ClassPathResource(path, cl);
            if (!r.exists()) {
                // finally try the context classloader
                cl = Thread.currentThread().getContextClassLoader();
                r = new ClassPathResource(path, cl);
            }/*from   w  ww  . j  a  v a 2s . co m*/
        }
    }
    return r;
}

From source file:com.google.api.ads.adwords.awalerting.AwAlerting.java

/**
 * Load JSON configuration file specified in the properties file. First try to load the JSON
 * configuration file from the same folder as the properties file; if it does not exist, try to
 * load it from the default location.//from  w ww .ja  va2s . com
 *
 * @param propertiesPath the path to the properties file
 * @return JSON configuration loaded from the json file
 * @throws AlertConfigLoadException error reading properties / json file
 */
private static JsonObject getAlertsConfig(String propertiesPath) throws AlertConfigLoadException {
    LOGGER.info("Using properties file: {}", propertiesPath);

    Resource propertiesResource = new ClassPathResource(propertiesPath);
    if (!propertiesResource.exists()) {
        propertiesResource = new FileSystemResource(propertiesPath);
    }

    JsonObject alertsConfig = null;
    try {
        Properties properties = initApplicationContextAndProperties(propertiesResource);

        // Load alerts config from the same folder as the properties file
        String alertsConfigFilename = properties.getProperty("aw.alerting.alerts");
        String propertiesFolder = propertiesResource.getFile().getParent();
        File alertsConfigFile = new File(propertiesFolder, alertsConfigFilename);

        // If it does not exist, try the default resource folder according to maven structure.
        if (!alertsConfigFile.exists()) {
            String alertsConfigFilepath = "src/main/resources/" + alertsConfigFilename;
            alertsConfigFile = new File(alertsConfigFilepath);
        }

        LOGGER.debug("Loading alerts config file from {}", alertsConfigFile.getAbsolutePath());
        JsonParser jsonParser = new JsonParser();
        alertsConfig = jsonParser.parse(new FileReader(alertsConfigFile)).getAsJsonObject();
        LOGGER.debug("Done.");
    } catch (IOException e) {
        throw new AlertConfigLoadException("Error loading alerts config at " + propertiesPath, e);
    } catch (JsonParseException e) {
        throw new AlertConfigLoadException("Error parsing config file at " + propertiesPath, e);
    }

    return alertsConfig;
}

From source file:org.solmix.runtime.support.spring.ContainerApplicationContext.java

/**
 * @param cfgFile/*from  w  ww  .  j a  v a2  s . c om*/
 * @return
 */
public static Resource findResource(final String cfgFile) {
    try {
        return AccessController.doPrivileged(new PrivilegedAction<Resource>() {

            @Override
            public Resource run() {
                Resource cpr = new ClassPathResource(cfgFile);
                if (cpr.exists()) {
                    return cpr;
                }
                try {
                    // see if it's a URL
                    URL url = new URL(cfgFile);
                    cpr = new UrlResource(url);
                    if (cpr.exists()) {
                        return cpr;
                    }
                } catch (MalformedURLException e) {
                    // ignore
                }
                // try loading it our way
                URL url = ClassLoaderUtils.getResource(cfgFile, ContainerApplicationContext.class);
                if (url != null) {
                    cpr = new UrlResource(url);
                    if (cpr.exists()) {
                        return cpr;
                    }
                }
                cpr = new FileSystemResource(cfgFile);
                if (cpr.exists()) {
                    return cpr;
                }
                return null;
            }
        });
    } catch (AccessControlException ex) {
        // cannot read the user config file
        return null;
    }
}

From source file:org.sakaiproject.entitybroker.util.spring.ResourceFinder.java

private static Resource makeResource(String path) {
    if (path.startsWith("/")) {
        path = path.substring(1);// w w  w .  j a va  2 s  .  c om
    }
    Resource r = null;
    // try the environment path first
    String envPath = getEnvironmentPath() + path;
    r = new FileSystemResource(envPath);
    if (!r.exists()) {
        // try the relative path next
        String relPath = getRelativePath() + path;
        r = new FileSystemResource(relPath);
        if (!r.exists()) {
            // now try the classloader
            ClassLoader cl = ResourceFinder.class.getClassLoader();
            r = new ClassPathResource(path, cl);
            if (!r.exists()) {
                // finally try the system classloader
                cl = ClassLoader.getSystemClassLoader();
                r = new ClassPathResource(path, cl);
            }
        }
    }
    if (!r.exists()) {
        throw new IllegalArgumentException(
                "Could not find this resource (" + path + ") in any of the checked locations");
    }
    return r;
}

From source file:com.edgenius.core.util.FileUtil.java

/**
 * IMPORTANT: this location must be Spring resource format, such as, file://c:/abc.txt
 * or classpath:abc.properties etc.  If there is not protocol prefix, this method may not 
 * return correct value (always false)./*  w w w  .  j av  a 2s  .  c om*/
 * @param location
 * @return
 */
public static boolean exist(String location) {
    DefaultResourceLoader loader = new DefaultResourceLoader();
    Resource res = loader.getResource(location);
    return res.exists();
}

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

/**
 * Initialize the application context, adding the properties configuration file depending on the
 * specified path./*  w  w w.  jav a  2 s  .  c  o  m*/
 *
 * @param propertiesPath the path to the file.
 * @return the resource loaded from the properties file.
 * @throws IOException error opening the properties file.
 */
private static Properties initApplicationContextAndProperties(String propertiesPath) 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);
    String dbType = (String) properties.get(AW_REPORT_MODEL_DB_TYPE);
    if (dbType != null && dbType.equals(DataBaseType.MONGODB.name())) {

        LOGGER.debug("Using MONGO DB configuration properties.");
        appCtx = new ClassPathXmlApplicationContext("classpath:aw-reporting-mongodb-beans.xml");
    } else {

        LOGGER.debug("Using SQL DB configuration properties.");
        appCtx = new ClassPathXmlApplicationContext("classpath:aw-reporting-sql-beans.xml");
    }

    return properties;
}

From source file:org.red5.server.stream.RecordingListener.java

/**
 * Get the file we'd be recording to based on scope and given name.
 *
 * @param scope//from  w w w.j  a v  a 2  s. c om
 *            scope
 * @param name
 *            name
 * @return file
 */
public static File getRecordFile(IScope scope, String name) {
    // get stream filename generator
    IStreamFilenameGenerator generator = (IStreamFilenameGenerator) ScopeUtils.getScopeService(scope,
            IStreamFilenameGenerator.class, DefaultStreamFilenameGenerator.class);
    // generate filename
    String fileName = generator.generateFilename(scope, name, ".flv", GenerationType.RECORD);
    File file = null;
    if (generator.resolvesToAbsolutePath()) {
        file = new File(fileName);
    } else {
        Resource resource = scope.getContext().getResource(fileName);
        if (resource.exists()) {
            try {
                file = resource.getFile();
                log.debug("File exists: {} writable: {}", file.exists(), file.canWrite());
            } catch (IOException ioe) {
                log.error("File error: {}", ioe);
            }
        } else {
            String appScopeName = ScopeUtils.findApplication(scope).getName();
            file = new File(
                    String.format("%s/webapps/%s/%s", System.getProperty("red5.root"), appScopeName, fileName));
        }
    }
    return file;
}