package org.jboss.axis.deployment.wsdd;
import org.jboss.axis.AxisEngine;
import org.jboss.axis.AxisFault;
import org.jboss.axis.ConfigurationException;
import org.jboss.axis.Constants;
import org.jboss.axis.EngineConfiguration;
import org.jboss.axis.FaultableHandler;
import org.jboss.axis.Handler;
import org.jboss.axis.MessageContext;
import org.jboss.axis.attachments.Attachments;
import org.jboss.axis.attachments.AttachmentsImpl;
import org.jboss.axis.description.ServiceDesc;
import org.jboss.axis.encoding.DeserializerFactory;
import org.jboss.axis.encoding.SerializationContext;
import org.jboss.axis.encoding.SerializerFactory;
import org.jboss.axis.encoding.TypeMapping;
import org.jboss.axis.encoding.TypeMappingRegistry;
import org.jboss.axis.encoding.TypeMappingRegistryImpl;
import org.jboss.axis.encoding.ser.BaseDeserializerFactory;
import org.jboss.axis.encoding.ser.BaseSerializerFactory;
import org.jboss.axis.enums.Style;
import org.jboss.axis.enums.Use;
import org.jboss.axis.handlers.HandlerInfoChainFactory;
import org.jboss.axis.handlers.soap.SOAPService;
import org.jboss.axis.providers.java.JavaProvider;
import org.jboss.axis.utils.Messages;
import org.jboss.axis.utils.XMLUtils;
import org.jboss.logging.Logger;
import org.w3c.dom.Element;
import org.xml.sax.helpers.AttributesImpl;
import javax.xml.namespace.QName;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Vector;
public class WSDDService
extends WSDDTargetedChain
implements WSDDTypeMappingContainer
{
private static Logger log = Logger.getLogger(WSDDService.class.getName());
private TypeMappingRegistry tmr = null;
private Vector faultFlows = new Vector();
private Vector typeMappings = new Vector();
private Vector operations = new Vector();
private Vector namespaces = new Vector();
private String descriptionURL;
private Style style = Style.DEFAULT;
private Use use = Use.DEFAULT;
private SOAPService cachedService = null;
private QName providerQName;
private WSDDJAXRPCHandlerInfoChain _wsddHIchain;
ServiceDesc desc = new ServiceDesc();
private boolean streaming = false;
private int sendType = Attachments.SEND_TYPE_NOTSET;
public WSDDService()
{
}
public WSDDService(Element e)
throws WSDDException
{
super(e);
desc.setName(getQName().getLocalPart());
String styleStr = e.getAttribute(ATTR_STYLE);
if (styleStr != null && !styleStr.equals(""))
{
style = Style.getStyle(styleStr, Style.DEFAULT);
desc.setStyle(style);
providerQName = style.getProvider();
}
String useStr = e.getAttribute(ATTR_USE);
if (useStr != null && !useStr.equals(""))
{
use = Use.getUse(useStr, Use.DEFAULT);
desc.setUse(use);
}
else
{
if (style != Style.RPC)
{
use = Use.LITERAL;
desc.setUse(use);
}
}
String streamStr = e.getAttribute(ATTR_STREAMING);
if (streamStr != null && streamStr.equals("on"))
{
streaming = true;
}
String attachmentStr = e.getAttribute(ATTR_ATTACHMENT_FORMAT);
if (attachmentStr != null && !attachmentStr.equals(""))
{
sendType = AttachmentsImpl.getSendType(attachmentStr);
}
Element[] operationElements = getChildElements(e, ELEM_WSDD_OPERATION);
for (int i = 0; i < operationElements.length; i++)
{
WSDDOperation operation = new WSDDOperation(operationElements[i],
desc);
addOperation(operation);
}
Element[] typeMappingElements = getChildElements(e, ELEM_WSDD_TYPEMAPPING);
for (int i = 0; i < typeMappingElements.length; i++)
{
WSDDTypeMapping mapping =
new WSDDTypeMapping(typeMappingElements[i]);
typeMappings.add(mapping);
}
Element[] beanMappingElements = getChildElements(e, ELEM_WSDD_BEANMAPPING);
for (int i = 0; i < beanMappingElements.length; i++)
{
WSDDBeanMapping mapping =
new WSDDBeanMapping(beanMappingElements[i]);
typeMappings.add(mapping);
}
Element[] namespaceElements = getChildElements(e, ELEM_WSDD_NAMESPACE);
for (int i = 0; i < namespaceElements.length; i++)
{
String ns = XMLUtils.getChildCharacterData(namespaceElements[i]);
namespaces.add(ns);
}
if (!namespaces.isEmpty())
desc.setNamespaceMappings(namespaces);
Element wsdlElem = getChildElement(e, ELEM_WSDD_WSDLFILE);
if (wsdlElem != null)
{
String fileName = XMLUtils.getChildCharacterData(wsdlElem);
desc.setWSDLFile(fileName);
}
Element urlElem = getChildElement(e, ELEM_WSDD_ENDPOINTURL);
if (urlElem != null)
{
String endpointURL = XMLUtils.getChildCharacterData(urlElem);
desc.setEndpointURL(endpointURL);
}
String providerStr = e.getAttribute(ATTR_PROVIDER);
if (providerStr != null && !providerStr.equals(""))
{
providerQName = XMLUtils.getQNameFromString(providerStr, e);
if (WSDDConstants.QNAME_JAVAMSG_PROVIDER.equals(providerQName))
{
desc.setStyle(Style.MESSAGE);
}
}
Element hcEl = getChildElement(e, ELEM_WSDD_JAXRPC_CHAIN);
if (hcEl != null)
{
_wsddHIchain = new WSDDJAXRPCHandlerInfoChain(hcEl);
}
initTMR();
validateDescriptors();
}
protected void initTMR() throws WSDDException
{
if (tmr == null)
{
tmr = new TypeMappingRegistryImpl();
for (int i = 0; i < typeMappings.size(); i++)
{
deployTypeMapping((WSDDTypeMapping)
typeMappings.get(i));
}
}
}
public void validateDescriptors() throws WSDDException
{
if (tmr == null)
{
initTMR();
}
desc.setTypeMappingRegistry(tmr);
desc.setTypeMapping(getTypeMapping(desc.getUse().getEncoding()));
String allowedMethods = getParameter(JavaProvider.OPTION_ALLOWEDMETHODS);
if (allowedMethods != null && !"*".equals(allowedMethods))
{
ArrayList methodList = new ArrayList();
StringTokenizer tokenizer = new StringTokenizer(allowedMethods, " ,");
while (tokenizer.hasMoreTokens())
{
methodList.add(tokenizer.nextToken());
}
desc.setAllowedMethods(methodList);
}
}
public void addTypeMapping(WSDDTypeMapping mapping)
{
typeMappings.add(mapping);
}
public void addOperation(WSDDOperation operation)
{
operations.add(operation);
desc.addOperationDesc(operation.getOperationDesc());
}
protected QName getElementName()
{
return QNAME_SERVICE;
}
public String getServiceDescriptionURL()
{
return descriptionURL;
}
public void setServiceDescriptionURL(String sdUrl)
{
descriptionURL = sdUrl;
}
public QName getProviderQName()
{
return providerQName;
}
public void setProviderQName(QName providerQName)
{
this.providerQName = providerQName;
}
public ServiceDesc getServiceDesc()
{
return desc;
}
public Style getStyle()
{
return style;
}
public void setStyle(Style style)
{
this.style = style;
}
public Use getUse()
{
return use;
}
public void setUse(Use use)
{
this.use = use;
}
public WSDDFaultFlow[] getFaultFlows()
{
WSDDFaultFlow[] t = new WSDDFaultFlow[faultFlows.size()];
faultFlows.toArray(t);
return t;
}
public Vector getNamespaces()
{
return namespaces;
}
public WSDDFaultFlow getFaultFlow(QName name)
{
WSDDFaultFlow[] t = getFaultFlows();
for (int n = 0; n < t.length; n++)
{
if (t[n].getQName().equals(name))
{
return t[n];
}
}
return null;
}
public Handler makeNewInstance(EngineConfiguration registry)
throws ConfigurationException
{
if (cachedService != null)
{
return cachedService;
}
initTMR();
Handler reqHandler = null;
WSDDChain request = getRequestFlow();
if (request != null)
{
reqHandler = request.getInstance(registry);
}
Handler providerHandler = null;
if (providerQName != null)
{
try
{
providerHandler = WSDDProvider.getInstance(providerQName,
this,
registry);
}
catch (Exception e)
{
throw new ConfigurationException(e);
}
if (providerHandler == null)
throw new WSDDException(Messages.getMessage("couldntConstructProvider00"));
}
Handler respHandler = null;
WSDDChain response = getResponseFlow();
if (response != null)
{
respHandler = response.getInstance(registry);
}
SOAPService service = new SOAPService(reqHandler, providerHandler,
respHandler);
service.setStyle(style);
service.setUse(use);
service.setHighFidelityRecording(!streaming);
service.setSendType(sendType);
if (getQName() != null)
service.setName(getQName().getLocalPart());
service.setOptions(getParametersTable());
service.setEngine(((WSDDDeployment)registry).getEngine());
if (use != Use.ENCODED)
{
service.setOption(AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
service.setOption(AxisEngine.PROP_SEND_XSI, Boolean.FALSE);
}
if (_wsddHIchain != null)
{
HandlerInfoChainFactory hiChainFactory = _wsddHIchain.getHandlerChainFactory();
service.setOption(Constants.ATTR_HANDLERINFOCHAIN, hiChainFactory);
}
AxisEngine.normaliseOptions(service);
tmr.delegate(registry.getTypeMappingRegistry());
WSDDFaultFlow[] faultFlows = getFaultFlows();
if (faultFlows != null && faultFlows.length > 0)
{
FaultableHandler wrapper = new FaultableHandler(service);
for (int i = 0; i < faultFlows.length; i++)
{
WSDDFaultFlow flow = faultFlows[i];
Handler faultHandler = flow.getInstance(registry);
wrapper.setOption("fault-" + flow.getQName().getLocalPart(),
faultHandler);
}
}
service.setServiceDescription(desc);
try
{
service.getInitializedServiceDesc(MessageContext.getCurrentContext());
}
catch (AxisFault axisFault)
{
throw new ConfigurationException(axisFault);
}
cachedService = service;
return service;
}
public void deployTypeMapping(WSDDTypeMapping mapping)
throws WSDDException
{
if (!typeMappings.contains(mapping))
{
typeMappings.add(mapping);
}
if (tmr == null)
{
tmr = new TypeMappingRegistryImpl();
}
try
{
String encodingStyle = mapping.getEncodingStyle();
if (encodingStyle == null)
{
encodingStyle = use.getEncoding();
}
TypeMapping tm = tmr.getOrMakeTypeMapping(encodingStyle);
desc.setTypeMappingRegistry(tmr);
desc.setTypeMapping(tm);
SerializerFactory ser = null;
DeserializerFactory deser = null;
if (mapping.getSerializerName() != null &&
!mapping.getSerializerName().equals(""))
{
ser = BaseSerializerFactory.createFactory(mapping.getSerializer(),
mapping.getLanguageSpecificType(),
mapping.getQName());
}
if (mapping.getDeserializerName() != null &&
!mapping.getDeserializerName().equals(""))
{
deser = BaseDeserializerFactory.createFactory(mapping.getDeserializer(),
mapping.getLanguageSpecificType(),
mapping.getQName());
}
tm.register(mapping.getLanguageSpecificType(), mapping.getQName(), ser, deser);
}
catch (ClassNotFoundException e)
{
log.error(Messages.getMessage("unabletoDeployTypemapping00", "" + mapping.getQName()), e);
throw new WSDDNonFatalException(e);
}
catch (Exception e)
{
throw new WSDDException(e);
}
}
public void writeToContext(SerializationContext context)
throws IOException
{
AttributesImpl attrs = new AttributesImpl();
QName name = getQName();
if (name != null)
{
attrs.addAttribute("", ATTR_NAME, ATTR_NAME,
"CDATA", context.qName2String(name));
}
if (providerQName != null)
{
attrs.addAttribute("", ATTR_PROVIDER, ATTR_PROVIDER,
"CDATA", context.qName2String(providerQName));
}
if (style != Style.DEFAULT)
{
attrs.addAttribute("", ATTR_STYLE, ATTR_STYLE,
"CDATA", style.getName());
}
if (use != Use.DEFAULT)
{
attrs.addAttribute("", ATTR_USE, ATTR_USE,
"CDATA", use.getName());
}
if (streaming)
{
attrs.addAttribute("", ATTR_STREAMING, ATTR_STREAMING,
"CDATA", "on");
}
if (sendType != Attachments.SEND_TYPE_NOTSET)
{
attrs.addAttribute("", ATTR_ATTACHMENT_FORMAT,
ATTR_ATTACHMENT_FORMAT, "CDATA",
AttachmentsImpl.getSendTypeString(sendType));
}
context.startElement(WSDDConstants.QNAME_SERVICE, attrs);
if (desc.getWSDLFile() != null)
{
context.startElement(QNAME_WSDLFILE, null);
context.writeSafeString(desc.getWSDLFile());
context.endElement();
}
for (int i = 0; i < operations.size(); i++)
{
WSDDOperation operation = (WSDDOperation)operations.elementAt(i);
operation.writeToContext(context);
}
writeFlowsToContext(context);
writeParamsToContext(context);
for (int i = 0; i < typeMappings.size(); i++)
{
((WSDDTypeMapping)typeMappings.elementAt(i)).writeToContext(context);
}
for (int i = 0; i < namespaces.size(); i++)
{
context.startElement(QNAME_NAMESPACE, null);
context.writeString((String)namespaces.get(i));
context.endElement();
}
String endpointURL = desc.getEndpointURL();
if (endpointURL != null)
{
context.startElement(QNAME_ENDPOINTURL, null);
context.writeSafeString(endpointURL);
context.endElement();
}
if (_wsddHIchain != null)
{
_wsddHIchain.writeToContext(context);
}
context.endElement();
}
public void setCachedService(SOAPService service)
{
cachedService = service;
}
public Vector getTypeMappings()
{
return typeMappings;
}
public void setTypeMappings(Vector typeMappings)
{
this.typeMappings = typeMappings;
}
public void deployToRegistry(WSDDDeployment registry)
{
registry.addService(this);
registry.registerNamespaceForService(getQName().getLocalPart(), this);
for (int i = 0; i < namespaces.size(); i++)
{
String namespace = (String)namespaces.elementAt(i);
registry.registerNamespaceForService(namespace, this);
}
super.deployToRegistry(registry);
}
public void removeNamespaceMappings(WSDDDeployment registry)
{
for (int i = 0; i < namespaces.size(); i++)
{
String namespace = (String)namespaces.elementAt(i);
registry.removeNamespaceMapping(namespace);
}
registry.removeNamespaceMapping(getQName().getLocalPart());
}
public TypeMapping getTypeMapping(String encodingStyle)
{
if (tmr == null)
{
return null;
}
return (TypeMapping)tmr.getTypeMapping(encodingStyle);
}
public WSDDJAXRPCHandlerInfoChain getHandlerInfoChain()
{
return _wsddHIchain;
}
public void setHandlerInfoChain(WSDDJAXRPCHandlerInfoChain hichain)
{
_wsddHIchain = hichain;
}
}