diff --git a/core/src/main/java/lucee/runtime/mvn/MavenUtil.java b/core/src/main/java/lucee/runtime/mvn/MavenUtil.java index d7ce88816c..998cf90657 100644 --- a/core/src/main/java/lucee/runtime/mvn/MavenUtil.java +++ b/core/src/main/java/lucee/runtime/mvn/MavenUtil.java @@ -11,6 +11,9 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Properties; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; @@ -21,7 +24,9 @@ import lucee.print; import lucee.commons.io.IOUtil; +import lucee.commons.io.log.Log; import lucee.commons.io.res.Resource; +import lucee.commons.lang.ExceptionUtil; import lucee.commons.lang.SerializableObject; import lucee.runtime.mvn.POMReader.Dependency; import lucee.runtime.type.util.ListUtil; @@ -98,10 +103,12 @@ public static Collection getRepositories(List } public static List getDependencies(List rawDependencies, POM current, POM parent, Map properties, Resource localDirectory, - boolean management) throws IOException { + boolean management, Log log) throws IOException { List dependencies = new ArrayList<>(); List parentDendencyManagement = null; + ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + if (parent != null) { parentDendencyManagement = current.getDependencyManagement(); for (POM pom: parent.getDependencies()) { @@ -109,15 +116,29 @@ public static List getDependencies(List rawDependenci } } if (rawDependencies != null) { + List> futures = new ArrayList<>(); for (POMReader.Dependency rd: rawDependencies) { GAVSO gavso = getDependency(rd, parent, current, properties, parentDendencyManagement, management); if (gavso == null) continue; - POM p = POM.getInstance(localDirectory, current.getRepositories(), gavso.g, gavso.a, gavso.v, gavso.s, gavso.o, current.getDependencyScope(), - current.getDependencyScopeManagement()); - dependencies.add(p); + Future future = executor.submit(() -> { + POM p = POM.getInstance(localDirectory, current.getRepositories(), gavso.g, gavso.a, gavso.v, gavso.s, gavso.o, current.getDependencyScope(), + current.getDependencyScopeManagement(), log); + p.initXML(); + return p; + }); + futures.add(future); + } + try { + for (Future future: futures) { + dependencies.add(future.get()); // Wait for init to complete + } + } + catch (Exception e) { + throw ExceptionUtil.toIOException(e); } } + executor.shutdown(); return dependencies; } @@ -214,8 +235,8 @@ private static POM getDendency(List dependencies, String groupId, String ar return null; } - public static List getDependencyManagement(List rawDependencies, POM current, POM parent, Map properties, Resource localDirectory) - throws IOException { + public static List getDependencyManagement(List rawDependencies, POM current, POM parent, Map properties, Resource localDirectory, + Log log) throws IOException { List dependencies = new ArrayList<>(); @@ -233,7 +254,7 @@ public static List getDependencyManagement(List rawDe GAVSO gavso = getDependency(rd, parent, current, properties, null, true); if (gavso == null) continue; POM p = POM.getInstance(localDirectory, current.getRepositories(), gavso.g, gavso.a, gavso.v, gavso.s, gavso.o, current.getDependencyScope(), - current.getDependencyScopeManagement()); + current.getDependencyScopeManagement(), log); dependencies.add(p); } } @@ -300,13 +321,14 @@ public static boolean hasPlaceholders(String str) { return str != null && str.indexOf("${") != -1; } - public static void downloadPOM(POM pom, Collection repositories) throws IOException { - Resource res = pom.getPath(); + public static void download(POM pom, Collection repositories, String type, Log log) throws IOException { + Resource res = pom.getArtifact(type); if (!res.isFile()) { - URL url = pom.getArtifact("pom", repositories); - print.o("download:" + url); + URL url = pom.getArtifact(type, repositories); + print.e("download:" + url); + if (log != null) log.info("maven", "download [" + url + "]"); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - HttpGet request = new HttpGet(pom.getArtifact("pom", repositories).toExternalForm()); + HttpGet request = new HttpGet(pom.getArtifact(type, repositories).toExternalForm()); HttpResponse response = httpClient.execute(request); HttpEntity entity = response.getEntity(); int sc = response.getStatusLine().getStatusCode(); @@ -326,7 +348,7 @@ public static void downloadPOM(POM pom, Collection repositories) thr } public static POM toPOM(Resource localDirectory, Collection repositories, POMReader.Dependency dependency, Map properties, int dependencyScope, - int dependencyScopeManagement) throws IOException { + int dependencyScopeManagement, Log log) throws IOException { return POM.getInstance(localDirectory, repositories, @@ -338,7 +360,9 @@ public static POM toPOM(Resource localDirectory, Collection reposito null, null, - dependencyScope, dependencyScopeManagement + dependencyScope, dependencyScopeManagement, + + log ); } diff --git a/core/src/main/java/lucee/runtime/mvn/POM.java b/core/src/main/java/lucee/runtime/mvn/POM.java index d213e7024b..10bdfc2976 100644 --- a/core/src/main/java/lucee/runtime/mvn/POM.java +++ b/core/src/main/java/lucee/runtime/mvn/POM.java @@ -12,8 +12,10 @@ import org.xml.sax.SAXException; import lucee.print; +import lucee.commons.io.log.Log; import lucee.commons.io.res.Resource; import lucee.commons.lang.ExceptionUtil; +import lucee.commons.lang.SerializableObject; import lucee.commons.tree.TreeNode; import lucee.runtime.mvn.POMReader.Dependency; import lucee.runtime.op.Caster; @@ -65,41 +67,48 @@ public class POM { private String description; private String url; + private Object token = new SerializableObject(); + private POMReader reader; - public static POM getInstance(Resource localDirectory, String groupId, String version, String artifactId) { - return getInstance(localDirectory, null, groupId, artifactId, version, null, null, SCOPE_NOT_TEST, SCOPE_ALL); + private Log log; + + private String artifactExtension; + + 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); } - public static POM getInstance(Resource localDirectory, Collection repositories, String groupId, String version, String artifactId) { - return getInstance(localDirectory, repositories, groupId, artifactId, version, null, null, SCOPE_NOT_TEST, SCOPE_ALL); + public static POM getInstance(Resource localDirectory, Collection repositories, String groupId, String version, String artifactId, Log log) { + return getInstance(localDirectory, repositories, groupId, artifactId, version, null, null, SCOPE_NOT_TEST, SCOPE_ALL, log); } - public static POM getInstance(Resource localDirectory, String groupId, String artifactId, String version, int dependencyScope) { - return getInstance(localDirectory, null, groupId, artifactId, version, null, null, dependencyScope, POM.SCOPE_ALL); + public static POM getInstance(Resource localDirectory, String groupId, String artifactId, String version, int dependencyScope, Log log) { + return getInstance(localDirectory, null, groupId, artifactId, version, null, null, dependencyScope, POM.SCOPE_ALL, log); } - public static POM getInstance(Resource localDirectory, Collection repositories, String groupId, String artifactId, String version, int dependencyScope) { - return getInstance(localDirectory, repositories, groupId, artifactId, version, null, null, dependencyScope, POM.SCOPE_ALL); + public static POM getInstance(Resource localDirectory, Collection repositories, String groupId, String artifactId, String version, int dependencyScope, Log log) { + return getInstance(localDirectory, repositories, groupId, artifactId, version, null, null, dependencyScope, POM.SCOPE_ALL, log); } public static POM getInstance(Resource localDirectory, Collection repositories, String groupId, String artifactId, String version, int dependencyScope, - int dependencyScopeManagement) { - return getInstance(localDirectory, repositories, groupId, artifactId, version, null, null, dependencyScope, dependencyScopeManagement); + int dependencyScopeManagement, Log log) { + return getInstance(localDirectory, repositories, groupId, artifactId, version, null, null, dependencyScope, dependencyScopeManagement, log); } - public static POM getInstance(Resource localDirectory, String groupId, String artifactId, String version, int dependencyScope, int dependencyScopeManagement) { - return getInstance(localDirectory, null, groupId, artifactId, version, null, null, dependencyScope, dependencyScopeManagement); + public static POM getInstance(Resource localDirectory, String groupId, String artifactId, String version, int dependencyScope, int dependencyScopeManagement, Log log) { + return getInstance(localDirectory, null, groupId, artifactId, version, null, null, dependencyScope, dependencyScopeManagement, log); } public static POM getInstance(Resource localDirectory, Collection repositories, String groupId, String artifactId, String version, String scope, String optional, - int dependencyScope, int dependencyScopeManagement) { + int dependencyScope, int dependencyScopeManagement, Log log) { String id = toId(localDirectory, groupId, artifactId, version, scope, optional, dependencyScope, dependencyScopeManagement); POM pom = cache.get(id); if (pom != null) { return pom; } - pom = new POM(localDirectory, repositories, groupId, artifactId, version, scope, optional, dependencyScope, dependencyScopeManagement); + + pom = new POM(localDirectory, repositories, groupId, artifactId, version, scope, optional, dependencyScope, dependencyScopeManagement, log); cache.put(id, pom); return pom; } @@ -111,7 +120,7 @@ private static String toId(Resource localDirectory, String groupId, String artif } private POM(Resource localDirectory, Collection repositories, String groupId, String artifactId, String version, String scope, String optional, int dependencyScope, - int dependencyScopeManagement) { + int dependencyScopeManagement, Log log) { if (groupId == null) throw new IllegalArgumentException("groupId cannot be null"); if (artifactId == null) throw new IllegalArgumentException("artifactId cannot be null"); if (version == null) throw new IllegalArgumentException("version cannot be null"); @@ -130,32 +139,43 @@ private POM(Resource localDirectory, Collection repositories, String this.optional = optional == null ? null : optional.trim(); this.dependencyScopeManagement = dependencyScopeManagement; this.dependencyScope = dependencyScope; + this.log = log; cache.put(id(), this); } - private void initXML() throws IOException { - if (isInitXML) return; - isInitXML = true; - if (debug) print.e("xxxxxx initXML " + this + " xxxxxx"); + void initXML() throws IOException { + if (!isInitXML) { + synchronized (token) { + if (!isInitXML) { + if (debug) print.e("xxxxxx initXML " + this + " xxxxxx"); - MavenUtil.downloadPOM(this, initRepositories); + MavenUtil.download(this, initRepositories, "pom", log); - reader = new POMReader(getPath()); - try { - reader.read(); - } - catch (SAXException e) { - IOException cause = ExceptionUtil.toIOException(e); - IOException ioe = new IOException("failed to load pom file [" + getArtifact("pom", initRepositories) + "]"); - ExceptionUtil.initCauseEL(ioe, cause); - throw ioe; - } + reader = new POMReader(getPath()); + try { + reader.read(); + } + catch (SAXException e) { + IOException cause = ExceptionUtil.toIOException(e); + IOException ioe = new IOException("failed to load pom file [" + getArtifact("pom", initRepositories) + "]"); + ExceptionUtil.initCauseEL(ioe, cause); + throw ioe; + } + this.packaging = reader.getPackaging(); + this.artifactExtension = this.packaging; + if ("bundle".equalsIgnoreCase(artifactExtension)) this.artifactExtension = "jar"; + + this.name = reader.getName(); + this.description = reader.getDescription(); + this.url = reader.getURL(); + + if (this.packaging != null && !"pom".equalsIgnoreCase(this.packaging)) MavenUtil.download(this, initRepositories, artifactExtension, log); - this.packaging = reader.getPackaging(); - this.name = reader.getName(); - this.description = reader.getDescription(); - this.url = reader.getURL(); + isInitXML = true; + } + } + } } private void initParent() throws IOException { @@ -167,7 +187,7 @@ private void initParent() throws IOException { Dependency p = reader.getParent(); if (p != null) { // chicken egg, because there is no parent yet, this cannot use properties from parent - this.parent = MavenUtil.toPOM(this.localDirectory, initRepositories, p, reader.getProperties(), dependencyScope, dependencyScopeManagement); + this.parent = MavenUtil.toPOM(this.localDirectory, initRepositories, p, reader.getProperties(), dependencyScope, dependencyScopeManagement, log); parent.init(); } } @@ -194,7 +214,7 @@ private void initDependencies() throws IOException { isInitDependencies = true; if (debug) print.e("xxxxxx initDependencies " + this + " xxxxxx"); initProperties(); - if (dependencyScope > 0) dependencies = MavenUtil.getDependencies(reader.getDependencies(), this, parent, properties, localDirectory, false); + if (dependencyScope > 0) dependencies = MavenUtil.getDependencies(reader.getDependencies(), this, parent, properties, localDirectory, false, log); } private void initDependencyManagement() throws IOException { @@ -203,7 +223,8 @@ private void initDependencyManagement() throws IOException { if (debug) print.e("xxxxxx initDependencyManagement " + this + " xxxxxx"); initProperties(); - if (dependencyScopeManagement > 0) dependencyManagement = MavenUtil.getDependencyManagement(reader.getDependencyManagements(), this, parent, properties, localDirectory); + if (dependencyScopeManagement > 0) + dependencyManagement = MavenUtil.getDependencyManagement(reader.getDependencyManagements(), this, parent, properties, localDirectory, log); } @@ -289,6 +310,15 @@ public Resource getPath() { return local(localDirectory, "pom"); } + Resource getArtifact(String type) { + return local(localDirectory, type); + } + + public Resource getArtifact() { + if (artifactExtension == null) return null; + return local(localDirectory, artifactExtension); + } + public boolean isInit() { return isInit; } diff --git a/core/src/main/java/lucee/runtime/mvn/Test.java b/core/src/main/java/lucee/runtime/mvn/Test.java index de6f654896..2af9bbc002 100644 --- a/core/src/main/java/lucee/runtime/mvn/Test.java +++ b/core/src/main/java/lucee/runtime/mvn/Test.java @@ -74,8 +74,9 @@ public static void main(String[] args) throws Exception { * for (Artifact a: examples) { print.e("------------ " + a); print.e(maven.download(a.groupId, * a.artifactId, a.version, true, false)); } */ + long start = System.currentTimeMillis(); for (GAVSO gav: arr) { - POM pom = POM.getInstance(dir, gav.g, gav.a, gav.v, POM.SCOPE_NOT_TEST); + POM pom = POM.getInstance(dir, gav.g, gav.a, gav.v, POM.SCOPE_NOT_TEST, null); print.e("=========================================="); print.e(pom.getName()); print.e(pom); @@ -87,6 +88,12 @@ public static void main(String[] args) throws Exception { print.e("--- packaging ---"); print.e(pom.getPackaging()); + print.e("--- path ---"); + print.e(pom.getPath()); + + print.e("--- artifact ---"); + print.e(pom.getArtifact()); + print.e("--- parents ---"); // print.e(pom.getAllParentsAsTree()); print.e(pom.getAllParents()); @@ -104,13 +111,14 @@ public static void main(String[] args) throws Exception { print.e("--- dependencies ---"); // print.e(getDependenciesAsTrees(pom, true)); print.e(pom.getAllDependencies()); + print.e(System.currentTimeMillis() - start); // pom.getScope(); // print.e(pom.getDependencyManagement()); // print.e(maven.getDependencies(groupId, artifactId, version, true, false, true)); - break; + // break; } }