Search in sources :

Example 6 with UrlClassLoader

use of com.intellij.util.lang.UrlClassLoader in project intellij-community by JetBrains.

the class HttpRequests method process.

private static <T> T process(RequestBuilderImpl builder, RequestProcessor<T> processor) throws IOException {
    LOG.assertTrue(ApplicationManager.getApplication() == null || ApplicationManager.getApplication().isUnitTestMode() || !ApplicationManager.getApplication().isReadAccessAllowed(), "Network shouldn't be accessed in EDT or inside read action");
    ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
    if (contextLoader != null && shouldOverrideContextClassLoader(contextLoader)) {
        // hack-around for class loader lock in sun.net.www.protocol.http.NegotiateAuthentication (IDEA-131621)
        try (URLClassLoader cl = new URLClassLoader(new URL[0], contextLoader)) {
            Thread.currentThread().setContextClassLoader(cl);
            return doProcess(builder, processor);
        } finally {
            Thread.currentThread().setContextClassLoader(contextLoader);
        }
    } else {
        return doProcess(builder, processor);
    }
}
Also used : UrlClassLoader(com.intellij.util.lang.UrlClassLoader)

Example 7 with UrlClassLoader

use of com.intellij.util.lang.UrlClassLoader in project intellij-community by JetBrains.

the class PluginDescriptorTest method testUrlTolerance.

@Test
public void testUrlTolerance() throws MalformedURLException {
    class SingleUrlEnumeration implements Enumeration<URL> {

        private final URL myUrl;

        private boolean hasMoreElements = true;

        public SingleUrlEnumeration(URL url) {
            myUrl = url;
        }

        @Override
        public boolean hasMoreElements() {
            return hasMoreElements;
        }

        @Override
        public URL nextElement() {
            if (!hasMoreElements)
                throw new NoSuchElementException();
            hasMoreElements = false;
            return myUrl;
        }
    }
    class TestLoader extends UrlClassLoader {

        private final URL myUrl;

        public TestLoader(String prefix, String suffix) throws MalformedURLException {
            super(build());
            myUrl = new URL(prefix + new File(getTestDataPath()).toURI().toURL().toString() + suffix + "META-INF/plugin.xml");
        }

        @Override
        public URL getResource(String name) {
            return null;
        }

        @Override
        public Enumeration<URL> getResources(String name) throws IOException {
            return new SingleUrlEnumeration(myUrl);
        }
    }
    ClassLoader loader1 = new TestLoader("", "/spaces%20spaces/");
    assertEquals(1, PluginManagerCore.testLoadDescriptorsFromClassPath(loader1).size());
    ClassLoader loader2 = new TestLoader("", "/spaces spaces/");
    assertEquals(1, PluginManagerCore.testLoadDescriptorsFromClassPath(loader2).size());
    ClassLoader loader3 = new TestLoader("jar:", "/jar%20spaces.jar!/");
    assertEquals(1, PluginManagerCore.testLoadDescriptorsFromClassPath(loader3).size());
    ClassLoader loader4 = new TestLoader("jar:", "/jar spaces.jar!/");
    assertEquals(1, PluginManagerCore.testLoadDescriptorsFromClassPath(loader4).size());
}
Also used : Enumeration(java.util.Enumeration) URLClassLoader(java.net.URLClassLoader) UrlClassLoader(com.intellij.util.lang.UrlClassLoader) File(java.io.File) UrlClassLoader(com.intellij.util.lang.UrlClassLoader) URL(java.net.URL) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 8 with UrlClassLoader

use of com.intellij.util.lang.UrlClassLoader in project intellij-community by JetBrains.

the class BootstrapClassLoaderUtil method addParentClasspath.

private static void addParentClasspath(Collection<URL> classpath, boolean ext) throws MalformedURLException {
    String[] extDirs = System.getProperty("java.ext.dirs", "").split(File.pathSeparator);
    if (ext && extDirs.length == 0)
        return;
    List<URLClassLoader> loaders = new ArrayList<URLClassLoader>(2);
    for (ClassLoader loader = BootstrapClassLoaderUtil.class.getClassLoader(); loader != null; loader = loader.getParent()) {
        if (loader instanceof URLClassLoader) {
            loaders.add(0, (URLClassLoader) loader);
        } else {
            getLogger().warn("Unknown class loader: " + loader.getClass().getName());
        }
    }
    // todo[r.sh] drop after migration to Java 9
    for (URLClassLoader loader : loaders) {
        URL[] urls = loader.getURLs();
        for (URL url : urls) {
            String path = urlToPath(url);
            boolean isExt = false;
            for (String extDir : extDirs) {
                if (path.startsWith(extDir) && path.length() > extDir.length() && path.charAt(extDir.length()) == File.separatorChar) {
                    isExt = true;
                    break;
                }
            }
            if (isExt == ext) {
                classpath.add(url);
            }
        }
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) UrlClassLoader(com.intellij.util.lang.UrlClassLoader) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL)

Example 9 with UrlClassLoader

use of com.intellij.util.lang.UrlClassLoader in project intellij-community by JetBrains.

the class BootstrapClassLoaderUtil method initClassLoader.

@NotNull
public static ClassLoader initClassLoader(boolean updatePlugins) throws MalformedURLException {
    PathManager.loadProperties();
    Collection<URL> classpath = new LinkedHashSet<URL>();
    addParentClasspath(classpath, false);
    addIDEALibraries(classpath);
    addAdditionalClassPath(classpath);
    addParentClasspath(classpath, true);
    UrlClassLoader.Builder builder = UrlClassLoader.build().urls(filterClassPath(new ArrayList<URL>(classpath))).allowLock().usePersistentClasspathIndexForLocalClassDirectories().useCache();
    if (Boolean.valueOf(System.getProperty(PROPERTY_ALLOW_BOOTSTRAP_RESOURCES, "true"))) {
        builder.allowBootstrapResources();
    }
    UrlClassLoader newClassLoader = builder.get();
    // prepare plugins
    if (updatePlugins && !isLoadingOfExternalPluginsDisabled()) {
        try {
            StartupActionScriptManager.executeActionScript();
        } catch (IOException e) {
            Main.showMessage("Plugin Installation Error", e);
        }
    }
    Thread.currentThread().setContextClassLoader(newClassLoader);
    return newClassLoader;
}
Also used : IOException(java.io.IOException) UrlClassLoader(com.intellij.util.lang.UrlClassLoader) URL(java.net.URL) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with UrlClassLoader

use of com.intellij.util.lang.UrlClassLoader in project intellij-community by JetBrains.

the class ExternalSystemApiUtil method reloadIfNecessary.

/**
   * There is a possible case that methods of particular object should be executed with classpath different from the one implied
   * by the current class' class loader. External system offers {@link ParametersEnhancer#enhanceLocalProcessing(List)} method
   * for defining that custom classpath.
   * <p/>
   * It's also possible that particular implementation of {@link ParametersEnhancer} is compiled using dependency to classes
   * which are provided by the {@link ParametersEnhancer#enhanceLocalProcessing(List) expanded classpath}. E.g. a class
   * <code>'A'</code> might use method of class <code>'B'</code> and 'A' is located at the current (system/plugin) classpath but
   * <code>'B'</code> is not. We need to reload <code>'A'</code> using its expanded classpath then, i.e. create new class loaded
   * with that expanded classpath and load <code>'A'</code> by it.
   * <p/>
   * This method allows to do that.
   *
   * @param clazz  custom classpath-aware class which instance should be created (is assumed to have a no-args constructor)
   * @param <T>    target type
   * @return       newly created instance of the given class loaded by custom classpath-aware loader
   * @throws IllegalAccessException     as defined by reflection processing
   * @throws InstantiationException     as defined by reflection processing
   * @throws NoSuchMethodException      as defined by reflection processing
   * @throws InvocationTargetException  as defined by reflection processing
   * @throws ClassNotFoundException     as defined by reflection processing
   */
@NotNull
public static <T extends ParametersEnhancer> T reloadIfNecessary(@NotNull final Class<T> clazz) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
    T instance = clazz.newInstance();
    List<URL> urls = ContainerUtilRt.newArrayList();
    instance.enhanceLocalProcessing(urls);
    if (urls.isEmpty()) {
        return instance;
    }
    final ClassLoader baseLoader = clazz.getClassLoader();
    Method method = baseLoader.getClass().getMethod("getUrls");
    if (method != null) {
        //noinspection unchecked
        urls.addAll((Collection<? extends URL>) method.invoke(baseLoader));
    }
    UrlClassLoader loader = new UrlClassLoader(UrlClassLoader.build().urls(urls).parent(baseLoader.getParent())) {

        @Override
        protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
            if (name.equals(clazz.getName())) {
                return super.loadClass(name, resolve);
            } else {
                try {
                    return baseLoader.loadClass(name);
                } catch (ClassNotFoundException e) {
                    return super.loadClass(name, resolve);
                }
            }
        }
    };
    //noinspection unchecked
    return (T) loader.loadClass(clazz.getName()).newInstance();
}
Also used : UrlClassLoader(com.intellij.util.lang.UrlClassLoader) Method(java.lang.reflect.Method) UrlClassLoader(com.intellij.util.lang.UrlClassLoader) URL(java.net.URL) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

UrlClassLoader (com.intellij.util.lang.UrlClassLoader)11 URL (java.net.URL)8 NotNull (org.jetbrains.annotations.NotNull)5 IOException (java.io.IOException)3 URLClassLoader (java.net.URLClassLoader)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2 Method (java.lang.reflect.Method)2 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 XmlSerializationException (com.intellij.util.xmlb.XmlSerializationException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 MalformedURLException (java.net.MalformedURLException)1 Enumeration (java.util.Enumeration)1 NoSuchElementException (java.util.NoSuchElementException)1 JarFile (java.util.jar.JarFile)1 Element (org.jdom.Element)1