package org.jboss.axis.providers.java;
import org.jboss.axis.AxisEngine;
import org.jboss.axis.AxisFault;
import org.jboss.axis.Constants;
import org.jboss.axis.Handler;
import org.jboss.axis.Message;
import org.jboss.axis.MessageContext;
import org.jboss.axis.description.ServiceDesc;
import org.jboss.axis.encoding.TypeMapping;
import org.jboss.axis.enums.Scope;
import org.jboss.axis.handlers.soap.SOAPService;
import org.jboss.axis.message.SOAPEnvelopeAxisImpl;
import org.jboss.axis.providers.BasicProvider;
import org.jboss.axis.session.Session;
import org.jboss.axis.utils.ClassUtils;
import org.jboss.axis.utils.Messages;
import org.jboss.axis.utils.cache.ClassCache;
import org.jboss.axis.utils.cache.JavaClass;
import org.jboss.axis.wsdl.fromJava.Emitter;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.rpc.JAXRPCException;
import javax.xml.rpc.holders.IntHolder;
import javax.xml.rpc.server.ServiceLifecycle;
import java.util.ArrayList;
import java.util.StringTokenizer;
public abstract class JavaProvider extends BasicProvider
{
private static Logger log = Logger.getLogger(JavaProvider.class.getName());
public static final String OPTION_CLASSNAME = "className";
public static final String OPTION_ALLOWEDMETHODS = "allowedMethods";
public static final String OPTION_IS_STATIC = "isStatic";
public static final String OPTION_CLASSPATH = "classPath";
public static final String OPTION_WSDL_PORTTYPE = "wsdlPortType";
public static final String OPTION_WSDL_SERVICEELEMENT = "wsdlServiceElement";
public static final String OPTION_WSDL_SERVICEPORT = "wsdlServicePort";
public static final String OPTION_WSDL_TARGETNAMESPACE = "wsdlTargetNamespace";
public static final String OPTION_WSDL_INPUTSCHEMA = "wsdlInputSchema";
public static final String OPTION_SCOPE = "scope";
public Object getServiceObject(MessageContext msgContext,
Handler service,
String clsName,
IntHolder scopeHolder)
throws Exception
{
String serviceName = msgContext.getService().getName();
Scope scope = Scope.getScope((String)service.getOption(OPTION_SCOPE), Scope.DEFAULT);
scopeHolder.value = scope.getValue();
if (scope == Scope.REQUEST)
{
return getNewServiceObject(msgContext, clsName);
}
else if (scope == Scope.SESSION)
{
if (serviceName == null)
serviceName = msgContext.getService().toString();
Session session = msgContext.getSession();
if (session != null)
{
return getSessionServiceObject(session, serviceName,
msgContext, clsName);
}
else
{
scopeHolder.value = Scope.DEFAULT.getValue();
return getNewServiceObject(msgContext, clsName);
}
}
else if (scope == Scope.APPLICATION)
{
AxisEngine engine = msgContext.getAxisEngine();
Session appSession = engine.getApplicationSession();
if (appSession != null)
{
return getSessionServiceObject(appSession, serviceName,
msgContext, clsName);
}
else
{
scopeHolder.value = Scope.DEFAULT.getValue();
return getNewServiceObject(msgContext, clsName);
}
}
else
{
return null;
}
}
class LockObject
{
private boolean completed = false;
synchronized void waitUntilComplete() throws InterruptedException
{
while (!completed)
{
wait();
}
}
synchronized void complete()
{
completed = true;
notifyAll();
}
}
private Object getSessionServiceObject(Session session,
String serviceName,
MessageContext msgContext,
String clsName) throws Exception
{
Object obj = null;
boolean makeNewObject = false;
synchronized (session.getLockObject())
{
obj = session.get(serviceName);
if (obj == null)
{
obj = new LockObject();
makeNewObject = true;
session.set(serviceName, obj);
}
}
if (LockObject.class == obj.getClass())
{
LockObject lock = (LockObject)obj;
if (makeNewObject)
{
try
{
obj = getNewServiceObject(msgContext, clsName);
session.set(serviceName, obj);
}
finally
{
lock.complete();
}
}
else
{
lock.waitUntilComplete();
obj = session.get(serviceName);
}
}
return obj;
}
private Object getNewServiceObject(MessageContext msgContext,
String clsName) throws Exception
{
Object serviceObject = makeNewServiceObject(msgContext, clsName);
if (serviceObject != null &&
serviceObject instanceof ServiceLifecycle)
{
((ServiceLifecycle)serviceObject).init(msgContext.getProperty(Constants.MC_SERVLET_ENDPOINT_CONTEXT));
}
return serviceObject;
}
public abstract void processMessage(MessageContext msgContext,
SOAPEnvelopeAxisImpl reqEnv,
SOAPEnvelopeAxisImpl resEnv,
Object obj)
throws Exception;
public void invoke(MessageContext msgContext) throws AxisFault
{
if (log.isDebugEnabled())
log.debug("Enter: JavaProvider::invoke (" + this + ")");
String serviceName = msgContext.getTargetService();
Handler service = msgContext.getService();
String clsName = getServiceClassName(service);
if ((clsName == null) || clsName.equals(""))
{
throw new AxisFault("Server.NoClassForService",
Messages.getMessage("noOption00", getServiceClassNameOptionName(), serviceName),
null, null);
}
IntHolder scope = new IntHolder();
Object serviceObject = null;
try
{
serviceObject = getServiceObject(msgContext, service, clsName, scope);
Message resMsg = msgContext.getResponseMessage();
SOAPEnvelopeAxisImpl resEnv;
if (resMsg == null)
{
resEnv = new SOAPEnvelopeAxisImpl(msgContext.getSOAPConstants(),
msgContext.getSchemaVersion());
resMsg = new Message(resEnv);
msgContext.setResponseMessage(resMsg);
}
else
{
resEnv = resMsg.getSOAPEnvelope();
}
Message reqMsg = msgContext.getRequestMessage();
SOAPEnvelopeAxisImpl reqEnv = reqMsg.getSOAPEnvelope();
processMessage(msgContext, reqEnv, resEnv, serviceObject);
}
catch (Exception ex)
{
processException(ex);
log.error("Detect unprocessed exception", ex);
throw AxisFault.makeFault(ex);
}
finally
{
if (serviceObject != null &&
scope.value == Scope.REQUEST.getValue() &&
serviceObject instanceof ServiceLifecycle)
{
((ServiceLifecycle)serviceObject).destroy();
}
}
if (log.isDebugEnabled())
log.debug("Exit: JavaProvider::invoke (" + this + ")");
}
protected void processException(Exception ex) throws AxisFault
{
if (ex instanceof AxisFault)
{
throw (AxisFault)ex;
}
if (ex instanceof JAXRPCException)
{
JAXRPCException rte = (JAXRPCException)ex;
if (rte.getLinkedCause() instanceof AxisFault)
throw (AxisFault)rte.getLinkedCause();
AxisFault fault = AxisFault.makeFault(ex);
fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_RUNTIMEEXCEPTION, "true");
throw fault;
}
if (ex instanceof SAXException)
{
SAXException exp = (SAXException)ex;
Exception real = exp.getException();
if (real != null)
throw AxisFault.makeFault(real);
}
AxisFault fault = AxisFault.makeFault(ex);
throw fault;
}
public void generateWSDL(MessageContext msgContext) throws AxisFault
{
if (log.isDebugEnabled())
log.debug("Enter: JavaProvider::generateWSDL (" + this + ")");
SOAPService service = msgContext.getService();
ServiceDesc serviceDesc = service.getInitializedServiceDesc(msgContext);
try
{
String locationUrl =
msgContext.getStrProp(MessageContext.WSDLGEN_SERV_LOC_URL);
if (locationUrl == null)
{
locationUrl = serviceDesc.getEndpointURL();
}
if (locationUrl == null)
{
locationUrl = msgContext.getStrProp(MessageContext.TRANS_URL);
}
String interfaceNamespace =
msgContext.getStrProp(MessageContext.WSDLGEN_INTFNAMESPACE);
if (interfaceNamespace == null)
{
interfaceNamespace = serviceDesc.getDefaultNamespace();
}
if (interfaceNamespace == null)
{
interfaceNamespace = locationUrl;
}
Emitter emitter = new Emitter();
String alias = (String)service.getOption("alias");
if (alias != null) emitter.setServiceElementName(alias);
emitter.setStyle(serviceDesc.getStyle());
emitter.setUse(serviceDesc.getUse());
emitter.setClsSmart(serviceDesc.getImplClass(), locationUrl);
String targetNamespace = (String)service.getOption(OPTION_WSDL_TARGETNAMESPACE);
if (targetNamespace == null ||
targetNamespace.length() == 0)
{
targetNamespace = interfaceNamespace;
}
emitter.setIntfNamespace(targetNamespace);
emitter.setLocationUrl(locationUrl);
emitter.setServiceDesc(serviceDesc);
emitter.setTypeMapping((TypeMapping)msgContext.getTypeMappingRegistry()
.getTypeMapping(serviceDesc.getUse().getEncoding()));
emitter.setDefaultTypeMapping((TypeMapping)msgContext.getTypeMappingRegistry().
getDefaultTypeMapping());
String wsdlPortType = (String)service.getOption(OPTION_WSDL_PORTTYPE);
String wsdlServiceElement = (String)service.getOption(OPTION_WSDL_SERVICEELEMENT);
String wsdlServicePort = (String)service.getOption(OPTION_WSDL_SERVICEPORT);
if (wsdlPortType != null && wsdlPortType.length() > 0)
{
emitter.setPortTypeName(wsdlPortType);
}
if (wsdlServiceElement != null && wsdlServiceElement.length() > 0)
{
emitter.setServiceElementName(wsdlServiceElement);
}
if (wsdlServicePort != null && wsdlServicePort.length() > 0)
{
emitter.setServicePortName(wsdlServicePort);
}
String wsdlInputSchema = (String)
service.getOption(OPTION_WSDL_INPUTSCHEMA);
if (null != wsdlInputSchema && wsdlInputSchema.length() > 0)
{
emitter.setInputSchema(wsdlInputSchema);
}
Document doc = emitter.emit(Emitter.MODE_ALL);
msgContext.setProperty("WSDL", doc);
}
catch (NoClassDefFoundError e)
{
log.info(Messages.getMessage("toAxisFault00"), e);
throw new AxisFault(e.toString(), e);
}
catch (Exception e)
{
log.info(Messages.getMessage("toAxisFault00"), e);
throw AxisFault.makeFault(e);
}
if (log.isDebugEnabled())
log.debug("Exit: JavaProvider::generateWSDL (" + this + ")");
}
private String getAllowedMethods(Handler service)
{
String val = (String)service.getOption(OPTION_ALLOWEDMETHODS);
if (val == null || val.length() == 0)
{
val = (String)service.getOption("methodName");
}
return val;
}
protected Object makeNewServiceObject(MessageContext msgContext, String clsName)
throws Exception
{
Object serviceObject = null;
ClassLoader cl = msgContext.getClassLoader();
ClassCache cache = msgContext.getAxisEngine().getClassCache();
if (cache != null)
{
log.debug("makeNewServiceObject from cache: " + clsName);
JavaClass jc = cache.lookup(clsName, cl);
serviceObject = jc.getJavaClass().newInstance();
}
else
{
log.debug("makeNewServiceObject for name: " + clsName);
Class serviceClass = ClassUtils.forName(clsName, true, cl);
serviceObject = serviceClass.newInstance();
}
return serviceObject;
}
protected String getServiceClassName(Handler service)
{
return (String)service.getOption(getServiceClassNameOptionName());
}
protected String getServiceClassNameOptionName()
{
return OPTION_CLASSNAME;
}
protected Class getServiceClass(String clsName, SOAPService service, MessageContext msgContext)
throws AxisFault
{
ClassLoader cl = null;
Class serviceClass = null;
AxisEngine engine = service.getEngine();
if (msgContext != null)
{
cl = msgContext.getClassLoader();
}
else
{
cl = Thread.currentThread().getContextClassLoader();
}
if (engine != null && engine.getClassCache() != null)
{
try
{
JavaClass jc = engine.getClassCache().lookup(clsName, cl);
serviceClass = jc.getJavaClass();
}
catch (ClassNotFoundException e)
{
log.error(Messages.getMessage("exception00"), e);
throw new AxisFault(Messages.getMessage("noClassForService00", clsName), e);
}
}
if (serviceClass == null)
{
try
{
serviceClass = ClassUtils.forName(clsName, true, cl);
}
catch (ClassNotFoundException e)
{
log.error(Messages.getMessage("exception00"), e);
throw new AxisFault(Messages.getMessage("noClassForService00", clsName), e);
}
}
return serviceClass;
}
public void initServiceDesc(SOAPService service, MessageContext msgContext)
throws AxisFault
{
String clsName = getServiceClassName(service);
if (clsName == null)
{
throw new AxisFault(Messages.getMessage("noServiceClass"));
}
Class cls = getServiceClass(clsName, service, msgContext);
ServiceDesc serviceDescription = service.getServiceDescription();
if (serviceDescription.getAllowedMethods() == null && service != null)
{
String allowedMethods = getAllowedMethods(service);
if (allowedMethods != null && !"*".equals(allowedMethods))
{
ArrayList methodList = new ArrayList();
StringTokenizer tokenizer = new StringTokenizer(allowedMethods, " ,");
while (tokenizer.hasMoreTokens())
{
methodList.add(tokenizer.nextToken());
}
serviceDescription.setAllowedMethods(methodList);
}
}
serviceDescription.loadServiceDescByIntrospection(cls);
}
}