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); }
JarDeployer getJarDeployer() { return ClassPathLoader.getLatest().getJarDeployer(); }
/** * Helper method equivalent to <code>ClassPathLoader.getLatest().asClassLoader();</code>. * * @return {@link ClassLoader} for current {@link ClassPathLoader}. * @since GemFire 8.1 */ public static ClassLoader getLatestAsClassLoader() { return getLatest().asClassLoader(); }
/** * Get {@link InputSource} for path in class path. * * @param path to resource to get {@link InputSource} for. * @return InputSource if resource found, otherwise null. * @since GemFire 8.1 */ protected InputSource getClassPathInputSource(final String publicId, final String systemId, final String path) { final InputStream stream = ClassPathLoader.getLatest().getResourceAsStream(getClass(), path); if (null == stream) { return null; } final InputSource inputSource = new InputSource(stream); inputSource.setPublicId(publicId); inputSource.setSystemId(systemId); return inputSource; }
/** * 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); } }
/** * Undeploy the given JAR file. * * @param jarName The name of the JAR file to undeploy * @return The path to the location on disk where the JAR file had been deployed * @throws IOException If there's a problem deleting the file */ public String undeploy(final String jarName) throws IOException { lock.lock(); try { DeployedJar deployedJar = deployedJars.remove(jarName); if (deployedJar == null) { throw new IllegalArgumentException("JAR not deployed"); } ClassPathLoader.getLatest().rebuildClassLoaderForDeployedJars(); deployedJar.cleanUp(null); deleteAllVersionsOfJar(jarName); return deployedJar.getFileCanonicalPath(); } finally { lock.unlock(); } }
return Proxy.getProxyClass(nonPublicLoader, classObjs); } else { return ClassPathLoader.getLatest().getProxyClass(classObjs);
/** * Verifies that {@link ClassPathLoader#getLatest()} can actually <tt>getResources</tt> when it * exists. */ @Test public void testGetResources() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testGetResources"); String resourceToGet = "org/apache/geode/internal/classpathloaderjunittest/DoesExist.class"; Enumeration<URL> urls = ClassPathLoader.getLatest().getResources(resourceToGet); assertThat(urls).isNotNull(); assertThat(urls.hasMoreElements()).isTrue(); URL url = urls.nextElement(); 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); }
/** * Verifies that {@link ClassPathLoader#getLatest()} is always initialized and returns a * <tt>ClassPathLoader</tt> instance. */ @Test public void testLatestExists() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testLatestExists"); assertThat(ClassPathLoader.getLatest()).isNotNull(); }
@Override public URL getResource(String name) { return ClassPathLoader.this.getResource(name); }
/** * 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 Class#forName(String, boolean, ClassLoader)} used with * {@link ClassPathLoader} works as expected with named object arrays, while * {@link ClassLoader#loadClass(String)} throws ClassNotFoundException for named object arrays. */ @Test public void testForNameWithObjectArray() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testForNameWithObjectArray"); ClassPathLoader dcl = ClassPathLoader.createWithDefaults(false); String classToLoad = "[Ljava.lang.String;"; Class<?> clazz = null; clazz = dcl.forName(classToLoad); assertThat(clazz.getName()).isEqualTo(classToLoad); }
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { return ClassPathLoader.this.forName(name); }
/** * Verifies that <tt>getResourceAsStream</tt> will skip TCCL if * <tt>excludeThreadContextClassLoader</tt> has been set to true. */ @Test public void testGetResourceAsStreamExcludeTCCL() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testGetResourceAsStreamExcludeTCCL"); ClassPathLoader dcl = ClassPathLoader.createWithDefaults(true); String resourceToGet = "com/nowhere/testGetResourceAsStreamExcludeTCCL.rsc"; assertThat(dcl.getResourceAsStream(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.getResourceAsStream(resourceToGet)).isNull(); } finally { Thread.currentThread().setContextClassLoader(cl); } }
@Override public InputStream getResourceAsStream(String name) { return ClassPathLoader.this.getResourceAsStream(name); } };
/** * Get delegate {@link XmlParser} for the given <code>namespaceUri</code> * * @param namespaceUri to find {@link XmlParser} for. * @return {@link XmlParser} if found, otherwise null. * @since GemFire 8.1 */ // UnitTest CacheXmlParser.testGetDelegate() private XmlParser getDelegate(final String namespaceUri) { XmlParser delegate = delegates.get(namespaceUri); if (null == delegate) { try { final ServiceLoader<XmlParser> serviceLoader = ServiceLoader.load(XmlParser.class, ClassPathLoader.getLatestAsClassLoader()); for (final XmlParser xmlParser : serviceLoader) { if (xmlParser.getNamespaceUri().equals(namespaceUri)) { delegate = xmlParser; delegate.setStack(stack); delegate.setDocumentLocator(documentLocator); delegates.put(xmlParser.getNamespaceUri(), xmlParser); break; } } } catch (final Exception e) { logger.error(e.getMessage(), e); } } return delegate; }
public VersionDescription(String name) { InputStream is = ClassPathLoader.getLatest().getResourceAsStream(getClass(), name); if (is == null) { error = Optional .of(String.format("<Could not find resource org/apache/geode/internal/%s>", name)); description = null; return; } description = new Properties(); try { description.load(is); } catch (Exception ex) { error = Optional .of(String.format( "<Could not read properties from resource org/apache/geode/internal/%s because: %s>", name, ex)); return; } error = validate(description); }
/** * Resolve entity using discovered {@link EntityResolver2}s. * * @return {@link InputSource} for resolved entity if found, otherwise null. * @since GemFire 8.1 */ private InputSource resolveEntityByEntityResolvers(String name, String publicId, String baseURI, String systemId) throws SAXException, IOException { final ServiceLoader<EntityResolver2> entityResolvers = ServiceLoader.load(EntityResolver2.class, ClassPathLoader.getLatest().asClassLoader()); for (final EntityResolver2 entityResolver : entityResolvers) { final InputSource inputSource = entityResolver.resolveEntity(name, publicId, baseURI, systemId); if (null != inputSource) { return inputSource; } } 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); } }