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);
}
}
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());
}
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);
}
}
}
}
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;
}
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();
}
Aggregations