package org.jboss.net.axis.server;
import org.jboss.axis.AxisProperties;
import org.jboss.axis.EngineConfiguration;
import org.jboss.axis.EngineConfigurationFactory;
import org.jboss.axis.configuration.EngineConfigurationFactoryFinder;
import org.jboss.axis.deployment.wsdd.WSDDUndeployment;
import org.jboss.axis.server.AxisServer;
import org.jboss.deployment.DeploymentException;
import org.jboss.deployment.DeploymentInfo;
import org.jboss.deployment.SubDeployerSupport;
import org.jboss.metadata.MetaData;
import org.jboss.net.axis.AttacheableService;
import org.jboss.net.axis.Deployment;
import org.jboss.net.axis.XMLResourceProvider;
import org.jboss.util.xml.JBossEntityResolver;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.management.MBeanRegistration;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.rpc.server.ServiceLifecycle;
import java.net.URL;
public class AxisService
extends SubDeployerSupport
implements AxisServiceMBean, MBeanRegistration
{
protected AxisServer axisServer;
protected XMLResourceProvider clientConfiguration;
protected XMLResourceProvider serverConfiguration;
protected InitialContext initialContext;
protected DocumentBuilder documentBuilder;
protected boolean validateDTDs;
public AxisService()
{
}
protected void copyChildren(Document sourceDoc,
Element source,
String match,
Element target)
{
NodeList children = source.getChildNodes();
for (int count = 0; count < children.getLength(); count++)
{
Node actNode = children.item(count);
if (actNode instanceof Element)
{
if (((Element)actNode).getLocalName().equals(match))
{
target.appendChild(sourceDoc.importNode(actNode, true));
}
}
}
}
protected void startAxisDeployment(String deploymentName, Element root, ClassLoader deploymentLoader)
throws DeploymentException
{
try
{
Document deployDoc = documentBuilder.newDocument();
Document deployClientDoc = documentBuilder.newDocument();
Element deploy =
deployDoc.createElementNS(root.getNamespaceURI(), "deployment");
Element deployClient =
deployClientDoc.createElementNS(root.getNamespaceURI(),
"deployment");
NamedNodeMap attributes = root.getAttributes();
for (int count = 0; count < attributes.getLength(); count++)
{
Attr attribute = (Attr)attributes.item(count);
deploy.setAttributeNodeNS((Attr)deployDoc.importNode(attribute, true));
deployClient.setAttributeNodeNS((Attr)deployClientDoc.importNode(attribute, true));
}
NodeList children = root.getChildNodes();
for (int count = 0; count < children.getLength(); count++)
{
Node actNode = children.item(count);
if (actNode instanceof Element)
{
Element actElement = (Element)actNode;
if (actElement.getTagName().equals("ejb-ref"))
{
String refName =
MetaData.getElementContent(MetaData.getUniqueChild((Element)actNode,
"ejb-ref-name"));
String linkName =
MetaData.getElementContent(MetaData.getUniqueChild((Element)actNode, "ejb-link"));
log.warn("Web Service Deployment "
+ deploymentName
+ " makes use of the deprecated ejb-ref feature. "
+ "Please adjust any ejb-providing service tag inside your web-service.xml pointing to "
+ refName
+ " to use the absolute "
+ linkName
+ " instead.");
}
else if (actElement.getTagName().equals("ext-service"))
{
deployExternalWebService(actElement);
}
else
{
if (!actElement.getTagName().equals("service"))
{
deployClient.appendChild(deployClientDoc.importNode(actNode, true));
}
deploy.appendChild(deployDoc.importNode(actNode, true));
}
}
else
{
deployClient.appendChild(deployClientDoc.importNode(actNode, true));
deploy.appendChild(deployDoc.importNode(actNode, true));
}
}
deployDoc.appendChild(deploy);
deployClientDoc.appendChild(deployClient);
Deployment.makeSafeDeployment(deploy, deploymentLoader).deployToRegistry(((XMLResourceProvider)axisServer.getConfig()).getDeployment());
Deployment.makeSafeDeployment(deployClient, deploymentLoader).deployToRegistry(clientConfiguration.getDeployment());
axisServer.refreshGlobalOptions();
axisServer.saveConfiguration();
}
catch (Exception e)
{
throw new DeploymentException(Constants.COULD_NOT_DEPLOY_DESCRIPTOR,
e);
}
}
protected void stopAxisDeployment(Element root) throws DeploymentException
{
try
{
Document undeployDoc = documentBuilder.newDocument();
Element undeploy =
undeployDoc.createElementNS(root.getNamespaceURI(), "undeployment");
NamedNodeMap attributes = root.getAttributes();
for (int count = 0; count < attributes.getLength(); count++)
{
Attr attribute = (Attr)attributes.item(count);
undeploy.setAttributeNodeNS((Attr)undeployDoc.importNode(attribute, true));
}
NodeList children = root.getElementsByTagName("ext-service");
for (int count = 0; count < children.getLength(); count++)
{
Element actNode = (Element)children.item(count);
undeployExternalWebService(actNode);
}
copyChildren(undeployDoc, root, "service", undeploy);
children = root.getElementsByTagName("service");
for (int count = 0; count < children.getLength(); count++)
{
Element actNode = (Element)children.item(count);
String serviceName = actNode.getAttribute("name");
ServiceLifecycle slc = (ServiceLifecycle)axisServer.
getApplicationSession().get(serviceName);
if (slc != null)
{
slc.destroy();
axisServer.getApplicationSession().remove(serviceName);
}
}
copyChildren(undeployDoc, root, "handler", undeploy);
copyChildren(undeployDoc, root, "typemapping", undeploy);
copyChildren(undeployDoc, root, "beanmapping", undeploy);
undeployDoc.appendChild(undeploy);
new WSDDUndeployment(undeploy).undeployFromRegistry(((XMLResourceProvider)axisServer.getConfig()).getDeployment());
new WSDDUndeployment(undeploy).undeployFromRegistry(clientConfiguration.getDeployment());
axisServer.refreshGlobalOptions();
axisServer.saveConfiguration();
}
catch (Exception e)
{
throw new DeploymentException(Constants.COULD_NOT_UNDEPLOY, e);
}
}
protected synchronized void deployExternalWebService(Element deployElement)
throws DeploymentException
{
try
{
if (initialContext == null)
{
initialContext = new InitialContext();
}
NamedNodeMap attributes = deployElement.getAttributes();
String jndiName = attributes.getNamedItem("jndiName").getNodeValue();
String serviceClassName =
attributes.getNamedItem("serviceImplClass").getNodeValue();
Object factory =
new AttacheableService(serviceClassName,
serviceName.getCanonicalName());
initialContext.bind(jndiName, factory);
}
catch (NamingException e)
{
throw new DeploymentException("Could not deploy item " + deployElement,
e);
}
}
protected synchronized void undeployExternalWebService(Element deployElement)
{
try
{
if (initialContext == null)
{
initialContext = new InitialContext();
}
NamedNodeMap attributes = deployElement.getAttributes();
String jndiName = attributes.getNamedItem("jndiName").getNodeValue();
if (jndiName != null)
{
initialContext.unbind(jndiName);
}
}
catch (NamingException e)
{
log.warn("Could not undeploy " + deployElement,
e);
}
}
protected void startService() throws Exception
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(validateDTDs);
documentBuilder = dbf.newDocumentBuilder();
documentBuilder.setEntityResolver(new JBossEntityResolver());
URL resource =
getClass().getClassLoader().getResource(Constants.AXIS_SERVER_CONFIGURATION_FILE);
if (resource == null)
{
log.warn("Could not find: " + Constants.AXIS_SERVER_CONFIGURATION_FILE);
throw new Exception(Constants.COULD_NOT_FIND_AXIS_CONFIGURATION_0);
}
serverConfiguration = new XMLResourceProvider(resource, getClass().getClassLoader());
axisServer = new AxisServer(serverConfiguration);
serverConfiguration.getGlobalOptions().put(org.jboss.net.axis.Constants.CONFIGURATION_CONTEXT,
serviceName.toString());
resource =
getClass().getClassLoader().getResource(Constants.AXIS_CLIENT_CONFIGURATION_FILE);
if (resource == null)
{
log.warn("Failed to find: " + Constants.AXIS_SERVER_CONFIGURATION_FILE);
throw new Exception(Constants.COULD_NOT_FIND_AXIS_CONFIGURATION_0);
}
clientConfiguration = new XMLResourceProvider(resource, getClass().getClassLoader());
clientConfiguration.buildDeployment();
clientConfiguration.getGlobalOptions().put(org.jboss.net.axis.Constants.CONFIGURATION_CONTEXT,
serviceName.toString());
AxisProperties.getNameDiscoverer();
Class initializeThisStaticStuff =
EngineConfigurationFactoryFinder.class;
System.setProperty(EngineConfigurationFactory.SYSTEM_PROPERTY_NAME,
JMXEngineConfigurationFactory.class.getName());
super.startService();
}
protected void stopService() throws Exception
{
super.stopService();
axisServer.stop();
documentBuilder = null;
}
public boolean accepts(DeploymentInfo sdi)
{
if (sdi.shortName.endsWith("-axis.xml")
|| sdi.localCl.getResource(Constants.WEB_SERVICE_DESCRIPTOR) != null)
{
return true;
}
return false;
}
public void init(DeploymentInfo sdi) throws DeploymentException
{
super.init(sdi);
try
{
if (sdi.document == null)
{
if (sdi.documentUrl == null)
{
if (sdi.isXML)
{
sdi.documentUrl = sdi.localUrl;
}
else
{
sdi.documentUrl =
sdi.localCl.getResource(Constants.WEB_SERVICE_DESCRIPTOR);
}
}
sdi.document = documentBuilder.parse(sdi.documentUrl.openStream());
}
if (sdi.isDirectory)
{
sdi.watch = new URL(sdi.url, Constants.WEB_SERVICE_DESCRIPTOR);
}
else
{
sdi.watch = sdi.url;
}
}
catch (Exception e)
{
throw new DeploymentException(e);
}
}
public void create(DeploymentInfo sdi) throws DeploymentException
{
log.debug("create, sdi=" + sdi);
super.create(sdi);
}
public void start(DeploymentInfo sdi) throws DeploymentException
{
log.debug("start, sdi=" + sdi);
super.start(sdi);
start(sdi.toString(), sdi.document, sdi.ucl);
}
public void start(String deploymentName,
Document doc,
ClassLoader serviceLoader)
throws DeploymentException
{
Element root = doc.getDocumentElement();
if (root.getNodeName().equals("deployments"))
{
NodeList children = root.getChildNodes();
for (int count = 0; count < children.getLength(); count++)
{
startAxisDeployment(deploymentName,
(Element)children.item(count), serviceLoader);
}
}
else
{
startAxisDeployment(deploymentName, root, serviceLoader);
}
}
public void stop(DeploymentInfo sdi) throws DeploymentException
{
log.debug("stop, sdi=" + sdi);
super.stop(sdi);
stop(sdi.document);
}
public void stop(Document doc) throws DeploymentException
{
Element root = (Element)doc.getDocumentElement();
if (root.getNodeName().equals("deployments"))
{
NodeList children = root.getChildNodes();
for (int count = 0; count < children.getLength(); count++)
{
stopAxisDeployment((Element)children.item(count));
}
}
else
{
stopAxisDeployment(root);
}
}
public void destroy(DeploymentInfo sdi) throws DeploymentException
{
log.debug("destroy, sdi=" + sdi);
super.destroy(sdi);
}
public EngineConfiguration getClientEngineConfiguration()
{
return clientConfiguration;
}
public EngineConfiguration getServerEngineConfiguration()
{
return serverConfiguration;
}
public AxisServer getAxisServer()
{
return axisServer;
}
}