Skip to content

Commit

Permalink
SHRINKRES-162 SHRINKRES-182 Fixed internal handling of packaging and …
Browse files Browse the repository at this point in the history
…types
  • Loading branch information
kpiwko committed May 28, 2014
1 parent 8ed43cc commit ee71e55
Show file tree
Hide file tree
Showing 59 changed files with 1,041 additions and 605 deletions.
Expand Up @@ -16,6 +16,8 @@
*/
package org.jboss.shrinkwrap.resolver.api.maven;

import java.util.concurrent.ConcurrentHashMap;

import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinate;

/**
Expand All @@ -26,20 +28,61 @@
*/
public class PackagingType {

public static final PackagingType POM = PackagingType.of("pom");
public static final PackagingType JAR = PackagingType.of("jar");
public static final PackagingType TEST_JAR = PackagingType.of("test-jar");
public static final PackagingType MAVEN_PLUGIN = PackagingType.of("maven-plugin");
public static final PackagingType EJB = PackagingType.of("ejb");
public static final PackagingType WAR = PackagingType.of("war");
public static final PackagingType EAR = PackagingType.of("ear");
public static final PackagingType RAR = PackagingType.of("rar");
public static final PackagingType PAR = PackagingType.of("par");
private static final ConcurrentHashMap<String, PackagingType> cache = new ConcurrentHashMap<String, PackagingType>();

public static final PackagingType POM = new PackagingType("pom");
public static final PackagingType JAR = new PackagingType("jar");
public static final PackagingType TEST_JAR = new PackagingType("test-jar", "jar", "tests");
public static final PackagingType MAVEN_PLUGIN = new PackagingType("maven-plugin", "jar", "");
public static final PackagingType EJB_CLIENT = new PackagingType("ejb-client", "jar", "client");
public static final PackagingType EJB = new PackagingType("ejb", "jar", "");
public static final PackagingType WAR = new PackagingType("war");
public static final PackagingType EAR = new PackagingType("ear");
public static final PackagingType RAR = new PackagingType("rar");
public static final PackagingType PAR = new PackagingType("par");
public static final PackagingType JAVADOC = new PackagingType("javadoc", "jar", "javadoc");
public static final PackagingType JAVA_SOURCE = new PackagingType("java-source", "jar", "sources");

private final String id;
private final String extension;
private final String classifier;

private final String value;
private PackagingType(final String id, final String extension, final String classifier) {
this.id = id;
this.extension = extension;
this.classifier = classifier;
cache.putIfAbsent(id, this);
}

private PackagingType(final String value) {
this.value = value;
private PackagingType(final String id) {
this(id, id, "");
}

/**
* Returns type of the packaging. Might be the same as extension.
*
* @return
*/
public String getId() {
return id;
}

/**
* Returns extension for packaging. Might be the same as id;
*
* @return
*/
public String getExtension() {
return extension;
}

/**
* Returns classifier for packaging. Might be empty string.
*
* @return
*/
public String getClassifier() {
return classifier;
}

/**
Expand All @@ -48,7 +91,7 @@ private PackagingType(final String value) {
*/
@Override
public String toString() {
return this.value;
return this.id;
}

/**
Expand All @@ -65,17 +108,20 @@ public static PackagingType of(String typeName) throws IllegalArgumentException
if (typeName == null || typeName.length() == 0) {
throw new IllegalArgumentException("Packaging type must not be null nor empty.");
}
return new PackagingType(typeName);
// return from cache if available
return cache.putIfAbsent(typeName, new PackagingType(typeName));

This comment has been minimized.

Copy link
@arcivanov

arcivanov Dec 29, 2014

This is incorrect: cache.putIfAbsent will return null if no value was previously there and existing value if there is one. Therefore, for any new type of packaging you'll always get nulls returned on the first invocation.

This comment has been minimized.

Copy link
@kpiwko

kpiwko Jan 5, 2015

Author Member

Thanks @arcivanov ! I've added this bug to the tracker as https://issues.jboss.org/browse/SHRINKRES-215

}

// we are using only id for hashCode() and equals(Object)
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}

// we are using only id for hashCode() and equals(Object)
@Override
public boolean equals(Object obj) {
if (this == obj)
Expand All @@ -85,12 +131,14 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass())
return false;
PackagingType other = (PackagingType) obj;
if (value == null) {
if (other.value != null)
if (id == null) {
if (other.id != null)
return false;
} else if (!value.equals(other.value))
} else if (!id.equals(other.id))
return false;
return true;
}



}
Expand Up @@ -51,17 +51,9 @@ class MavenCoordinateImpl extends MavenGABaseImpl implements MavenCoordinate {
// Set properties
this.version = version;

// if user provided type instead of packaging, enforce packaging and classifier update
// see https://issues.jboss.org/browse/SHRINKRES-102
if (PackagingType.TEST_JAR.equals(packaging)) {
this.packaging = PackagingType.JAR;
this.classifier = "tests";
}
else {
this.packaging = packaging == null ? PackagingType.JAR : packaging;
// Adjust this for compatibility with Aether parser
this.classifier = classifier == null ? EMPTY_STRING : classifier;
}
this.packaging = packaging == null ? PackagingType.JAR : packaging;
// Adjust this for compatibility with Aether parser
this.classifier = classifier == null ? packaging.getClassifier() : classifier;

}

Expand Down Expand Up @@ -117,11 +109,11 @@ public final String toCanonicalForm() {
return sb.toString();
}
if (classifier != null && classifier.length() > 0 && packaging != null) {
sb.append(SEPARATOR_COORDINATE).append(packaging.toString()).append(SEPARATOR_COORDINATE)
sb.append(SEPARATOR_COORDINATE).append(packaging.getId()).append(SEPARATOR_COORDINATE)
.append(classifier).append(SEPARATOR_COORDINATE).append(version);
}
if ((classifier == null || classifier.length() == 0) && packaging != null) {
sb.append(SEPARATOR_COORDINATE).append(packaging.toString()).append(SEPARATOR_COORDINATE).append(version);
sb.append(SEPARATOR_COORDINATE).append(packaging.getId()).append(SEPARATOR_COORDINATE).append(version);
}

return sb.toString();
Expand Down
Expand Up @@ -140,4 +140,16 @@ public void defaultPackagingType() {
final MavenCoordinate coordinate = new MavenCoordinateImpl(groupId, artifactId, version, null, classifier);
Assert.assertEquals(PackagingType.JAR, coordinate.getPackaging());
}

@Test
public void ejbPackaging() {

final String groupId = "groupId";
final String artifactId = "artifactId";
final PackagingType packaging = PackagingType.of("ejb");
final String version = "version";
final MavenCoordinate coordinate = new MavenCoordinateImpl(groupId, artifactId, version, packaging, null);
Assert.assertEquals(PackagingType.EJB, coordinate.getPackaging());
Assert.assertEquals("jar", coordinate.getPackaging().getExtension());
}
}
Expand Up @@ -32,10 +32,12 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
Expand Down Expand Up @@ -65,9 +67,11 @@ public static Collection<Object[]> jars() {
{ "test-exclusion", new Class<?>[] { ArrayList.class, LinkedList.class } },
{ "test-dependency-provided", new Class<?>[] { List.class, Map.class } },
{ "test-dependency-test", new Class<?>[] { ArrayList.class, HashMap.class } },
{ "test-parent", new Class<?>[] { File.class } }, { "test-child", new Class<?>[] { InputStream.class } },
{ "test-parent", new Class<?>[] { File.class } },
{ "test-child", new Class<?>[] { InputStream.class } },
{ "test-remote-parent", new Class<?>[] { OutputStream.class } },
{ "test-deps-a", new Class<?>[] { System.class } }, { "test-deps-b", new Class<?>[] { Field.class } },
{ "test-deps-a", new Class<?>[] { System.class } },
{ "test-deps-b", new Class<?>[] { Field.class } },
{ "test-deps-c", new Class<?>[] { Integer.class } },
{ "test-deps-d", new Class<?>[] { Float.class, Double.class } },
{ "test-deps-e", new Class<?>[] { String.class, StringBuilder.class } },
Expand All @@ -82,7 +86,9 @@ public static Collection<Object[]> jars() {
{ "test-dependency-test-scope", new Class<?>[] { Method.class, Type.class, Field.class } },
{ "test-dependency-with-test-jar", new Class<?>[] { Method.class } },
{ "test-dependency-with-test-jar-tests", new Class<?>[] { Field.class } },
{ "test-wrong-scope", new Class<?>[] { ArrayList.class, Key.class, KeyException.class } },};
{ "test-wrong-scope", new Class<?>[] { ArrayList.class, Key.class, KeyException.class } },
{ "test-ejb", new Class<?>[] { List.class, Collection.class, Set.class } },
{ "test-deps-ejb", new Class<?>[] { ArrayList.class, HashMap.class, Iterator.class } },};

return Arrays.asList(data);
}
Expand Down
Expand Up @@ -11,6 +11,7 @@
import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinate;
import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinates;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.ArtifactProperties;
import org.eclipse.aether.graph.DependencyNode;

/**
Expand Down Expand Up @@ -42,8 +43,12 @@ protected MavenArtifactInfoImpl(final MavenCoordinate mavenCoordinate, final Str

protected MavenArtifactInfoImpl(final Artifact artifact, final ScopeType scopeType,
final List<DependencyNode> children) {

final PackagingType packaging = PackagingType.of(artifact.getProperty(ArtifactProperties.TYPE, artifact.getExtension()));
final String classifier = artifact.getClassifier().length() == 0 ? packaging.getClassifier() : artifact.getClassifier();

this.mavenCoordinate = MavenCoordinates.createCoordinate(artifact.getGroupId(), artifact.getArtifactId(),
artifact.getBaseVersion(), PackagingType.of(artifact.getExtension()), artifact.getClassifier());
artifact.getBaseVersion(), packaging, classifier);
this.resolvedVersion = artifact.getVersion();
this.snapshotVersion = artifact.isSnapshot();
this.extension = artifact.getExtension();
Expand Down
Expand Up @@ -237,8 +237,8 @@ public Collection<MavenResolvedArtifact> resolveDependencies(final MavenResoluti

final List<RemoteRepository> repos = this.getRemoteRepositories();

final CollectRequest request = new CollectRequest(MavenConverter.asDependencies(depsForResolution),
MavenConverter.asDependencies(depManagement), repos);
final CollectRequest request = new CollectRequest(MavenConverter.asDependencies(depsForResolution, session.getArtifactTypeRegistry()),
MavenConverter.asDependencies(depManagement, session.getArtifactTypeRegistry()), repos);

Collection<ArtifactResult> results = Collections.emptyList();

Expand Down Expand Up @@ -284,7 +284,7 @@ public Collection<MavenResolvedArtifact> resolveDependencies(final MavenResoluti

@Override
public MavenVersionRangeResult resolveVersionRange(final MavenCoordinate coordinate) throws VersionResolutionException {
final Artifact artifact = MavenConverter.asArtifact(coordinate);
final Artifact artifact = MavenConverter.asArtifact(coordinate, session.getArtifactTypeRegistry());
final VersionRangeRequest versionRangeRequest = new VersionRangeRequest(artifact, this.getRemoteRepositories(), null);

try {
Expand Down
Expand Up @@ -24,6 +24,8 @@
import org.eclipse.aether.RepositoryListener;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.artifact.DefaultArtifactType;
import org.eclipse.aether.collection.DependencyGraphTransformer;
import org.eclipse.aether.collection.DependencyManager;
import org.eclipse.aether.collection.DependencyTraverser;
Expand All @@ -34,6 +36,7 @@
import org.eclipse.aether.repository.WorkspaceReader;
import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
import org.eclipse.aether.transfer.TransferListener;
import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry;
import org.eclipse.aether.util.graph.manager.ClassicDependencyManager;
import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
import org.eclipse.aether.util.graph.transformer.ConflictResolver;
Expand Down Expand Up @@ -187,6 +190,28 @@ public ArtifactDescriptorPolicy artifactRepositoryPolicy() {
return new SimpleArtifactDescriptorPolicy(true, true);
}

/**
* Returns artifact type registry. Defines standard Maven stereotypes.
*
* @return
*/
public ArtifactTypeRegistry artifactTypeRegistry() {
DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry();
stereotypes.add(new DefaultArtifactType("pom"));
stereotypes.add(new DefaultArtifactType("maven-plugin", "jar", "", "java"));
stereotypes.add(new DefaultArtifactType("jar", "jar", "", "java"));
stereotypes.add(new DefaultArtifactType("ejb", "jar", "", "java"));
stereotypes.add(new DefaultArtifactType("ejb-client", "jar", "client", "java"));
stereotypes.add(new DefaultArtifactType("test-jar", "jar", "tests", "java"));
stereotypes.add(new DefaultArtifactType("javadoc", "jar", "javadoc", "java"));
stereotypes.add(new DefaultArtifactType("java-source", "jar", "sources", "java", false, false));
stereotypes.add(new DefaultArtifactType("war", "war", "", "java", false, true));
stereotypes.add(new DefaultArtifactType("ear", "ear", "", "java", false, true));
stereotypes.add(new DefaultArtifactType("rar", "rar", "", "java", false, true));
stereotypes.add(new DefaultArtifactType("par", "par", "", "java", false, true));
return stereotypes;
}

/**
* Gets a dependency traverser. This traverser behaves the same as the one if Maven
*
Expand Down
Expand Up @@ -88,6 +88,9 @@ public DefaultRepositorySystemSession getSession(final Settings settings) {
session.setDependencyTraverser(builder.dependencyTraverser());
session.setDependencyGraphTransformer(builder.dependencyGraphTransformer());

// set artifact stereotypes
session.setArtifactTypeRegistry(builder.artifactTypeRegistry());

// set system properties for interpolation
session.setSystemProperties(SecurityActions.getProperties());
session.setConfigProperties(SecurityActions.getProperties());
Expand Down

0 comments on commit ee71e55

Please sign in to comment.