package org.jboss.deployment.spi.factories;
import org.jboss.deployment.spi.DeploymentManagerImpl;
import org.jboss.logging.Logger;
import javax.enterprise.deploy.spi.DeploymentManager;
import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
import javax.enterprise.deploy.spi.factories.DeploymentFactory;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
public class DeploymentFactoryImpl implements DeploymentFactory
{
private static final Logger log = Logger.getLogger(DeploymentFactoryImpl.class);
private static String DISPLAY_NAME;
private static String PRODUCT_VERSION;
static
{
Package pkg = Package.getPackage("org.jboss.deploy.spi.factories");
if (pkg != null)
{
DISPLAY_NAME = pkg.getImplementationVendor();
PRODUCT_VERSION = pkg.getImplementationVersion();
}
if (DISPLAY_NAME == null || PRODUCT_VERSION == null)
{
DISPLAY_NAME = "DeploymentFactoryImpl";
PRODUCT_VERSION = "1.1";
}
}
public boolean handlesURI(String uri)
{
boolean handlesURI = false;
try
{
URI deployURI = parseURI(uri);
String scheme = deployURI.getScheme();
if (scheme.equals(DeploymentManagerImpl.DEPLOYER_SCHEME))
handlesURI = true;
}
catch (URISyntaxException e)
{
log.warn("Failed to parse uri: " + uri, e);
}
log.debug("handlesURI [" + uri + "]: " + handlesURI);
return handlesURI;
}
public DeploymentManager getDeploymentManager(String uri, String userName, String password)
throws DeploymentManagerCreationException
{
log.debug("getDeploymentManager (uri=" + uri + ")");
if (handlesURI(uri) == false)
throw new DeploymentManagerCreationException("Cannot handle URI: " + uri);
DeploymentManager mgr = null;
try
{
URI deployURI = parseURI(uri);
mgr = new DeploymentManagerImpl(deployURI, true, userName, password);
}
catch (URISyntaxException e)
{
DeploymentManagerCreationException ex =
new DeploymentManagerCreationException("Failed to create DeploymentManagerImpl");
ex.initCause(e);
throw ex;
}
return mgr;
}
public DeploymentManager getDisconnectedDeploymentManager(String uri)
throws DeploymentManagerCreationException
{
log.debug("getDisconnectedDeploymentManager (uri=" + uri + ")");
if (handlesURI(uri) == false)
throw new DeploymentManagerCreationException("Cannot handle URI: " + uri);
DeploymentManager mgr = null;
try
{
URI deployURI = parseURI(uri);
mgr = new DeploymentManagerImpl(deployURI, false);
}
catch (URISyntaxException e)
{
DeploymentManagerCreationException ex =
new DeploymentManagerCreationException("Failed to create DeploymentManagerImpl");
ex.initCause(e);
throw ex;
}
return mgr;
}
public String getDisplayName()
{
return DISPLAY_NAME;
}
public String getProductVersion()
{
return PRODUCT_VERSION;
}
private URI parseURI(String uri) throws URISyntaxException
{
URI deployURI;
try
{
if (uri.equals(DeploymentManagerImpl.DEPLOYER_SCHEME))
deployURI = new URI(uri + "://" + InetAddress.getLocalHost().getHostName());
else
deployURI = new URI(uri);
}
catch (UnknownHostException e)
{
throw new URISyntaxException(uri, e.toString());
}
return deployURI;
}
}