List of usage examples for java.net URLClassLoader getURLs
public URL[] getURLs()
From source file:org.eclipse.wb.android.internal.support.AndroidBridge.java
/** * Checks for legacy API and rewrites legacy bridge to be able to collect View instances. *///ww w.j av a 2 s.c om private LayoutLibrary checkLegacy(AndroidTargetData data, LayoutLibrary layoutLibrary) throws Exception { if (ReflectionUtils.getFieldObject(layoutLibrary, "mBridge") != null) { // do nothing, modern api return layoutLibrary; } // create new bridge using rewriting class loader Object legacyBridge = ReflectionUtils.getFieldObject(layoutLibrary, "mLegacyBridge"); URLClassLoader legacyClassLoader = (URLClassLoader) legacyBridge.getClass().getClassLoader(); URLClassLoader newClassLoader = new LegacyBridgeClassLoader(legacyClassLoader.getURLs(), AndroidBridge.class.getClassLoader()); // create new Class<?> legacyClass = newClassLoader.loadClass(LayoutLibrary.CLASS_BRIDGE); ILayoutBridge newLegacyBridge = (ILayoutBridge) legacyClass.newInstance(); // re-init IAndroidTarget target = (IAndroidTarget) ReflectionUtils.getFieldObject(data, "mTarget"); String fontPath = target.getPath(IAndroidTarget.FONTS); newLegacyBridge.init(fontPath, data.getEnumValueMap()); // store ReflectionUtils.setField(layoutLibrary, "mLegacyBridge", newLegacyBridge); ReflectionUtils.setField(layoutLibrary, "mClassLoader", newClassLoader); // setup collector ReflectionUtils.setField(newLegacyBridge, "collector", new IViewsCollector() { public void collect(Object view, Object key) { if (view != null) { m_legacyViewsCollection.put(key, view); } } }); return layoutLibrary; }
From source file:org.fuin.utils4j.Utils4J.java
/** * Adds an URL to the class path.// ww w. ja va 2 s . com * * @param url * URL to add - Cannot be <code>null</code>. * @param classLoader * Class loader to use - Cannot be <code>null</code>. */ public static void addToClasspath(final URL url, final ClassLoader classLoader) { checkNotNull("url", url); checkNotNull("classLoader", classLoader); if (!(classLoader instanceof URLClassLoader)) { throw new IllegalArgumentException( "Cannot add '" + url + "' to classloader because it's not an URL classloader"); } final URLClassLoader urlClassLoader = (URLClassLoader) classLoader; if (!containsURL(urlClassLoader.getURLs(), url)) { try { final Method addURL = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class }); addURL.setAccessible(true); addURL.invoke(urlClassLoader, new Object[] { url }); } catch (final NoSuchMethodException e) { throw new RuntimeException(e); } catch (final IllegalArgumentException e) { throw new RuntimeException(e); } catch (final IllegalAccessException e) { throw new RuntimeException(e); } catch (final InvocationTargetException e) { throw new RuntimeException(e); } } }
From source file:org.fuin.utils4j.Utils4JTest.java
/** * @testng.test//from ww w . java 2s. co m */ public final void testAddToClasspathString() throws MalformedURLException { final ClassLoader classLoader = Utils4J.class.getClassLoader(); if (!(classLoader instanceof URLClassLoader)) { throw new IllegalStateException( "Classloader is not an URL classloader! [" + classLoader.getClass().getName() + "]"); } final URLClassLoader urlClassLoader = (URLClassLoader) classLoader; final URL url = new URL("file:/test1.jar"); Assert.assertFalse(Utils4J.containsURL(urlClassLoader.getURLs(), url)); Utils4J.addToClasspath(url.toExternalForm()); Assert.assertTrue(Utils4J.containsURL(urlClassLoader.getURLs(), url)); }
From source file:org.fuin.utils4j.Utils4JTest.java
/** * @testng.test/* ww w .j av a 2 s . c o m*/ */ public final void testAddToClasspathURL() throws MalformedURLException { final ClassLoader classLoader = Utils4J.class.getClassLoader(); if (!(classLoader instanceof URLClassLoader)) { throw new IllegalStateException( "Classloader is not an URL classloader! [" + classLoader.getClass().getName() + "]"); } final URLClassLoader urlClassLoader = (URLClassLoader) classLoader; final URL url = new URL("file:/test2.jar"); Assert.assertFalse(Utils4J.containsURL(urlClassLoader.getURLs(), url)); Utils4J.addToClasspath(url); Assert.assertTrue(Utils4J.containsURL(urlClassLoader.getURLs(), url)); }
From source file:org.fusesource.cloudmix.agent.mop.MopProcess.java
void dumpClassLoader(ClassLoader cl) { if (LOG.isDebugEnabled()) { if (cl instanceof URLClassLoader) { URLClassLoader urlClassLoader = (URLClassLoader) cl; URL[] urls = urlClassLoader.getURLs(); for (URL url : urls) { LOG.debug("ClassLoader URL: " + url); }// w ww .j ava 2 s . c o m } ClassLoader parent = cl.getParent(); if (parent != null) { LOG.debug("Parent Class Loader: " + parent); dumpClassLoader(parent); } } }
From source file:org.gridgain.grid.spi.deployment.uri.GridUriDeploymentFileProcessor.java
/** * Cleanup class loaders resource./*from w ww . ja va 2 s . co m*/ * * @param clsLdr Released class loader. * @param log Logger. */ static void cleanupUnit(ClassLoader clsLdr, GridLogger log) { assert clsLdr != null; assert log != null; if (clsLdr instanceof URLClassLoader) { URLClassLoader clsLdr0 = (URLClassLoader) clsLdr; U.close(clsLdr0, log); try { URL url = clsLdr0.getURLs()[0]; File dir = new File(url.toURI()); U.delete(dir); if (dir.getName().startsWith("dirzip_")) { File jarFile = new File(dir.getParentFile(), dir.getName().substring(7)); U.delete(jarFile); } } catch (Exception e) { U.error(log, "Failed to cleanup unit [clsLdr=" + clsLdr + ']', e); } } }
From source file:org.jsweet.JSweetConfig.java
/** * Initialize the classpath to include tools.jar. * /*from ww w . ja v a 2s. com*/ * @param jdkHome * the jdkHome option value (if not set or not found, fall back * to the JAVA_HOME environment variable) * @param handler * the transpilation handler that should report an error if * tools.jar is not found (if null uses the default logger) */ public static void initClassPath(String jdkHome) { try { URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); boolean found = false; for (URL url : urlClassLoader.getURLs()) { if (url.getPath().endsWith("/tools.jar") || url.getPath().endsWith("/Classes/classes.jar")) { found = true; logger.debug("tools.jar already in classpath"); break; } } if (!found) { logger.debug("adding tools.jar in classpath"); File toolsLib = null; if (!StringUtils.isBlank(jdkHome)) { logger.debug("lookup in " + jdkHome); toolsLib = new File(jdkHome, "lib/tools.jar"); if (!toolsLib.exists()) { // we may be pointing to the JDK's jre toolsLib = new File(jdkHome, "../lib/tools.jar"); } // for Mac if (!toolsLib.exists()) { toolsLib = new File(jdkHome, "/Classes/classes.jar"); } if (!toolsLib.exists()) { toolsLib = new File(jdkHome, "../Classes/classes.jar"); } } if (toolsLib == null || !toolsLib.exists()) { logger.debug("lookup in JAVA_HOME=" + System.getenv("JAVA_HOME")); toolsLib = new File(System.getenv("JAVA_HOME"), "lib/tools.jar"); if (!toolsLib.exists()) { // we may be pointing to the JDK's jre toolsLib = new File(System.getenv("JAVA_HOME"), "../lib/tools.jar"); } // for Mac if (!toolsLib.exists()) { toolsLib = new File(System.getenv("JAVA_HOME"), "/Classes/classes.jar"); } if (!toolsLib.exists()) { toolsLib = new File(System.getenv("JAVA_HOME"), "../Classes/classes.jar"); } } if (!toolsLib.exists()) { return; } Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); method.setAccessible(true); method.invoke(urlClassLoader, toolsLib.toURI().toURL()); logger.debug("updated classpath with: " + toolsLib); } } catch (Exception e) { logger.error(e.getMessage(), e); } }
From source file:org.kantega.dogmaticmvc.java.JavaScriptCompiler.java
@Override public Class compile(HttpServletRequest request) { String className = request.getServletPath().substring(1).replace('/', '.'); List<JavaFileObject> compilationUnits = new ArrayList<JavaFileObject>(); for (String path : (Set<String>) servletContext.getResourcePaths("/WEB-INF/dogmatic/")) { if (path.endsWith("java")) { try { String classNAme = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf(".")); compilationUnits.add(new JavaSourceFromURL(classNAme, servletContext.getResource(path))); } catch (MalformedURLException e) { throw new RuntimeException(e); }/*from w w w.j a va2 s . c o m*/ } } DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); InMemoryJavaFileManager fileManager = new InMemoryJavaFileManager( compiler.getStandardFileManager(null, null, null), new ClassLoaderImpl(getClass().getClassLoader())); String cp = ""; for (ClassLoader cl : Arrays.asList(getClass().getClassLoader(), getClass().getClassLoader().getParent())) { if (cl instanceof URLClassLoader) { URLClassLoader ucl = (URLClassLoader) cl; for (URL url : ucl.getURLs()) { if (cp.length() > 0) { cp += File.pathSeparator; } cp += url.getFile(); } } } List<String> options = new ArrayList(Arrays.asList("-classpath", cp)); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits); boolean success = task.call(); StringWriter sw = new StringWriter(); PrintWriter w = new PrintWriter(sw); for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { w.println(diagnostic.getCode()); w.println(diagnostic.getKind()); w.println(diagnostic.getPosition()); w.println(diagnostic.getStartPosition()); w.println(diagnostic.getEndPosition()); w.println(diagnostic.getSource()); w.println(diagnostic.getMessage(null)); } System.out.println("Success: " + success); if (success) { try { return fileManager.getClassLoader(null).loadClass(className); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } else { throw new RuntimeException("Compilation failed: " + sw); } }
From source file:org.kie.workbench.common.services.backend.maven.plugins.dependency.BuildInMemoryClasspathMojoTest.java
@Test public void testCompilerClassloaderUtilsTests() { Path path = Paths.get(".").resolve("target/test-classes//dummy_deps_complex"); Optional<ClassLoader> classloaderOptional = CompilerClassloaderUtils.getClassloaderFromAllDependencies( path.toAbsolutePath().toString(), mavenRepoPath, TestUtilMaven.getSettingsFile()); assertThat(classloaderOptional).isPresent(); ClassLoader classloader = classloaderOptional.get(); URLClassLoader urlsc = (URLClassLoader) classloader; assertThat(urlsc.getURLs()).hasSize(7); }
From source file:org.kitodo.serviceloader.KitodoServiceLoader.java
/** * Loads jars from the pluginsFolder to the classpath, so the ServiceLoader * can find them./*from w w w .j a v a 2 s . c o m*/ */ private void loadModulesIntoClasspath() { Path moduleFolder = FileSystems.getDefault().getPath(modulePath); URLClassLoader sysLoader; try (DirectoryStream<Path> stream = Files.newDirectoryStream(moduleFolder, JAR)) { for (Path f : stream) { File loc = new File(f.toString()); sysLoader = (URLClassLoader) this.getClass().getClassLoader(); ArrayList<URL> urls = new ArrayList<>(Arrays.asList(sysLoader.getURLs())); URL udir = loc.toURI().toURL(); if (!urls.contains(udir)) { Class<URLClassLoader> sysClass = URLClassLoader.class; Method method = sysClass.getDeclaredMethod("addURL", URL.class); method.setAccessible(true); method.invoke(sysLoader, udir); } } } catch (IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { logger.error(ERROR, e.getMessage()); } }