This is an automated email from the ASF dual-hosted git repository. jbonofre pushed a commit to branch karaf-4.1.x in repository https://gitbox.apache.org/repos/asf/karaf.git
The following commit(s) were added to refs/heads/karaf-4.1.x by this push: new 89de305 [KARAF-5604] Speed up descriptor generation (#446) 89de305 is described below commit 89de305e4747abc1cbf1d4805ae0da92a22bd315 Author: rovarga <n...@hq.sk> AuthorDate: Sat Feb 10 08:03:34 2018 +0100 [KARAF-5604] Speed up descriptor generation (#446) * KARAF-5604: Check log.isDebugEnabled() before string concat & cache aether Artifacts --- .../tooling/features/GenerateDescriptorMojo.java | 25 ++++++---- .../karaf/tooling/utils/Dependency31Helper.java | 25 ++++++---- .../org/apache/karaf/tooling/utils/MavenUtil.java | 53 ++++++++++++++++++++++ 3 files changed, 86 insertions(+), 17 deletions(-) diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java index 0d0301f..253793a 100644 --- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java +++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/GenerateDescriptorMojo.java @@ -475,7 +475,7 @@ public class GenerateDescriptorMojo extends MojoSupport { // TODO Initialise the repositories from the existing feature file if any Map<Dependency, Feature> otherFeatures = new HashMap<>(); Map<Feature, String> featureRepositories = new HashMap<>(); - FeaturesCache cache = new FeaturesCache(featuresCacheSize); + FeaturesCache cache = new FeaturesCache(featuresCacheSize, artifactCacheSize); for (final LocalDependency entry : localDependencies) { Object artifact = entry.getArtifact(); @@ -589,10 +589,10 @@ public class GenerateDescriptorMojo extends MojoSupport { throw new MojoExecutionException( "Cannot locate file for feature: " + artifact + " at " + featuresFile); } - Features includedFeatures = cache.get(featuresFile); + Features includedFeatures = cache.getFeature(featuresFile); for (String repository : includedFeatures.getRepository()) { processFeatureArtifact(features, feature, otherFeatures, featureRepositories, cache, - new DefaultArtifact(MavenUtil.mvnToAether(repository)), parent, false); + cache.getArtifact(repository), parent, false); } for (Feature includedFeature : includedFeatures.getFeature()) { Dependency dependency = new Dependency(includedFeature.getName(), includedFeature.getVersion()); @@ -963,20 +963,27 @@ public class GenerateDescriptorMojo extends MojoSupport { } private static final class FeaturesCache { - private final SimpleLRUCache<File, Features> cache; + // Maven-to-Aether Artifact cache, as parsing strings is expensive + private final SimpleLRUCache<String, DefaultArtifact> artifactCache; + private final SimpleLRUCache<File, Features> featuresCache; - FeaturesCache(int featuresCacheSize) { - cache = new SimpleLRUCache<>(featuresCacheSize); + FeaturesCache(int featuresCacheSize, int artifactCacheSize) { + featuresCache = new SimpleLRUCache<>(featuresCacheSize); + artifactCache = new SimpleLRUCache<>(artifactCacheSize); } - Features get(final File featuresFile) throws XMLStreamException, JAXBException, IOException { - final Features existing = cache.get(featuresFile); + DefaultArtifact getArtifact(String mavenName) { + return artifactCache.computeIfAbsent(mavenName, MavenUtil::mvnToArtifact); + } + + Features getFeature(final File featuresFile) throws XMLStreamException, JAXBException, IOException { + final Features existing = featuresCache.get(featuresFile); if (existing != null) { return existing; } final Features computed = readFeaturesFile(featuresFile); - cache.put(featuresFile, computed); + featuresCache.put(featuresFile, computed); return computed; } } diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/Dependency31Helper.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/Dependency31Helper.java index 823b7ad..3868efc 100644 --- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/Dependency31Helper.java +++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/Dependency31Helper.java @@ -330,7 +330,9 @@ public class Dependency31Helper implements DependencyHelper { @Override public File resolve(Object artifact, Log log) { - log.debug("Resolving artifact " + artifact + " from " + projectRepositories); + if (log.isDebugEnabled()) { + log.debug("Resolving artifact " + artifact + " from " + projectRepositories); + } ArtifactResult result; try { @@ -340,7 +342,10 @@ public class Dependency31Helper implements DependencyHelper { return null; } - log.debug("Resolved artifact " + artifact + " to " + result.getArtifact().getFile() + " from " + result.getRepository()); + if (log.isDebugEnabled()) { + log.debug("Resolved artifact " + artifact + " to " + result.getArtifact().getFile() + " from " + + result.getRepository()); + } return result.getArtifact().getFile(); } @@ -357,17 +362,22 @@ public class Dependency31Helper implements DependencyHelper { } id = MavenUtil.mvnToAether(id); - log.debug("Resolving artifact " + id + " from " + projectRepositories); + if (log.isDebugEnabled()) { + log.debug("Resolving artifact " + id + " from " + projectRepositories); + } ArtifactResult result; try { - result = resolveArtifact(new DefaultArtifact(id)); + result = resolveArtifact(MavenUtil.aetherToArtifact(id)); } catch (ArtifactResolutionException e) { log.warn("Could not resolve " + id, e); throw new MojoFailureException(format("Couldn't resolve artifact %s", id), e); } - log.debug("Resolved artifact " + id + " to " + result.getArtifact().getFile() + " from " + result.getRepository()); + if (log.isDebugEnabled()) { + log.debug("Resolved artifact " + id + " to " + result.getArtifact().getFile() + " from " + + result.getRepository()); + } return result.getArtifact().getFile(); } @@ -413,8 +423,7 @@ public class Dependency31Helper implements DependencyHelper { @Override public org.apache.maven.artifact.Artifact mvnToArtifact(String name) throws MojoExecutionException { - name = MavenUtil.mvnToAether(name); - DefaultArtifact artifact = new DefaultArtifact(name); + DefaultArtifact artifact = MavenUtil.mvnToArtifact(name); org.apache.maven.artifact.Artifact mavenArtifact = toArtifact(artifact); return mavenArtifact; } @@ -433,7 +442,7 @@ public class Dependency31Helper implements DependencyHelper { @Override public String pathFromAether(String name) throws MojoExecutionException { - DefaultArtifact artifact = new DefaultArtifact(name); + DefaultArtifact artifact = MavenUtil.aetherToArtifact(name); org.apache.maven.artifact.Artifact mavenArtifact = toArtifact(artifact); return MavenUtil.layout.pathOf(mavenArtifact); } diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MavenUtil.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MavenUtil.java index 4d11dd5..cd26874 100644 --- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MavenUtil.java +++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/utils/MavenUtil.java @@ -33,6 +33,7 @@ import org.apache.maven.artifact.repository.metadata.Snapshot; import org.apache.maven.artifact.repository.metadata.SnapshotVersion; import org.apache.maven.artifact.repository.metadata.Versioning; import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer; +import org.eclipse.aether.artifact.DefaultArtifact; /** * Util method for Maven manipulation (URL convert, metadata generation, etc). @@ -77,6 +78,58 @@ public class MavenUtil { return b.toString(); } + /** + * Convert PAX URL mvn format to an aether Artifact. + * N.B. we do not handle repository-url in mvn urls. + * N.B. version is required in mvn urls. + * + * @param name PAX URL mvn format: mvn-uri := [ 'wrap:' ] 'mvn:' [ repository-url '!' ] group-id '/' artifact-id [ '/' [version] [ '/' [type] [ '/' classifier ] ] ] ] + * @return aether Artifact + */ + public static DefaultArtifact mvnToArtifact(String name) { + Matcher m = mvnPattern.matcher(name); + if (!m.matches()) { + return new DefaultArtifact(name); + } + + String groupId = m.group(1); + String artifactId = m.group(2); + String version = m.group(3); + String extension = m.group(5); + if (!present(extension)) { + extension = "jar"; + } + String classifier = m.group(7); + + return new DefaultArtifact(groupId, artifactId, present(classifier) ? classifier : "", extension, version); + } + + /** + * Convert Aether coordinate format to an aether Artifact. + * N.B. we do not handle repository-url in mvn urls. + * N.B. version is required in mvn urls. + * + * @param name aether coordinate format: <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version> + * @return aether Artifact + */ + public static DefaultArtifact aetherToArtifact(String name) { + Matcher m = aetherPattern.matcher(name); + if (!m.matches()) { + return new DefaultArtifact(name); + } + + String groupId = m.group(1); + String artifactId = m.group(2); + String version = m.group(7); + String extension = m.group(4); + if (!present(extension)) { + extension = "jar"; + } + String classifier = m.group(6); + + return new DefaultArtifact(groupId, artifactId, present(classifier) ? classifier : "", extension, version); + } + private static boolean present(String part) { return part != null && !part.isEmpty(); } -- To stop receiving notification emails like this one, please contact jbono...@apache.org.