private static List<URL> getURLs(ClassLoaderInterface classLoader, String[] dirNames) { List<URL> urls = new ArrayList<>(); for (String dirName : dirNames) { try { Enumeration<URL> classLoaderURLs = classLoader.getResources(dirName); while (classLoaderURLs.hasMoreElements()) { URL url = classLoaderURLs.nextElement(); urls.add(url); } } catch (IOException ioe) { LOG.error("Could not read directory [{}]", dirName, ioe); } } return urls; }
private URL getResource(String fullUri) { if (urls == null){ return classLoaderInterface.getResource(fullUri); } return findResource(fullUri, urls); }
/** * Executes {@link #findString(String)} assuming the contents URL found is the name of * a class that should be loaded and returned. * * @param uri * @return * @throws IOException * @throws ClassNotFoundException */ public Class findClass(String uri) throws IOException, ClassNotFoundException { String className = findString(uri); return (Class) classLoaderInterface.loadClass(className); }
private Enumeration<URL> getResources(String fulluri) throws IOException { if (urls == null) { return classLoaderInterface.getResources(fulluri); } Vector<URL> resources = new Vector(); for (URL url : urls) { URL resource = findResource(fulluri, url); if (resource != null){ resources.add(resource); } } return resources.elements(); }
private void readClassDef(String className) { if (!className.endsWith(".class")) { className = className.replace('.', '/') + ".class"; } try { URL resource = classLoaderInterface.getResource(className); if (resource != null) { InputStream in = resource.openStream(); try { ClassReader classReader = new ClassReader(in); classReader.accept(new InfoBuildingVisitor(this), ClassReader.SKIP_DEBUG); } finally { in.close(); } } else { throw new XWorkException("Could not load " + className); } } catch (IOException e) { throw new XWorkException("Could not load " + className, e); } }
/** * Executes findAllStrings assuming the strings are * the names of a classes that should be loaded and returned. * <p/> * Any URL or class that cannot be loaded will cause an exception to be thrown. * * @param uri * @return * @throws IOException * @throws ClassNotFoundException */ public List<Class> findAllClasses(String uri) throws IOException, ClassNotFoundException { List<Class> classes = new ArrayList<Class>(); List<String> strings = findAllStrings(uri); for (String className : strings) { Class clazz = classLoaderInterface.loadClass(className); classes.add(clazz); } return classes; }
private List<URL> getUrls(ClassLoaderInterface classLoader) throws IOException { List<URL> list = new ArrayList<URL>(); //find jars ArrayList<URL> urls = Collections.list(classLoader.getResources("META-INF")); for (URL url : urls) { if ("jar".equalsIgnoreCase(url.getProtocol())) { String externalForm = url.toExternalForm(); //build a URL pointing to the jar, instead of the META-INF dir url = new URL(StringUtils.substringBefore(externalForm, "META-INF")); list.add(url); } else if (LOG.isDebugEnabled()) LOG.debug("Ignoring URL [#0] because it is not a jar", url.toExternalForm()); } //usually the "classes" dir list.addAll(Collections.list(classLoader.getResources(""))); return list; }
private void readClassDef(String className) { if (!className.endsWith(".class")) { className = className.replace('.', '/') + ".class"; } try { URL resource = classLoaderInterface.getResource(className); if (resource != null) { try (InputStream in = resource.openStream()) { ClassReader classReader = new ClassReader(in); classReader.accept(new InfoBuildingVisitor(this), ClassReader.SKIP_DEBUG); } } else { throw new XWorkException("Could not load " + className); } } catch (IOException e) { throw new XWorkException("Could not load " + className, e); } }
public Class get() throws ClassNotFoundException { if (clazz != null) return clazz; if (notFound != null) throw notFound; try { this.clazz = classFinder.getClassLoaderInterface().loadClass(name); return clazz; } catch (ClassNotFoundException notFound) { classFinder.getClassesNotLoaded().add(name); this.notFound = notFound; throw notFound; } }
private static List<URL> getURLs(ClassLoaderInterface classLoader, String[] dirNames) { List<URL> urls = new ArrayList<URL>(); for (String dirName : dirNames) { try { Enumeration<URL> classLoaderURLs = classLoader.getResources(dirName); while (classLoaderURLs.hasMoreElements()) { URL url = classLoaderURLs.nextElement(); urls.add(url); } } catch (IOException ioe) { if (LOG.isErrorEnabled()) LOG.error("Could not read driectory [#0]", ioe, dirName); } } return urls; }
/** * Executes findAvailableStrings assuming the strings are * the names of a classes that should be loaded and returned. * <p/> * Any class that cannot be loaded will be skipped and placed in the * 'resourcesNotLoaded' collection. * * @param uri * @return * @throws IOException if classLoader.getResources throws an exception */ public List<Class> findAvailableClasses(String uri) throws IOException { resourcesNotLoaded.clear(); List<Class> classes = new ArrayList<Class>(); List<String> strings = findAvailableStrings(uri); for (String className : strings) { try { Class clazz = classLoaderInterface.loadClass(className); classes.add(clazz); } catch (Exception notAvailable) { resourcesNotLoaded.add(className); } } return classes; }
private List<URL> getUrls(ClassLoaderInterface classLoader, Set<String> protocols) throws IOException { if (protocols == null) { return getUrls(classLoader); } List<URL> list = new ArrayList<URL>(); //find jars ArrayList<URL> urls = Collections.list(classLoader.getResources("META-INF")); for (URL url : urls) { if (protocols.contains(url.getProtocol())) { String externalForm = url.toExternalForm(); //build a URL pointing to the jar, instead of the META-INF dir url = new URL(StringUtils.substringBefore(externalForm, "META-INF")); list.add(url); } else if (LOG.isDebugEnabled()) LOG.debug("Ignoring URL [#0] because it is not a valid protocol", url.toExternalForm()); } return list; }
/** * Assumes the class specified points to a file in the classpath that contains * the name of a class that implements or is a subclass of the specfied class. * <p/> * Any class that cannot be loaded will be cause an exception to be thrown. * <p/> * Example classpath: * <p/> * META-INF/java.io.InputStream # contains the classname org.acme.AcmeInputStream * META-INF/java.io.OutputStream * <p/> * ResourceFinder finder = new ResourceFinder("META-INF/"); * Class clazz = finder.findImplementation(java.io.InputStream.class); * clazz.getName(); // returns "org.acme.AcmeInputStream" * * @param interfase a superclass or interface * @return * @throws IOException if the URL cannot be read * @throws ClassNotFoundException if the class found is not loadable * @throws ClassCastException if the class found is not assignable to the specified superclass or interface */ public Class findImplementation(Class interfase) throws IOException, ClassNotFoundException { String className = findString(interfase.getName()); Class impl = classLoaderInterface.loadClass(className); if (!interfase.isAssignableFrom(impl)) { throw new ClassCastException("Class not of type: " + interfase.getName()); } return impl; }
/** * Try to find a classes directory inside a war file add its normalized url to this set */ public UrlSet includeClassesUrl(ClassLoaderInterface classLoaderInterface, FileProtocolNormalizer normalizer) throws IOException { Enumeration<URL> rootUrlEnumeration = classLoaderInterface.getResources(""); while (rootUrlEnumeration.hasMoreElements()) { URL url = rootUrlEnumeration.nextElement(); String externalForm = StringUtils.removeEnd(url.toExternalForm(), "/"); if (externalForm.endsWith(".war/WEB-INF/classes")) { //if it is inside a war file, get the url to the file externalForm = StringUtils.substringBefore(externalForm, "/WEB-INF/classes"); URL warUrl = new URL(externalForm); URL normalizedUrl = normalizer.normalizeToFileProtocol(warUrl); URL finalUrl = ObjectUtils.defaultIfNull(normalizedUrl, warUrl); Map<String, URL> newUrls = new HashMap<String, URL>(this.urls); if ("jar".equals(finalUrl.getProtocol()) || "file".equals(finalUrl.getProtocol())) { newUrls.put(finalUrl.toExternalForm(), finalUrl); } return new UrlSet(newUrls); } } return this; }
String string = entry.getKey(); String className = entry.getValue(); Class clazz = classLoaderInterface.loadClass(className); classes.put(string, clazz);
private List<URL> readUrls() throws IOException { List<URL> resourceUrls = new ArrayList<URL>(); // Usually the "classes" dir. ArrayList<URL> classesList = Collections.list(getClassLoaderInterface().getResources("")); for (URL url : classesList) { resourceUrls.addAll(fileManager.getAllPhysicalUrls(url)); } return buildUrlSet(resourceUrls).getUrls(); }
List<String> strings = findAllStrings(interfase.getName()); for (String className : strings) { Class impl = classLoaderInterface.loadClass(className); if (!interfase.isAssignableFrom(impl)) { throw new ClassCastException("Class not of type: " + interfase.getName());
private List<URL> readUrls() throws IOException { List<URL> resourceUrls = new ArrayList<>(); // Usually the "classes" dir. ArrayList<URL> classesList = Collections.list(getClassLoaderInterface().getResources("")); for (URL url : classesList) { resourceUrls.addAll(fileManager.getAllPhysicalUrls(url)); } return buildUrlSet(resourceUrls).getUrls(); }