Search in sources :

Example 26 with URLClassLoader

use of java.net.URLClassLoader in project zeppelin by apache.

the class InterpreterSettingManager method registerInterpreterFromResource.

private boolean registerInterpreterFromResource(ClassLoader cl, String interpreterDir, String interpreterJson) throws IOException, RepositoryException {
    URL[] urls = recursiveBuildLibList(new File(interpreterDir));
    ClassLoader tempClassLoader = new URLClassLoader(urls, cl);
    Enumeration<URL> interpreterSettings = tempClassLoader.getResources(interpreterJson);
    if (!interpreterSettings.hasMoreElements()) {
        return false;
    }
    for (URL url : Collections.list(interpreterSettings)) {
        try (InputStream inputStream = url.openStream()) {
            logger.debug("Reading {} from {}", interpreterJson, url);
            List<RegisteredInterpreter> registeredInterpreterList = getInterpreterListFromJson(inputStream);
            registerInterpreters(registeredInterpreterList, interpreterDir);
        }
    }
    return true;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL) RegisteredInterpreter(org.apache.zeppelin.interpreter.Interpreter.RegisteredInterpreter)

Example 27 with URLClassLoader

use of java.net.URLClassLoader in project zeppelin by apache.

the class InterpreterFactory method createRepl.

private Interpreter createRepl(String dirName, String className, Properties property) throws InterpreterException {
    logger.info("Create repl {} from {}", className, dirName);
    ClassLoader oldcl = Thread.currentThread().getContextClassLoader();
    try {
        URLClassLoader ccl = cleanCl.get(dirName);
        if (ccl == null) {
            // classloader fallback
            ccl = URLClassLoader.newInstance(new URL[] {}, oldcl);
        }
        boolean separateCL = true;
        try {
            // check if server's classloader has driver already.
            Class cls = this.getClass().forName(className);
            if (cls != null) {
                separateCL = false;
            }
        } catch (Exception e) {
            logger.error("exception checking server classloader driver", e);
        }
        URLClassLoader cl;
        if (separateCL == true) {
            cl = URLClassLoader.newInstance(new URL[] {}, ccl);
        } else {
            cl = ccl;
        }
        Thread.currentThread().setContextClassLoader(cl);
        Class<Interpreter> replClass = (Class<Interpreter>) cl.loadClass(className);
        Constructor<Interpreter> constructor = replClass.getConstructor(new Class[] { Properties.class });
        Interpreter repl = constructor.newInstance(property);
        repl.setClassloaderUrls(ccl.getURLs());
        LazyOpenInterpreter intp = new LazyOpenInterpreter(new ClassloaderInterpreter(repl, cl));
        return intp;
    } catch (SecurityException e) {
        throw new InterpreterException(e);
    } catch (NoSuchMethodException e) {
        throw new InterpreterException(e);
    } catch (IllegalArgumentException e) {
        throw new InterpreterException(e);
    } catch (InstantiationException e) {
        throw new InterpreterException(e);
    } catch (IllegalAccessException e) {
        throw new InterpreterException(e);
    } catch (InvocationTargetException e) {
        throw new InterpreterException(e);
    } catch (ClassNotFoundException e) {
        throw new InterpreterException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(oldcl);
    }
}
Also used : RemoteInterpreter(org.apache.zeppelin.interpreter.remote.RemoteInterpreter) RegisteredInterpreter(org.apache.zeppelin.interpreter.Interpreter.RegisteredInterpreter) URL(java.net.URL) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NullArgumentException(org.apache.commons.lang.NullArgumentException) MalformedURLException(java.net.MalformedURLException) RepositoryException(org.sonatype.aether.RepositoryException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader)

Example 28 with URLClassLoader

use of java.net.URLClassLoader in project hive by apache.

the class TestClassNameCompleter method addingEmptyFile.

@Test
public void addingEmptyFile() throws IOException {
    String fileName = "empty.file";
    File p = tmpFolder.newFile(fileName);
    URLClassLoader classLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
    try {
        URLClassLoader newClassLoader = new URLClassLoader(new URL[] { p.toURL() }, classLoader);
        Thread.currentThread().setContextClassLoader(newClassLoader);
        ClassNameCompleter.getClassNames();
    } finally {
        Thread.currentThread().setContextClassLoader(classLoader);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) File(java.io.File) Test(org.junit.Test)

Example 29 with URLClassLoader

use of java.net.URLClassLoader in project buck by facebook.

the class FileClassPathRunner method main.

public static void main(String[] args) throws IOException, ReflectiveOperationException {
    // We must have the name of the class to delegate to added.
    if (args.length < 1) {
        System.exit(-1);
    }
    ClassLoader sysLoader = ClassLoader.getSystemClassLoader();
    if (!(sysLoader instanceof URLClassLoader)) {
        System.exit(-2);
    }
    URLClassLoader urlClassLoader = (URLClassLoader) sysLoader;
    modifyClassLoader(urlClassLoader, true);
    // Now read the main class from the args and invoke it
    Method main = getMainClass(args);
    String[] mainArgs = constructArgs(args);
    main.invoke(null, new Object[] { mainArgs });
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Method(java.lang.reflect.Method)

Example 30 with URLClassLoader

use of java.net.URLClassLoader in project hibernate-orm by hibernate.

the class HibernatePlugin method toClassLoader.

private ClassLoader toClassLoader(FileCollection runtimeClasspath) {
    List<URL> urls = new ArrayList<URL>();
    for (File file : runtimeClasspath) {
        try {
            urls.add(file.toURI().toURL());
            logger.debug("Adding classpath entry for " + file.getAbsolutePath());
        } catch (MalformedURLException e) {
            throw new GradleException("Unable to resolve classpath entry to URL : " + file.getAbsolutePath(), e);
        }
    }
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), Enhancer.class.getClassLoader());
}
Also used : MalformedURLException(java.net.MalformedURLException) Enhancer(org.hibernate.bytecode.enhance.spi.Enhancer) GradleException(org.gradle.api.GradleException) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) File(java.io.File) URL(java.net.URL)

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