From a78e6af064ed2802b995ad98528e623a74c99f6c Mon Sep 17 00:00:00 2001 From: walterxie Date: Fri, 8 Dec 2023 15:44:59 +1300 Subject: [PATCH] fix ExtManager after merging SPI and loaders #400 --- .../java/lphystudio/app/ExtManagerApp.java | 2 +- .../app/docgenerator/GenerateDocs.java | 2 +- .../lphystudio/app/manager/Dependency.java | 22 ++++++++++++++++++- .../app/manager/DependencyUtils.java | 14 +++++++----- .../lphystudio/app/manager/ExtManager.java | 17 +++++++------- .../app/manager/ExtManagerPanel.java | 2 +- .../app/manager/ExtManagerTableModel.java | 4 ++-- .../{Extension.java => LPhyExtension.java} | 8 +++---- .../lphystudio/app/manager/POMXMLHandler.java | 6 ++--- .../java/lphystudio/spi/LPhyStudioImpl.java | 2 +- lphy-studio/src/main/java/module-info.java | 2 +- 11 files changed, 51 insertions(+), 30 deletions(-) rename lphy-studio/src/main/java/lphystudio/app/manager/{Extension.java => LPhyExtension.java} (89%) diff --git a/lphy-studio/src/main/java/lphystudio/app/ExtManagerApp.java b/lphy-studio/src/main/java/lphystudio/app/ExtManagerApp.java index f0309f792..cf7963a49 100644 --- a/lphy-studio/src/main/java/lphystudio/app/ExtManagerApp.java +++ b/lphy-studio/src/main/java/lphystudio/app/ExtManagerApp.java @@ -11,7 +11,7 @@ */ public class ExtManagerApp extends JFrame { - public static final String APP_NAME = "Extension Manager"; + public static final String APP_NAME = "LPhyExtension Manager"; static { LPhyAppConfig.setupEcoSys(APP_NAME); } diff --git a/lphy-studio/src/main/java/lphystudio/app/docgenerator/GenerateDocs.java b/lphy-studio/src/main/java/lphystudio/app/docgenerator/GenerateDocs.java index 60271b421..077bd02fe 100644 --- a/lphy-studio/src/main/java/lphystudio/app/docgenerator/GenerateDocs.java +++ b/lphy-studio/src/main/java/lphystudio/app/docgenerator/GenerateDocs.java @@ -59,7 +59,7 @@ public static void main(String[] args) throws IOException { // Do not change default String extName = LPHY_DOC_TITLE; // for extension only, e.g. - // args = 0.0.5 "LPhy Extension Phylonco" phylonco.lphy.spi.Phylonco + // args = 0.0.5 "LPhy LPhyExtension Phylonco" phylonco.lphy.spi.Phylonco // set WD = ~/WorkSpace/beast-phylonco/PhyloncoL/doc if (args.length > 2) { extName = args[1]; diff --git a/lphy-studio/src/main/java/lphystudio/app/manager/Dependency.java b/lphy-studio/src/main/java/lphystudio/app/manager/Dependency.java index fc6215e5f..17489a068 100644 --- a/lphy-studio/src/main/java/lphystudio/app/manager/Dependency.java +++ b/lphy-studio/src/main/java/lphystudio/app/manager/Dependency.java @@ -6,7 +6,7 @@ * Data class maps to pom.xml {@code }. * @author Walter Xie */ -public class Dependency { +public class Dependency implements Comparable { private String groupId; private String artifactId; @@ -51,6 +51,26 @@ public String toString() { return groupId + ':' + artifactId + ':' + version; } + @Override + public int compareTo(Dependency other) { + // Handle null cases to avoid NullPointerException + if (other == null) { + return 1; // Consider non-null greater than null + } + + // Concatenate the fields for comparison + String thisKey = getDependencyId().toLowerCase(); + String otherKey = other.getDependencyId().toLowerCase(); + + // Compare the concatenated strings + return thisKey.compareTo(otherKey); + } + + public String getDependencyId() { + return groupId + artifactId + version; + } + + // public String getScope() { // return scope; // } diff --git a/lphy-studio/src/main/java/lphystudio/app/manager/DependencyUtils.java b/lphy-studio/src/main/java/lphystudio/app/manager/DependencyUtils.java index e4a4e4cd6..5ae584846 100644 --- a/lphy-studio/src/main/java/lphystudio/app/manager/DependencyUtils.java +++ b/lphy-studio/src/main/java/lphystudio/app/manager/DependencyUtils.java @@ -65,7 +65,7 @@ public static String getVersion(Class cls, String propertyKey) { // TODO retu // if not, then try pom.xml if (version == null) { try { - Extension lphyExt = getExtensionFrom(module); + LPhyExtension lphyExt = getExtensionFrom(module); if (lphyExt != null) { version = lphyExt.getVersion(); } @@ -89,10 +89,12 @@ public static String getVersion(Class cls, String propertyKey) { // TODO retu } /** - * @param module the Java {@link Module} contains the classes of the {@link Extension}. - * @return an {@link Extension} created from pom.xml, or from MANIFEST.MF, or from source + * This could extract the same LPhyExtension multiple times, + * because the Extension SPI can be multiple in the same LPhyExtension. + * @param module the Java {@link Module} contains the classes of the {@link LPhyExtension}. + * @return an {@link LPhyExtension} created from pom.xml, or from MANIFEST.MF, or from source */ - public static Extension getExtensionFrom(Module module) + public static LPhyExtension getExtensionFrom(Module module) throws IOException, ParserConfigurationException, SAXException { // 1. first look for pom.xml in jar InputStream in = module.getResourceAsStream(POM_XML_LOCATION); @@ -110,7 +112,7 @@ public static Extension getExtensionFrom(Module module) String version = attr.getValue("Implementation-Version"); System.out.println(", extract meta-data from MANIFEST.MF"); - return new Extension("", name, version); + return new LPhyExtension("", name, version); } } @@ -141,7 +143,7 @@ public static Extension getExtensionFrom(Module module) // 4. unknown if (in == null) { System.err.println("Warning : cannot find extension information from module " + module.getName() + " !"); - return new Extension("", module.getName(), "unknown"); + return new LPhyExtension("", module.getName(), "unknown"); } // String pom = new String(in.readAllBytes(), StandardCharsets.UTF_8); // System.out.println(pom); diff --git a/lphy-studio/src/main/java/lphystudio/app/manager/ExtManager.java b/lphy-studio/src/main/java/lphystudio/app/manager/ExtManager.java index b2c25221d..3389f7cf0 100644 --- a/lphy-studio/src/main/java/lphystudio/app/manager/ExtManager.java +++ b/lphy-studio/src/main/java/lphystudio/app/manager/ExtManager.java @@ -37,10 +37,10 @@ public ExtManager() { /** * Find loaded LPhy extensions from loaded modular jars. - * @return a list of {@link Extension} + * @return a list of {@link LPhyExtension} */ - public List getLoadedLPhyExts() { - List extList = new ArrayList<>(); + public List getLPhyExtensions() { + Set extSet = new TreeSet<>(); // find all loaded lphy exts for (lphy.core.spi.Extension ext : extensions) { @@ -57,26 +57,25 @@ public List getLoadedLPhyExts() { // use module path if (module != null) { try { - Extension lphyExt = DependencyUtils.getExtensionFrom(module); - extList.add(lphyExt); + LPhyExtension lphyExt = DependencyUtils.getExtensionFrom(module); + extSet.add(lphyExt); } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace(); } } else { // use class path // URL jarURL = cls.getResource(pkgNm); // jarURL = cls.getResource(pkgNm); -// Extension lphyExt = new Extension(jarURL); +// LPhyExtension lphyExt = new LPhyExtension(jarURL); // extList.add(lphyExt); throw new UnsupportedOperationException("Do not support class path ! Please use module path."); } } - // sort by ext artifactId - extList.sort(Comparator.comparing(Extension::getArtifactId)); + List extList = new ArrayList<>(extSet); // pin lphy on the top for (int i = 0; i < extList.size(); i++) { - Extension ext = extList.get(i); + LPhyExtension ext = extList.get(i); if (LPHY_ID.equalsIgnoreCase(ext.getArtifactId())) { extList.add(0, extList.remove(i)); } diff --git a/lphy-studio/src/main/java/lphystudio/app/manager/ExtManagerPanel.java b/lphy-studio/src/main/java/lphystudio/app/manager/ExtManagerPanel.java index 541886c0b..cdad69161 100644 --- a/lphy-studio/src/main/java/lphystudio/app/manager/ExtManagerPanel.java +++ b/lphy-studio/src/main/java/lphystudio/app/manager/ExtManagerPanel.java @@ -16,7 +16,7 @@ public class ExtManagerPanel extends JPanel { public ExtManagerPanel(ExtManager manager) { this.manager = manager; - final List extList = manager.getLoadedLPhyExts(); + final List extList = manager.getLPhyExtensions(); String dirStr = manager.getJarDirStr(); if (dirStr.trim().isEmpty()) System.err.println("Warning: no directory is found to store lphy extensions " + dirStr); diff --git a/lphy-studio/src/main/java/lphystudio/app/manager/ExtManagerTableModel.java b/lphy-studio/src/main/java/lphystudio/app/manager/ExtManagerTableModel.java index 5735ed97d..188bfccea 100644 --- a/lphy-studio/src/main/java/lphystudio/app/manager/ExtManagerTableModel.java +++ b/lphy-studio/src/main/java/lphystudio/app/manager/ExtManagerTableModel.java @@ -9,13 +9,13 @@ */ public class ExtManagerTableModel extends EasyTableModel { - public ExtManagerTableModel(List extList) { + public ExtManagerTableModel(List extList) { super(new String[]{"ID", "GroupID", "Installed", "Dependencies", "Description"}, extList); } @Override public Object getValueAt(int row, int col) { - Extension ext = (Extension) dataList.get(row); + LPhyExtension ext = (LPhyExtension) dataList.get(row); return switch (col) { case 0 -> ext.getArtifactId(); case 1 -> ext.getGroupId(); diff --git a/lphy-studio/src/main/java/lphystudio/app/manager/Extension.java b/lphy-studio/src/main/java/lphystudio/app/manager/LPhyExtension.java similarity index 89% rename from lphy-studio/src/main/java/lphystudio/app/manager/Extension.java rename to lphy-studio/src/main/java/lphystudio/app/manager/LPhyExtension.java index 2c427545f..1aa9964be 100644 --- a/lphy-studio/src/main/java/lphystudio/app/manager/Extension.java +++ b/lphy-studio/src/main/java/lphystudio/app/manager/LPhyExtension.java @@ -9,7 +9,7 @@ * metadata only for the extension. * @author Walter Xie */ -public class Extension extends Dependency { +public class LPhyExtension extends Dependency { private String name; // may be diff to artifactId private String desc; @@ -17,11 +17,11 @@ public class Extension extends Dependency { private List dependencies = new ArrayList<>(); - public Extension() { + public LPhyExtension() { super(); } - public Extension(String groupId, String artifactId, String version) { + public LPhyExtension(String groupId, String artifactId, String version) { super(groupId, artifactId, version); } @@ -67,7 +67,7 @@ public void setWebsite(String website) { // * @param jarURL * @throws FileNotFoundException @Deprecated - public Extension(URL jarURL) throws FileNotFoundException { + public LPhyExtension(URL jarURL) throws FileNotFoundException { String jarLoc = Objects.requireNonNull(jarURL).toString(); if (!jarLoc.startsWith("jar:file:") || !jarLoc.contains(".jar")) diff --git a/lphy-studio/src/main/java/lphystudio/app/manager/POMXMLHandler.java b/lphy-studio/src/main/java/lphystudio/app/manager/POMXMLHandler.java index 6efc0290e..fd0d3c2aa 100644 --- a/lphy-studio/src/main/java/lphystudio/app/manager/POMXMLHandler.java +++ b/lphy-studio/src/main/java/lphystudio/app/manager/POMXMLHandler.java @@ -20,7 +20,7 @@ public class POMXMLHandler extends DefaultHandler { public static final String DEPD_LIST = "dependencies"; public static final String SCOPE = "scope"; - private Extension extension; + private LPhyExtension extension; private StringBuffer currVal = new StringBuffer(); // same tags (e.g. groupId, artifactId) in dependencies @@ -30,14 +30,14 @@ public class POMXMLHandler extends DefaultHandler { private Dependency currDepd; - public Extension getExtension() { + public LPhyExtension getExtension() { // System.out.println(extension.getArtifactId()); return extension; } @Override public void startDocument() { - extension = new Extension(); + extension = new LPhyExtension(); // extension artifactId in the beginning of XML isDependency = false; dependencies = new ArrayList<>(); diff --git a/lphy-studio/src/main/java/lphystudio/spi/LPhyStudioImpl.java b/lphy-studio/src/main/java/lphystudio/spi/LPhyStudioImpl.java index 996f303a2..bdf4365bf 100644 --- a/lphy-studio/src/main/java/lphystudio/spi/LPhyStudioImpl.java +++ b/lphy-studio/src/main/java/lphystudio/spi/LPhyStudioImpl.java @@ -9,7 +9,7 @@ /** * TODO: it is unused now, overwrite the register() when any new classes being to add. - * Empty class to show studio ext in the Extension Manager. + * Empty class to show studio ext in the LPhyExtension Manager. * @author Walter Xie */ public class LPhyStudioImpl extends LPhyCoreImpl { //} implements LPhyExtension { diff --git a/lphy-studio/src/main/java/module-info.java b/lphy-studio/src/main/java/module-info.java index 75aee7923..caf1ebf0a 100644 --- a/lphy-studio/src/main/java/module-info.java +++ b/lphy-studio/src/main/java/module-info.java @@ -21,7 +21,7 @@ exports lphystudio.core.valueeditor; exports lphystudio.core.theme; - // Both are empty now, but must be declared in order to show studio ext in the Extension Manager. + // Both are empty now, but must be declared in order to show studio ext in the LPhyExtension Manager. // LPhy extensions uses lphy.core.spi.Extension; // declare what service interface the provider intends to use