List of usage examples for java.net URLClassLoader getURLs
public URL[] getURLs()
From source file:offstage.licensor.WriteJNLP.java
public static String getReleaseVersion3() { URLClassLoader cl = (URLClassLoader) WriteJNLP.class.getClassLoader(); URL[] urls = cl.getURLs(); String surl = urls[0].toString(); //System.out.println(surl); int slash = surl.lastIndexOf('/'); int dash = surl.indexOf('-', slash + 1); String version = surl.substring(dash + 1); return version; // System.out.println(version); }
From source file:org.apache.accumulo.minicluster.MiniAccumuloCluster.java
private String getClasspath() throws IOException { try {/*from ww w. j av a2s. c om*/ ArrayList<ClassLoader> classloaders = new ArrayList<ClassLoader>(); ClassLoader cl = this.getClass().getClassLoader(); while (cl != null) { classloaders.add(cl); cl = cl.getParent(); } Collections.reverse(classloaders); StringBuilder classpathBuilder = new StringBuilder(); classpathBuilder.append(config.getConfDir().getAbsolutePath()); if (config.getClasspathItems() == null) { // assume 0 is the system classloader and skip it for (int i = 1; i < classloaders.size(); i++) { ClassLoader classLoader = classloaders.get(i); if (classLoader instanceof URLClassLoader) { URLClassLoader ucl = (URLClassLoader) classLoader; for (URL u : ucl.getURLs()) { append(classpathBuilder, u); } } else if (classLoader instanceof VFSClassLoader) { VFSClassLoader vcl = (VFSClassLoader) classLoader; for (FileObject f : vcl.getFileObjects()) { append(classpathBuilder, f.getURL()); } } else { throw new IllegalArgumentException( "Unknown classloader type : " + classLoader.getClass().getName()); } } } else { for (String s : config.getClasspathItems()) classpathBuilder.append(File.pathSeparator).append(s); } return classpathBuilder.toString(); } catch (URISyntaxException e) { throw new IOException(e); } }
From source file:org.apache.axis2.jaxws.message.databinding.impl.ClassFinderImpl.java
public ArrayList<Class> getClassesFromJarFile(String pkg, ClassLoader cl) throws ClassNotFoundException { try {//from ww w. jav a 2 s .co m ArrayList<Class> classes = new ArrayList<Class>(); URLClassLoader ucl = (URLClassLoader) cl; URL[] srcURL = ucl.getURLs(); String path = pkg.replace('.', '/'); //Read resources as URL from class loader. for (URL url : srcURL) { if ("file".equals(url.getProtocol())) { File f = new File(url.toURI().getPath()); //If file is not of type directory then its a jar file if (f.exists() && !f.isDirectory()) { try { JarFile jf = new JarFile(f); Enumeration<JarEntry> entries = jf.entries(); //read all entries in jar file while (entries.hasMoreElements()) { JarEntry je = entries.nextElement(); String clazzName = je.getName(); if (clazzName != null && clazzName.endsWith(".class")) { //Add to class list here. clazzName = clazzName.substring(0, clazzName.length() - 6); clazzName = clazzName.replace('/', '.').replace('\\', '.').replace(':', '.'); //We are only going to add the class that belong to the provided package. if (clazzName.startsWith(pkg + ".")) { try { Class clazz = forName(clazzName, false, cl); // Don't add any interfaces or JAXWS specific classes. // Only classes that represent data and can be marshalled // by JAXB should be added. if (!clazz.isInterface() && clazz.getPackage().getName().equals(pkg) && ClassUtils.getDefaultPublicConstructor(clazz) != null && !ClassUtils.isJAXWSClass(clazz)) { if (log.isDebugEnabled()) { log.debug("Adding class: " + clazzName); } classes.add(clazz); } //catch Throwable as ClassLoader can throw an NoClassDefFoundError that //does not extend Exception, so lets catch everything that extends Throwable //rather than just Exception. } catch (Throwable e) { if (log.isDebugEnabled()) { log.debug("Tried to load class " + clazzName + " while constructing a JAXBContext. This class will be skipped. Processing Continues."); log.debug(" The reason that class could not be loaded:" + e.toString()); log.trace(JavaUtils.stackToString(e)); } } } } } } catch (IOException e) { throw new ClassNotFoundException(Messages.getMessage("ClassUtilsErr4")); } } } } return classes; } catch (Exception e) { throw new ClassNotFoundException(e.getMessage()); } }
From source file:org.apache.axis2.wsdl.util.WSDLWrapperReloadImpl.java
private static URL getURLFromJAR(URLClassLoader urlLoader, URL relativeURL) throws WSDLException { URL[] urlList = urlLoader.getURLs(); if (urlList == null) { return null; }//from ww w.j a v a2s . c om for (int i = 0; i < urlList.length; i++) { URL url = urlList[i]; if (url == null) { return null; } if ("file".equals(url.getProtocol())) { File f = new File(url.getPath()); //If file is not of type directory then its a jar file if (f.exists() && !f.isDirectory()) { try { JarFile jf = new JarFile(f); Enumeration entries = jf.entries(); // read all entries in jar file and return the first // wsdl file that matches the relative path while (entries.hasMoreElements()) { JarEntry je = (JarEntry) entries.nextElement(); String name = je.getName(); if (name.endsWith(".wsdl")) { String relativePath = relativeURL.getPath(); if (relativePath.endsWith(name)) { String path = f.getAbsolutePath(); // This check is necessary because Unix/Linux file paths begin // with a '/'. When adding the prefix 'jar:file:/' we may end // up with '//' after the 'file:' part. This causes the URL // object to treat this like a remote resource if (path != null && path.indexOf("/") == 0) { path = path.substring(1, path.length()); } URL absoluteUrl = new URL("jar:file:/" + path + "!/" + je.getName()); return absoluteUrl; } } } } catch (Exception e) { WSDLException we = new WSDLException("WSDLWrapperReloadImpl : ", e.getMessage(), e); throw we; } } } } return null; }
From source file:org.apache.hadoop.hive.metastore.MetaStoreUtils.java
/** * Add new elements to the classpath.//w w w . j a v a 2 s . co m * * @param newPaths * Array of classpath elements */ public static ClassLoader addToClassPath(ClassLoader cloader, String[] newPaths) throws Exception { URLClassLoader loader = (URLClassLoader) cloader; List<URL> curPath = Arrays.asList(loader.getURLs()); ArrayList<URL> newPath = new ArrayList<URL>(curPath.size()); // get a list with the current classpath components for (URL onePath : curPath) { newPath.add(onePath); } curPath = newPath; for (String onestr : newPaths) { URL oneurl = urlFromPathString(onestr); if (oneurl != null && !curPath.contains(oneurl)) { curPath.add(oneurl); } } return new URLClassLoader(curPath.toArray(new URL[0]), loader); }
From source file:org.apache.hadoop.hive.metastore.utils.MetaStoreUtils.java
/** * Add new elements to the classpath./*from www . j a v a2 s . c o m*/ * * @param newPaths * Array of classpath elements */ public static ClassLoader addToClassPath(ClassLoader cloader, String[] newPaths) throws Exception { URLClassLoader loader = (URLClassLoader) cloader; List<URL> curPath = Arrays.asList(loader.getURLs()); ArrayList<URL> newPath = new ArrayList<>(curPath.size()); // get a list with the current classpath components for (URL onePath : curPath) { newPath.add(onePath); } curPath = newPath; for (String onestr : newPaths) { URL oneurl = urlFromPathString(onestr); if (oneurl != null && !curPath.contains(oneurl)) { curPath.add(oneurl); } } return new URLClassLoader(curPath.toArray(new URL[0]), loader); }
From source file:org.apache.hadoop.hive.ql.exec.Utilities.java
public static ClassLoader createUDFClassLoader(URLClassLoader loader, String[] newPaths) { final Set<URL> curPathsSet = Sets.newHashSet(loader.getURLs()); final List<URL> curPaths = Lists.newArrayList(curPathsSet); for (String onestr : newPaths) { final URL oneurl = urlFromPathString(onestr); if (oneurl != null && !curPathsSet.contains(oneurl)) { curPaths.add(oneurl);/*from w w w . ja v a2 s .co m*/ } } return new UDFClassLoader(curPaths.toArray(new URL[0]), loader); }
From source file:org.apache.hadoop.hive.ql.exec.Utilities.java
/** * remove elements from the classpath./*from www . j a va 2 s . c o m*/ * * @param pathsToRemove * Array of classpath elements */ public static void removeFromClassPath(String[] pathsToRemove) throws IOException { Thread curThread = Thread.currentThread(); URLClassLoader loader = (URLClassLoader) curThread.getContextClassLoader(); Set<URL> newPath = new HashSet<URL>(Arrays.asList(loader.getURLs())); for (String onestr : pathsToRemove) { URL oneurl = urlFromPathString(onestr); if (oneurl != null) { newPath.remove(oneurl); } } JavaUtils.closeClassLoader(loader); // This loader is closed, remove it from cached registry loaders to avoid removing it again. Registry reg = SessionState.getRegistry(); if (reg != null) { reg.removeFromUDFLoaders(loader); } loader = new UDFClassLoader(newPath.toArray(new URL[0])); curThread.setContextClassLoader(loader); SessionState.get().getConf().setClassLoader(loader); }
From source file:org.apache.hive.spark.client.SparkClientUtilities.java
/** * Add new elements to the classpath.//from w ww .j a v a 2 s . co m * * @param newPaths Map of classpath elements and corresponding timestamp * @return locally accessible files corresponding to the newPaths */ public static List<String> addToClassPath(Map<String, Long> newPaths, Configuration conf, File localTmpDir) throws Exception { URLClassLoader loader = (URLClassLoader) Thread.currentThread().getContextClassLoader(); List<URL> curPath = Lists.newArrayList(loader.getURLs()); List<String> localNewPaths = new ArrayList<>(); boolean newPathAdded = false; for (Map.Entry<String, Long> entry : newPaths.entrySet()) { URL newUrl = urlFromPathString(entry.getKey(), entry.getValue(), conf, localTmpDir); localNewPaths.add(newUrl.toString()); if (newUrl != null && !curPath.contains(newUrl)) { curPath.add(newUrl); LOG.info("Added jar[" + newUrl + "] to classpath."); newPathAdded = true; } } if (newPathAdded) { URLClassLoader newLoader = new URLClassLoader(curPath.toArray(new URL[curPath.size()]), loader); Thread.currentThread().setContextClassLoader(newLoader); } return localNewPaths; }
From source file:org.apache.ignite.spi.deployment.uri.GridUriDeploymentFileProcessor.java
/** * Cleanup class loaders resource.// w w w . java 2 s . com * * @param clsLdr Released class loader. * @param log Logger. */ static void cleanupUnit(ClassLoader clsLdr, IgniteLogger 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); } } }