package org.jboss.shrinkwrap.impl.base.container;
import java.io.File;
import java.net.URL;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset;
import org.jboss.shrinkwrap.api.asset.FileAsset;
import org.jboss.shrinkwrap.api.asset.UrlAsset;
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;
public abstract class WebContainerBase<T extends Archive<T>> extends ContainerBase<T> implements WebContainer<T> {
protected WebContainerBase(Class<T> actualType, Archive<?> archive) {
super(actualType, archive);
}
protected abstract ArchivePath getWebPath();
protected abstract ArchivePath getWebInfPath();
protected abstract ArchivePath getServiceProvidersPath();
@Override
public T setWebXML(String resourceName) throws IllegalArgumentException {
Validate.notNull(resourceName, "ResourceName should be specified");
return setWebXML(new ClassLoaderAsset(resourceName));
}
@Override
public T setWebXML(File resource) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
return setWebXML(new FileAsset(resource));
}
@Override
public T setWebXML(URL resource) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
return setWebXML(new UrlAsset(resource));
}
@Override
public T setWebXML(Asset resource) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
return addAsWebInfResource(resource, "web.xml");
}
@Override
public T setWebXML(Package resourcePackage, String resourceName) throws IllegalArgumentException {
Validate.notNull(resourcePackage, "ResourcePackage must be specified");
Validate.notNull(resourceName, "ResourceName must be specified");
String classloaderResourceName = AssetUtil.getClassLoaderResourceName(resourcePackage, resourceName);
return setWebXML(new ClassLoaderAsset(classloaderResourceName));
}
@Override
public T addAsWebResource(String resourceName) throws IllegalArgumentException {
Validate.notNull(resourceName, "ResourceName should be specified");
return addAsWebResource(new ClassLoaderAsset(resourceName),
AssetUtil.getNameForClassloaderResource(resourceName));
}
@Override
public T addAsWebResource(File resource) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
return addAsWebResource(new FileAsset(resource), resource.getName());
}
@Override
public T addAsWebResource(String resourceName, String target) throws IllegalArgumentException {
Validate.notNull(resourceName, "ResourceName should be specified");
Validate.notNull(target, "Target should be specified");
return addAsWebResource(new ClassLoaderAsset(resourceName), target);
}
@Override
public T addAsWebResource(File resource, String target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNull(target, "Target should be specified");
return addAsWebResource(new FileAsset(resource), target);
}
@Override
public T addAsWebResource(URL resource, String target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNull(target, "Target should be specified");
return addAsWebResource(new UrlAsset(resource), target);
}
@Override
public T addAsWebResource(Asset resource, String target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNull(target, "Target should be specified");
return addAsWebResource(resource, new BasicPath(target));
}
@Override
public T addAsWebResource(String resourceName, ArchivePath target) throws IllegalArgumentException {
Validate.notNull(resourceName, "ResourceName should be specified");
Validate.notNull(target, "Target should be specified");
return addAsWebResource(new ClassLoaderAsset(resourceName), target);
}
@Override
public T addAsWebResource(File resource, ArchivePath target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNull(target, "Target should be specified");
return addAsWebResource(new FileAsset(resource), target);
}
@Override
public T addAsWebResource(URL resource, ArchivePath target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNull(target, "Target should be specified");
return addAsWebResource(new UrlAsset(resource), target);
}
@Override
public T addAsWebResource(Asset resource, ArchivePath target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNull(target, "Target should be specified");
ArchivePath location = new BasicPath(getWebPath(), target);
return add(resource, location);
}
@Override
public T addAsWebResources(Package resourcePackage, String... resourceNames) throws IllegalArgumentException {
Validate.notNull(resourcePackage, "ResourcePackage must be specified");
Validate.notNullAndNoNullValues(resourceNames,
"ResourceNames must be specified and can not container null values");
for (final String resourceName : resourceNames) {
addAsWebResource(resourcePackage, resourceName);
}
return covarientReturn();
}
@Override
public T addAsWebResource(Package resourcePackage, String resourceName) throws IllegalArgumentException {
Validate.notNull(resourcePackage, "ResourcePackage must be specified");
Validate.notNull(resourceName, "ResourceName must be specified");
String classloaderResourceName = AssetUtil.getClassLoaderResourceName(resourcePackage, resourceName);
ArchivePath target = ArchivePaths.create(classloaderResourceName);
return addAsWebResource(resourcePackage, resourceName, target);
}
@Override
public T addAsWebResource(Package resourcePackage, String resourceName, String target)
throws IllegalArgumentException {
Validate.notNull(resourcePackage, "ResourcePackage must be specified");
Validate.notNull(resourceName, "ResourceName must be specified");
Validate.notNull(target, "Target must be specified");
return addAsWebResource(resourcePackage, resourceName, ArchivePaths.create(target));
}
@Override
public T addAsWebResource(Package resourcePackage, String resourceName, ArchivePath target)
throws IllegalArgumentException {
Validate.notNull(resourcePackage, "ResourcePackage must be specified");
Validate.notNull(resourceName, "ResourceName must be specified");
Validate.notNull(target, "Target must be specified");
String classloaderResourceName = AssetUtil.getClassLoaderResourceName(resourcePackage, resourceName);
Asset resource = new ClassLoaderAsset(classloaderResourceName);
return addAsWebResource(resource, target);
}
@Override
public T addAsWebInfResource(final String resourceName) throws IllegalArgumentException {
Validate.notNull(resourceName, "ResourceName should be specified");
return addAsWebInfResource(new ClassLoaderAsset(resourceName),
AssetUtil.getNameForClassloaderResource(resourceName));
}
@Override
public T addAsWebInfResource(final File resource) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
return addAsWebInfResource(new FileAsset(resource), resource.getName());
}
@Override
public T addAsWebInfResource(final String resourceName, final String target) throws IllegalArgumentException {
Validate.notNull(resourceName, "ResourceName should be specified");
Validate.notNull(target, "Target should be specified");
return addAsWebInfResource(new ClassLoaderAsset(resourceName), target);
}
@Override
public T addAsWebInfResource(final File resource, final String target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNullOrEmpty(target, "Target should be specified");
return addAsWebInfResource(new FileAsset(resource), target);
}
@Override
public T addAsWebInfResource(final URL resource, final String target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNullOrEmpty(target, "Target should be specified");
return addAsWebInfResource(new UrlAsset(resource), target);
}
@Override
public T addAsWebInfResource(final Asset resource, final String target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNullOrEmpty(target, "Target should be specified");
return addAsWebInfResource(resource, ArchivePaths.create(target));
}
@Override
public T addAsWebInfResource(final String resourceName, final ArchivePath target) throws IllegalArgumentException {
Validate.notNull(resourceName, "ResourceName should be specified");
Validate.notNull(target, "Target should be specified");
return addAsWebInfResource(new ClassLoaderAsset(resourceName), target);
}
@Override
public T addAsWebInfResource(final File resource, final ArchivePath target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNull(target, "Target should be specified");
return addAsWebInfResource(new FileAsset(resource), target);
}
@Override
public T addAsWebInfResource(final URL resource, final ArchivePath target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNull(target, "Target should be specified");
return addAsWebInfResource(new UrlAsset(resource), target);
}
@Override
public T addAsWebInfResource(final Asset resource, final ArchivePath target) throws IllegalArgumentException {
Validate.notNull(resource, "Resource should be specified");
Validate.notNull(target, "Target should be specified");
final ArchivePath location = new BasicPath(getWebInfPath(), target);
return add(resource, location);
}
@Override
public T addAsWebInfResources(final Package resourcePackage, final String... resourceNames)
throws IllegalArgumentException {
Validate.notNull(resourcePackage, "ResourcePackage must be specified");
Validate.notNullAndNoNullValues(resourceNames,
"ResourceNames must be specified and can not container null values");
for (String resourceName : resourceNames) {
addAsWebInfResource(resourcePackage, resourceName);
}
return covarientReturn();
}
@Override
public T addAsWebInfResource(final Package resourcePackage, final String resourceName)
throws IllegalArgumentException {
Validate.notNull(resourcePackage, "ResourcePackage must be specified");
Validate.notNull(resourceName, "ResourceName must be specified");
final String classloaderResourceName = AssetUtil.getClassLoaderResourceName(resourcePackage, resourceName);
final ArchivePath target = ArchivePaths.create(classloaderResourceName);
return addAsWebInfResource(resourcePackage, resourceName, target);
}
@Override
public T addAsWebInfResource(final Package resourcePackage, final String resourceName, final String target)
throws IllegalArgumentException {
Validate.notNull(resourcePackage, "ResourcePackage must be specified");
Validate.notNullOrEmpty(resourceName, "ResourceName must be specified");
Validate.notNullOrEmpty(target, "Target must be specified");
return addAsWebInfResource(resourcePackage, resourceName, ArchivePaths.create(target));
}
@Override
public T addAsWebInfResource(final Package resourcePackage, final String resourceName, ArchivePath target)
throws IllegalArgumentException {
Validate.notNull(resourcePackage, "ResourcePackage must be specified");
Validate.notNullOrEmpty(resourceName, "ResourceName must be specified");
Validate.notNull(target, "Target must be specified");
final String classloaderResourceName = AssetUtil.getClassLoaderResourceName(resourcePackage, resourceName);
final Asset resource = new ClassLoaderAsset(classloaderResourceName);
return addAsWebInfResource(resource, target);
}
@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);
}
@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");
Asset asset = new ServiceProviderAsset(serviceImpls);
ArchivePath path = new BasicPath(getServiceProvidersPath(), serviceInterface);
return add(asset, path);
}
@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);
}
}