Skip to content

Commit

Permalink
add initial version of MavenClassLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Jul 16, 2024
1 parent 30e4c35 commit e340fb0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
25 changes: 25 additions & 0 deletions core/src/main/java/lucee/commons/io/res/util/MavenClassLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lucee.commons.io.res.util;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import lucee.runtime.mvn.POM;

public class MavenClassLoader extends ResourceClassLoader {

private static Map<String, MavenClassLoader> instances = new ConcurrentHashMap<>();

public MavenClassLoader(POM pom, ClassLoader parent) throws IOException {
super(pom.getJars(), parent);
}

public static MavenClassLoader getInstance(POM pom, ClassLoader parent) throws IOException {
MavenClassLoader mcl = instances.get(pom.hash());
if (mcl == null) {
mcl = new MavenClassLoader(pom, parent);
instances.put(pom.hash(), mcl);
}
return mcl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/**
* Classloader that load classes from resources
*/
public final class ResourceClassLoader extends URLClassLoader implements Closeable {
public class ResourceClassLoader extends URLClassLoader implements Closeable {

private List<Resource> resources = new ArrayList<Resource>();
private Map<String, SoftReference<ResourceClassLoader>> customCLs;
Expand Down
38 changes: 34 additions & 4 deletions core/src/main/java/lucee/runtime/mvn/POM.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public class POM {
public static final int SCOPE_ALL = SCOPE_NOT_TEST + SCOPE_TEST;

private Resource localDirectory;
private String groupId;
private String artifactId;
private String version;
private final String groupId;
private final String artifactId;
private final String version;
private String scope;
private String optional;
private int dependencyScope = SCOPE_ALL;
Expand Down Expand Up @@ -76,6 +76,8 @@ public class POM {

private String artifactExtension;

private String hash;

public static POM getInstance(Resource localDirectory, String groupId, String version, String artifactId, Log log) {
return getInstance(localDirectory, null, groupId, artifactId, version, null, null, SCOPE_NOT_TEST, SCOPE_ALL, log);
}
Expand Down Expand Up @@ -323,7 +325,14 @@ private StringBuilder _hash(StringBuilder sb) throws IOException {
}

public String hash() throws IOException {
return HashUtil.create64BitHashAsString(_hash(new StringBuilder()));
if (hash == null) {
synchronized (groupId) {
if (hash == null) {
hash = HashUtil.create64BitHashAsString(_hash(new StringBuilder()));
}
}
}
return hash;
}

Resource getArtifact(String type) {
Expand Down Expand Up @@ -514,4 +523,25 @@ private static TreeNode<POM> getParents(POM pom, TreeNode<POM> parents) throws I
}
return parents;
}

public Resource[] getJars() throws IOException {
List<Resource> jars = new ArrayList<>();

// current
if ("jar".equalsIgnoreCase(this.artifactExtension)) {
Resource r = getArtifact();
if (r != null) jars.add(r);
}

List<POM> dependencies = getAllDependencies();
if (dependencies != null) {
for (POM p: dependencies) {
if ("jar".equalsIgnoreCase(p.artifactExtension)) {
Resource r = p.getArtifact();
if (r != null) jars.add(r);
}
}
}
return jars.toArray(new Resource[jars.size()]);
}
}

0 comments on commit e340fb0

Please sign in to comment.