Skip to content

Commit

Permalink
SHRINKWRAP-240 Add ServiceProviderContainer interface, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD authored and Andrew Lee Rubinger committed Jun 3, 2011
1 parent b397b22 commit 2fe40c0
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.shrinkwrap.api.container;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.asset.Asset;

/**
* Defines the contract for a component capable of storing
* service provider related resources.
* <br/><br/>
* The actual path to the service provider within the Archive
* is up to the implementations/specifications.
*
* @author Davide D'Alto
* @version $Revision: $
* @param <T>
*/
public interface ServiceProviderContainer<T extends Archive<T>> extends ManifestContainer<T>, ClassContainer<T>
{
/**
* Adds a META-INF/services/ServiceInterfaceName {@link Asset} and the classes related to the service
* to the archive.
*
* @param serviceInterface The Service Interface class
* @param serviceImpls The Service Interface Implementations
* @return This virtual archive
* @throws IllegalArgumentException if serviceInterface is null
* @throws IllegalArgumentException if serviceImpls is null or contain null values
*/
/*
* TODO: The interface should have been like this:
* <X> T addServiceProvider(Class<X> serviceInterface, Class<? extends X>... serviceImpls) throws IllegalArgumentException;
* But due to how java generic works, this will cause a unsafe warning for the user.
*/
T addAsServiceProviderAndClasses(Class<?> serviceInterface, Class<?>... serviceImpls) throws IllegalArgumentException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
package org.jboss.shrinkwrap.api.spec;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.container.ClassContainer;
import org.jboss.shrinkwrap.api.container.ManifestContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;

/**
* Traditional JAR (Java Archive) structure. Used in
Expand All @@ -32,7 +31,6 @@
public interface JavaArchive
extends
Archive<JavaArchive>,
ManifestContainer<JavaArchive>,
ClassContainer<JavaArchive>
ServiceProviderContainer<JavaArchive>
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package org.jboss.shrinkwrap.api.spec;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.container.ClassContainer;
import org.jboss.shrinkwrap.api.container.LibraryContainer;
import org.jboss.shrinkwrap.api.container.ManifestContainer;
import org.jboss.shrinkwrap.api.container.ResourceContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.api.container.WebContainer;

/**
Expand All @@ -33,10 +32,9 @@
public interface WebArchive
extends
Archive<WebArchive>,
ClassContainer<WebArchive>,
LibraryContainer<WebArchive>,
WebContainer<WebArchive>,
ResourceContainer<WebArchive>,
ManifestContainer<WebArchive>
ServiceProviderContainer<WebArchive>
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
package org.jboss.shrinkwrap.sip.api.spec;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.container.ClassContainer;
import org.jboss.shrinkwrap.api.container.LibraryContainer;
import org.jboss.shrinkwrap.api.container.ManifestContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.sip.api.container.ConvergedSipWebContainer;

/**
Expand All @@ -32,8 +31,7 @@
public interface ConvergedSipWebArchive
extends
Archive<ConvergedSipWebArchive>,
ManifestContainer<ConvergedSipWebArchive>,
ClassContainer<ConvergedSipWebArchive>,
ServiceProviderContainer<ConvergedSipWebArchive>,
LibraryContainer<ConvergedSipWebArchive>,
ConvergedSipWebContainer<ConvergedSipWebArchive>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jboss.shrinkwrap.api.container.LibraryContainer;
import org.jboss.shrinkwrap.api.container.ManifestContainer;
import org.jboss.shrinkwrap.api.container.ResourceContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.impl.base.test.ArchiveType;
import org.jboss.shrinkwrap.sip.api.container.ConvergedSipWebContainer;
import org.jboss.shrinkwrap.sip.api.spec.ConvergedSipWebArchive;
Expand Down Expand Up @@ -111,6 +112,12 @@ protected ManifestContainer<ConvergedSipWebArchive> getManifestContainer()
return getArchive();
}

@Override
protected ServiceProviderContainer<ConvergedSipWebArchive> getServiceProviderContainer()
{
return getArchive();
}

@Override
protected ResourceContainer<ConvergedSipWebArchive> getResourceContainer()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.jboss.shrinkwrap.api.container.LibraryContainer;
import org.jboss.shrinkwrap.api.container.ManifestContainer;
import org.jboss.shrinkwrap.api.container.ResourceContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.api.exporter.StreamExporter;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.formatter.Formatter;
Expand All @@ -66,7 +67,7 @@
* @param <T>
*/
public abstract class ContainerBase<T extends Archive<T>> extends AssignableBase<Archive<?>> implements
Archive<T>, ManifestContainer<T>, ResourceContainer<T>, ClassContainer<T>, LibraryContainer<T>
Archive<T>, ManifestContainer<T>, ServiceProviderContainer<T>, ResourceContainer<T>, ClassContainer<T>, LibraryContainer<T>
{
//-------------------------------------------------------------------------------------||
// Class Members ----------------------------------------------------------------------||
Expand Down Expand Up @@ -765,6 +766,20 @@ public T addAsServiceProvider(Class<?> serviceInterface, Class<?>... serviceImpl
ArchivePath path = new BasicPath("services", serviceInterface.getName());
return addAsManifestResource(asset, path);
}

/* (non-Javadoc)
* @see org.jboss.shrinkwrap.api.container.ServiceProviderContainerContainer#addServiceProvideraddAsServiceAndClasses(java.lang.Class, java.lang.Class<?>[])
*/
@Override
public T addAsServiceProviderAndClasses(Class<?> serviceInterface, Class<?>... serviceImpls) throws IllegalArgumentException
{
Validate.notNull(serviceInterface, "ServiceInterface must be specified");
Validate.notNullAndNoNullValues(serviceImpls, "ServiceImpls must be specified and can not contain null values");

addAsServiceProvider(serviceInterface, serviceImpls);
addClass(serviceInterface);
return addClasses(serviceImpls);
}

//-------------------------------------------------------------------------------------||
// Required Implementations - ResourceContainer ---------------------------------------||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,18 @@ public T addAsServiceProvider(Class<?> serviceInterface, Class<?>... serviceImpl
ArchivePath path = new BasicPath(getServiceProvidersPath(), serviceInterface.getName());
return add(asset, path);
}

/* (non-Javadoc)
* @see org.jboss.shrinkwrap.api.container.ServiceProviderContainer#addServiceProviderAndClasses(java.lang.Class, java.lang.Class<?>[])
*/
@Override
public T addAsServiceProviderAndClasses(Class<?> serviceInterface, Class<?>... serviceImpls) throws IllegalArgumentException
{
Validate.notNull(serviceInterface, "ServiceInterface must be specified");
Validate.notNullAndNoNullValues(serviceImpls, "ServiceImpls must be specified and can not contain null values");

addAsServiceProvider(serviceInterface, serviceImpls);
addClass(serviceInterface);
return addClasses(serviceImpls);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jboss.shrinkwrap.api.container.LibraryContainer;
import org.jboss.shrinkwrap.api.container.ManifestContainer;
import org.jboss.shrinkwrap.api.container.ResourceContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
import org.jboss.shrinkwrap.impl.base.test.ArchiveType;
Expand Down Expand Up @@ -112,6 +113,12 @@ protected ArchivePath getClassPath()
throw new UnsupportedOperationException("EnterpriseArchives do not support classes");
}

@Override
protected ServiceProviderContainer<EnterpriseArchive> getServiceProviderContainer()
{
throw new UnsupportedOperationException("EnterpriseArchives do not support service provider classes");
}

@Override
protected LibraryContainer<EnterpriseArchive> getLibraryContainer()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jboss.shrinkwrap.api.container.LibraryContainer;
import org.jboss.shrinkwrap.api.container.ManifestContainer;
import org.jboss.shrinkwrap.api.container.ResourceContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.impl.base.GenericArchiveImpl;
import org.jboss.shrinkwrap.impl.base.test.ArchiveType;
import org.jboss.shrinkwrap.impl.base.test.DynamicContainerTestBase;
Expand Down Expand Up @@ -121,6 +122,12 @@ protected LibraryContainer<GenericArchive> getLibraryContainer()
{
throw UNSUPPORTED;
}

@Override
protected ServiceProviderContainer<GenericArchive> getServiceProviderContainer()
{
throw UNSUPPORTED;
}

@Override
protected ArchivePath getManifestPath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jboss.shrinkwrap.api.container.LibraryContainer;
import org.jboss.shrinkwrap.api.container.ManifestContainer;
import org.jboss.shrinkwrap.api.container.ResourceContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
import org.jboss.shrinkwrap.impl.base.test.ArchiveType;
Expand Down Expand Up @@ -114,6 +115,12 @@ protected ManifestContainer<JavaArchive> getManifestContainer()
return getArchive();
}

@Override
protected ServiceProviderContainer<JavaArchive> getServiceProviderContainer()
{
return getArchive();
}

@Override
protected LibraryContainer<JavaArchive> getLibraryContainer()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jboss.shrinkwrap.api.container.ManifestContainer;
import org.jboss.shrinkwrap.api.container.ResourceAdapterContainer;
import org.jboss.shrinkwrap.api.container.ResourceContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.api.spec.ResourceAdapterArchive;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
import org.jboss.shrinkwrap.impl.base.test.ArchiveType;
Expand Down Expand Up @@ -118,6 +119,12 @@ protected LibraryContainer<ResourceAdapterArchive> getLibraryContainer()
{
return archive;
}

@Override
protected ServiceProviderContainer<ResourceAdapterArchive> getServiceProviderContainer()
{
throw new UnsupportedOperationException("ResourceAdapterArchive do not support service provider classes");
}

@Override
protected ArchivePath getManifestPath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jboss.shrinkwrap.api.container.LibraryContainer;
import org.jboss.shrinkwrap.api.container.ManifestContainer;
import org.jboss.shrinkwrap.api.container.ResourceContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.api.container.WebContainer;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.impl.base.test.ArchiveType;
Expand Down Expand Up @@ -110,6 +111,12 @@ protected ManifestContainer<WebArchive> getManifestContainer()
return getArchive();
}

@Override
protected ServiceProviderContainer<WebArchive> getServiceProviderContainer()
{
return getArchive();
}

@Override
protected ResourceContainer<WebArchive> getResourceContainer()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.jboss.shrinkwrap.api.container.LibraryContainer;
import org.jboss.shrinkwrap.api.container.ManifestContainer;
import org.jboss.shrinkwrap.api.container.ResourceContainer;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.impl.base.TestIOUtil;
import org.jboss.shrinkwrap.impl.base.asset.AssetUtil;
Expand Down Expand Up @@ -82,6 +83,7 @@ public abstract class DynamicContainerTestBase<T extends Archive<T>> extends Arc
protected abstract ClassContainer<T> getClassContainer();
protected abstract ArchivePath getManifestPath();
protected abstract ManifestContainer<T> getManifestContainer();
protected abstract ServiceProviderContainer<T> getServiceProviderContainer();
protected abstract ArchivePath getLibraryPath();
protected abstract LibraryContainer<T> getLibraryContainer();

Expand Down Expand Up @@ -381,12 +383,28 @@ public void testAddManifestPathTargetAsset() throws Exception {
@ArchiveType(ManifestContainer.class)
public void testAddServiceProvider() throws Exception {
getManifestContainer().addAsServiceProvider(DummyInterfaceForTest.class, DummyClassForTest.class);

ArchivePath testPath = new BasicPath(getManifestPath(), "services/" + DummyInterfaceForTest.class.getName());
Assert.assertTrue(
"Archive should contain " + testPath,
getArchive().contains(testPath));
}

@Test
@ArchiveType(ServiceProviderContainer.class)
public void testAddServiceProviderWithClasses() throws Exception {
getServiceProviderContainer().addAsServiceProviderAndClasses(DummyInterfaceForTest.class, DummyClassForTest.class);

ArchivePath testPath = new BasicPath(getManifestPath(), "services/" + DummyInterfaceForTest.class.getName());
Assert.assertTrue("Archive should contain " + testPath, getArchive().contains(testPath));

Class<?>[] expectedResources = {DummyInterfaceForTest.class, DummyClassForTest.class};
for (Class<?> expectedResource : expectedResources)
{
ArchivePath expectedClassPath = new BasicPath(getClassPath(), AssetUtil.getFullPathForClassResource(expectedResource));
assertContainsClass(expectedClassPath);
}
}

@Test
@ArchiveType(ManifestContainer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.api.container.WebContainer;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.impl.base.asset.AssetUtil;
Expand Down Expand Up @@ -478,6 +479,31 @@ public void testAddServiceProvider() throws Exception
Assert.assertTrue("Archive should contain " + testPath, webArchive.contains(testPath));
}

/**
* Override to handle web archive special case (service providers in WEB-INF/classes/META-INF/services)
* @throws Exception if an exception occurs
*/
@Test
@Override
@ArchiveType(ServiceProviderContainer.class)
public void testAddServiceProviderWithClasses() throws Exception {
ServiceProviderPathExposingWebArchive webArchive = new ServiceProviderPathExposingWebArchive(ShrinkWrap.create(WebArchive.class));
webArchive.addAsServiceProviderAndClasses(DummyInterfaceForTest.class, DummyClassForTest.class);

ArchivePath testPath = webArchive.getServiceProvidersPath();
Assert.assertTrue("Archive should contain " + testPath, webArchive.contains(testPath));

testPath = new BasicPath(webArchive.getServiceProvidersPath(), DummyInterfaceForTest.class.getName());
Assert.assertTrue("Archive should contain " + testPath, webArchive.contains(testPath));

Class<?>[] expectedResources = {DummyInterfaceForTest.class, DummyClassForTest.class};
for (Class<?> expectedResource : expectedResources)
{
ArchivePath expectedClassPath = new BasicPath(getClassPath(), AssetUtil.getFullPathForClassResource(expectedResource));
Assert.assertTrue("Archive should contain " + testPath, webArchive.contains(expectedClassPath));
}
}

private class ServiceProviderPathExposingWebArchive extends WebArchiveImpl
{
private ServiceProviderPathExposingWebArchive(final Archive<?> delegate)
Expand Down

0 comments on commit 2fe40c0

Please sign in to comment.