KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > handlers > HandlerChainImpl


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 package org.apache.axis.handlers;
17
18 import org.apache.axis.components.logger.LogFactory;
19 import org.apache.axis.utils.ClassUtils;
20 import org.apache.axis.utils.Messages;
21 import org.apache.commons.logging.Log;
22
23 import javax.xml.rpc.JAXRPCException JavaDoc;
24 import javax.xml.rpc.handler.Handler JavaDoc;
25 import javax.xml.rpc.handler.HandlerInfo JavaDoc;
26 import javax.xml.rpc.handler.MessageContext JavaDoc;
27 import javax.xml.rpc.handler.soap.SOAPMessageContext JavaDoc;
28 import javax.xml.rpc.soap.SOAPFaultException JavaDoc;
29 import javax.xml.soap.SOAPBody JavaDoc;
30 import javax.xml.soap.SOAPElement JavaDoc;
31 import javax.xml.soap.SOAPEnvelope JavaDoc;
32 import javax.xml.soap.SOAPException JavaDoc;
33 import javax.xml.soap.SOAPMessage JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Arrays JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * Implementation of HandlerChain
42  */

43 public class HandlerChainImpl extends ArrayList JavaDoc implements javax.xml.rpc.handler
44
JavaDoc        .HandlerChain {
45     protected static Log log =
46             LogFactory.getLog(HandlerChainImpl.class.getName());
47
48     public static final String JavaDoc JAXRPC_METHOD_INFO = "jaxrpc.method.info";
49
50     private String JavaDoc[] _roles;
51
52     private int falseIndex = -1;
53
54     public String JavaDoc[] getRoles() {
55         return _roles;
56     }
57
58     public void setRoles(String JavaDoc[] roles) {
59         if (roles != null) {
60             // use clone for cheap array copy
61
_roles = (String JavaDoc[])roles.clone();
62         }
63     }
64
65     public void init(Map JavaDoc map) {
66         // DO SOMETHING WITH THIS
67
}
68
69     protected List JavaDoc handlerInfos = new ArrayList JavaDoc();
70
71     public HandlerChainImpl() {
72     }
73
74     public HandlerChainImpl(List JavaDoc handlerInfos) {
75         this.handlerInfos = handlerInfos;
76         for (int i = 0; i < handlerInfos.size(); i++) {
77             add(newHandler(getHandlerInfo(i)));
78         }
79     }
80
81     public void addNewHandler(String JavaDoc className, Map JavaDoc config) {
82         try {
83             HandlerInfo JavaDoc handlerInfo =
84                 new HandlerInfo JavaDoc(ClassUtils.forName(className), config, null);
85             handlerInfos.add(handlerInfo);
86             add(newHandler(handlerInfo));
87         } catch (Exception JavaDoc ex) {
88             String JavaDoc messageText =
89                     Messages.getMessage("NoJAXRPCHandler00", className);
90             throw new JAXRPCException JavaDoc(messageText, ex);
91         }
92     }
93
94     public boolean handleFault(MessageContext JavaDoc _context) {
95         SOAPMessageContext JavaDoc context = (SOAPMessageContext JavaDoc)_context;
96         preInvoke(context);
97         try {
98             int endIdx = size() - 1;
99             if (falseIndex != -1) {
100                 endIdx = falseIndex;
101             }
102             for (int i = endIdx; i >= 0; i--) {
103                 if (getHandlerInstance(i).handleFault(context) == false) {
104                     return false;
105                 }
106             }
107             return true;
108         } finally {
109             postInvoke(context);
110         }
111     }
112
113     public ArrayList JavaDoc getMessageInfo(SOAPMessage JavaDoc message) {
114         ArrayList JavaDoc list = new ArrayList JavaDoc();
115         try {
116             if(message == null || message.getSOAPPart() == null)
117                 return list;
118             SOAPEnvelope JavaDoc env = message.getSOAPPart().getEnvelope();
119             SOAPBody JavaDoc body = env.getBody();
120             Iterator JavaDoc it = body.getChildElements();
121             SOAPElement JavaDoc operation = (SOAPElement JavaDoc)it.next();
122             list.add(operation.getElementName().toString());
123             for (Iterator JavaDoc i = operation.getChildElements(); i.hasNext();) {
124                 SOAPElement JavaDoc elt = (SOAPElement JavaDoc)i.next();
125                 list.add(elt.getElementName().toString());
126             }
127         } catch (Exception JavaDoc e) {
128             log.debug("Exception in getMessageInfo : ", e);
129         }
130         return list;
131     }
132
133     public boolean handleRequest(MessageContext JavaDoc _context) {
134         org.apache.axis.MessageContext actx =
135                 (org.apache.axis.MessageContext)_context;
136         actx.setRoles(getRoles());
137         SOAPMessageContext JavaDoc context = (SOAPMessageContext JavaDoc)_context;
138         preInvoke(context);
139         try {
140             for (int i = 0; i < size(); i++) {
141                 Handler JavaDoc currentHandler = getHandlerInstance(i);
142                 try {
143                     if (currentHandler.handleRequest(context) == false) {
144                         falseIndex = i;
145                         return false;
146                     }
147                 } catch (SOAPFaultException JavaDoc sfe) {
148                     falseIndex = i;
149                     throw sfe;
150                 }
151             }
152             return true;
153         } finally {
154             postInvoke(context);
155         }
156     }
157
158     public boolean handleResponse(MessageContext JavaDoc context) {
159         SOAPMessageContext JavaDoc scontext = (SOAPMessageContext JavaDoc)context;
160         preInvoke(scontext);
161         try {
162             int endIdx = size() - 1;
163             if (falseIndex != -1) {
164                 endIdx = falseIndex;
165             }
166             for (int i = endIdx; i >= 0; i--) {
167                 if (getHandlerInstance(i).handleResponse(context) == false) {
168                     return false;
169                 }
170             }
171             return true;
172         } finally {
173             postInvoke(scontext);
174         }
175     }
176
177     private void preInvoke(SOAPMessageContext JavaDoc msgContext) {
178         try {
179             SOAPMessage JavaDoc message = msgContext.getMessage();
180             // Ensure that message is already in the form we want
181
if(message != null && message.getSOAPPart() != null)
182                 message.getSOAPPart().getEnvelope();
183             msgContext.setProperty(org.apache.axis.SOAPPart.ALLOW_FORM_OPTIMIZATION,
184                     Boolean.FALSE);
185             msgContext.setProperty(JAXRPC_METHOD_INFO, getMessageInfo(message));
186         } catch (Exception JavaDoc e) {
187             log.debug("Exception in preInvoke : ", e);
188             throw new RuntimeException JavaDoc("Exception in preInvoke : " + e.toString());
189         }
190     }
191
192     private void postInvoke(SOAPMessageContext JavaDoc msgContext) {
193         Boolean JavaDoc propFormOptimization = (Boolean JavaDoc)msgContext.getProperty(org.apache.axis.SOAPPart.ALLOW_FORM_OPTIMIZATION);
194         if (propFormOptimization != null && !propFormOptimization.booleanValue()) {
195             msgContext.setProperty(org.apache.axis.SOAPPart.ALLOW_FORM_OPTIMIZATION,
196                     Boolean.TRUE);
197             SOAPMessage JavaDoc message = msgContext.getMessage();
198             ArrayList JavaDoc oldList =
199                     (ArrayList JavaDoc)msgContext.getProperty(JAXRPC_METHOD_INFO);
200             if (oldList != null) {
201                 if (!Arrays.equals(oldList.toArray(), getMessageInfo(message)
202                                 .toArray())) {
203                     throw new RuntimeException JavaDoc(Messages.getMessage("invocationArgumentsModified00"));
204                 }
205             }
206             try {
207                 if (message != null) {
208                     message.saveChanges();
209                 }
210             } catch (SOAPException JavaDoc e) {
211                 log.debug("Exception in postInvoke : ", e);
212                 throw new RuntimeException JavaDoc("Exception in postInvoke : " + e.toString());
213             }
214         }
215     }
216
217     public void destroy() {
218         int endIdx = size() - 1;
219         if (falseIndex != -1) {
220             endIdx = falseIndex;
221         }
222         for (int i = endIdx; i >= 0; i--) {
223             getHandlerInstance(i).destroy();
224         }
225         falseIndex = -1;
226         clear();
227     }
228
229     private Handler JavaDoc getHandlerInstance(int index) {
230         return (Handler JavaDoc)get(index);
231     }
232
233     private HandlerInfo JavaDoc getHandlerInfo(int index) {
234         return (HandlerInfo JavaDoc)handlerInfos.get(index);
235     }
236
237     private Handler JavaDoc newHandler(HandlerInfo JavaDoc handlerInfo) {
238         try {
239             Handler JavaDoc handler = (Handler JavaDoc)handlerInfo.getHandlerClass()
240                             .newInstance();
241             handler.init(handlerInfo);
242             return handler;
243         } catch (Exception JavaDoc ex) {
244             String JavaDoc messageText =
245                     Messages.getMessage("NoJAXRPCHandler00",
246                     handlerInfo.getHandlerClass().toString());
247             throw new JAXRPCException JavaDoc(messageText, ex);
248         }
249     }
250 }
Popular Tags