/** * This is a convenience method to load a resource as a stream. * * The algorithm used to find the resource is given in getResource() * * @param resourceName The name of the resource to load * @param callingClass The Class object of the calling object */ public static InputStream getResourceAsStream(String resourceName, Class callingClass) { URL url = getResource(resourceName, callingClass); try { return (url != null) ? url.openStream() : null; } catch (IOException e) { return null; } }
/** * Load a given resource. * <p/> * This method will try to load the resource using the following methods (in order): * <ul> * <li>From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()} * <li>From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()} * <li>From the {@link Class#getClassLoader() callingClass.getClassLoader() } * </ul> * * @param resourceName The name of the resource to load * @param callingClass The Class object of the calling object */ public static URL getResource(String resourceName, Class callingClass) { URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName); if (url == null) { url = ClassLoaderUtil.class.getClassLoader().getResource(resourceName); } if (url == null) { ClassLoader cl = callingClass.getClassLoader(); if (cl != null) { url = cl.getResource(resourceName); } } if ((url == null) && (resourceName != null) && ((resourceName.length() == 0) || (resourceName.charAt(0) != '/'))) { return getResource('/' + resourceName, callingClass); } return url; }
protected Result findResult(String path, String resultCode, String ext, ActionContext actionContext, Map<String, ResultTypeConfig> resultsByExtension) { try { LOG.trace("Checking ServletContext for {}", path); URL resource = servletContext.getResource(path); if (resource != null && resource.getPath().endsWith(path)) { LOG.trace("Found resource {}", resource); return buildResult(path, resultCode, resultsByExtension.get(ext), actionContext); } LOG.trace("Checking ClassLoader for {}", path); String classLoaderPath = path.startsWith("/") ? path.substring(1, path.length()) : path; resource = ClassLoaderUtil.getResource(classLoaderPath, getClass()); if (resource != null && resource.getPath().endsWith(classLoaderPath)) { LOG.trace("Found resource {}", resource); return buildResult(path, resultCode, resultsByExtension.get(ext), actionContext); } } catch (MalformedURLException e) { LOG.error("Unable to parse template path: {} skipping...", path); } return null; }
private List<ValidatorConfig> loadFile(String fileName, Class clazz, boolean checkFile) { List<ValidatorConfig> retList = Collections.emptyList(); URL fileUrl = ClassLoaderUtil.getResource(fileName, clazz); if ((checkFile && fileManager.fileNeedsReloading(fileUrl)) || !validatorFileCache.containsKey(fileName)) { InputStream is = null; try { is = fileManager.loadFile(fileUrl); if (is != null) { retList = new ArrayList<ValidatorConfig>(validatorFileParser.parseActionValidatorConfigs(validatorFactory, is, fileName)); } } finally { if (is != null) { try { is.close(); } catch (IOException e) { LOG.error("Unable to close input stream for " + fileName, e); } } } validatorFileCache.put(fileName, retList); } else { retList = validatorFileCache.get(fileName); } return retList; } }
private Map<String, Object> conditionalReload(Class clazz, Map<String, Object> oldValues) throws Exception { Map<String, Object> mapping = oldValues; if (reloadingConfigs) { URL fileUrl = ClassLoaderUtil.getResource(buildConverterFilename(clazz), clazz); if (fileManager.fileNeedsReloading(fileUrl)) { mapping = buildConverterMapping(clazz); } } return mapping; }
private List<ValidatorConfig> loadFile(String fileName, Class clazz, boolean checkFile) { List<ValidatorConfig> retList = Collections.emptyList(); URL fileUrl = ClassLoaderUtil.getResource(fileName, clazz);
uri = uri.substring(0, uri.lastIndexOf('/') + 1); uri = uri + trace.getFileName(); URL url = ClassLoaderUtil.getResource(uri, LocationUtils.class); if (url != null) { uri = url.toString();
public void process(Map<String, Object> mapping, Class clazz, String converterFilename) { try { InputStream is = fileManager.loadFile(ClassLoaderUtil.getResource(converterFilename, clazz));