Search in sources :

Example 96 with URLClassLoader

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;
}
Also used : URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Example 97 with URLClassLoader

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;
}
Also used : URLClassLoader(java.net.URLClassLoader) IOException(java.io.IOException) URL(java.net.URL) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException)

Example 98 with URLClassLoader

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);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Example 99 with URLClassLoader

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;
}
Also used : URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL)

Example 100 with URLClassLoader

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);
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) HBaseConfiguration(org.apache.hadoop.hbase.HBaseConfiguration) HBaseTestingUtility(org.apache.hadoop.hbase.HBaseTestingUtility) Statement(java.sql.Statement) URLClassLoader(java.net.URLClassLoader) Connection(java.sql.Connection) URL(java.net.URL) BeforeClass(org.junit.BeforeClass)

Aggregations

URLClassLoader (java.net.URLClassLoader)1351 URL (java.net.URL)872 File (java.io.File)514 Test (org.junit.Test)317 IOException (java.io.IOException)256 ArrayList (java.util.ArrayList)202 MalformedURLException (java.net.MalformedURLException)186 Method (java.lang.reflect.Method)177 InvocationTargetException (java.lang.reflect.InvocationTargetException)68 JarFile (java.util.jar.JarFile)54 InputStream (java.io.InputStream)50 HashSet (java.util.HashSet)49 HashMap (java.util.HashMap)44 URISyntaxException (java.net.URISyntaxException)41 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)35 Path (java.nio.file.Path)33 QuickTest (com.hazelcast.test.annotation.QuickTest)32 Test (org.junit.jupiter.api.Test)28 URI (java.net.URI)27 List (java.util.List)27