/** * Check whether the given class implements an interface with a given class name. * @param clazz the class to check * @param ifaceClassName the interface class name to check */ private static boolean implementsInterface(Class<?> clazz, String ifaceClassName) { try { Class<?> ifaceClass = ClassLoaderUtil.loadClass(ifaceClassName, ProxyUtil.class); return ifaceClass.isAssignableFrom(clazz); } catch (ClassNotFoundException e) { return false; } }
/** * 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; } }
protected Iterator<URL> getConfigurationUrls(String fileName) throws IOException { return ClassLoaderUtil.getResources(fileName, XmlConfigurationProvider.class, false); }
private void retrieveValidatorConfiguration(String resourceName) { InputStream is = ClassLoaderUtil.getResourceAsStream(resourceName, DefaultValidatorFactory.class); if (is != null) { validatorFileParser.parseValidatorDefinitions(validators, is, resourceName); } } }
/** * Prints the classloader hierarchy from a given classloader - useful for debugging. */ public static void printClassLoader(ClassLoader cl) { System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")"); if (cl != null) { printClassLoader(cl.getParent()); } }
@Override public InputSource resolveEntity(String publicId, String systemId) { if (dtdMappings != null && dtdMappings.containsKey(publicId)) { String dtdFile = dtdMappings.get(publicId); return new InputSource(ClassLoaderUtil.getResourceAsStream(dtdFile, DomHelper.class)); } else if (LOG.isWarnEnabled()) { LOG.warn("Local DTD is missing for publicID: #0 - defined mappings: #1", publicId, dtdMappings); } return null; }
/** * Prints the current classloader hierarchy - useful for debugging. */ public static void printClassLoader() { System.out.println("ClassLoaderUtils.printClassLoader"); printClassLoader(Thread.currentThread().getContextClassLoader()); }
/** * Utility method to obtain the class matched to className. Caches look ups so that subsequent * lookups will be faster. * * @param className The fully qualified name of the class to return * @return The class itself * @throws ClassNotFoundException */ public Class getClassInstance(String className) throws ClassNotFoundException { if (ccl != null) { return ccl.loadClass(className); } return ClassLoaderUtil.loadClass(className, this.getClass()); }
/** * 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; }
public Iterator<URL> getPluginsLoaded() { try { return ClassLoaderUtil.getResources("struts-plugin.xml", ShowJarsAction.class, false); } catch (IOException e) { LOG.error("Cannot load struts-plugin.xml", e); } return null; }
private Class getClassInstance() { try { return ClassLoaderUtil.loadClass(clazz, ActionContext.getContext().getClass()); } catch (Exception e) { LOG.error("Class '" + clazz + "' not found...",e); } return null; } }
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; }
return getResources('/' + resourceName, callingClass, aggregate);
/** * Check whether the given member is a member of a spring proxy. * @param member the member to check */ private static boolean isSpringProxyMember(Member member) { try { Class<?> clazz = ClassLoaderUtil.loadClass(SPRING_ADVISED_CLASS_NAME, ProxyUtil.class); if (hasMember(clazz, member)) return true; clazz = ClassLoaderUtil.loadClass(SPRING_TARGETCLASSAWARE_CLASS_NAME, ProxyUtil.class); if (hasMember(clazz, member)) return true; clazz = ClassLoaderUtil.loadClass(SPRING_SPRINGPROXY_CLASS_NAME, ProxyUtil.class); if (hasMember(clazz, member)) return true; } catch (ClassNotFoundException ignored) { } return false; }
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; } }
public void loadConversionProperties(String propsName, boolean require) { try { Iterator<URL> resources = ClassLoaderUtil.getResources(propsName, getClass(), true); while (resources.hasNext()) { URL url = resources.next();
/** * {@inheritDoc} */ public String determineResultPath(ActionConfig actionConfig) { if (actionConfig == null) { return resultPath; } try { return determineResultPath(ClassLoaderUtil.loadClass(actionConfig.getClassName(), this.getClass())); } catch (ClassNotFoundException e) { throw new RuntimeException("Invalid action class configuration that references an unknown " + "class named [" + actionConfig.getClassName() + "]", e); } }
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; }
try { Iterator<URL> urls = ClassLoaderUtil.getResources("", DefaultValidatorFactory.class, false); while (urls.hasNext()) { URL u = urls.next();