Skip to content

Commit

Permalink
SHRINKRES-78 Custom FormatProcessors registered via SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
kpiwko authored and ALRubinger committed Nov 6, 2012
1 parent 8b061a8 commit 30f4020
Show file tree
Hide file tree
Showing 43 changed files with 1,383 additions and 823 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,45 @@
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.importer.ZipImporter;
import org.jboss.shrinkwrap.resolver.api.formatprocessor.FormatProcessor;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolvedArtifact;

/**
* {@link FormatProcessor} implementation to return an artifact as a ShrinkWrap {@link Archive}
*
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
* @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a>
*/
public final class ArchiveFormatProcessor<ARCHIVETYPE extends Archive<ARCHIVETYPE>> implements
FormatProcessor<ARCHIVETYPE> {

private final Class<ARCHIVETYPE> clazz;

/**
* Creates a new instance capable of processing the input {@link File} as the specified {@link Class} type
*
* @param clazz
* @throws IllegalArgumentException
* If the class type is not specified
*/
public ArchiveFormatProcessor(final Class<ARCHIVETYPE> clazz) throws IllegalArgumentException {
if (clazz == null) {
throw new IllegalArgumentException("clazz must be specified");
}
this.clazz = clazz;
}
FormatProcessor<MavenResolvedArtifact, ARCHIVETYPE> {

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.resolver.api.formatprocessor.FormatProcessor#process(java.io.File)
*/
@Override
public ARCHIVETYPE process(final File input) throws IllegalArgumentException {
if (input == null) {
throw new IllegalArgumentException("input file must be specified");
public ARCHIVETYPE process(final MavenResolvedArtifact artifact, final Class<ARCHIVETYPE> returnType)
throws IllegalArgumentException {

if (artifact == null) {
throw new IllegalArgumentException("Resolution artifact must be specified");
}
return ShrinkWrap.create(ZipImporter.class, input.getName()).importFrom(input).as(clazz);
File file = artifact.asFile();
if (file == null) {
throw new IllegalArgumentException("Artifact was not resolved");
}

return ShrinkWrap.create(ZipImporter.class, file.getName()).importFrom(file).as(returnType);
}

@Override
public boolean handles(Class<?> resolvedTypeClass) {
return MavenResolvedArtifact.class.isAssignableFrom(resolvedTypeClass);
}

@Override
public boolean returns(Class<?> returnTypeClass) {
return Archive.class.isAssignableFrom(returnTypeClass);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.jboss.shrinkwrap.resolver.api.maven.archive;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.resolver.api.NoResolvedResultException;
import org.jboss.shrinkwrap.resolver.api.NonUniqueResultException;
import org.jboss.shrinkwrap.resolver.api.maven.MavenFormatStage;

/**
Expand All @@ -28,30 +26,4 @@
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
*/
public interface MavenArchiveFormatStage extends MavenFormatStage {
/**
* Formats the resultant artifacts as an array of {@link Archive}s. If nothing matches resolution, an empty array
* will be returned.
*
* @param type
* @return
* @throws IllegalArgumentException
* If the type is not specified
*/
<ARCHIVETYPE extends Archive<ARCHIVETYPE>> ARCHIVETYPE[] as(Class<ARCHIVETYPE> type)
throws IllegalArgumentException;

/**
* Formats the resultant artifact as an {@link Archive}; assumes a single artifact is returned from resolution.
*
* @param type
* @return
* @throws IllegalArgumentException
* If the type is not specified
* @throws NonUniqueResultException
* If the resolution resulted in more than one result
* @throws NoResolvedResultException
* If the resolution did not yield any result
*/
<ARCHIVETYPE extends Archive<ARCHIVETYPE>> ARCHIVETYPE asSingle(Class<ARCHIVETYPE> type)
throws IllegalArgumentException, NonUniqueResultException, NoResolvedResultException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jboss.shrinkwrap.resolver.api.maven.archive.ArchiveFormatProcessor
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,15 @@
package org.jboss.shrinkwrap.resolver.api.maven;

import org.jboss.shrinkwrap.resolver.api.FormatStage;
import org.jboss.shrinkwrap.resolver.api.NoResolvedResultException;
import org.jboss.shrinkwrap.resolver.api.NonUniqueResultException;
import org.jboss.shrinkwrap.resolver.api.formatprocessor.FormatProcessor;

/**
* Represents the formatting stage of Maven-based resolution in which the resolved artifact is returned in the desired
* format. Supports extensible formats by optionally supplying a {@link FormatProcessor}
*
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
* @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a>
*/
public interface MavenFormatStage extends FormatStage {
/**
* Formats the resultant artifacts as an array of {@link ResolvedArtifactInfo}s. If nothing matches resolution, an
* empty array will be returned.
*
* @param type
* @return
* @throws IllegalArgumentException
* If the type is not specified
*/
ResolvedArtifactInfo[] as(Class<ResolvedArtifactInfo> type) throws IllegalArgumentException;
public interface MavenFormatStage extends FormatStage<MavenResolvedArtifact> {

/**
* Formats the resultant artifact as a {@link ResolvedArtifactInfo}; assumes a single artifact is returned from
* resolution.
*
* @param type
* @return
* @throws IllegalArgumentException
* If the type is not specified
* @throws NonUniqueResultException
* If the resolution resulted in more than one result
* @throws NoResolvedResultException
* If the resolution did not yield any result
*/
ResolvedArtifactInfo asSingle(Class<ResolvedArtifactInfo> type) throws IllegalArgumentException,
NonUniqueResultException, NoResolvedResultException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

/**
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
* @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a>
*/
public interface MavenResolveStageBase<RESOLVESTAGETYPE extends MavenResolveStageBase<RESOLVESTAGETYPE, STRATEGYSTAGETYPE, FORMATSTAGETYPE>, STRATEGYSTAGETYPE extends MavenStrategyStageBase<STRATEGYSTAGETYPE, FORMATSTAGETYPE>, FORMATSTAGETYPE extends MavenFormatStage>
extends
ResolveStage<MavenDependency, MavenResolutionFilter, RESOLVESTAGETYPE, STRATEGYSTAGETYPE, FORMATSTAGETYPE, MavenResolutionStrategy> {
extends
ResolveStage<MavenDependency, MavenResolutionFilter, RESOLVESTAGETYPE, STRATEGYSTAGETYPE, MavenResolvedArtifact, FORMATSTAGETYPE, MavenResolutionStrategy> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,19 @@
*/
package org.jboss.shrinkwrap.resolver.api.maven;

import java.io.File;
import java.io.InputStream;

import org.jboss.shrinkwrap.resolver.api.formatprocessor.FileFormatProcessor;
import org.jboss.shrinkwrap.resolver.api.formatprocessor.FormatProcessor;
import org.jboss.shrinkwrap.resolver.api.formatprocessor.InputStreamFormatProcessor;
import org.jboss.shrinkwrap.resolver.api.ResolvedArtifact;
import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinate;

/**
* Encapsulation of a resolved Maven-based artifact's metadata
*
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
* @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a>
*/
public interface ResolvedArtifactInfo {
public interface MavenResolvedArtifact extends ResolvedArtifact<MavenResolvedArtifact> {

/**
* Returns the defined coordinate (ie. address) of this resolved artifact.
* Returns the defined coordinate (i.e. address) of this resolved artifact.
*
* @return
*/
Expand Down Expand Up @@ -61,34 +57,4 @@ public interface ResolvedArtifactInfo {
* @return The file extension, which is never null
*/
String getExtension();

/**
* Returns the resolved artifact using the specified <code>FORMATTERTYPE</code> instance
*
* @param <FORMATTERTYPE>
* The type of formatter used
* @param <RETURNTYPE>
* The type returned by the formatter
* @param formatter
* The formatter to use
* @return The resolved artifact
*/
<FORMATTERTYPE extends FormatProcessor<RETURNTYPE>, RETURNTYPE> RETURNTYPE getArtifact(FORMATTERTYPE formatter);

/**
* Returns the resolved artifact as a {@link File}; alias for
* {@link ResolvedArtifactInfo#getArtifact(FormatProcessor)} passing in argument of {@link FileFormatProcessor}
*
* @return
*/
File getArtifact(Class<File> clazz);

/**
* Returns the resolved artifact as an {@link InputStream}; alias for
* {@link ResolvedArtifactInfo#getArtifact(FormatProcessor)} passing in argument of
* {@link InputStreamFormatProcessor}
*
* @return
*/
InputStream getArtifact(Class<InputStream> clazz);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a>
*/
public interface MavenStrategyStageBase<STRATEGYSTAGETYPE extends MavenStrategyStageBase<STRATEGYSTAGETYPE, FORMATSTAGETYPE>, FORMATSTAGETYPE extends MavenFormatStage>
extends TransitiveStrategyStage<MavenDependency, MavenResolutionFilter, FORMATSTAGETYPE, MavenResolutionStrategy> {
extends TransitiveStrategyStage<MavenDependency, MavenResolutionFilter, MavenResolvedArtifact, FORMATSTAGETYPE, MavenResolutionStrategy> {

/**
* Sets that resolution from the ClassPath should be permitted in addition to configured repositories - defaults to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.jboss.shrinkwrap.resolver.api.maven.coordinate;

import org.jboss.shrinkwrap.resolver.api.maven.PackagingType;
import org.jboss.shrinkwrap.resolver.api.maven.ResolvedArtifactInfo;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolvedArtifact;

/**
* Represents a single Maven coordinate (an address in canonical form
Expand Down Expand Up @@ -64,7 +64,7 @@ public interface MavenCoordinate extends MavenGABase {
* Returns the declared "version" portion of this artifact's coordinates, for instance "1.2.0-alpha-2" or
* "1.2.0-SNAPSHOT". This is the value of the "version" field as declared in the POM. During artifact resolution,
* SNAPSHOT versions may be set to a fixed SNAPSHOT as represented by
* {@link ResolvedArtifactInfo#getResolvedVersion()}.
* {@link MavenResolvedArtifact#getResolvedVersion()}.
*
* @return The base version, never {@code null}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jboss.shrinkwrap.resolver.api.maven.formatprocessor;

import org.jboss.shrinkwrap.resolver.api.formatprocessor.FormatProcessor;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolvedArtifact;

/**
* A format processor which returns {@link MavenResolvedArtifact}. As {@link MavenResolvedArtifact} is the default format for
* Maven resolution, this is a no-op format processor.
*
* @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a>
*
*/
public class MavenResolvedArtifactProcessor implements FormatProcessor<MavenResolvedArtifact, MavenResolvedArtifact> {

@Override
public boolean handles(Class<?> resolvedTypeClass) {
return MavenResolvedArtifact.class.isAssignableFrom(resolvedTypeClass);
}

@Override
public boolean returns(Class<?> returnTypeClass) {
return MavenResolvedArtifact.class.equals(returnTypeClass);
}

@Override
public MavenResolvedArtifact process(MavenResolvedArtifact input, Class<MavenResolvedArtifact> returnType)
throws IllegalArgumentException {
// no-op
return input;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jboss.shrinkwrap.resolver.api.maven.formatprocessor.MavenResolvedArtifactProcessor

0 comments on commit 30f4020

Please sign in to comment.