KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > clientapi > Stub


1 /*
2 * Copyright 2001-2004 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.clientapi;
18
19 import org.apache.axis2.addressing.EndpointReference;
20 import org.apache.axis2.context.ConfigurationContext;
21 import org.apache.axis2.context.MessageContext;
22 import org.apache.axis2.context.ServiceContext;
23 import org.apache.axis2.deployment.DeploymentException;
24 import org.apache.axis2.description.ServiceDescription;
25 import org.apache.axis2.engine.AxisFault;
26 import org.apache.axis2.om.OMAbstractFactory;
27 import org.apache.axis2.om.OMElement;
28 import org.apache.axis2.om.OMFactory;
29 import org.apache.axis2.om.OMNamespace;
30 import org.apache.axis2.om.impl.llom.builder.StAXOMBuilder;
31 import org.apache.axis2.om.impl.llom.factory.OMXMLBuilderFactory;
32 import org.apache.axis2.soap.SOAPBody;
33 import org.apache.axis2.soap.SOAPEnvelope;
34 import org.apache.axis2.soap.SOAPFactory;
35 import org.apache.axis2.soap.impl.llom.SOAPProcessingException;
36 import org.apache.wsdl.WSDLService;
37
38 import javax.xml.stream.XMLStreamReader;
39
40
41
42 /**
43  * @author chathura@opensource.lk
44  *
45  */

46 public abstract class Stub {
47
48     protected ConfigurationContext _configurationContext;
49     protected static ServiceDescription _service;
50     protected ServiceContext _serviceContext;
51     protected EndpointReference toEPR ;
52
53
54
55     /**
56      * If _maintainSession is set to True all the calls will use the same
57      * ServiceContext and the user can Share information through that
58      * ServiceContext across operations.
59      */

60     protected boolean _maintainSession = false;
61     protected String JavaDoc _currentSessionId = null;
62
63
64     protected Stub()throws DeploymentException, AxisFault{
65
66     }
67
68 // public abstract void _setSessionInfo(Object key, Object value) throws Exception;
69
//
70
// public abstract Object _getSessionInfo(Object key) throws Exception ;
71

72     public void _setSessionInfo(String JavaDoc key, Object JavaDoc value)throws java.lang.Exception JavaDoc{
73         if(!_maintainSession){
74             //TODO Comeup with a Exception
75
throw new java.lang.Exception JavaDoc("Client is running the session OFF mode: Start session before saving to a session ");
76         }
77         _configurationContext.getServiceContext(_currentSessionId).setProperty(key, value);
78     }
79
80
81     public Object JavaDoc _getSessionInfo(String JavaDoc key) throws java.lang.Exception JavaDoc{
82         if(!_maintainSession){
83             //TODO Comeup with a Exception
84
throw new java.lang.Exception JavaDoc("Client is running the session OFF mode: Start session before saving to a session ");
85         }
86         return _configurationContext.getServiceContext(_currentSessionId).getProperty(key);
87     }
88
89     public void _startSession(){
90         _maintainSession = true;
91         _currentSessionId = getID() ;
92     }
93
94     public void _endSession(){
95         _maintainSession = false;
96     }
97
98     protected String JavaDoc _getServiceContextID(){
99         if(_maintainSession)
100             return _currentSessionId;
101         else
102             return getID();
103     }
104
105     private String JavaDoc getID(){
106         //TODO Get the UUID generator to generate values
107
return Long.toString(System.currentTimeMillis());
108     }
109
110     //todo make this compliant with the SOAP12
111
protected SOAPEnvelope createEnvelope() throws SOAPProcessingException {
112         SOAPEnvelope env = getFactory().getDefaultEnvelope();
113         return env;
114     }
115
116     protected void setValueRPC(SOAPEnvelope env,String JavaDoc methodNamespaceURI,String JavaDoc methodName,String JavaDoc[] paramNames,Object JavaDoc[] values){
117         SOAPBody body = env.getBody();
118         OMFactory fac = this.getFactory();
119
120         OMNamespace methodNamespace = fac.createOMNamespace(methodNamespaceURI,"ns1");
121         OMElement elt = fac.createOMElement(methodName,methodNamespace);
122         if (paramNames!=null){
123             //find the relevant object here, convert it and add it to the elt
124
for (int i = 0; i < paramNames.length; i++) {
125                 String JavaDoc paramName = paramNames[i];
126                 Object JavaDoc value = values[i];
127                 elt.addChild(StubSupporter.createRPCMappedElement(paramName,
128                         fac.createOMNamespace("",null),//empty namespace
129
value,
130                         fac));
131             }
132         }
133         body.addChild(elt);
134     }
135
136
137     protected OMElement getElementFromReader(XMLStreamReader reader) {
138         StAXOMBuilder builder = OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(),reader) ;
139         return builder.getDocumentElement();
140     }
141
142     protected void setValueDoc(SOAPEnvelope env,OMElement value){
143         if (value!=null){
144             SOAPBody body = env.getBody();
145             body.addChild(value);
146         }
147     }
148
149     protected OMElement getElement(SOAPEnvelope env,String JavaDoc type){
150         SOAPBody body = env.getBody();
151         OMElement element = body.getFirstElement();
152
153         if (WSDLService.STYLE_RPC.equals(type)){
154             return element.getFirstElement(); //todo this needs to be fixed
155
}else if (WSDLService.STYLE_DOC.equals(type)){
156             return element;
157         }else {
158             throw new UnsupportedOperationException JavaDoc("Unsupported type");
159         }
160
161     }
162
163 // protected Object getValue(SOAPEnvelope env,String type,Class outputType){
164
// SOAPBody body = env.getBody();
165
// OMElement element = body.getFirstElement();
166
//
167
// if (WSDLService.STYLE_RPC.equals(type)){
168
// return StubSupporter.getRPCMappedElementValue(element.getFirstElement(),outputType);
169
// }else if (WSDLService.STYLE_DOC.equals(type)){
170
// return element;
171
// }else {
172
// throw new UnsupportedOperationException("Unsupported type");
173
// }
174
//
175
// }
176
/**
177      * get the message context
178      */

179     protected MessageContext getMessageContext() throws AxisFault {
180         return new MessageContext(_configurationContext);
181     }
182
183
184     private SOAPFactory getFactory(){
185         return OMAbstractFactory.getSOAP11Factory();
186     }
187 }
188
189
Popular Tags