Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[SHRINKWRAP-336] Provide addAsServiceProvider overload that supports String #47

Open
wants to merge 1 commit into from

1 participant

@DavideD

I hope is correct.

Cheers

@DavideD DavideD commented on the diff
.../shrinkwrap/impl/base/asset/ServiceProviderAsset.java
@@ -53,8 +72,8 @@ public ServiceProviderAsset(Class<?>... providerImpls) {
@Override
public InputStream openStream() {
StringBuilder content = new StringBuilder();
- for (Class<?> providerImpl : providerImpls) {
- content.append(providerImpl.getName()).append('\n');
+ for (String providerImpl : providerImpls) {
+ content.append(providerImpl).append('\n');
@DavideD
DavideD added a note

Shuoldn't be used System.getProperty("line.separator") instead of '\n'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@DavideD

Shame on me

Is this solution better? I've moved the getServiceProvidersPath method from WebContainerBase to ContainerBase so that I now have a method for the location of the services.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
16 api/src/main/java/org/jboss/shrinkwrap/api/container/ManifestContainer.java
@@ -374,6 +374,22 @@ T addAsManifestResource(Package resourcePackage, String resourceName, ArchivePat
T addAsServiceProvider(Class<?> serviceInterface, Class<?>... serviceImpls) throws IllegalArgumentException;
/**
+ * Adds a META-INF/services/ServiceInterfaceName {@link Asset} representing this service.
+ *
+ * @param serviceInterface
+ * The Service Interface class name
+ * @param serviceImpls
+ * The Service Interface Implementations class names
+ * @return This virtual archive
+ * @throws IllegalArgumentException
+ * if serviceInterface is null
+ * @throws IllegalArgumentException
+ * if serviceImpls is null or contain null values
+ * @see ManifestContainer#addAsServiceProvider(Class, Class...)
+ */
+ T addAsServiceProvider(String serviceInterface, String... serviceImpls);
+
+ /**
* Adds a default generated MANIFEST.MF manifest to the current archive.
*
* @return This virtual archive
View
25 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/asset/ServiceProviderAsset.java
@@ -29,7 +29,8 @@
* @version $Revision: $
*/
public class ServiceProviderAsset implements Asset {
- private Class<?>[] providerImpls;
+
+ private final String[] providerImpls;
/**
* Creates a newline separated text file off the providerImpls class names.
@@ -42,6 +43,24 @@
public ServiceProviderAsset(Class<?>... providerImpls) {
Validate.notNullAndNoNullValues(providerImpls,
"ProviderImpls must be specified and can not contain null values");
+ String[] names = new String[providerImpls.length];
+ for (int i = 0; i < providerImpls.length; i++) {
+ names[i] = providerImpls[i].getName();
+ }
+ this.providerImpls = names;
+ }
+
+ /**
+ * Creates a newline separated text file off the providerImpls class names.
+ *
+ * @param providerImpls
+ * The class names to use
+ * @throws IllegalArgumentException
+ * if providerImpls is null or contain null values
+ */
+ public ServiceProviderAsset(String... providerImpls) {
+ Validate.notNullAndNoNullValues(providerImpls,
+ "ProviderImpls must be specified and can not contain null values");
this.providerImpls = providerImpls;
}
@@ -53,8 +72,8 @@ public ServiceProviderAsset(Class<?>... providerImpls) {
@Override
public InputStream openStream() {
StringBuilder content = new StringBuilder();
- for (Class<?> providerImpl : providerImpls) {
- content.append(providerImpl.getName()).append('\n');
+ for (String providerImpl : providerImpls) {
+ content.append(providerImpl).append('\n');
@DavideD
DavideD added a note

Shuoldn't be used System.getProperty("line.separator") instead of '\n'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
}
return new ByteArrayInputStream(content.toString().getBytes());
}
View
32 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/container/ContainerBase.java
@@ -857,9 +857,35 @@ public T addAsServiceProvider(Class<?> serviceInterface, Class<?>... serviceImpl
Validate.notNull(serviceInterface, "ServiceInterface must be specified");
Validate.notNullAndNoNullValues(serviceImpls, "ServiceImpls must be specified and can not contain null values");
- Asset asset = new ServiceProviderAsset(serviceImpls);
- ArchivePath path = new BasicPath("services", serviceInterface.getName());
- return addAsManifestResource(asset, path);
+ return addAsServiceProvider(serviceInterface.getName(), new ServiceProviderAsset(serviceImpls));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.jboss.shrinkwrap.api.container.ManifestContainer#addServiceProvider(java.lang.String,
+ * java.lang.String[])
+ */
+ @Override
+ public T addAsServiceProvider(String serviceInterface, String... serviceImpls) throws IllegalArgumentException {
+ Validate.notNull(serviceInterface, "ServiceInterface must be specified");
+ Validate.notNullAndNoNullValues(serviceImpls, "ServiceImpls must be specified and can not contain null values");
+
+ return addAsServiceProvider(serviceInterface, new ServiceProviderAsset(serviceImpls));
+ }
+
+ T addAsServiceProvider(String serviceInterface, ServiceProviderAsset asset)
+ {
+ return add(asset, new BasicPath(getServiceProvidersPath(), serviceInterface));
+ }
+
+ /**
+ * Returns the path to container service providers
+ *
+ * @return the path to container service providers
+ */
+ protected ArchivePath getServiceProvidersPath() {
+ return new BasicPath(getManifestPath(), "services");
}
/*
View
24 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/container/WebContainerBase.java
@@ -29,7 +29,6 @@
import org.jboss.shrinkwrap.api.container.WebContainer;
import org.jboss.shrinkwrap.impl.base.Validate;
import org.jboss.shrinkwrap.impl.base.asset.AssetUtil;
-import org.jboss.shrinkwrap.impl.base.asset.ServiceProviderAsset;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
/**
@@ -76,13 +75,6 @@ protected WebContainerBase(Class<T> actualType, Archive<?> archive) {
*/
protected abstract ArchivePath getWebInfPath();
- /**
- * Returns the path to web container service providers
- *
- * @return the path to web container service providers
- */
- protected abstract ArchivePath getServiceProvidersPath();
-
/*
* (non-Javadoc)
*
@@ -549,22 +541,6 @@ public T addAsWebInfResource(final Package resourcePackage, final String resourc
/*
* (non-Javadoc)
*
- * @see org.jboss.shrinkwrap.api.container.ManifestContainer#addServiceProvider(java.lang.Class,
- * java.lang.Class<?>[])
- */
- @Override
- public T addAsServiceProvider(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");
-
- Asset asset = new ServiceProviderAsset(serviceImpls);
- 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<?>[])
*/
View
54 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/DynamicContainerTestBase.java
@@ -16,10 +16,13 @@
*/
package org.jboss.shrinkwrap.impl.base.test;
+import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
@@ -37,6 +40,7 @@
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.Filters;
+import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset;
@@ -47,6 +51,7 @@
import org.jboss.shrinkwrap.api.container.ServiceProviderContainer;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.impl.base.TestIOUtil;
import org.jboss.shrinkwrap.impl.base.asset.AssetUtil;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
@@ -383,6 +388,55 @@ public void testAddServiceProvider() throws Exception {
}
@Test
+ @ArchiveType(ManifestContainer.class)
+ public void testAddServiceProviderString() throws Exception {
+ String[] impls = {"do.not.exist.impl.Dummy1", "do.not.exist.impl.Dummy2", "do.not.exist.impl.Dummy3"};
+ String serviceInterface = "do.not.exist.api.Dummy";
+ getManifestContainer().addAsServiceProvider(serviceInterface, impls);
+
+ ArchivePath testPath = new BasicPath(getManifestPath(), "services/" + serviceInterface);
+ Assert.assertTrue("Archive should contain " + testPath, getArchive().contains(testPath));
+
+ assertServiceProviderContent(getArchive().get(testPath), impls);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ @ArchiveType(ManifestContainer.class)
+ public void testAddServiceProviderStringInterfaceValidation() throws Exception {
+ String[] impls = {"do.not.exist.impl.Dummy1", "do.not.exist.impl.Dummy2", "do.not.exist.impl.Dummy3"};
+ getManifestContainer().addAsServiceProvider(null, impls);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ @ArchiveType(ManifestContainer.class)
+ public void testAddServiceProviderStringImplementationsValidation() throws Exception {
+ getManifestContainer().addAsServiceProvider("do.not.exist.impl.Dummy1", (String[]) null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ @ArchiveType(ManifestContainer.class)
+ public void testAddServiceProviderStringImplementationsValueValidation() throws Exception {
+ String[] impls = {"do.not.exist.impl.Dummy1", null};
+ getManifestContainer().addAsServiceProvider("do.not.exist.impl.Dummy", impls);
+ }
+
+ protected void assertServiceProviderContent(Node node, String[] impls) throws IOException {
+ BufferedReader reader = createReader(node.getAsset());
+ try {
+ for (String impl : impls) {
+ Assert.assertEquals("Wrong entry in service provider: " + impl, impl, reader.readLine());
+ }
+ } finally {
+ reader.close();
+ }
+ }
+
+ private BufferedReader createReader(Asset asset) {
+ InputStream openStream = asset.openStream();
+ return new BufferedReader(new InputStreamReader(openStream));
+ }
+
+ @Test
@ArchiveType(ServiceProviderContainer.class)
public void testAddServiceProviderWithClasses() throws Exception {
getServiceProviderContainer().addAsServiceProviderAndClasses(DummyInterfaceForTest.class,
View
42 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/DynamicWebContainerTestBase.java
@@ -434,6 +434,48 @@ public void testAddServiceProvider() throws Exception {
*/
@Test
@Override
+ public void testAddServiceProviderString() throws Exception {
+ String serviceInterface = "do.not.exist.api.Dummy";
+ String[] impls = {"do.not.exist.impl.Dummy1", "do.not.exist.impl.Dummy2", "do.not.exist.impl.Dummy3"};
+
+ ServiceProviderPathExposingWebArchive webArchive = new ServiceProviderPathExposingWebArchive(
+ ShrinkWrap.create(WebArchive.class));
+ webArchive.addAsServiceProvider(serviceInterface, impls);
+
+ ArchivePath testPath = new BasicPath(webArchive.getServiceProvidersPath(), serviceInterface);
+ Assert.assertTrue("Archive should contain " + testPath, webArchive.contains(testPath));
+
+ assertServiceProviderContent(webArchive.get(testPath), impls);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ @Override
+ public void testAddServiceProviderStringInterfaceValidation() throws Exception {
+ String[] impls = {"do.not.exist.impl.Dummy1", "do.not.exist.impl.Dummy2", "do.not.exist.impl.Dummy3"};
+ ShrinkWrap.create(WebArchive.class).addAsServiceProvider(null, impls);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ @Override
+ public void testAddServiceProviderStringImplementationsValidation() throws Exception {
+ ShrinkWrap.create(WebArchive.class).addAsServiceProvider("do.not.exist.impl.Dummy", (String[]) null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ @Override
+ public void testAddServiceProviderStringImplementationsValueValidation() throws Exception {
+ String[] impls = {"do.not.exist.impl.Dummy1", null};
+ ShrinkWrap.create(WebArchive.class).addAsServiceProvider("do.not.exist.impl.Dummy", impls);
+ }
+
+ /**
+ * 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(
Something went wrong with that request. Please try again.