Search in sources :

Example 91 with URLClassLoader

use of java.net.URLClassLoader in project lucene-solr by apache.

the class SolrResourceLoader method addURLsToClassLoader.

private static URLClassLoader addURLsToClassLoader(final URLClassLoader oldLoader, List<URL> urls) {
    if (urls.size() == 0) {
        return oldLoader;
    }
    List<URL> allURLs = new ArrayList<>();
    allURLs.addAll(Arrays.asList(oldLoader.getURLs()));
    allURLs.addAll(urls);
    for (URL url : urls) {
        log.debug("Adding '{}' to classloader", url.toString());
    }
    ClassLoader oldParent = oldLoader.getParent();
    IOUtils.closeWhileHandlingException(oldLoader);
    return URLClassLoader.newInstance(allURLs.toArray(new URL[allURLs.size()]), oldParent);
}
Also used : ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Example 92 with URLClassLoader

use of java.net.URLClassLoader in project lucene-solr by apache.

the class SolrResourceLoader method addToClassLoader.

/**
   * Adds URLs to the ResourceLoader's internal classloader.  This method <b>MUST</b>
   * only be called prior to using this ResourceLoader to get any resources, otherwise
   * its behavior will be non-deterministic. You also have to {link @reloadLuceneSPI}
   * before using this ResourceLoader.
   *
   * @param urls    the URLs of files to add
   */
void addToClassLoader(List<URL> urls) {
    URLClassLoader newLoader = addURLsToClassLoader(classLoader, urls);
    if (newLoader != classLoader) {
        this.classLoader = newLoader;
    }
    log.info("[{}] Added {} libs to classloader, from paths: {}", getCoreName("null"), urls.size(), urls.stream().map(u -> u.getPath().substring(0, u.getPath().lastIndexOf("/"))).sorted().distinct().collect(Collectors.toList()));
}
Also used : URLClassLoader(java.net.URLClassLoader)

Example 93 with URLClassLoader

use of java.net.URLClassLoader in project SpaciousLib by anhcraft.

the class SPlugin method unloadPlugin.

public static boolean unloadPlugin(Plugin plugin) throws NoSuchFieldException, IllegalAccessException, IOException {
    String name = plugin.getName();
    PluginManager pluginManager = Bukkit.getPluginManager();
    SimpleCommandMap commandMap = null;
    List<Plugin> plugins = null;
    Map<String, Plugin> names = null;
    Map<String, Command> commands = null;
    disablePlugin(plugin);
    if (pluginManager != null) {
        Field pluginsField = Bukkit.getPluginManager().getClass().getDeclaredField("plugins");
        Field lookupNamesField = Bukkit.getPluginManager().getClass().getDeclaredField("lookupNames");
        Field commandMapField = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
        Field commandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
        commandsField.setAccessible(true);
        pluginsField.setAccessible(true);
        commandMapField.setAccessible(true);
        lookupNamesField.setAccessible(true);
        commandMap = (SimpleCommandMap) commandMapField.get(pluginManager);
        commands = (Map<String, Command>) commandsField.get(commandMap);
        plugins = (List<Plugin>) pluginsField.get(pluginManager);
        names = (Map<String, Plugin>) lookupNamesField.get(pluginManager);
    }
    if (plugins != null && plugins.contains(plugin)) {
        plugins.remove(plugin);
    }
    if (names != null && names.containsKey(name)) {
        names.remove(name);
    }
    if (commandMap != null) {
        for (Iterator<Map.Entry<String, Command>> it = commands.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, Command> entry = it.next();
            if (entry.getValue() instanceof PluginCommand) {
                PluginCommand c = (PluginCommand) entry.getValue();
                if (c.getPlugin() == plugin) {
                    c.unregister(commandMap);
                    it.remove();
                }
            }
        }
    }
    ClassLoader cl = plugin.getClass().getClassLoader();
    if (cl instanceof URLClassLoader) {
        ((URLClassLoader) cl).close();
    }
    System.gc();
    return true;
}
Also used : SimpleCommandMap(org.bukkit.command.SimpleCommandMap) PluginManager(org.bukkit.plugin.PluginManager) Field(java.lang.reflect.Field) PluginCommand(org.bukkit.command.PluginCommand) Command(org.bukkit.command.Command) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Plugin(org.bukkit.plugin.Plugin) PluginCommand(org.bukkit.command.PluginCommand)

Example 94 with URLClassLoader

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

the class DataflowRunnerTest method detectClassPathResourceWithNonFileResources.

@Test
public void detectClassPathResourceWithNonFileResources() throws Exception {
    String url = "http://www.google.com/all-the-secrets.jar";
    URLClassLoader classLoader = new URLClassLoader(new URL[] { new URL(url) });
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Unable to convert url (" + url + ") to file.");
    DataflowRunner.detectClassPathResourcesToStage(classLoader);
}
Also used : URLClassLoader(java.net.URLClassLoader) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) URL(java.net.URL) Test(org.junit.Test)

Example 95 with URLClassLoader

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

the class DataflowRunnerTest method detectClassPathResourceWithFileResources.

@Test
public void detectClassPathResourceWithFileResources() throws Exception {
    File file = tmpFolder.newFile("file");
    File file2 = tmpFolder.newFile("file2");
    URLClassLoader classLoader = new URLClassLoader(new URL[] { file.toURI().toURL(), file2.toURI().toURL() });
    assertEquals(ImmutableList.of(file.getAbsolutePath(), file2.getAbsolutePath()), DataflowRunner.detectClassPathResourcesToStage(classLoader));
}
Also used : URLClassLoader(java.net.URLClassLoader) File(java.io.File) Test(org.junit.Test)

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