/** * Returns true if f1 supersedes f2 */ private static boolean supersedes(Fmri f1, Fmri f2, String pAuth) { boolean f1Preferred = f1.getAuthority().equals(pAuth); boolean f2Preferred = f2.getAuthority().equals(pAuth); // If f1 is from the preferred authority and f2 is not, then // f1 supersedes. if (f1Preferred && ! f2Preferred) { return true; } // If f2 is from the preferred authority and f1 is not, then // f1 does not supersede if (f2Preferred && ! f1Preferred) { return false; } // Otherwise compare versions. f1 supersedes if it is a successor return f1.isSuccessor(f2); } public static void doGlassFishList(File cwd) throws java.lang.Exception { Image image = new Image(cwd); List installed = new ArrayList(); for (Image.FmriState each : image.getInventory(null, false)) { installed.add(each.fmri.getName()); } String pAuth = image.getPreferredAuthorityName(); SortedMap addOnMap = new TreeMap(); for (Image.FmriState each : image.getInventory(null, true)) { Fmri fmri = each.fmri; // If this exact package is installed, or another version // of this package is installed, then skip it. if (each.installed || installed.contains(fmri.getName())) { continue; } if (addOnMap.containsKey(fmri.getName())) { // We have seen this package name already. See if this // version should replace the saved version. Fmri saved = addOnMap.get(fmri.getName()); if (supersedes(fmri, saved, pAuth)) { addOnMap.put(fmri.getName(), fmri); } } else { // We haven't seen this package name yet. Save fmri addOnMap.put(fmri.getName(), fmri); } } for (Fmri f : addOnMap.values()) { System.out.println(f.toString() + ":" + f.getAuthority()); } }