@Override public URL getResource(String name) { return ClassPathLoader.this.getResource(name); }
/** * Verifies that the {@link BrokenClassLoader} is broken and always throws errors even when used * as a TCCL from {@link ClassPathLoader}. This is primarily a control which ensures that tests * depending on <tt>BrokenClassLoader</tt> are valid, but it also verifies that TCCL is included * by default by <tt>ClassPathLoader</tt>. */ @Test public void testBrokenTCCLThrowsErrors() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testBrokenTCCLThrowsErrors"); ClassPathLoader dcl = ClassPathLoader.createWithDefaults(false); ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { // set the TCCL to throw errors Thread.currentThread().setContextClassLoader(new BrokenClassLoader()); String classToLoad = "java.lang.String"; assertThatThrownBy(() -> { dcl.forName(classToLoad); }).isInstanceOf(BrokenError.class); String resourceToGet = "java/lang/String.class"; assertThatThrownBy(() -> { dcl.getResource(resourceToGet); }).isInstanceOf(BrokenError.class); assertThatThrownBy(() -> { dcl.getResourceAsStream(resourceToGet); }).isInstanceOf(BrokenError.class); } finally { Thread.currentThread().setContextClassLoader(cl); } }
/** * Returns an input stream for reading the specified resource. * <p> * The search order is described in the documentation for {@link #getResource(String)}. * * @param name The resource name * @return An input stream for reading the resource, or <tt>null</tt> if the resource could not be * found */ public InputStream getResourceAsStream(final String name) { URL url = getResource(name); try { return url != null ? url.openStream() : null; } catch (IOException e) { return null; } }
/** * Verifies that the class classloader or system classloader will find the class or resource. * Parent is a {@link NullClassLoader} while the TCCL is an excluded {@link BrokenClassLoader}. */ @Test public void testEverythingWithDefaultLoader() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testEverythingWithDefaultLoader"); // create DCL such that parent cannot find anything ClassPathLoader dcl = ClassPathLoader.createWithDefaults(true); ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { // set the TCCL to never find anything Thread.currentThread().setContextClassLoader(new BrokenClassLoader()); String classToLoad = "java.lang.String"; Class<?> clazz = dcl.forName(classToLoad); assertThat(clazz).isNotNull(); String resourceToGet = "java/lang/String.class"; URL url = dcl.getResource(resourceToGet); assertThat(url).isNotNull(); InputStream is = dcl.getResourceAsStream(resourceToGet); assertThat(is).isNotNull(); } finally { Thread.currentThread().setContextClassLoader(cl); } }
/** * Finds the resource with the given name. This method will first search the class loader of the * context class for the resource. That failing, this method will invoke * {@link #getResource(String)} to find the resource. * * @param contextClass The class whose class loader will first be searched * @param name The resource name * @return A <tt>URL</tt> object for reading the resource, or <tt>null</tt> if the resource could * not be found or the invoker doesn't have adequate privileges to get the resource. */ public URL getResource(final Class<?> contextClass, final String name) { if (contextClass != null) { URL url = contextClass.getResource(name); if (url != null) { return url; } } return getResource(name); }
@Override public Collection<String> getSerializationAcceptlist() throws IOException { URL sanctionedSerializables = ClassPathLoader.getLatest().getResource(getClass(), "sanctioned-geode-wan-serializables.txt"); return InternalDataSerializer.loadClassNames(sanctionedSerializables); } }
@Override public Collection<String> getSerializationAcceptlist() throws IOException { URL sanctionedSerializables = ClassPathLoader.getLatest().getResource(getClass(), "sanctioned-geode-cq-serializables.txt"); return InternalDataSerializer.loadClassNames(sanctionedSerializables); } }
@Override public Collection<String> getSerializationAcceptlist() throws IOException { URL sanctionedSerializables = ClassPathLoader.getLatest().getResource(getClass(), "sanctioned-geode-connectors-serializables.txt"); return InternalDataSerializer.loadClassNames(sanctionedSerializables); } }
@Override public Collection<String> getSerializationAcceptlist() throws IOException { URL sanctionedSerializables = ClassPathLoader.getLatest().getResource(getClass(), "sanctioned-geode-lucene-serializables.txt"); return InternalDataSerializer.loadClassNames(sanctionedSerializables); } }
private static URL getFileURL(String fileName) { File file = new File(fileName).getAbsoluteFile(); if (file.exists()) { try { return IOUtils.tryGetCanonicalFileElseGetAbsoluteFile(file).toURI().toURL(); } catch (MalformedURLException ignore) { } } file = new File(System.getProperty("user.home"), fileName); if (file.exists()) { try { return IOUtils.tryGetCanonicalFileElseGetAbsoluteFile(file).toURI().toURL(); } catch (MalformedURLException ignore) { } } return ClassPathLoader.getLatest().getResource(DistributedSystem.class, fileName); }
public void validateWithLocalCacheXSD() { // find the local Cache-1.0.xsd URL local_cache_xsd = ClassPathLoader.getLatest() .getResource("META-INF/schemas/geode.apache.org/schema/cache/cache-1.0.xsd"); validateWith(local_cache_xsd); }
return ClassPathLoader.getLatest().getResource(AgentConfigImpl.class, propFileLocation);
resource = resource.substring(1); url = ClassPathLoader.getLatest().getResource(getClass(), resource); } else { try {
.getResource(InternalDataSerializer.class, "sanctioned-geode-core-serializables.txt"); Collection<String> coreClassNames = InternalDataSerializer.loadClassNames(sanctionedSerializables);
/** * Verifies that <tt>getResource</tt> will skip TCCL if <tt>excludeThreadContextClassLoader</tt> * has been set to true. */ @Test public void testGetResourceExcludeTCCL() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testGetResourceExcludeTCCL"); ClassPathLoader dcl = ClassPathLoader.createWithDefaults(true); String resourceToGet = "com/nowhere/testGetResourceExcludeTCCL.rsc"; assertThat(dcl.getResource(resourceToGet)).isNull(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { // ensure that TCCL is only CL that can find this resource Thread.currentThread().setContextClassLoader(new GeneratingClassLoader()); assertThat(dcl.getResource(resourceToGet)).isNull(); } finally { Thread.currentThread().setContextClassLoader(cl); } }
/** * Verifies that {@link ClassPathLoader#getLatest()} can actually <tt>getResource</tt> when it * exists. */ @Test public void testGetResource() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testGetResource"); String resourceToGet = "org/apache/geode/internal/classpathloaderjunittest/DoesExist.class"; URL url = ClassPathLoader.getLatest().getResource(resourceToGet); assertThat(url).isNotNull(); InputStream is = url != null ? url.openStream() : null; assertThat(is).isNotNull(); int totalBytesRead = 0; byte[] input = new byte[256]; BufferedInputStream bis = new BufferedInputStream(is); for (int bytesRead = bis.read(input); bytesRead > -1;) { totalBytesRead += bytesRead; bytesRead = bis.read(input); } bis.close(); // if the following fails then maybe javac changed and DoesExist.class // contains other than 374 bytes of data... consider updating this test assertThat(totalBytesRead).isEqualTo(GENERATED_CLASS_BYTES_COUNT); }
URL url = ClassPathLoader.getLatest().getResource(MBeanUtil.class, mbeansResource); raiseOnFailure(url != null, String.format("Failed to find %s", new Object[] {mbeansResource}));