KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > receivers > AbstractMessageReceiver


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis2.receivers;
18
19 import org.apache.axis2.Constants;
20 import org.apache.axis2.context.MessageContext;
21 import org.apache.axis2.context.SessionContext;
22 import org.apache.axis2.description.Parameter;
23 import org.apache.axis2.description.ServiceDescription;
24 import org.apache.axis2.engine.AxisFault;
25 import org.apache.axis2.engine.MessageReceiver;
26 import org.apache.axis2.om.OMAbstractFactory;
27 import org.apache.axis2.soap.SOAPFactory;
28 import org.apache.axis2.soap.impl.llom.soap11.SOAP11Constants;
29 import org.apache.axis2.soap.impl.llom.soap12.SOAP12Constants;
30
31 import javax.xml.namespace.QName JavaDoc;
32
33 public abstract class AbstractMessageReceiver implements MessageReceiver {
34     public static final String JavaDoc SERVICE_CLASS = "ServiceClass";
35     public static final String JavaDoc SCOPE = "scope";
36
37     protected SOAPFactory fac;
38
39
40     /**
41      * Method makeNewServiceObject
42      *
43      * @param msgContext
44      * @return
45      * @throws AxisFault
46      */

47     protected Object JavaDoc makeNewServiceObject(MessageContext msgContext) throws AxisFault {
48         try {
49
50             String JavaDoc nsURI = msgContext.getEnvelope().getNamespace().getName();
51             if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(nsURI)) {
52                 fac = OMAbstractFactory.getSOAP12Factory();
53             } else if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(nsURI)) {
54                 fac = OMAbstractFactory.getSOAP11Factory();
55             }else {
56                 throw new AxisFault("Unknown SOAP Version. Current Axis handles only SOAP 1.1 and SOAP 1.2 messages");
57             }
58
59             ServiceDescription service =
60                 msgContext.getOperationContext().getServiceContext().getServiceConfig();
61             ClassLoader JavaDoc classLoader = service.getClassLoader();
62             Parameter implInfoParam = service.getParameter(SERVICE_CLASS);
63             if (implInfoParam != null) {
64                 Class JavaDoc implClass =
65                     Class.forName((String JavaDoc) implInfoParam.getValue(), true, classLoader);
66                 return implClass.newInstance();
67             } else {
68                 throw new AxisFault("SERVICE_CLASS parameter is not specified");
69             }
70
71         } catch (Exception JavaDoc e) {
72             throw AxisFault.makeFault(e);
73         }
74     }
75
76     /**
77       * Method getTheImplementationObject
78       *
79       * @param msgContext
80       * @return
81       * @throws AxisFault
82       */

83     protected Object JavaDoc getTheImplementationObject(MessageContext msgContext) throws AxisFault {
84         ServiceDescription service =
85             msgContext.getOperationContext().getServiceContext().getServiceConfig();
86
87         Parameter scopeParam = service.getParameter(SCOPE);
88         QName JavaDoc serviceName = service.getName();
89         if (scopeParam != null && Constants.SESSION_SCOPE.equals(scopeParam.getValue())) {
90             SessionContext sessionContext = msgContext.getSessionContext();
91             synchronized(sessionContext){
92                 Object JavaDoc obj = sessionContext.getProperty(serviceName.getLocalPart());
93                 if (obj == null) {
94                     obj = makeNewServiceObject(msgContext);
95                     sessionContext.setProperty(serviceName.getLocalPart(), obj);
96                 }
97                 return obj;
98             }
99         } else if (scopeParam != null && Constants.APPLICATION_SCOPE.equals(scopeParam.getValue())) {
100             SessionContext globalContext = msgContext.getSessionContext();
101             synchronized(globalContext){
102                 Object JavaDoc obj = globalContext.getProperty(serviceName.getLocalPart());
103                 if (obj == null) {
104                     obj = makeNewServiceObject(msgContext);
105                     globalContext.setProperty(serviceName.getLocalPart(), obj);
106                 }
107                 return obj;
108             }
109         } else {
110             return makeNewServiceObject(msgContext);
111         }
112     }
113
114     public SOAPFactory getSOAPFactory(){
115         return fac;
116     }
117 }
118
Popular Tags