ModulesPackage { protected static void sortByDependencies(Map<String, PackagedModule> modules) throws CycleDetectedException { if (modules.size() <= 1) { return; } Map<String, PackagedModule> copy = new LinkedHashMap<>(modules); DAG dag = new DAG(); Map<String, String> exports = new HashMap<>(); for (PackagedModule module : modules.values()) { Collection<String> exportPackages = getPackageNames(module, org.osgi.framework.Constants.EXPORT_PACKAGE); for (String exportPackage : exportPackages) { exports.put(exportPackage, module.getName()); } } for (PackagedModule module : modules.values()) { String moduleName = module.getName(); dag.addVertex(moduleName); for (String dependency : module.getDepends()) { if (modules.containsKey(dependency)) { dag.addEdge(moduleName, dependency); } } Collection<String> importPackages = getPackageNames(module, org.osgi.framework.Constants.IMPORT_PACKAGE); for (String importPackageName : importPackages) { if (exports.containsKey(importPackageName) && !exports.get(importPackageName).equals(moduleName) && !dag.hasEdge(moduleName, exports.get(importPackageName))) { dag.addEdge(moduleName, exports.get(importPackageName)); } } } @SuppressWarnings("unchecked") List<String> vertexes = TopologicalSorter.sort(dag); modules.clear(); for (String vertex : vertexes) { modules.put(vertex, copy.get(vertex)); } } private  ModulesPackage(JarFile jarFile); static ModulesPackage create(JarFile jarFile); String getName(); Map<String, PackagedModule> getModules(); String getDescription(); Version getVersion();  }
@Test public void testSortByDependencies() throws CycleDetectedException { Map<String, PackagedModule> modules = new LinkedHashMap<>(); Attributes attrs = new Attributes(); attrs.put(Constants.ATTR_NAME_JAHIA_DEPENDS, "default,moduleb"); PackagedModule pkg = new ModulesPackage.PackagedModule("modulec", attrs, null); modules.put(pkg.getName(), pkg); attrs = new Attributes(); attrs.put(Constants.ATTR_NAME_JAHIA_DEPENDS, "default,assets"); attrs.putValue(org.osgi.framework.Constants.IMPORT_PACKAGE, "org.jahia.modules.external;resolution:=optional;version=\"[3.0,4)\""); pkg = new ModulesPackage.PackagedModule("moduleb", attrs, null); modules.put(pkg.getName(), pkg); attrs = new Attributes(); attrs.put(Constants.ATTR_NAME_JAHIA_DEPENDS, "default,assets"); attrs.putValue(org.osgi.framework.Constants.EXPORT_PACKAGE, "org.jahia.modules.external;uses:=\"javax.jcr,javax.jcr.lock,javax.jcr.nodetype,javax.jcr.observation,javax.jcr.query,javax.jcr.query.qom," + "javax.jcr.retention,javax.jcr.security,javax.jcr.version,org.apache.jackrabbit.spi.commons.conversion,org.jahia.exceptions," + "org.jahia.modules.external.acl,org.jahia.services.content,org.jahia.services.content.nodetypes,org.jahia.utils.security," + "org.springframework.beans.factory,org.xml.sax\";version=\"3.1.1\",org.jahia.modules.external.query;uses:=\"javax.jcr" + ",javax.jcr.query,javax.jcr.query.qom,org.jahia.modules.external\";version=\"3.1.1\""); pkg = new ModulesPackage.PackagedModule("modulea", attrs, null); modules.put(pkg.getName(), pkg); ModulesPackage.sortByDependencies(modules); String[] keys = modules.keySet().toArray(new String[] {}); assertEquals("modulea", keys[0]); assertEquals("moduleb", keys[1]); assertEquals("modulec", keys[2]); }