public static Map<Package, ClassLoader[]> getPackageMap( List<ClassLoader> classLoaders, Set<String> ignorePackages) { Map<Package, ClassLoader[]> answer = new HashMap<Package, ClassLoader[]>(); ClassLoader[] globalClassLoaders = { Thread.currentThread().getContextClassLoader(), ClassScanner.class.getClassLoader() }; Set<Package> packages = new HashSet<Package>(); add(answer, Package.getPackages(), globalClassLoaders, ignorePackages); ClassLoader[] classLoaderArray = new ClassLoader[classLoaders.size()]; classLoaders.toArray(classLoaderArray); for (ClassLoader classLoader : classLoaders) { Package[] loaderPackages = findPackagesForClassLoader(classLoader); add(answer, loaderPackages, classLoaderArray, ignorePackages); } SortedSet<String> names = new TreeSet<String>(); for (Package aPackage : packages) { names.add(aPackage.getName()); } for (String name : names) { LOG.info("Got package " + name); } return answer; }
/** @tests java.lang.Package#getPackages() */ public void test_getPackages() throws Exception { Package[] pckgs = Package.getPackages(); boolean found = false; for (int i = 0; i < pckgs.length; i++) { if (pckgs[i].getName().equals("java.util")) { found = true; break; } } assertTrue("Package getPackages failed to retrieve a package", found); }
public static void main(String[] args) { Package pckgs[]; pckgs = Package.getPackages(); for (Package pckg : pckgs) { System.out.println( pckg.getName() + " " + pckg.getImplementationTitle() + " " + pckg.getImplementationVendor() + " " + pckg.getImplementationVersion()); } }
/** * @param args * @return */ private static void parseCommandLine(RunTimeData anRtData, String[] args) { Trace.println(Trace.UTIL, "parseCommandLine( runTimeData, args )", true); CmdLineParser cmdLine = new CmdLineExecutionParser(APPLICATIONNAME); cmdLine.setDefaultCommand("execute"); cmdLine.acceptFlag(Testium.VERSION, "Displays the versions of Testium and the plugins"); try { cmdLine.parse(anRtData, args); } catch (ParameterException pe) { Trace.print(Trace.UTIL, pe); cmdLine.printHelpOn(System.out); throw new Error("Error on command line.", pe); } if (anRtData.getValueAsBoolean(Testium.HELP)) { cmdLine.printHelpOn(System.out); System.exit(0); } if (anRtData.getValueAsBoolean(Testium.VERSION)) { System.out.println("The version of Testium"); Package[] packages = Package.getPackages(); for (Package pkg : packages) { String pkgName = pkg.getName(); if (!pkgName.startsWith("java") && !pkgName.startsWith("sun") && !pkgName.startsWith("joptsimple") && !pkgName.startsWith("org.xml")) { System.out.println(pkgName + ":\t" + pkg.getImplementationVersion()); } } System.exit(0); } }
static { /* This resolves a know deadlock that can occur if one thread is in the process of defining a package as part of defining a class, and another thread is defining the system package that can result in loading a class. One holds the Package.pkgs lock and one holds the Classloader lock. */ Package.getPackages(); String locklessDefault = "false"; try { Class.forName("sun.misc.Unsafe", false, null); locklessDefault = AccessController.doPrivileged(new PropertyReadAction("java.vm.name", "")) .toUpperCase(Locale.US) .contains("JROCKIT") ? "false" : "true"; } catch (Throwable t) { // ignored } LOCKLESS = Boolean.parseBoolean( AccessController.doPrivileged( new PropertyReadAction("jboss.modules.lockless", locklessDefault))); }
@Override public void run() { /* This resolves a know deadlock that can occur if one thread is in the process of defining a package as part of defining a class, and another thread is defining the system package that can result in loading a class. One holds the Package.pkgs lock and one holds the Classloader lock. */ Package.getPackages(); final Queue<LoadRequest> queue = LoaderThreadHolder.REQUEST_QUEUE; for (; ; ) { try { LoadRequest request; synchronized (queue) { while ((request = queue.poll()) == null) { queue.wait(); } } final ModuleClassLoader loader = request.requester; Class<?> result = null; synchronized (loader) { try { result = loader.performLoadClass(request.className, request.exportsOnly); } finally { // no matter what, the requester MUST be notified request.result = result; request.done = true; loader.notifyAll(); } } } catch (Throwable t) { // ignore } } }
/** Returns all the classes found in a sorted map */ public SortedMap<String, Class<?>> getAllClassesMap() { Package[] packages = Package.getPackages(); return getClassesMap(packages); }