KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Runtime state of the engine
17  */

18 package org.apache.axis2.clientapi;
19
20 import org.apache.axis2.context.ConfigurationContext;
21 import org.apache.axis2.context.ConfigurationContextFactory;
22 import org.apache.axis2.context.MessageContext;
23 import org.apache.axis2.context.ServiceContext;
24 import org.apache.axis2.description.OperationDescription;
25 import org.apache.axis2.description.ServiceDescription;
26 import org.apache.axis2.engine.AxisFault;
27 import org.apache.axis2.om.OMElement;
28 import org.apache.axis2.soap.SOAPEnvelope;
29
30 import javax.xml.namespace.QName JavaDoc;
31 import java.util.HashMap JavaDoc;
32
33 /**
34  * This class is the pretty convineance class for the user without see the comlplexites of Axis2.
35  */

36 public class Call extends InOutMEPClient {
37
38     private HashMap JavaDoc properties;
39     protected static OperationDescription operationTemplate;
40     private MessageContext lastResponseMessage;
41     /**
42      * this is a convenience Class, here the Call will assume a Annoynmous Service.
43      * @throws AxisFault
44      */

45
46     public Call() throws AxisFault {
47         super(assumeServiceContext(null));
48     }
49
50     /**
51      * This is used to create call object with client home , using onky this constructor it can
52      * able to engage modules , addning client side parameters
53      * @param clientHome
54      * @throws AxisFault
55      */

56     public Call(String JavaDoc clientHome) throws AxisFault {
57       super(assumeServiceContext(clientHome));
58     }
59
60     /**
61      * @see InOutMEPClient constructer
62      * @param service
63      */

64     public Call(ServiceContext service) {
65         super(service);
66     }
67
68     /**
69      * Invoke the blocking/Synchronous call
70      * @param axisop
71      * @param toSend - This should be OM Element (payload)
72      * @return
73      * @throws AxisFault
74      */

75
76     public OMElement invokeBlocking(String JavaDoc axisop, OMElement toSend) throws AxisFault {
77
78         OperationDescription axisConfig =
79             serviceContext.getServiceConfig().getOperation(new QName JavaDoc(axisop));
80          if (axisConfig == null) {
81             axisConfig = new OperationDescription(new QName JavaDoc(axisop));
82             axisConfig.setRemainingPhasesInFlow(operationTemplate.getRemainingPhasesInFlow());
83             axisConfig.setPhasesOutFlow(operationTemplate.getPhasesOutFlow());
84             axisConfig.setPhasesInFaultFlow(operationTemplate.getPhasesInFaultFlow());
85             axisConfig.setPhasesOutFaultFlow(operationTemplate.getPhasesOutFaultFlow());
86             serviceContext.getServiceConfig().addOperation(axisConfig);
87         }
88
89 // if (axisConfig == null) {
90
// axisConfig = new OperationDescription(new QName(axisop));
91
// serviceContext.getServiceConfig().addOperation(axisConfig);
92
// }
93
MessageContext msgctx = prepareTheSystem(toSend);
94
95         this.lastResponseMessage = super.invokeBlocking(axisConfig, msgctx);
96         SOAPEnvelope resEnvelope = lastResponseMessage.getEnvelope();
97         return resEnvelope.getBody().getFirstElement();
98     }
99     /**
100      * Invoke the nonblocking/Asynchronous call
101      * @param axisop
102      * @param toSend - This should be OM Element (payload)
103      * invocation behaves accordingly
104      * @param callback
105      * @throws AxisFault
106      */

107
108     public void invokeNonBlocking(String JavaDoc axisop, OMElement toSend, Callback callback)
109         throws AxisFault {
110         OperationDescription axisConfig =
111             serviceContext.getServiceConfig().getOperation(new QName JavaDoc(axisop));
112         if (axisConfig == null) {
113             axisConfig = new OperationDescription(new QName JavaDoc(axisop));
114             axisConfig.setRemainingPhasesInFlow(operationTemplate.getRemainingPhasesInFlow());
115             axisConfig.setPhasesOutFlow(operationTemplate.getPhasesOutFlow());
116             axisConfig.setPhasesInFaultFlow(operationTemplate.getPhasesInFaultFlow());
117             axisConfig.setPhasesOutFaultFlow(operationTemplate.getPhasesOutFaultFlow());
118             serviceContext.getServiceConfig().addOperation(axisConfig);
119         }
120         MessageContext msgctx = prepareTheSystem(toSend);
121
122         super.invokeNonBlocking(axisConfig, msgctx, callback);
123     }
124
125     /**
126      * Assume the values for the ConfigurationContext and ServiceContext to make the NON WSDL cases simple.
127      * @return ServiceContext that has a ConfigurationContext set in and has assumed values.
128      * @throws AxisFault
129      */

130     protected static ServiceContext assumeServiceContext(String JavaDoc clinetHome) throws AxisFault {
131         ConfigurationContext sysContext = null;
132         if (ListenerManager.configurationContext == null) {
133             ConfigurationContextFactory efac = new ConfigurationContextFactory();
134             sysContext = efac.buildClientConfigurationContext(clinetHome);
135         }else{
136             sysContext = ListenerManager.configurationContext;
137         }
138
139         //create new service
140
QName JavaDoc assumedServiceName = new QName JavaDoc("AnonnoymousService");
141         ServiceDescription axisService = new ServiceDescription(assumedServiceName);
142         operationTemplate = new OperationDescription(new QName JavaDoc("TemplateOperatin"));
143         axisService.addOperation(operationTemplate);
144         sysContext.getAxisConfiguration().addService(axisService);
145         ServiceContext service = sysContext.createServiceContext(assumedServiceName);
146         return service;
147     }
148
149     /**
150      * @param key
151      * @return
152      */

153     public Object JavaDoc get(String JavaDoc key) {
154         return serviceContext.getProperty(key);
155     }
156
157     /**
158      * @param key
159      * @param value
160      * @return
161      */

162     public void set(String JavaDoc key, Object JavaDoc value) {
163         serviceContext.getEngineContext().setProperty(key, value);
164     }
165     /**
166      * @return
167      */

168     public MessageContext getLastResponseMessage() {
169         return lastResponseMessage;
170     }
171
172 }
173
Popular Tags