use of java.net.URLClassLoader in project beam by apache.
the class FlinkRunner method detectClassPathResourcesToStage.
/**
* Attempts to detect all the resources the class loader has access to. This does not recurse
* to class loader parents stopping it from pulling in resources from the system class loader.
*
* @param classLoader The URLClassLoader to use to detect resources to stage.
* @return A list of absolute paths to the resources the class loader uses.
* @throws IllegalArgumentException If either the class loader is not a URLClassLoader or one
* of the resources the class loader exposes is not a file resource.
*/
protected static List<String> detectClassPathResourcesToStage(ClassLoader classLoader) {
if (!(classLoader instanceof URLClassLoader)) {
String message = String.format("Unable to use ClassLoader to detect classpath elements. " + "Current ClassLoader is %s, only URLClassLoaders are supported.", classLoader);
LOG.error(message);
throw new IllegalArgumentException(message);
}
List<String> files = new ArrayList<>();
for (URL url : ((URLClassLoader) classLoader).getURLs()) {
try {
files.add(new File(url.toURI()).getAbsolutePath());
} catch (IllegalArgumentException | URISyntaxException e) {
String message = String.format("Unable to convert url (%s) to file.", url);
LOG.error(message);
throw new IllegalArgumentException(message, e);
}
}
return files;
}
use of java.net.URLClassLoader in project zm-mailbox by Zimbra.
the class ZimletUtil method getHandler.
/**
* Loads all the Zimlets, locates the server side ZimletHandler for each Zimlets,
* loads the class and instantiate the object, then returns the instance.
*
* @param name of the Zimlet
* @return ZimletHandler object
*/
public static ZimletHandler getHandler(String name) {
loadZimlets();
Class zh = sZimletHandlers.get(name);
if (zh == null) {
ZimletFile zf = sZimlets.get(name);
if (zf == null) {
return null;
}
URLClassLoader cl = null;
try {
String clazz = zf.getZimletDescription().getServerExtensionClass();
if (clazz != null) {
URL[] urls = { zf.toURL() };
cl = new URLClassLoader(urls, ZimletUtil.class.getClassLoader());
zh = cl.loadClass(clazz);
ZimbraLog.zimlet.info("Loaded class " + zh.getName());
sZimletHandlers.put(name, zh);
}
} catch (Exception e) {
ZimbraLog.zimlet.warn("Unable to load zimlet handler for %s", name, e);
return null;
} finally {
if (cl != null) {
try {
cl.close();
} catch (IOException e) {
ZimbraLog.zimlet.warn("failed to close URLClassLoader", e);
}
}
}
}
try {
if (zh != null) {
return (ZimletHandler) zh.newInstance();
}
} catch (Exception e) {
ZimbraLog.zimlet.warn("Unable to instantiate zimlet handler for " + name, e);
}
return null;
}
use of java.net.URLClassLoader in project phoenix by apache.
the class PhoenixContextExecutorTest method testCall.
@Test
public void testCall() {
URLClassLoader customerClassLoader = new URLClassLoader(new URL[] {});
ClassLoader saveCcl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(customerClassLoader);
try {
PhoenixContextExecutor.callWithoutPropagation(new Callable<Object>() {
@Override
public Object call() {
assertEquals(PhoenixContextExecutor.class.getClassLoader(), Thread.currentThread().getContextClassLoader());
return null;
}
});
} finally {
Thread.currentThread().setContextClassLoader(saveCcl);
}
}
use of java.net.URLClassLoader in project maven-plugins by apache.
the class JavadocUtil method getTagletClassNames.
/**
* Auto-detect the class names of the implementation of <code>com.sun.tools.doclets.Taglet</code> class from a
* given jar file.
* <br/>
* <b>Note</b>: <code>JAVA_HOME/lib/tools.jar</code> is a requirement to find
* <code>com.sun.tools.doclets.Taglet</code> class.
*
* @param jarFile not null
* @return the list of <code>com.sun.tools.doclets.Taglet</code> class names from a given jarFile.
* @throws IOException if jarFile is invalid or not found, or if the <code>JAVA_HOME/lib/tools.jar</code>
* is not found.
* @throws ClassNotFoundException if any
* @throws NoClassDefFoundError if any
*/
protected static List<String> getTagletClassNames(File jarFile) throws IOException, ClassNotFoundException, NoClassDefFoundError {
List<String> classes = getClassNamesFromJar(jarFile);
ClassLoader cl;
// Needed to find com.sun.tools.doclets.Taglet class
File tools = new File(System.getProperty("java.home"), "../lib/tools.jar");
if (tools.exists() && tools.isFile()) {
cl = new URLClassLoader(new URL[] { jarFile.toURI().toURL(), tools.toURI().toURL() }, null);
} else {
cl = new URLClassLoader(new URL[] { jarFile.toURI().toURL() }, null);
}
List<String> tagletClasses = new ArrayList<String>();
Class<?> tagletClass = cl.loadClass("com.sun.tools.doclets.Taglet");
for (String s : classes) {
Class<?> c = cl.loadClass(s);
if (tagletClass.isAssignableFrom(c) && !Modifier.isAbstract(c.getModifiers())) {
tagletClasses.add(c.getName());
}
}
return tagletClasses;
}
use of java.net.URLClassLoader in project phoenix by apache.
the class ContextClassloaderIT method setUpBeforeClass.
@BeforeClass
public static void setUpBeforeClass() throws Exception {
Configuration conf = HBaseConfiguration.create();
setUpConfigForMiniCluster(conf);
hbaseTestUtil = new HBaseTestingUtility(conf);
hbaseTestUtil.startMiniCluster();
String clientPort = hbaseTestUtil.getConfiguration().get(QueryServices.ZOOKEEPER_PORT_ATTRIB);
String url = JDBC_PROTOCOL + JDBC_PROTOCOL_SEPARATOR + LOCALHOST + JDBC_PROTOCOL_SEPARATOR + clientPort + JDBC_PROTOCOL_TERMINATOR + PHOENIX_TEST_DRIVER_URL_PARAM;
driver = initAndRegisterTestDriver(url, ReadOnlyProps.EMPTY_PROPS);
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE test (ID INTEGER NOT NULL PRIMARY KEY, NAME VARCHAR)");
stmt.execute("UPSERT INTO test VALUES (1, 'name1')");
stmt.execute("UPSERT INTO test VALUES (2, 'name2')");
stmt.close();
conn.commit();
conn.close();
badContextClassloader = new URLClassLoader(new URL[] { File.createTempFile("invalid", ".jar").toURI().toURL() }, null);
}
Aggregations