KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > providers > java > MsgProvider


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.axis.providers.java;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.MessageContext;
21 import org.apache.axis.handlers.soap.SOAPService;
22 import org.apache.axis.description.OperationDesc;
23 import org.apache.axis.description.ServiceDesc;
24 import org.apache.axis.i18n.Messages;
25 import org.apache.axis.message.SOAPBodyElement;
26 import org.apache.axis.message.SOAPEnvelope;
27 import org.apache.axis.message.MessageElement;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import javax.xml.namespace.QName JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 /**
35  * Deal with message-style Java services. For now, these are services
36  * with exactly ONE OperationDesc, pointing to a method which looks like
37  * one of the following:
38  *
39  * public Element [] method(Vector v);
40  * (NOTE : This is silly, we should change it to either be Vector/Vector
41  * or Element[]/Element[])
42  *
43  * public Document method(Document doc);
44  *
45  * public void method(MessageContext mc);
46  *
47  * @author Doug Davis (dug@us.ibm.com)
48  * @author Glen Daniels (gdaniels@apache.org)
49  */

50 public class MsgProvider extends JavaProvider {
51     /**
52      * Process the message. Figure out the method "style" (one of the three
53      * allowed signatures, which has already been determined and cached in
54      * the OperationDesc) and do the actual invocation. Note that we don't
55      * catch exceptions here, preferring to bubble them right up through to
56      * someone who'll catch it above us.
57      *
58      * @param msgContext the active MessageContext
59      * @param reqEnv the request SOAPEnvelope
60      * @param resEnv the response SOAPEnvelope (we should fill this in)
61      * @param obj the service target object
62      * @throws Exception
63      */

64     public void processMessage (MessageContext msgContext,
65                                 SOAPEnvelope reqEnv,
66                                 SOAPEnvelope resEnv,
67                                 Object JavaDoc obj)
68         throws Exception JavaDoc
69     {
70         OperationDesc operation = msgContext.getOperation();
71         SOAPService service = msgContext.getService();
72         ServiceDesc serviceDesc = service.getServiceDescription();
73         QName JavaDoc opQName = null;
74         
75         if (operation == null) {
76             Vector JavaDoc bodyElements = reqEnv.getBodyElements();
77             if(bodyElements.size() > 0) {
78                 MessageElement element = (MessageElement) bodyElements.get(0);
79                 if (element != null) {
80                     opQName = new QName JavaDoc(element.getNamespaceURI(),
81                             element.getLocalName());
82                     operation = serviceDesc.getOperationByElementQName(opQName);
83                 }
84             }
85         }
86
87         if (operation == null) {
88             throw new AxisFault(Messages.getMessage("noOperationForQName",
89                                 opQName == null ? "null" : opQName.toString()));
90         }
91         
92         Method JavaDoc method = operation.getMethod();
93
94         int methodType = operation.getMessageOperationStyle();
95
96         if (methodType != OperationDesc.MSG_METHOD_SOAPENVELOPE) {
97             // dig out just the body, and pass it on
98
Vector JavaDoc bodies = reqEnv.getBodyElements();
99             Object JavaDoc argObjects[] = new Object JavaDoc [1];
100
101             switch (methodType) {
102                 // SOAPBodyElement [] / SOAPBodyElement []
103
case OperationDesc.MSG_METHOD_BODYARRAY:
104                     SOAPBodyElement [] bodyElements =
105                             new SOAPBodyElement[bodies.size()];
106                     bodies.toArray(bodyElements);
107                     argObjects[0] = bodyElements;
108                     SOAPBodyElement [] bodyResult =
109                             (SOAPBodyElement [])method.invoke(obj, argObjects);
110                     if (bodyResult != null) {
111                         for (int i = 0; i < bodyResult.length; i++) {
112                             SOAPBodyElement bodyElement = bodyResult[i];
113                             resEnv.addBodyElement(bodyElement);
114                         }
115                     }
116                     return;
117
118                 // Element [] / Element []
119
case OperationDesc.MSG_METHOD_ELEMENTARRAY:
120                     Element [] elements = new Element [bodies.size()];
121                     for (int i = 0; i < elements.length; i++) {
122                         SOAPBodyElement body = (SOAPBodyElement)bodies.get(i);
123                         elements[i] = body.getAsDOM();
124                     }
125                     argObjects[0] = elements;
126                     Element[] elemResult =
127                             (Element[]) method.invoke( obj, argObjects );
128                     if (elemResult != null) {
129                         for ( int i = 0 ; i < elemResult.length ; i++ ) {
130                             if(elemResult[i] != null)
131                                 resEnv.addBodyElement(
132                                         new SOAPBodyElement(elemResult[i]));
133                         }
134                     }
135                     return;
136
137                 // Element [] / Element []
138
case OperationDesc.MSG_METHOD_DOCUMENT:
139                     Document JavaDoc doc = ((SOAPBodyElement)bodies.get(0)).getAsDocument();
140                     argObjects[0] = doc;
141                     Document JavaDoc resultDoc =
142                             (Document JavaDoc) method.invoke( obj, argObjects );
143                     if (resultDoc != null) {
144                         resEnv.addBodyElement(new SOAPBodyElement(
145                                 resultDoc.getDocumentElement()));
146                     }
147                     return;
148             }
149         } else {
150             Object JavaDoc argObjects[] = new Object JavaDoc [2];
151
152             // SOAPEnvelope / SOAPEnvelope
153
argObjects[0] = reqEnv;
154             argObjects[1] = resEnv;
155             method.invoke(obj, argObjects);
156             return;
157         }
158
159         // SHOULD NEVER GET HERE...
160
throw new AxisFault(Messages.getMessage("badMsgMethodStyle"));
161     }
162 };
163
Popular Tags