Skip to content

Commit

Permalink
[SHRINKRES-82] Introduce an SPI module
Browse files Browse the repository at this point in the history
  • Loading branch information
ALRubinger committed Nov 6, 2012
1 parent 010bf40 commit 80e570d
Show file tree
Hide file tree
Showing 23 changed files with 117 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package org.jboss.shrinkwrap.resolver.api.maven;

import org.jboss.shrinkwrap.resolver.api.FormatStage;
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}
* format.
*
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
* @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
import java.io.File;
import java.io.InputStream;

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

/**
* Represents the formatting stage of resolution in which the {@code RESOLVEDTYPE} is returned in the desired format.
* Supports extensible formats by registering a {@link FormatProcessor}.
* Supports extensible formats by registering a FormatProcessor with the SPI.
*
* @param <RESOLVEDTYPE>
* @author <a href="mailto:alr@jboss.org">Andrew Lee Rubinger</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import java.io.File;
import java.io.InputStream;

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

/**
* Representation of resolved artifact
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@
*/
package org.jboss.shrinkwrap.resolver.api;

import org.jboss.shrinkwrap.resolver.api.loadable.ServiceLoader;
import org.jboss.shrinkwrap.resolver.api.loadable.ServiceRegistry;
import org.jboss.shrinkwrap.resolver.api.loadable.SpiServiceLoader;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
/**
* Utility capable of creating {@link ResolverSystem} instances given a requested end-user view.
*
* @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
*/
final class ResolverSystemFactory {
// -------------------------------------------------------------------------------------||
// Class Members -----------------------------------------------------------------------||
// -------------------------------------------------------------------------------------||

// -------------------------------------------------------------------------------------||
// Constructor -------------------------------------------------------------------------||
// -------------------------------------------------------------------------------------||
private static final String CLASS_NAME_SERVICELOADER = "org.jboss.shrinkwrap.resolver.spi.loader.ServiceLoader";
private static final String CLASS_NAME_SPISERVICELOADER = "org.jboss.shrinkwrap.resolver.spi.loader.SpiServiceLoader";
private static final String CLASS_NAME_SERVICEREGISTRY = "org.jboss.shrinkwrap.resolver.spi.loader.ServiceRegistry";
private static final String METHOD_NAME_ONLY_ONE = "onlyOne";
private static final String METHOD_NAME_REGISTER = "register";

/**
* Internal constructor; not to be called
Expand All @@ -41,10 +38,6 @@ private ResolverSystemFactory() {
throw new UnsupportedOperationException("No instances permitted");
}

// -------------------------------------------------------------------------------------||
// Functional Methods ------------------------------------------------------------------||
// -------------------------------------------------------------------------------------||

/**
* Creates a new {@link ResolverSystem} instance of the specified user view type using the {@link Thread} Context
* {@link ClassLoader}. Will consult a configuration file visible to the {@link Thread} Context {@link ClassLoader} named
Expand All @@ -71,25 +64,36 @@ static <RESOLVERSYSTEMTYPE extends ResolverSystem> RESOLVERSYSTEMTYPE createFrom
* @param userViewClass
* @param cl
* @return
* @throws IllegalArgumentException
* If either argument was not specified
*/
static <RESOLVERSYSTEMTYPE extends ResolverSystem> RESOLVERSYSTEMTYPE createFromUserView(
final Class<RESOLVERSYSTEMTYPE> userViewClass, final ClassLoader cl) throws IllegalArgumentException {
final Class<RESOLVERSYSTEMTYPE> userViewClass, final ClassLoader cl) {

// create service loader using default SPI Service Loader
// use SPI Service loader to check if other Service Loader was registered and if so, use it instead of default one
ServiceLoader loader = new SpiServiceLoader(cl).onlyOne(ServiceLoader.class, SpiServiceLoader.class);
if (loader instanceof SpiServiceLoader) {
((SpiServiceLoader) loader).setClassLoader(cl);
}

// create and register service registry
ServiceRegistry registry = new ServiceRegistry(loader);
ServiceRegistry.register(registry);
assert userViewClass != null : "user view class must be specified";
assert cl != null : "ClassLoader must be specified";

// load implementation and cache results in registry
return userViewClass.cast(registry.onlyOne(userViewClass));
try {
final Class<?> spiServiceLoaderClass = cl.loadClass(CLASS_NAME_SPISERVICELOADER);
final Constructor<?> serviceLoaderCtor = spiServiceLoaderClass.getConstructor(ClassLoader.class);
final Object spiServiceLoader = serviceLoaderCtor.newInstance(cl);
final Method onlyOneMethod = spiServiceLoader.getClass().getMethod(METHOD_NAME_ONLY_ONE, Class.class,
Class.class);
final Object serviceLoader = onlyOneMethod.invoke(spiServiceLoader, spiServiceLoaderClass,
spiServiceLoader.getClass());
final Class<?> serviceRegistryClass = cl.loadClass(CLASS_NAME_SERVICEREGISTRY);
final Class<?> serviceLoaderClass = cl.loadClass(CLASS_NAME_SERVICELOADER);
final Constructor<?> serviceRegistryCtor = serviceRegistryClass.getConstructor(serviceLoaderClass);
final Object serviceRegistry = serviceRegistryCtor.newInstance(serviceLoader);
final Method registerMethod = serviceRegistry.getClass().getMethod(METHOD_NAME_REGISTER,
serviceRegistry.getClass());
registerMethod.invoke(null, serviceRegistry);
final Method onlyOneMethodSingleArg = serviceRegistry.getClass().getMethod(METHOD_NAME_ONLY_ONE,
Class.class);
final Object userViewObject = onlyOneMethodSingleArg.invoke(serviceRegistry, userViewClass);
return userViewClass.cast(userViewObject);
} catch (final Exception e) {
// Don't bother to catch all the reflection exceptions separately
throw new RuntimeException("Could not create object from user view", e);
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.shrinkwrap.resolver.api.maven.archive;
package org.jboss.shrinkwrap.resolver.impl.maven.archive;

import java.io.File;

import org.jboss.shrinkwrap.api.Archive;
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;
import org.jboss.shrinkwrap.resolver.spi.format.FormatProcessor;

/**
* {@link FormatProcessor} implementation to return an artifact as a ShrinkWrap {@link Archive}
Expand All @@ -36,7 +36,7 @@ public final class ArchiveFormatProcessor<ARCHIVETYPE extends Archive<ARCHIVETYP
/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.resolver.api.formatprocessor.FormatProcessor#process(java.io.File)
* @see org.jboss.shrinkwrap.resolver.spi.format.FormatProcessor#process(java.io.File)
*/
@Override
public ARCHIVETYPE process(final MavenResolvedArtifact artifact, final Class<ARCHIVETYPE> returnType)
Expand All @@ -54,12 +54,12 @@ public ARCHIVETYPE process(final MavenResolvedArtifact artifact, final Class<ARC
}

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jboss.shrinkwrap.resolver.impl.maven.archive.ArchiveFormatProcessor
5 changes: 5 additions & 0 deletions impl-maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<artifactId>shrinkwrap-resolver-api-maven</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-spi</artifactId>
<version>${project.version}</version>
</dependency>

<!--
External Projects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

import org.jboss.shrinkwrap.resolver.api.NoResolvedResultException;
import org.jboss.shrinkwrap.resolver.api.NonUniqueResultException;
import org.jboss.shrinkwrap.resolver.api.formatprocessor.FormatProcessor;
import org.jboss.shrinkwrap.resolver.api.formatprocessor.FormatProcessors;
import org.jboss.shrinkwrap.resolver.api.maven.MavenFormatStage;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolvedArtifact;
import org.jboss.shrinkwrap.resolver.impl.maven.util.Validate;
import org.jboss.shrinkwrap.resolver.spi.format.FormatProcessor;
import org.jboss.shrinkwrap.resolver.spi.format.FormatProcessors;

/**
* Implementation of {@link MavenFormatStage}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.jboss.shrinkwrap.resolver.api.formatprocessor.FormatProcessor;
import org.jboss.shrinkwrap.resolver.api.formatprocessor.FormatProcessors;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolvedArtifact;
import org.jboss.shrinkwrap.resolver.api.maven.PackagingType;
import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinate;
import org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenCoordinates;
import org.jboss.shrinkwrap.resolver.impl.maven.util.IOUtil;
import org.jboss.shrinkwrap.resolver.impl.maven.util.Validate;
import org.jboss.shrinkwrap.resolver.spi.format.FormatProcessor;
import org.jboss.shrinkwrap.resolver.spi.format.FormatProcessors;
import org.sonatype.aether.artifact.Artifact;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package org.jboss.shrinkwrap.resolver.api.maven.formatprocessor;
package org.jboss.shrinkwrap.resolver.impl.maven.format;

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

/**
* 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) {
public boolean handles(final Class<?> resolvedTypeClass) {
return MavenResolvedArtifact.class.isAssignableFrom(resolvedTypeClass);
}

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

@Override
public MavenResolvedArtifact process(MavenResolvedArtifact input, Class<MavenResolvedArtifact> returnType)
public MavenResolvedArtifact process(final MavenResolvedArtifact input, final Class<MavenResolvedArtifact> returnType)
throws IllegalArgumentException {
// no-op
return input;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.jboss.shrinkwrap.resolver.spi.format.FileFormatProcessor
org.jboss.shrinkwrap.resolver.spi.format.InputStreamFormatProcessor
org.jboss.shrinkwrap.resolver.impl.maven.format.MavenResolvedArtifactProcessor
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,11 @@
<module>api-maven</module>
<module>api-maven-archive</module>
<module>depchain</module>
<module>maven-plugin</module>
<module>maven-plugin-tests</module>
<module>impl-maven</module>
<module>impl-maven-archive</module>
<module>maven-plugin</module>
<module>maven-plugin-tests</module>
<module>spi</module>
<!-- <module>impl-maven-integration-tests</module> Disabled by SHRINKRES-49, to be put back in place by SHRINKRES-61 -->
</modules>

Expand Down
40 changes: 40 additions & 0 deletions spi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<!-- Parent -->
<parent>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-parent</artifactId>
<version>2.0.0-alpha-5-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<!-- Model Version -->
<modelVersion>4.0.0</modelVersion>

<!-- Artifact Configuration -->
<artifactId>shrinkwrap-resolver-spi</artifactId>
<name>ShrinkWrap Resolver SPI</name>
<description>SPI to Resolve Dependencies from a Generic Backend</description>


<!-- Dependencies -->
<dependencies>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.shrinkwrap.resolver.api.formatprocessor;
package org.jboss.shrinkwrap.resolver.spi.format;

import java.io.File;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.shrinkwrap.resolver.api.formatprocessor;
package org.jboss.shrinkwrap.resolver.spi.format;

import org.jboss.shrinkwrap.resolver.api.ResolvedArtifact;
import org.jboss.shrinkwrap.resolver.api.loadable.ServiceLoader;
import org.jboss.shrinkwrap.resolver.spi.loader.ServiceLoader;

/**
* Processes an input {@link ResolvedArtifact} and returns as a typed format.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.shrinkwrap.resolver.api.formatprocessor;
package org.jboss.shrinkwrap.resolver.spi.format;

import java.util.Collection;

import org.jboss.shrinkwrap.resolver.api.ResolvedArtifact;
import org.jboss.shrinkwrap.resolver.api.loadable.ServiceRegistry;
import org.jboss.shrinkwrap.resolver.spi.loader.ServiceRegistry;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.shrinkwrap.resolver.api.formatprocessor;
package org.jboss.shrinkwrap.resolver.spi.format;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -38,7 +38,7 @@ public enum InputStreamFormatProcessor implements FormatProcessor {
/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.resolver.api.formatprocessor.FormatProcessor#process(File, Class)
* @see org.jboss.shrinkwrap.resolver.spi.format.FormatProcessor#process(File, Class)
*/
@Override
public InputStream process(final ResolvedArtifact artifact, final Class returnType)
Expand Down

0 comments on commit 80e570d

Please sign in to comment.