Skip to content

Commit

Permalink
fix ExtManager after merging SPI and loaders #400
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Dec 8, 2023
1 parent 59fe48d commit a78e6af
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
22 changes: 21 additions & 1 deletion lphy-studio/src/main/java/lphystudio/app/manager/Dependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Data class maps to pom.xml {@code <dependency>}.
* @author Walter Xie
*/
public class Dependency {
public class Dependency implements Comparable<Dependency> {

private String groupId;
private String artifactId;
Expand Down Expand Up @@ -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;
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down
17 changes: 8 additions & 9 deletions lphy-studio/src/main/java/lphystudio/app/manager/ExtManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Extension> getLoadedLPhyExts() {
List<Extension> extList = new ArrayList<>();
public List<LPhyExtension> getLPhyExtensions() {
Set<LPhyExtension> extSet = new TreeSet<>();

// find all loaded lphy exts
for (lphy.core.spi.Extension ext : extensions) {
Expand All @@ -57,26 +57,25 @@ public List<Extension> 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<LPhyExtension> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ExtManagerPanel extends JPanel {
public ExtManagerPanel(ExtManager manager) {
this.manager = manager;

final List<Extension> extList = manager.getLoadedLPhyExts();
final List<LPhyExtension> extList = manager.getLPhyExtensions();
String dirStr = manager.getJarDirStr();
if (dirStr.trim().isEmpty())
System.err.println("Warning: no directory is found to store lphy extensions " + dirStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
*/
public class ExtManagerTableModel extends EasyTableModel {

public ExtManagerTableModel(List<Extension> extList) {
public ExtManagerTableModel(List<LPhyExtension> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
* 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;
private String website;

private List<Dependency> 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);

}
Expand Down Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lphy-studio/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a78e6af

Please sign in to comment.