KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > context > OperationContext


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

18
19 import org.apache.axis2.description.OperationDescription;
20 import org.apache.axis2.engine.AxisError;
21 import org.apache.axis2.engine.AxisFault;
22 import org.apache.wsdl.WSDLConstants;
23
24 import java.util.Map JavaDoc;
25
26 /**
27  * An OperationContext represents a running "instance" of an operation, which is
28  * represented by an OperationDescription object. This concept is needed to allow
29  * messages to be grouped into operations as in WSDL 2.0-speak operations are
30  * essentially arbitrary message exchange patterns. So as messages are being
31  * exchanged the OperationContext remembers the state of where in the message
32  * exchange pattern it is in.
33  * <p/>
34  * OperationContextFactory factory. The base implementation of OperationContext
35  * supports MEPs which have one input message and/or one output message. That
36  * is, it supports the all the MEPs that are in the WSDL 2.0 specification. In
37  * order to support another MEP one must extend this class and register its
38  * creation in the OperationContexFactory.
39  */

40 public class OperationContext extends AbstractContext {
41     // the in and out messages that may be present
42
private MessageContext inMessageContext;
43
44     private MessageContext outMessageContext;
45
46     // the OperationDescription of which this is a running instance. The MEP of this
47
// OperationDescription must be one of the 8 predefined ones in WSDL 2.0.
48
private OperationDescription axisOperation;
49
50     private int operationMEP;
51
52     private boolean isComplete = false;
53
54     // this is the global MessageID -> OperationContext map which is stored in
55
// the EngineContext. We're caching it here for faster acccess.
56
private Map JavaDoc operationContextMap;
57
58     /**
59      * Construct a new OperationContext.
60      *
61      * @param axisOperation the OperationDescription whose running instances' state this
62      * OperationContext represents.
63      * @param serviceContext the parent ServiceContext representing any state related to
64      * the set of all operations of the service.
65      */

66     public OperationContext(OperationDescription axisOperation,
67                             ServiceContext serviceContext) {
68         super(serviceContext);
69         this.axisOperation = axisOperation;
70         this.operationMEP = axisOperation.getAxisSpecifMEPConstant();
71         this.operationContextMap = getServiceContext().getEngineContext()
72                 .getOperationContextMap();
73     }
74
75     /**
76      * @return Returns the axisOperation.
77      */

78     public OperationDescription getAxisOperation() {
79         return axisOperation;
80     }
81
82     /**
83      * Return the ServiceContext in which this OperationContext lives.
84      *
85      * @return parent ServiceContext
86      */

87     public ServiceContext getServiceContext() {
88         return (ServiceContext) parent;
89     }
90
91     /**
92      * Return the EngineContext in which the parent ServiceContext lives.
93      *
94      * @return parent ServiceContext's parent EngineContext
95      */

96     public ConfigurationContext getEngineContext() {
97         return (ConfigurationContext) parent.parent;
98     }
99
100     /**
101      * When a new message is added to the <code>MEPContext</code> the logic
102      * should be included remove the MEPContext from the table in the
103      * <code>EngineContext</code>. Example: IN_IN_OUT At the second IN
104      * message the MEPContext should be removed from the OperationDescription
105      *
106      * @param msgContext
107      */

108     public synchronized void addMessageContext(MessageContext msgContext) throws AxisFault {
109         // this needs to store the msgContext in either inMessageContext or
110
// outMessageContext depending on the MEP of the OperationDescription
111
// and on the current state of the operation.
112
if (WSDLConstants.MEP_CONSTANT_IN_OUT == operationMEP
113                 || WSDLConstants.MEP_CONSTANT_IN_OPTIONAL_OUT == operationMEP
114                 || WSDLConstants.MEP_CONSTANT_ROBUST_IN_ONLY == operationMEP) {
115             if (inMessageContext == null) {
116                 inMessageContext = msgContext;
117             } else {
118                 outMessageContext = msgContext;
119                 isComplete = true;
120             }
121         } else if (WSDLConstants.MEP_CONSTANT_IN_ONLY == operationMEP) {
122             inMessageContext = msgContext;
123             isComplete = true;
124         } else if (WSDLConstants.MEP_CONSTANT_OUT_ONLY == operationMEP) {
125             outMessageContext = msgContext;
126             isComplete = true;
127         } else if (WSDLConstants.MEP_CONSTANT_OUT_IN == operationMEP
128                 || WSDLConstants.MEP_CONSTANT_OUT_OPTIONAL_IN == operationMEP
129                 || WSDLConstants.MEP_CONSTANT_ROBUST_IN_ONLY == operationMEP) {
130             if (outMessageContext == null) {
131                 outMessageContext = msgContext;
132             } else {
133                 inMessageContext = msgContext;
134                 isComplete = true;
135             }
136         } else {
137             // NOT REACHED: the factory created this context incorrectly
138
throw new AxisError("Invalid behavior of OperationContextFactory");
139         }
140     }
141
142     /**
143      * @param messageLabel
144      * @return
145      * @throws AxisFault
146      */

147     public MessageContext getMessageContext(int messageLabel) throws AxisFault {
148         if (messageLabel == WSDLConstants.MESSAGE_LABEL_IN) {
149             return inMessageContext;
150         } else if (messageLabel == WSDLConstants.MESSAGE_LABEL_OUT) {
151             return outMessageContext;
152         } else {
153             throw new AxisFault("Unrecognized message label: '" + messageLabel
154                     + "'");
155         }
156     }
157
158    /**
159      * Checks to see if the MEP is complete. i.e. whether all the messages that
160      * are associated with the MEP has arrived and MEP is complete.
161      *
162      * @return
163      */

164     public boolean isComplete() {
165         return isComplete;
166     }
167
168     /**
169      * Removes the pointers to this <code>OperationContext</code> in the
170      * <code>EngineContext</code>'s OperationContextMap so that this
171      * <code>OperationContext</code> will eventually get garbage collected
172      * along with the <code>MessageContext</code>'s it contains. Note that if
173      * the caller wants to make sure its safe to clean up this OperationContext
174      * he should call isComplete() first. However, in cases like IN_OPTIONAL_OUT
175      * and OUT_OPTIONAL_IN, it is possibe this will get called without the MEP
176      * being complete due to the optional nature of the MEP.
177      */

178     public void cleanup() {
179         if (null != this.inMessageContext) {
180             operationContextMap.remove(inMessageContext.getMessageID());
181         }
182         if (null != this.outMessageContext) {
183             operationContextMap.remove(outMessageContext.getMessageID());
184         }
185     }
186
187
188 // public MessageContext createMessageContext(AxisM){
189
//
190
// }
191

192 }
Popular Tags