KickJava   Java API By Example, From Geeks To Geeks.

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


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.handlers;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Handler;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.components.logger.LogFactory;
23 import org.apache.axis.utils.LockableHashtable;
24 import org.apache.commons.logging.Log;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27
28 import javax.xml.namespace.QName JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.List JavaDoc;
32
33
34 /** <code>BasicHandler</code> is a utility class which implements simple
35  * property setting/getting behavior, and stubs out a lot of the Handler
36  * methods. Extend this class to make writing your Handlers easier, and
37  * then override what you need to.
38  *
39  * @author Glen Daniels (gdaniels@allaire.com)
40  * @author Doug Davis (dug@us.ibm.com
41  */

42 public abstract class BasicHandler implements Handler {
43     private static Log log =
44         LogFactory.getLog(BasicHandler.class.getName());
45
46     protected boolean makeLockable = false;
47     protected Hashtable options;
48     protected String JavaDoc name;
49
50
51     /**
52      * Should this Handler use a LockableHashtable for options?
53      * Default is 'false'.
54      */

55     protected void setOptionsLockable(boolean makeLockable) {
56         this.makeLockable = makeLockable;
57     }
58     
59     protected void initHashtable()
60     {
61         if (makeLockable) {
62             options = new LockableHashtable();
63         } else {
64             options = new Hashtable();
65         }
66     }
67
68     /**
69      * Stubbed-out methods. Override in your child class to implement
70      * any real behavior. Note that there is NOT a stub for invoke(), since
71      * we require any Handler derivative to implement that.
72      */

73
74
75     public void init()
76     {
77     }
78
79     public void cleanup()
80     {
81     }
82
83     public boolean canHandleBlock(QName JavaDoc qname)
84     {
85         return false;
86     }
87
88     public void onFault(MessageContext msgContext)
89     {
90     }
91     
92     /**
93      * Set the given option (name/value) in this handler's bag of options
94      */

95     public void setOption(String JavaDoc name, Object JavaDoc value) {
96         if ( options == null ) initHashtable();
97         options.put( name, value );
98     }
99
100     /**
101      * Set a default value for the given option:
102      * if the option is not already set, then set it.
103      * if the option is already set, then do not set it.
104      * <p>
105      * If this is called multiple times, the first with a non-null value
106      * if 'value' will set the default, remaining calls will be ignored.
107      * <p>
108      * Returns true if value set (by this call), otherwise false;
109      */

110     public boolean setOptionDefault(String JavaDoc name, Object JavaDoc value) {
111         boolean val = (options == null || options.get(name) == null) && value != null;
112         if (val) {
113             setOption(name, value);
114         }
115         return val;
116     }
117     
118     /**
119      * Returns the option corresponding to the 'name' given
120      */

121     public Object JavaDoc getOption(String JavaDoc name) {
122         if ( options == null ) return( null );
123         return( options.get(name) );
124     }
125
126     /**
127      * Return the entire list of options
128      */

129     public Hashtable getOptions() {
130         return( options );
131     }
132
133     public void setOptions(Hashtable opts) {
134         options = opts;
135     }
136
137     /**
138      * Set the name (i.e. registry key) of this Handler
139      */

140     public void setName(String JavaDoc name)
141     {
142         this.name = name;
143     }
144
145     /**
146      * Return the name (i.e. registry key) for this Handler
147      */

148     public String JavaDoc getName()
149     {
150         return name;
151     }
152
153     public Element JavaDoc getDeploymentData(Document JavaDoc doc) {
154         log.debug("Enter: BasicHandler::getDeploymentData");
155
156         Element JavaDoc root = doc.createElementNS("", "handler");
157
158         root.setAttribute( "class", this.getClass().getName() );
159         options = this.getOptions();
160         if ( options != null ) {
161             Enumeration JavaDoc e = options.keys();
162             while ( e.hasMoreElements() ) {
163                 String JavaDoc k = (String JavaDoc) e.nextElement();
164                 Object JavaDoc v = options.get(k);
165                 Element JavaDoc e1 = doc.createElementNS("", "option" );
166                 e1.setAttribute( "name", k );
167                 e1.setAttribute( "value", v.toString() );
168                 root.appendChild( e1 );
169             }
170         }
171         log.debug("Exit: BasicHandler::getDeploymentData");
172         return( root );
173     }
174
175     public void generateWSDL(MessageContext msgContext) throws AxisFault
176     {
177     }
178
179     /**
180      * Return a list of QNames which this Handler understands. By returning
181      * a particular QName here, we are committing to fulfilling any contracts
182      * defined in the specification of the SOAP header with that QName.
183      */

184     public List JavaDoc getUnderstoodHeaders() {
185         return null;
186     }
187 }
188
Popular Tags