Example usage for java.net URLClassLoader URLClassLoader

List of usage examples for java.net URLClassLoader URLClassLoader

Introduction

In this page you can find the example usage for java.net URLClassLoader URLClassLoader.

Prototype

URLClassLoader(URL[] urls, AccessControlContext acc) 

Source Link

Usage

From source file:com.heliosdecompiler.helios.controller.addons.JarLauncher.java

@Override
public void run(File file) {
    JarFile jarFile = null;/*www .  jav  a2s  .c  o m*/
    InputStream inputStream = null;
    try {
        System.out.println("Loading addon: " + file.getAbsolutePath());
        jarFile = new JarFile(file);
        ZipEntry entry = jarFile.getEntry("addon.json");
        if (entry != null) {
            inputStream = jarFile.getInputStream(entry);
            JsonObject jsonObject = new Gson()
                    .fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), JsonObject.class);
            String main = jsonObject.get("main").getAsString();
            URL[] url = new URL[] { file.toURI().toURL() };
            ClassLoader classLoader = AccessController
                    .doPrivileged((PrivilegedAction<ClassLoader>) () -> new URLClassLoader(url,
                            Helios.class.getClassLoader()));
            Class<?> clazz = Class.forName(main, true, classLoader);
            if (Addon.class.isAssignableFrom(clazz)) {
                Addon addon = Addon.class.cast(clazz.newInstance());
                //                    registerAddon(addon.getName(), addon);
            } else {
                throw new IllegalArgumentException("Addon main does not extend Addon");
            }
        } else {
            throw new IllegalArgumentException("No addon.json found");
        }
    } catch (Exception e) {
        ExceptionHandler.handle(e);
    } finally {
        IOUtils.closeQuietly(jarFile);
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:com.mirth.connect.server.util.javascript.MirthContextFactory.java

public ClassLoader getIsolatedClassLoader() {
    if (isolatedClassLoader == null && ArrayUtils.isNotEmpty(urls)) {
        synchronized (this) {
            if (isolatedClassLoader == null) {
                isolatedClassLoader = new URLClassLoader(urls, null);
            }/*  www .ja  va  2s.  c  o m*/
        }
    }
    return isolatedClassLoader;
}

From source file:com.heliosdecompiler.helios.handler.addons.JarLauncher.java

@Override
public void run(File file) {
    JarFile jarFile = null;//from w w  w  .  j  av a 2 s.co  m
    InputStream inputStream = null;
    try {
        System.out.println("Loading addon: " + file.getAbsolutePath());
        jarFile = new JarFile(file);
        ZipEntry entry = jarFile.getEntry("addon.json");
        if (entry != null) {
            inputStream = jarFile.getInputStream(entry);
            JsonObject jsonObject = Json.parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
                    .asObject();
            String main = jsonObject.get("main").asString();
            URL[] url = new URL[] { file.toURI().toURL() };
            ClassLoader classLoader = AccessController
                    .doPrivileged((PrivilegedAction<ClassLoader>) () -> new URLClassLoader(url,
                            Helios.class.getClassLoader()));
            Class<?> clazz = Class.forName(main, true, classLoader);
            if (Addon.class.isAssignableFrom(clazz)) {
                Addon addon = Addon.class.cast(clazz.newInstance());
                registerAddon(addon.getName(), addon);
            } else {
                throw new IllegalArgumentException("Addon main does not extend Addon");
            }
        } else {
            throw new IllegalArgumentException("No addon.json found");
        }
    } catch (Exception e) {
        ExceptionHandler.handle(e);
    } finally {
        IOUtils.closeQuietly(jarFile);
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:com.retroduction.carma.resolvers.util.TestCaseInstantiationVerifier.java

private void reinitPrivateClassLoader() {

    Set<File> combinedClassPathSet = new HashSet<File>();

    if (this.getClassPath() != null) {
        combinedClassPathSet.addAll(this.getClassPath());
    }/*from w  w  w  . j  a  v a 2 s.c  o  m*/

    if (this.getTestClassPath() != null) {
        combinedClassPathSet.addAll(this.getTestClassPath());
    }

    if (this.getDependencyClassPath() != null) {

        for (URL url : this.getDependencyClassPath()) {
            File file = new File(url.getFile());
            combinedClassPathSet.add(file);
        }

    }

    Set<URL> validURLs = this.filterInvalidURLs(combinedClassPathSet);

    this.setLoader(new URLClassLoader(validURLs.toArray(new URL[0]), this.getClass().getClassLoader()));
}

From source file:org.pdfsam.configuration.EnhancedClassloaderProvider.java

static ClassLoader classLoader(ClassLoader classLoader) {
    requireNotNull(classLoader, "Cannot enhance null class loader");
    try {/*  w w  w .  j  a  va  2 s  .c om*/
        Path modulesPath = Optional.ofNullable(getUserSpecifiedModulesPath()).orElse(getModulesPath());
        if (!Files.isDirectory(modulesPath)) {
            LOG.warn(DefaultI18nContext.getInstance().i18n("Modules directory {0} does not exist",
                    modulesPath.toString()));
            return classLoader;
        }
        LOG.debug(DefaultI18nContext.getInstance().i18n("Loading modules from {0}", modulesPath.toString()));
        try (Stream<Path> files = Files.list(modulesPath)) {
            URL[] modules = getUrls(files);
            if (modules.length > 0) {
                LOG.trace(DefaultI18nContext.getInstance().i18n("Found modules jars {0}",
                        Arrays.toString(modules)));
                return AccessController.doPrivileged(
                        (PrivilegedAction<URLClassLoader>) () -> new URLClassLoader(modules, classLoader));
            }
        }
    } catch (IOException | URISyntaxException ex) {
        LOG.warn(DefaultI18nContext.getInstance().i18n("Error finding modules paths"), ex);
    }
    LOG.trace(DefaultI18nContext.getInstance().i18n("No module has been found"));
    return classLoader;
}

From source file:com.github.persapiens.jsfboot.jetty.JsfJettyServerCustomizer.java

@Override
public void customize(Server server) {
    Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class);
    final WebAppContext webAppContext = (WebAppContext) childHandlersByClass[0];

    try {/*from   w w  w . ja  v  a  2s. c om*/
        ClassPathResource classPathResource = new ClassPathResource(
                this.jettyProperties.getClassPathResource());
        webAppContext.setBaseResource(new ResourceCollection(classPathResource.getURI().toString()));

        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
            public Void run() {
                webAppContext.setClassLoader(new URLClassLoader(new URL[0], this.getClass().getClassLoader()));
                return null;
            }
        });

        LOGGER.info(
                "Setting Jetty classLoader to " + this.jettyProperties.getClassPathResource() + " directory");
    } catch (IOException exception) {
        LOGGER.error("Unable to configure Jetty classLoader to " + this.jettyProperties.getClassPathResource()
                + " directory " + exception.getMessage());

        throw new RuntimeException(exception);
    }
}

From source file:com.thoughtworks.go.util.NestedJarClassLoader.java

private ClassLoader createLoaderForJar(URL jarURL) {
    LOGGER.debug("Creating Loader For jar: {}", jarURL);
    ClassLoader jarLoader = new URLClassLoader(enumerateJar(jarURL), this);
    if (jarLoader == null) {
        LOGGER.warn("No jar found with url: {}", jarURL);
    }/* ww w . j a  v  a2  s  .c o m*/
    return jarLoader;
}

From source file:org.apache.nifi.util.file.classloader.ClassLoaderUtils.java

protected static ClassLoader createModuleClassLoader(URL[] modules, ClassLoader parentClassLoader) {
    return new URLClassLoader(modules, parentClassLoader);
}

From source file:ch.epfl.data.squall.utilities.SquallSerializationDelegate.java

@Override
public Object deserialize(byte[] bytes) {
    try {/* w  w w.  ja  va2  s .  c om*/
        return super.deserialize(bytes);
    } catch (RuntimeException e) {
        try {
            if (classdir == null)
                throw e;

            URLClassLoader classloader = new URLClassLoader(new URL[] { classdir },
                    Thread.currentThread().getContextClassLoader());

            // Read the object
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ClassLoaderObjectInputStream(classloader, bis);
            Object ret = ois.readObject();
            ois.close();
            return ret;
        } catch (ClassNotFoundException error) {
            throw new RuntimeException(error);
        } catch (IOException error) {
            throw new RuntimeException(error);
        }
    }
}

From source file:com.clican.pluto.orm.dynamic.impl.DynamicClassLoader.java

/**
 * This will be initialize by Spring Framework.
 * /*w  w w .j  av a2s  . co m*/
 * @throws MalformedURLException
 * @throws ClassNotFoundException
 */
public void init() throws MalformedURLException, ClassNotFoundException {
    File file = new File(tempORMCfgPojoFolder);
    if (!file.exists()) {
        file.mkdirs();
    }
    urlClassLoader = new URLClassLoader(new URL[] { file.toURI().toURL() }, parent);
    loadAllClasses(file);
}