List of usage examples for java.lang ClassLoader getResources
public Enumeration<URL> getResources(String name) throws IOException
From source file:main.okapi.cf.annotations.AnnotationsInfo.java
private Iterable<Class> getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<File>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); }//from w ww . j a va2s . c o m List<Class> classes = new ArrayList<Class>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes; }
From source file:de.micromata.genome.gwiki.plugin.CombinedClassLoader.java
@Override public Enumeration<URL> findResources(String name) throws IOException { List<URL> res = new ArrayList<URL>(); for (ClassLoader cl : parents) { Enumeration<URL> resource = cl.getResources(name); CollectionUtils.addAll(res, resource); }//from w ww . j a va 2 s. c o m return new IteratorEnumeration<URL>(res.iterator()); }
From source file:com.puppycrawl.tools.checkstyle.PackageNamesLoaderTest.java
@Test @SuppressWarnings("unchecked") public void testPackagesWithIoExceptionGetResources() throws Exception { ClassLoader classLoader = mock(ClassLoader.class); when(classLoader.getResources("checkstyle_packages.xml")).thenThrow(IOException.class); try {/*from ww w. ja v a 2 s . c om*/ PackageNamesLoader.getPackageNames(classLoader); fail(); } catch (CheckstyleException ex) { assertTrue(ex.getCause() instanceof IOException); assertEquals("unable to get package file resources", ex.getMessage()); } }
From source file:com.iflytek.edu.cloud.frame.web.servlet.PrintProjectVersionServlet.java
@Override public void init(ServletConfig config) throws ServletException { StringBuilder sBuilder = new StringBuilder("\n"); try {//from w w w .ja v a 2 s . com Enumeration<java.net.URL> urls; ClassLoader classLoader = findClassLoader(); if (classLoader != null) { urls = classLoader.getResources("META-INF/res/env.properties"); } else { urls = ClassLoader.getSystemResources("META-INF/res/env.properties"); } if (urls != null) { while (urls.hasMoreElements()) { java.net.URL url = urls.nextElement(); try { BufferedReader reader = new BufferedReader( new InputStreamReader(url.openStream(), "utf-8")); Properties properties = new Properties(); properties.load(reader); sBuilder.append("??").append(properties.getProperty("project.name")) .append(", "); sBuilder.append("").append(properties.getProperty("build.version")) .append(", "); sBuilder.append("").append(properties.getProperty("build.time")) .append(".\n"); } catch (Throwable t) { logger.error(t.getMessage(), t); } } } } catch (Throwable t) { logger.error(t.getMessage(), t); } String projcode = PropertiesConfiguration.getProjCode(); if (StringUtils.hasLength(projcode)) { sBuilder.append("superdiamond client info: project=").append(projcode); sBuilder.append(", profile=").append(PropertiesConfiguration.getProfile()); sBuilder.append(", host=").append(PropertiesConfiguration.getHost()); sBuilder.append(", port=").append(PropertiesConfiguration.getPort()).append(".\n"); } String info = sBuilder.toString(); System.out.println( "==========================================================================================================================="); System.out.println(info); System.out.println( "==========================================================================================================================="); }
From source file:com.puppycrawl.tools.checkstyle.PackageNamesLoaderTest.java
@Test @SuppressWarnings("unchecked") public void testPackagesWithSaxException() throws Exception { final URLConnection mockConnection = Mockito.mock(URLConnection.class); when(mockConnection.getInputStream()).thenReturn(new ByteArrayInputStream(EMPTY_BYTE_ARRAY)); URL url = getMockUrl(mockConnection); Enumeration<URL> enumeration = (Enumeration<URL>) mock(Enumeration.class); when(enumeration.hasMoreElements()).thenReturn(true); when(enumeration.nextElement()).thenReturn(url); ClassLoader classLoader = mock(ClassLoader.class); when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumeration); try {//w w w. ja va 2s . c o m PackageNamesLoader.getPackageNames(classLoader); fail(); } catch (CheckstyleException ex) { assertTrue(ex.getCause() instanceof SAXException); } }
From source file:uniol.apt.io.parser.AbstractParsers.java
/** * Constructor//w w w. ja va 2 s . c o m * * @param clazz Class object of the which the parsers should generate */ @SuppressWarnings("unchecked") // I hate type erasure and other java things ... protected AbstractParsers(Class<T> clazz) { this.parsers = new HashMap<>(); String className = clazz.getCanonicalName(); ClassLoader cl = getClass().getClassLoader(); try { Enumeration<URL> parserNames = cl.getResources( "META-INF/uniol/apt/compiler/" + Parser.class.getCanonicalName() + "/" + className); while (parserNames.hasMoreElements()) { try (InputStream is = parserNames.nextElement().openStream()) { LineIterator lIter = IOUtils.lineIterator(is, "UTF-8"); while (lIter.hasNext()) { String parserName = lIter.next(); Class<? extends Parser<T>> parserClass; try { parserClass = (Class<? extends Parser<T>>) cl.loadClass(parserName); } catch (ClassNotFoundException ex) { throw new RuntimeException(String.format("Could not load class %s", parserName), ex); } Parser<T> parser; try { parser = parserClass.newInstance(); } catch (ClassCastException | IllegalAccessException | InstantiationException ex) { throw new RuntimeException(String.format("Could not instantiate %s", parserName), ex); } String format = parser.getFormat(); if (format == null || format.equals("") || !format.equals(format.toLowerCase())) { throw new RuntimeException( String.format("Parser %s reports an invalid format: %s", parserName, format)); } Parser<T> oldParser = this.parsers.get(format); if (oldParser != null && !oldParser.getClass().equals(parserClass)) { throw new RuntimeException( String.format("Different parsers claim, to interpret format %s:" + " %s and %s", format, oldParser.getClass().getCanonicalName(), parserName)); } this.parsers.put(format, parser); } } } } catch (IOException ex) { throw new RuntimeException("Failed to discover parsers", ex); } }
From source file:com.puppycrawl.tools.checkstyle.PackageNamesLoaderTest.java
@Test @SuppressWarnings("unchecked") public void testPackagesWithIoException() throws Exception { final URLConnection mockConnection = Mockito.mock(URLConnection.class); when(mockConnection.getInputStream()).thenReturn(null); URL url = getMockUrl(mockConnection); Enumeration<URL> enumer = (Enumeration<URL>) mock(Enumeration.class); when(enumer.hasMoreElements()).thenReturn(true); when(enumer.nextElement()).thenReturn(url); ClassLoader classLoader = mock(ClassLoader.class); when(classLoader.getResources("checkstyle_packages.xml")).thenReturn(enumer); try {/*from w ww . ja v a2s .c om*/ PackageNamesLoader.getPackageNames(classLoader); fail(); } catch (CheckstyleException ex) { assertTrue(ex.getCause() instanceof IOException); assertNotEquals("unable to get package file resources", ex.getMessage()); } }
From source file:uniol.apt.io.renderer.AbstractRenderers.java
/** * Constructor//from w w w .ja v a 2s . c om * * @param clazz Class object of the which the renderers should render */ @SuppressWarnings("unchecked") // I hate type erasure and other java things ... protected AbstractRenderers(Class<T> clazz) { this.renderers = new HashMap<>(); String className = clazz.getCanonicalName(); ClassLoader cl = getClass().getClassLoader(); try { Enumeration<URL> rendererNames = cl.getResources( "META-INF/uniol/apt/compiler/" + Renderer.class.getCanonicalName() + "/" + className); while (rendererNames.hasMoreElements()) { try (InputStream is = rendererNames.nextElement().openStream()) { LineIterator lIter = IOUtils.lineIterator(is, "UTF-8"); while (lIter.hasNext()) { String rendererName = lIter.next(); Class<? extends Renderer<T>> rendererClass; try { rendererClass = (Class<? extends Renderer<T>>) cl.loadClass(rendererName); } catch (ClassNotFoundException ex) { throw new RuntimeException(String.format("Could not load class %s", rendererName), ex); } Renderer<T> renderer; try { renderer = rendererClass.newInstance(); } catch (ClassCastException | IllegalAccessException | InstantiationException ex) { throw new RuntimeException(String.format("Could not instantiate %s", rendererName), ex); } String format = renderer.getFormat(); if (format == null || format.equals("") || !format.equals(format.toLowerCase())) { throw new RuntimeException(String.format("Renderer %s reports an invalid format: %s", rendererName, format)); } Renderer<T> oldRenderer = this.renderers.get(format); if (oldRenderer != null && !oldRenderer.getClass().equals(rendererClass)) { throw new RuntimeException(String.format( "Different renderers claim, to interpret format %s:" + " %s and %s", format, oldRenderer.getClass().getCanonicalName(), rendererName)); } this.renderers.put(format, renderer); } } } } catch (IOException ex) { throw new RuntimeException("Failed to discover renderers", ex); } }
From source file:org.eclipse.wb.internal.core.utils.reflect.CompositeClassLoader.java
@Override protected Enumeration<URL> findResources(String name) throws IOException { Set<URL> allResources = Sets.newHashSet(); for (ClassLoader classLoader : m_classLoaders) { Enumeration<URL> resources = classLoader.getResources(name); CollectionUtils.addAll(allResources, resources); }/*from w ww . j av a2s .co m*/ return Iterators.asEnumeration(allResources.iterator()); }
From source file:org.openvpms.web.component.im.archetype.ConfigReader.java
/** * Returns all resources with the given name. * * @return the paths to resource with name {@code name} *//* w w w.j a v a 2 s .c om*/ protected Set<URL> getPaths(String name) { Set<URL> paths = new HashSet<URL>(); for (ClassLoader loader : getClassLoaders()) { if (loader != null) { try { Enumeration<URL> urls = loader.getResources(name); while (urls.hasMoreElements()) { paths.add(urls.nextElement()); } } catch (IOException exception) { log.error(exception, exception); } } } return paths; }