KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > handlers > BasicHandler


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

54
55 package org.jboss.axis.handlers;
56
57 import org.jboss.axis.AxisFault;
58 import org.jboss.axis.Handler;
59 import org.jboss.axis.MessageContext;
60 import org.jboss.axis.utils.LockableHashtable;
61 import org.jboss.logging.Logger;
62 import org.w3c.dom.Document JavaDoc;
63 import org.w3c.dom.Element JavaDoc;
64
65 import javax.xml.namespace.QName JavaDoc;
66 import java.util.Enumeration JavaDoc;
67 import java.util.Hashtable JavaDoc;
68 import java.util.List JavaDoc;
69
70
71 /**
72  * <code>BasicHandler</code> is a utility class which implements simple
73  * property setting/getting behavior, and stubs out a lot of the Handler
74  * methods. Extend this class to make writing your Handlers easier, and
75  * then override what you need to.
76  *
77  * @author Glen Daniels (gdaniels@allaire.com)
78  * @author Doug Davis (dug@us.ibm.com
79  */

80 public abstract class BasicHandler implements Handler
81 {
82    private static Logger log = Logger.getLogger(BasicHandler.class.getName());
83
84    protected boolean makeLockable = false;
85    protected Hashtable options;
86    protected String JavaDoc name;
87
88
89    /**
90     * Should this Handler use a LockableHashtable for options?
91     * Default is 'false'.
92     */

93    protected void setOptionsLockable(boolean makeLockable)
94    {
95       this.makeLockable = makeLockable;
96    }
97
98    protected void initHashtable()
99    {
100       if (makeLockable)
101       {
102          options = new LockableHashtable();
103       }
104       else
105       {
106          options = new Hashtable();
107       }
108    }
109
110    /**
111     * Stubbed-out methods. Override in your child class to implement
112     * any real behavior.
113     */

114    public void init()
115    {
116    }
117
118    public void cleanup()
119    {
120    }
121
122    public boolean canHandleBlock(QName JavaDoc qname)
123    {
124       return false;
125    }
126
127    public void onFault(MessageContext msgContext)
128    {
129    }
130
131    /**
132     * Must implement this in subclasses.
133     */

134    public abstract void invoke(MessageContext msgContext) throws AxisFault;
135
136    /**
137     * Set the given option (name/value) in this handler's bag of options
138     */

139    public void setOption(String JavaDoc name, Object JavaDoc value)
140    {
141       if (options == null) initHashtable();
142       options.put(name, value);
143    }
144
145    /**
146     * Set a default value for the given option:
147     * if the option is not already set, then set it.
148     * if the option is already set, then do not set it.
149     * <p/>
150     * If this is called multiple times, the first with a non-null value
151     * if 'value' will set the default, remaining calls will be ignored.
152     * <p/>
153     * Returns true if value set (by this call), otherwise false;
154     */

155    public boolean setOptionDefault(String JavaDoc name, Object JavaDoc value)
156    {
157       boolean val = (options == null || options.get(name) == null) && value != null;
158       if (val)
159       {
160          setOption(name, value);
161       }
162       return val;
163    }
164
165    /**
166     * Returns the option corresponding to the 'name' given
167     */

168    public Object JavaDoc getOption(String JavaDoc name)
169    {
170       if (options == null) return (null);
171       return (options.get(name));
172    }
173
174    /**
175     * Return the entire list of options
176     */

177    public Hashtable getOptions()
178    {
179       return (options);
180    }
181
182    public void setOptions(Hashtable opts)
183    {
184       options = opts;
185    }
186
187    /**
188     * Set the name (i.e. registry key) of this Handler
189     */

190    public void setName(String JavaDoc name)
191    {
192       this.name = name;
193    }
194
195    /**
196     * Return the name (i.e. registry key) for this Handler
197     */

198    public String JavaDoc getName()
199    {
200       return name;
201    }
202
203    public Element JavaDoc getDeploymentData(Document JavaDoc doc)
204    {
205       log.debug("Enter: BasicHandler::getDeploymentData");
206
207       Element JavaDoc root = doc.createElementNS("", "handler");
208
209       root.setAttribute("class", this.getClass().getName());
210       options = this.getOptions();
211       if (options != null)
212       {
213          Enumeration JavaDoc e = options.keys();
214          while (e.hasMoreElements())
215          {
216             String JavaDoc k = (String JavaDoc)e.nextElement();
217             Object JavaDoc v = options.get(k);
218             Element JavaDoc e1 = doc.createElementNS("", "option");
219             e1.setAttribute("name", k);
220             e1.setAttribute("value", v.toString());
221             root.appendChild(e1);
222          }
223       }
224       log.debug("Exit: BasicHandler::getDeploymentData");
225       return (root);
226    }
227
228    public void generateWSDL(MessageContext msgContext) throws AxisFault
229    {
230    }
231
232    /**
233     * Return a list of QNames which this Handler understands. By returning
234     * a particular QName here, we are committing to fulfilling any contracts
235     * defined in the specification of the SOAP header with that QName.
236     */

237    public List JavaDoc getUnderstoodHeaders()
238    {
239       return null;
240    }
241 }
242
Popular Tags