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

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

Introduction

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

Prototype

@Override
    public boolean exists() 

Source Link

Usage

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

@Override
protected Resource[] getConfigResources() {
    List<Resource> resources = new ArrayList<Resource>();
    if (includeDefault) {
        try {/*ww w  .j av  a2s .  c o  m*/
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
                    Thread.currentThread().getContextClassLoader());

            Collections.addAll(resources, resolver.getResources(DEFAULT_CFG_FILE));

            Resource[] exts = resolver.getResources(DEFAULT_EXT_CFG_FILE);
            for (Resource r : exts) {
                InputStream is = r.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String line = rd.readLine();
                while (line != null) {
                    if (!"".equals(line)) {
                        resources.add(resolver.getResource(line));
                    }
                    line = rd.readLine();
                }
                is.close();
            }

        } catch (IOException ex) {
            // ignore
        }
    }
    boolean usingDefault = false;
    if (cfgFiles == null) {
        String userConfig = System.getProperty(BeanConfigurer.USER_CFG_FILE_PROPERTY_NAME);
        if (userConfig != null) {
            cfgFiles = new String[] { userConfig };
        }
    }
    if (cfgFiles == null) {
        usingDefault = true;
        cfgFiles = new String[] { BeanConfigurer.USER_CFG_FILE };
    }
    for (String cfgFile : cfgFiles) {
        final Resource f = findResource(cfgFile);
        if (f != null && f.exists()) {
            resources.add(f);
            LOG.info("Used configed file {}", cfgFile);
        } else {
            if (!usingDefault) {
                LOG.warn("Can't find configure file {}", cfgFile);
                throw new ApplicationContextException("Can't find configure file");
            }
        }
    }
    if (cfgURLs != null) {
        for (URL u : cfgURLs) {
            UrlResource ur = new UrlResource(u);
            if (ur.exists()) {
                resources.add(ur);
            } else {
                LOG.warn("Can't find configure file {}", u.getPath());
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating application context with resources " + resources.size());
    }
    if (0 == resources.size()) {
        return null;
    }
    Resource[] res = new Resource[resources.size()];
    res = resources.toArray(res);
    return res;

}

From source file:org.atricore.idbus.kernel.planning.jbpm.OsgiProcessFragmentRegistryApplicationContext.java

protected Resource[] getConfigResources() {

    List<Resource> resources = new ArrayList<Resource>();

    try {//from w  w w  .  ja v  a2  s  .c  o m
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
                Thread.currentThread().getContextClassLoader());

        Collections.addAll(resources, resolver.getResources(DEFAULT_JBPM_FRAGMENT_CFG_FILE));

        Resource[] exts = resolver.getResources(DEFAULT_JBPM_EXT_FRAGMENT_CFG_FILE);
        for (Resource r : exts) {
            InputStream is = r.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = rd.readLine();
            while (line != null) {
                if (!"".equals(line)) {
                    resources.add(resolver.getResource(line));
                }
                line = rd.readLine();
            }
            is.close();
        }

    } catch (IOException ex) {
        // ignore
    }

    if (null == cfgFiles) {
        cfgFiles = new String[] { DEFAULT_PROCESS_DESCRIPTOR_FILE };
    }

    for (String cfgFile : cfgFiles) {
        boolean found = false;
        Resource cpr = new ClassPathResource(cfgFile);
        if (!cpr.exists()) {
            try {
                //see if it's a URL
                URL url = new URL(cfgFile);
                cpr = new UrlResource(url);
                if (cpr.exists()) {
                    resources.add(cpr);
                    found = true;
                }
            } catch (MalformedURLException e) {
                //ignore
            }
            if (!found) {
                //try loading it our way
                URL url = getResource(cfgFile, this.getClass());
                if (url != null) {
                    cpr = new UrlResource(url);
                    if (cpr.exists()) {
                        resources.add(cpr);
                        found = true;
                    }
                }
            }
        } else {
            resources.add(cpr);
            found = true;
        }
        if (!found) {
            logger.warn("No Process Descriptor found: " + cfgFile);
        }
    }

    if (null != cfgFileURLs) {
        for (URL cfgFileURL : cfgFileURLs) {
            UrlResource ur = new UrlResource(cfgFileURL);
            if (ur.exists()) {
                resources.add(ur);
            } else {
                logger.warn("No Process Descriptor found: " + cfgFileURL);
            }
        }
    }

    logger.info("Creating application context with resources: " + resources);

    if (0 == resources.size()) {
        return null;
    }

    Resource[] res = new Resource[resources.size()];
    res = resources.toArray(res);
    return res;
}

From source file:org.atricore.idbus.kernel.planning.jbpm.ProcessFragmentRegistryApplicationContext.java

@Override
protected Resource[] getConfigResources() {

    List<Resource> resources = new ArrayList<Resource>();

    try {/*from ww  w . j  a  v a 2  s.  c o m*/
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
                Thread.currentThread().getContextClassLoader());

        Collections.addAll(resources, resolver.getResources(DEFAULT_JBPM_FRAGMENT_CFG_FILE));

        Resource[] exts = resolver.getResources(DEFAULT_JBPM_EXT_FRAGMENT_CFG_FILE);
        for (Resource r : exts) {
            InputStream is = r.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = rd.readLine();
            while (line != null) {
                if (!"".equals(line)) {
                    resources.add(resolver.getResource(line));
                }
                line = rd.readLine();
            }
            is.close();
        }

    } catch (IOException ex) {
        // ignore
    }

    if (null == cfgFiles) {
        cfgFiles = new String[] { DEFAULT_PROCESS_DESCRIPTOR_FILE };
    }

    for (String cfgFile : cfgFiles) {
        boolean found = false;
        Resource cpr = new ClassPathResource(cfgFile);
        if (!cpr.exists()) {
            try {
                //see if it's a URL
                URL url = new URL(cfgFile);
                cpr = new UrlResource(url);
                if (cpr.exists()) {
                    resources.add(cpr);
                    found = true;
                }
            } catch (MalformedURLException e) {
                //ignore
            }
            if (!found) {
                //try loading it our way
                URL url = getResource(cfgFile, this.getClass());
                if (url != null) {
                    cpr = new UrlResource(url);
                    if (cpr.exists()) {
                        resources.add(cpr);
                        found = true;
                    }
                }
            }
        } else {
            resources.add(cpr);
            found = true;
        }
        if (!found) {
            logger.warn("No Process Descriptor found: " + cfgFile);
        }
    }

    if (null != cfgFileURLs) {
        for (URL cfgFileURL : cfgFileURLs) {
            UrlResource ur = new UrlResource(cfgFileURL);
            if (ur.exists()) {
                resources.add(ur);
            } else {
                logger.warn("No Process Descriptor found: " + cfgFileURL);
            }
        }
    }

    logger.info("Creating application context with resources: " + resources);

    if (0 == resources.size()) {
        return null;
    }

    Resource[] res = new Resource[resources.size()];
    res = resources.toArray(res);
    return res;
}

From source file:org.jahia.modules.tools.taglibs.GroovyConsoleHelper.java

/**
 * Returns a generated HTML with form elements for the script parameters.
 * //from  w  ww  .j a  v a  2s. co m
 * @param scriptURI
 * @param request
 * @return
 */
public static String getScriptCustomFormElements(String scriptURI, HttpServletRequest request) {
    if (StringUtils.isBlank(scriptURI)) {
        return StringUtils.EMPTY;
    }
    final StringBuilder sb = new StringBuilder();
    try {
        final UrlResource resource = new UrlResource(
                StringUtils.substringBeforeLast(scriptURI, ".groovy") + ".properties");
        if (resource.exists()) {
            final Properties confs = new Properties();
            confs.load(resource.getInputStream());
            final String[] paramNames = StringUtils
                    .split(confs.getProperty("script.parameters.names", "").replaceAll("\\s", ""), ",");
            for (String paramName : paramNames) {
                generateFormElement(paramName.trim(), sb, confs, request);
            }

        }
    } catch (IOException e) {
        logger.error("An error occured while reading the configurations for the script " + scriptURI, e);
        return StringUtils.EMPTY;
    }
    final String formElements = sb.toString();
    if (StringUtils.isBlank(formElements)) {
        return StringUtils.EMPTY;
    }
    return sb.delete(0, sb.length()).append("<fieldset><legend>Script configuration</legend>")
            .append(formElements).append("</fieldset>").toString();
}

From source file:org.jahia.modules.tools.taglibs.GroovyConsoleHelper.java

/**
 * Returns an array of parameter names for the specified script or <code>null</code> if the script has no parameters.
 * /*from w  ww .j a  v  a 2 s.c  o m*/
 * @param scriptURI the script URI to get parameter names for
 * @return an array of parameter names for the specified script or <code>null</code> if the script has no parameters
 */
public static String[] getScriptParamNames(String scriptURI) {
    try {
        final UrlResource resource = new UrlResource(
                StringUtils.substringBeforeLast(scriptURI, ".groovy") + ".properties");
        if (resource.exists()) {
            final Properties confs = new Properties();
            confs.load(resource.getInputStream());
            return StringUtils.split(confs.getProperty("script.parameters.names", "").replaceAll("\\s", ""),
                    ",");
        }
    } catch (IOException e) {
        logger.error("An error occured while reading the configurations for the script " + scriptURI, e);
    }
    return null;
}

From source file:org.josso.tooling.gshell.core.spring.GShellApplicationContext.java

@Override
protected Resource[] getConfigResources() {

    List<Resource> resources = new ArrayList<Resource>();

    try {/*  w  ww . j  av a 2  s .  co m*/
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
                Thread.currentThread().getContextClassLoader());

        Collections.addAll(resources, resolver.getResources(DEFAULT_JOSSO_GSHELL_CFG_FILE));

    } catch (IOException ex) {
        // ignore
    }

    if (null == cfgFiles) {
        cfgFiles = new String[] { "josso-gshell.xml" };
    }

    for (String cfgFile : cfgFiles) {
        boolean found = false;
        Resource cpr = new ClassPathResource(cfgFile);
        if (!cpr.exists()) {
            try {
                //see if it's a URL
                URL url = new URL(cfgFile);
                cpr = new UrlResource(url);
                if (cpr.exists()) {
                    resources.add(cpr);
                    found = true;
                }
            } catch (MalformedURLException e) {
                //ignore
            }
            if (!found) {
                //try loading it our way
                URL url = getResource(cfgFile, this.getClass());
                if (url != null) {
                    cpr = new UrlResource(url);
                    if (cpr.exists()) {
                        resources.add(cpr);
                        found = true;
                    }
                }
            }
        } else {
            resources.add(cpr);
            found = true;
        }
        if (!found) {
            logger.warn("No Process Descriptor found: " + cfgFile);
        }
    }

    if (null != cfgFileURLs) {
        for (URL cfgFileURL : cfgFileURLs) {
            UrlResource ur = new UrlResource(cfgFileURL);
            if (ur.exists()) {
                resources.add(ur);
            } else {
                logger.warn("No Process Descriptor found: " + cfgFileURL);
            }
        }
    }

    logger.info("Creating application context with resources: " + resources);

    if (0 == resources.size()) {
        return null;
    }

    Resource[] res = new Resource[resources.size()];
    res = resources.toArray(res);
    return res;
}

From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java

/**
 * Search all {@link URLClassLoader} URLs for jar file references and add them to the
 * given set of resources in the form of pointers to the root of the jar file content.
 * @param classLoader the ClassLoader to search (including its ancestors)
 * @param result the set of resources to add jar roots to
 * @since 4.1.1//from ww w.  java  2  s.  co  m
 */
protected void addAllClassLoaderJarRoots(@Nullable ClassLoader classLoader, Set<Resource> result) {
    if (classLoader instanceof URLClassLoader) {
        try {
            for (URL url : ((URLClassLoader) classLoader).getURLs()) {
                try {
                    UrlResource jarResource = new UrlResource(
                            ResourceUtils.JAR_URL_PREFIX + url + ResourceUtils.JAR_URL_SEPARATOR);
                    if (jarResource.exists()) {
                        result.add(jarResource);
                    }
                } catch (MalformedURLException ex) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Cannot search for matching files underneath [" + url
                                + "] because it cannot be converted to a valid 'jar:' URL: " + ex.getMessage());
                    }
                }
            }
        } catch (Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Cannot introspect jar files since ClassLoader [" + classLoader
                        + "] does not support 'getURLs()': " + ex);
            }
        }
    }

    if (classLoader == ClassLoader.getSystemClassLoader()) {
        // "java.class.path" manifest evaluation...
        addClassPathManifestEntries(result);
    }

    if (classLoader != null) {
        try {
            // Hierarchy traversal...
            addAllClassLoaderJarRoots(classLoader.getParent(), result);
        } catch (Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Cannot introspect jar files in parent ClassLoader since [" + classLoader
                        + "] does not support 'getParent()': " + ex);
            }
        }
    }
}

From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java

/**
 * Determine jar file references from the "java.class.path." manifest property and add them
 * to the given set of resources in the form of pointers to the root of the jar file content.
 * @param result the set of resources to add jar roots to
 * @since 4.3//from  w w  w. jav a 2 s.  co m
 */
protected void addClassPathManifestEntries(Set<Resource> result) {
    try {
        String javaClassPathProperty = System.getProperty("java.class.path");
        for (String path : StringUtils.delimitedListToStringArray(javaClassPathProperty,
                System.getProperty("path.separator"))) {
            try {
                String filePath = new File(path).getAbsolutePath();
                int prefixIndex = filePath.indexOf(':');
                if (prefixIndex == 1) {
                    // Possibly "c:" drive prefix on Windows, to be upper-cased for proper duplicate detection
                    filePath = StringUtils.capitalize(filePath);
                }
                UrlResource jarResource = new UrlResource(ResourceUtils.JAR_URL_PREFIX
                        + ResourceUtils.FILE_URL_PREFIX + filePath + ResourceUtils.JAR_URL_SEPARATOR);
                // Potentially overlapping with URLClassLoader.getURLs() result above!
                if (!result.contains(jarResource) && !hasDuplicate(filePath, result) && jarResource.exists()) {
                    result.add(jarResource);
                }
            } catch (MalformedURLException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Cannot search for matching files underneath [" + path
                            + "] because it cannot be converted to a valid 'jar:' URL: " + ex.getMessage());
                }
            }
        }
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to evaluate 'java.class.path' manifest entries: " + ex);
        }
    }
}