KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > SimpleChain


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;
57
58 import org.jboss.axis.handlers.BasicHandler;
59 import org.jboss.axis.strategies.InvocationStrategy;
60 import org.jboss.axis.strategies.WSDLGenStrategy;
61 import org.jboss.axis.utils.Messages;
62 import org.jboss.logging.Logger;
63 import org.w3c.dom.Document JavaDoc;
64 import org.w3c.dom.Element JavaDoc;
65
66 import javax.xml.namespace.QName JavaDoc;
67 import java.util.Enumeration JavaDoc;
68 import java.util.Vector JavaDoc;
69
70 /**
71  * A Simple Chain is a 'composite' Handler in that it aggregates a collection
72  * of Handlers and also acts as a Handler which delegates its operations to
73  * the collection.
74  * <p/>
75  * A Simple Chain initially has no Handlers. Handlers may be added until the
76  * chain is invoke()d after which Handlers may not be added (and any attempt
77  * to do so will throw an exception).
78  *
79  * @author Doug Davis (dug@us.ibm.com)
80  * @author Glyn Normington (norm@uk.ibm.com)
81  */

82 public class SimpleChain extends BasicHandler implements Chain
83 {
84    private static Logger log = Logger.getLogger(SimpleChain.class.getName());
85
86    protected Vector JavaDoc handlers = new Vector JavaDoc();
87    protected boolean invoked = false;
88
89    private String JavaDoc CAUGHTFAULT_PROPERTY =
90            "org.jboss.axis.SimpleChain.caughtFaultInResponse";
91
92    public void init()
93    {
94       for (int i = 0; i < handlers.size(); i++)
95          ((Handler)handlers.elementAt(i)).init();
96    }
97
98    public void cleanup()
99    {
100       for (int i = 0; i < handlers.size(); i++)
101          ((Handler)handlers.elementAt(i)).cleanup();
102    }
103
104    private static final HandlerIterationStrategy iVisitor =
105            new InvocationStrategy();
106
107    private static final HandlerIterationStrategy wsdlVisitor =
108            new WSDLGenStrategy();
109
110    /**
111     * Iterate over the chain invoking each handler. If there's a fault
112     * then call 'onFault' for each completed handler in reverse order, then
113     * rethrow the exception.
114     */

115    public void invoke(MessageContext msgContext) throws AxisFault
116    {
117       if (log.isDebugEnabled())
118       {
119          log.debug("Enter: SimpleChain::invoke");
120       }
121
122       invoked = true;
123       doVisiting(msgContext, iVisitor);
124
125       if (log.isDebugEnabled())
126       {
127          log.debug("Exit: SimpleChain::invoke");
128       }
129    }
130
131    /**
132     * Iterate over the chain letting each handler have a crack at
133     * contributing to a WSDL description.
134     */

135    public void generateWSDL(MessageContext msgContext) throws AxisFault
136    {
137       if (log.isDebugEnabled())
138       {
139          log.debug("Enter: SimpleChain::generateWSDL");
140       }
141
142       invoked = true;
143       doVisiting(msgContext, wsdlVisitor);
144
145       if (log.isDebugEnabled())
146       {
147          log.debug("Exit: SimpleChain::generateWSDL");
148       }
149    }
150
151    private void doVisiting(MessageContext msgContext,
152                            HandlerIterationStrategy visitor) throws AxisFault
153    {
154       int i = 0;
155       try
156       {
157          Enumeration JavaDoc en = handlers.elements();
158          while (en.hasMoreElements())
159          {
160             visitor.visit((Handler)en.nextElement(), msgContext);
161             i++;
162          }
163       }
164       catch (AxisFault f)
165       {
166          // Something went wrong. If we haven't already put this fault
167
// into the MessageContext's response message, do so and make sure
168
// we only do it once. This allows onFault() methods to safely
169
// set headers and such in the response message without them
170
// getting stomped.
171
if (!msgContext.isPropertyTrue(CAUGHTFAULT_PROPERTY))
172          {
173             // Attach the fault to the response message; enabling access to the
174
// fault details while inside the handler onFault methods.
175
Message respMsg = new Message(f);
176             msgContext.setResponseMessage(respMsg);
177             msgContext.setProperty(CAUGHTFAULT_PROPERTY, Boolean.TRUE);
178          }
179          while (--i >= 0)
180             ((Handler)handlers.elementAt(i)).onFault(msgContext);
181          throw f;
182       }
183    }
184
185    /**
186     * Notify the handlers in this chain because some handler
187     * later on has faulted - in reverse order. If any handlers
188     * have been added since we visited the chain, they will get
189     * notified too!
190     */

191    public void onFault(MessageContext msgContext)
192    {
193       if (log.isDebugEnabled())
194       {
195          log.debug("Enter: SimpleChain::onFault");
196       }
197
198       for (int i = handlers.size() - 1; i >= 0; i--)
199          ((Handler)handlers.elementAt(i)).onFault(msgContext);
200
201       if (log.isDebugEnabled())
202       {
203          log.debug("Exit: SimpleChain::onFault");
204       }
205    }
206
207    public boolean canHandleBlock(QName JavaDoc qname)
208    {
209       for (int i = 0; i < handlers.size(); i++)
210          if (((Handler)handlers.elementAt(i)).canHandleBlock(qname))
211             return (true);
212       return (false);
213    }
214
215    public void addHandler(Handler handler)
216    {
217       if (handler == null)
218          throw new InternalException(Messages.getMessage("nullHandler00",
219                  "SimpleChain::addHandler"));
220
221       if (invoked)
222          throw new InternalException(Messages.getMessage("addAfterInvoke00",
223                  "SimpleChain::addHandler"));
224
225       handlers.add(handler);
226    }
227
228    public boolean contains(Handler handler)
229    {
230       return (handlers.contains(handler));
231    }
232
233    public Handler[] getHandlers()
234    {
235       if (handlers.size() == 0)
236          return null;
237
238       Handler[] ret = new Handler[handlers.size()];
239       return ((Handler[])handlers.toArray(ret));
240    }
241
242    public Element JavaDoc getDeploymentData(Document JavaDoc doc)
243    {
244       if (log.isDebugEnabled())
245       {
246          log.debug(Messages.getMessage("enter00",
247                  "SimpleChain::getDeploymentData"));
248       }
249
250       Element JavaDoc root = doc.createElementNS("", "chain");
251
252       StringBuffer JavaDoc str = new StringBuffer JavaDoc();
253       int i = 0;
254       while (i < handlers.size())
255       {
256          if (i != 0) str.append(",");
257          Handler h = (Handler)handlers.elementAt(i);
258          str.append(h.getName());
259          i++;
260       }
261       if (i > 0)
262       {
263          root.setAttribute("flow", str.toString());
264       }
265
266       if (options != null)
267       {
268          Enumeration JavaDoc e = options.keys();
269          while (e.hasMoreElements())
270          {
271             String JavaDoc k = (String JavaDoc)e.nextElement();
272             Object JavaDoc v = options.get(k);
273             Element JavaDoc e1 = doc.createElementNS("", "option");
274             e1.setAttribute("name", k);
275             e1.setAttribute("value", v.toString());
276             root.appendChild(e1);
277          }
278       }
279
280       if (log.isDebugEnabled())
281       {
282          log.debug("Exit: SimpleChain::getDeploymentData");
283       }
284
285       return (root);
286    }
287 }
288
Popular Tags