public static void main(String[] args) { try { ServerConfigHelper.initConfig(); ServerConfigHelper.initLog4j("log4j-test.xml"); JpaContext.init(); JarFileService srv = JpaContext.getService(JarFileService.class); ReleasedJarFile ff= srv.getFileById(6l); URL url =new URL("mem", null, 0, ff.getFileName(), new BytesURLStreamHandler(ff.getContent())); // GemliteDeployer.getInstance().deploy(url); URLClassPath ucp = new URLClassPath(new URL[]{url}); Object obj =ucp.getResource("gemlite.core.internal.GemliteBuilder"); System.out.println(obj); } catch (MalformedURLException e) { e.printStackTrace(); } }
/** * Finds and loads the class with the specified name from the URL search * path. Any URLs referring to JAR files are loaded and opened as needed * until the class is found. * * @param name the name of the class * @return the resulting class * @throws ClassNotFoundException if the class could not be found, * or if the loader is closed. * @throws NullPointerException if {@code name} is {@code null}. */ @Override @SneakyThrows(ReflectiveOperationException.class) protected Class<?> findClass(String name) throws ClassNotFoundException { String path = name.replace('.', '/') + ".class"; Field ucpField = URLClassLoader.class.getDeclaredField("ucp"); ucpField.setAccessible(true); Resource res = ((URLClassPath) ucpField.get(this)).getResource(path, false); if (res != null) { try { return defineClass(name, res); } catch (IOException e) { throw new ClassNotFoundException(name, e); } } else { throw new ClassNotFoundException(name); } }
/** * @param contents Reference to the JarContents object. * @param parent * @param factory ASMFactory. */ public JarClassLoader(LocateableJarContents<ClassNode> contents, ClassLoader parent, ASMFactory<ClassNode> factory) { this.factory = factory; ClassLoader _parent = parent; if (_parent == null) _parent = getClass().getClassLoader(); myParent = _parent; cache = new HashMap<String, Class<?>>(); fastNodeCache = new HashMap<String, ClassNode>(); ucp = new URLClassPath(new URL[0]); add(contents); StackTraceElement e = creator(false); System.err.println( String.format("Creating new JarClassLoader, parent ClassLoader: %s of %s.%s:%d", myParent, e.getClassName(), e.getMethodName(), e.getLineNumber())); }
public static void removeURL(URL url) { try { Field f = URLClassLoader.class.getDeclaredField("ucp"); f.setAccessible(true); URL[] old = ((URLClassLoader)ClassLoader.getSystemClassLoader()).getURLs(); URL[] newurls = new URL[old.length - 1]; for (int i = 0, j = 0; i < old.length; ++i) { if (!old[i].equals(url)) { newurls[j] = old[i]; ++j; } } f.set(ClassLoader.getSystemClassLoader(), new URLClassPath(newurls)); } catch (Throwable t) { throw new RuntimeException("Could not remove " + url + " from the classpath."); } }
private SandboxLoader doGetSubLoaderByClassContext(String clazz) { String path = clazz.replace('.', '/').concat(".class"); for(Entry<URLClassPath, SandboxLoader> e : subLoaderByJar.entrySet()){ Resource res = e.getKey().getResource(path, false); if (res != null) return e.getValue(); } SandboxLoader subLoader = subLoaderCache.get(clazz); if(null != subLoader) return subLoader; for(String prefix : subLoaderPrefixCache.keySet()) if(clazz.startsWith(prefix)) return subLoaderPrefixCache.get(prefix); return null; }
URLClassLoader(URL[] urls, ClassLoader parent, AccessControlContext acc) { super(parent); // this is to make the stack depth consistent with 1.1 SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkCreateClassLoader(); } ucp = new URLClassPath(urls); this.acc = acc; }
URLClassLoader(URL[] urls, AccessControlContext acc) { super(); // this is to make the stack depth consistent with 1.1 SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkCreateClassLoader(); } ucp = new URLClassPath(urls); this.acc = acc; }
URLClassLoader(URL[] urls, ClassLoader parent, AccessControlContext acc) { super(parent); // this is to make the stack depth consistent with 1.1 SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkCreateClassLoader(); } this.acc = acc; ucp = new URLClassPath(urls, acc); }
URLClassLoader(URL[] urls, AccessControlContext acc) { super(); // this is to make the stack depth consistent with 1.1 SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkCreateClassLoader(); } this.acc = acc; ucp = new URLClassPath(urls, acc); }
private void deleteMod(File mod) { if (mod.delete()) return; try { ClassLoader cl = DepLoader.class.getClassLoader(); URL url = mod.toURI().toURL(); Field f_ucp = URLClassLoader.class.getDeclaredField("ucp"); Field f_loaders = URLClassPath.class.getDeclaredField("loaders"); Field f_lmap = URLClassPath.class.getDeclaredField("lmap"); f_ucp.setAccessible(true); f_loaders.setAccessible(true); f_lmap.setAccessible(true); URLClassPath ucp = (URLClassPath) f_ucp.get(cl); Closeable loader = ((Map<String, Closeable>) f_lmap.get(ucp)).remove(URLUtil.urlNoFragString(url)); if (loader != null) { loader.close(); ((List<?>) f_loaders.get(ucp)).remove(loader); } } catch (Exception e) { e.printStackTrace(); } if (!mod.delete()) { mod.deleteOnExit(); String msg = owner + " was unable to delete file " + mod.getPath() + " the game will now try to delete it on exit. If this dialog appears again, delete it manually."; System.err.println(msg); if (!GraphicsEnvironment.isHeadless()) JOptionPane.showMessageDialog(null, msg, "An update error has occured", JOptionPane.ERROR_MESSAGE); System.exit(1); } }
/** * Uses reflection to replace all white spaces in the ClassLoader's URLs to %20. */ static void changeClassLoaderToSupportWhiteSpacesRepository(ClassLoader mavenClassLoader) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, MalformedURLException { // Get the class of the ClassLoader, this should be RealmClassLoader. Class mavenClassLoaderClass = mavenClassLoader.getClass(); // Get the super class of RealmClassLoader, this should be URLClassLoader. Class clazz2 = mavenClassLoaderClass.getSuperclass(); // get the ucp field of URLClassLoader that holds the URLs Field ucpField = clazz2.getDeclaredField("ucp"); // make this field accessible if (!ucpField.isAccessible()) { ucpField.setAccessible(true); } // get the value of the field, this should be URLClassPath. Object value = ucpField.get(mavenClassLoader); // get the URLs URLClassPath urlClassPath = (URLClassPath) value; URL[] urls = urlClassPath.getURLs(); URL[] urls2 = new URL[urls.length]; // replace the white spaces of all URLs with %20 for (int i = 0; i < urls.length; i++) { String path = urls[i].toExternalForm(); path = path.replaceAll(" ", "%20"); urls2[i] = new URL(path); } // create the new URLClassPath URLClassPath uRLClassPath2 = new URLClassPath(urls2); // update the ucf field value ucpField.set(mavenClassLoader, uRLClassPath2); }
/** * @param urls * @param parent * @param factory */ public HijackingClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) { super(urls, parent, factory); ucp = new URLClassPath(urls, factory); acc = AccessController.getContext(); }
/** * 加入新的來源位址到開頭。 * <p> * 依賴{@link URLClassLoader}及{@link URLClassPath}內部結構,驗證Java版本: * <ul> * <li>Oracle Java SE 8u20</li> * </ul> * </p> * * @param classLoader 要加入新位址的類別載入器 * @param newURL 新的類別來源位址 */ public static void insertURL(URLClassLoader classLoader, URL newURL) { synchronized (classLoader) { try { // Modified from http://pastebin.com/SNgmGMwq URLClassPath ucp = (URLClassPath) URLClassLoader_ucpField.get(classLoader); ucp.addURL(newURL); @SuppressWarnings("unchecked") List<URL> path = (List<URL>) URLClassPath_pathField.get(ucp); @SuppressWarnings("unchecked") List<Object> loaders = (List<Object>) URLClassPath_loadersField.get(ucp); // 強制建立loader (要將loader排到首位) URLClassPath_getLoaderMethod.invoke(ucp, path.size() - 1); if (path.size() != loaders.size()) { throw new RuntimeException("Ehh... they should be same size!!"); } else { int lastIndex = path.size() - 1; path.add(0, path.remove(lastIndex)); loaders.add(0, loaders.remove(lastIndex)); } } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } } /* for (URL p : path) { System.out.println("ext path: " + p); } for (Object l : loaders) { URL url = (URL) URLClassPath_Loader_getBaseURLMethod.invoke(l); System.out.println("ext loader: " + url); } */ }
@SuppressWarnings("unchecked") public static boolean deleteFile(File file) { if (file.delete()) { return true; } try { ClassLoader cl = RemoveHandler.class.getClassLoader(); URL url = file.toURI().toURL(); Field f_ucp = URLClassLoader.class.getDeclaredField("ucp"); Field f_loaders = URLClassPath.class.getDeclaredField("loaders"); Field f_lmap = URLClassPath.class.getDeclaredField("lmap"); f_ucp.setAccessible(true); f_loaders.setAccessible(true); f_lmap.setAccessible(true); URLClassPath ucp = (URLClassPath) f_ucp.get(cl); Closeable loader = ((Map<String, Closeable>) f_lmap.get(ucp)).remove(URLUtil.urlNoFragString(url)); if (loader != null) { loader.close(); ((List<?>) f_loaders.get(ucp)).remove(loader); } } catch (Exception e) { e.printStackTrace(); } return file.delete(); }
/** *Sets up variables used in closing a loader's jar files. *@throws NoSuchFieldException in case a field of interest is not found where expected */ private static void initForClosingJars() throws NoSuchFieldException { jcpField = getField(URLClassLoader.class, URLCLASSLOADER_UCP_FIELD_NAME); loadersField = getField(URLClassPath.class, URLCLASSPATH_LOADERS_FIELD_NAME); urlsField = getField(URLClassPath.class, URLCLASSPATH_URLS_FIELD_NAME); lmapField = getField(URLClassPath.class, URLCLASSPATH_LMAP_FIELD_NAME); pathField = getField(URLClassPath.class, URLCLASSPATH_PATH_FIELD_NAME); jarLoaderInnerClass = getInnerClass(URLClassPath.class, URLCLASSPATH_JARLOADER_INNER_CLASS_NAME); jarFileField = getField(jarLoaderInnerClass, URLCLASSPATH_JARLOADER_JARFILE_FIELD_NAME); }
public ReloadableClassLoader(URL[] urls, String classesDir) throws IOException, ClassNotFoundException { super(urls, ReloadableClassLoader.class.getClassLoader()); this.ucp = new URLClassPath(urls); this.classesDir = classesDir; List<Path> paths = FileMatcher.match(classesDir, "glob:*.class"); logger.trace("Loading classes: ", paths); for (Path path : paths) { loadClass(normalizeClassname(path.toString())); } }