KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.providers.java;
57
58 import org.jboss.axis.AxisFault;
59 import org.jboss.axis.MessageContext;
60 import org.jboss.axis.description.OperationDesc;
61 import org.jboss.axis.i18n.Messages;
62 import org.jboss.axis.message.SOAPBodyElementAxisImpl;
63 import org.jboss.axis.message.SOAPEnvelopeAxisImpl;
64 import org.w3c.dom.Document JavaDoc;
65 import org.w3c.dom.Element JavaDoc;
66
67 import java.lang.reflect.Method JavaDoc;
68 import java.util.Vector JavaDoc;
69
70 /**
71  * Deal with message-style Java services. For now, these are services
72  * with exactly ONE OperationDesc, pointing to a method which looks like
73  * one of the following:
74  * <p/>
75  * public Element [] method(Vector v);
76  * (NOTE : This is silly, we should change it to either be Vector/Vector
77  * or Element[]/Element[])
78  * <p/>
79  * public Document method(Document doc);
80  * <p/>
81  * public void method(MessageContext mc);
82  *
83  * @author Doug Davis (dug@us.ibm.com)
84  * @author Glen Daniels (gdaniels@apache.org)
85  */

86 public class MsgProvider extends JavaProvider
87 {
88    /**
89     * Process the message. Figure out the method "style" (one of the three
90     * allowed signatures, which has already been determined and cached in
91     * the OperationDesc) and do the actual invocation. Note that we don't
92     * catch exceptions here, preferring to bubble them right up through to
93     * someone who'll catch it above us.
94     *
95     * @param msgContext the active MessageContext
96     * @param reqEnv the request SOAPEnvelope
97     * @param resEnv the response SOAPEnvelope (we should fill this in)
98     * @param obj the service target object
99     * @throws Exception
100     */

101    public void processMessage(MessageContext msgContext,
102                               SOAPEnvelopeAxisImpl reqEnv,
103                               SOAPEnvelopeAxisImpl resEnv,
104                               Object JavaDoc obj)
105            throws Exception JavaDoc
106    {
107       OperationDesc operation = msgContext.getOperation();
108       if (operation == null)
109       {
110          throw new AxisFault(Messages.getMessage("noOperationForQName",
111                  reqEnv.getFirstBody().
112                  getQName().toString()));
113       }
114
115       Method JavaDoc method = operation.getMethod();
116
117       int methodType = operation.getMessageOperationStyle();
118
119       if (methodType != OperationDesc.MSG_METHOD_SOAPENVELOPE)
120       {
121          // dig out just the body, and pass it on
122
Vector JavaDoc bodies = reqEnv.getBodyElements();
123          Object JavaDoc argObjects[] = new Object JavaDoc[1];
124
125          switch (methodType)
126          {
127             // SOAPBodyElement [] / SOAPBodyElement []
128
case OperationDesc.MSG_METHOD_BODYARRAY:
129                SOAPBodyElementAxisImpl[] bodyElements =
130                        new SOAPBodyElementAxisImpl[bodies.size()];
131                bodies.toArray(bodyElements);
132                argObjects[0] = bodyElements;
133                SOAPBodyElementAxisImpl[] bodyResult = (SOAPBodyElementAxisImpl[])method.invoke(obj, argObjects);
134                if (bodyResult != null)
135                {
136                   for (int i = 0; i < bodyResult.length; i++)
137                   {
138                      SOAPBodyElementAxisImpl bodyElement = bodyResult[i];
139                      resEnv.addBodyElement(bodyElement);
140                   }
141                }
142                return;
143
144                // Element [] / Element []
145
case OperationDesc.MSG_METHOD_ELEMENTARRAY:
146                Element JavaDoc[] elements = new Element JavaDoc[bodies.size()];
147                for (int i = 0; i < elements.length; i++)
148                {
149                   SOAPBodyElementAxisImpl body = (SOAPBodyElementAxisImpl)bodies.get(i);
150                   elements[i] = body.getAsDOM();
151                }
152                argObjects[0] = elements;
153                Element JavaDoc[] elemResult = (Element JavaDoc[])method.invoke(obj, argObjects);
154                if (elemResult != null)
155                {
156                   for (int i = 0; i < elemResult.length; i++)
157                   {
158                      if (elemResult[i] != null)
159                         resEnv.addBodyElement(new SOAPBodyElementAxisImpl(elemResult[i]));
160                   }
161                }
162                return;
163
164                // Document / Document
165
case OperationDesc.MSG_METHOD_DOCUMENT:
166                Document JavaDoc doc = ((SOAPBodyElementAxisImpl)bodies.get(0)).getAsDocument();
167                argObjects[0] = doc;
168                Document JavaDoc resultDoc = (Document JavaDoc)method.invoke(obj, argObjects);
169                if (resultDoc != null)
170                {
171                   resEnv.addBodyElement(new SOAPBodyElementAxisImpl(resultDoc.getDocumentElement()));
172                }
173                return;
174          }
175       }
176       else
177       {
178          Object JavaDoc argObjects[] = new Object JavaDoc[2];
179
180          // SOAPEnvelope / SOAPEnvelope
181
argObjects[0] = reqEnv;
182          argObjects[1] = resEnv;
183          method.invoke(obj, argObjects);
184          return;
185       }
186
187       // SHOULD NEVER GET HERE...
188
throw new AxisFault(Messages.getMessage("badMsgMethodStyle"));
189    }
190 }
191
192 ;
193
Popular Tags