KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > deployment > wsdd > WSDDDeployableItem


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 package org.jboss.axis.deployment.wsdd;
56
57 import org.jboss.axis.ConfigurationException;
58 import org.jboss.axis.EngineConfiguration;
59 import org.jboss.axis.Handler;
60 import org.jboss.axis.encoding.SerializationContext;
61 import org.jboss.axis.providers.java.JavaProvider;
62 import org.jboss.axis.utils.ClassUtils;
63 import org.jboss.axis.utils.JavaUtils;
64 import org.jboss.axis.utils.LockableHashtable;
65 import org.jboss.axis.utils.XMLUtils;
66 import org.jboss.logging.Logger;
67 import org.w3c.dom.Element JavaDoc;
68 import org.xml.sax.helpers.AttributesImpl JavaDoc;
69
70 import javax.xml.namespace.QName JavaDoc;
71 import java.io.IOException JavaDoc;
72 import java.util.Hashtable JavaDoc;
73 import java.util.Iterator JavaDoc;
74 import java.util.Map JavaDoc;
75 import java.util.Set JavaDoc;
76
77
78 /**
79  * WSDD DeployableItem complexType
80  */

81 public abstract class WSDDDeployableItem
82         extends WSDDElement
83 {
84    public static final int SCOPE_PER_ACCESS = 0;
85    public static final int SCOPE_PER_REQUEST = 1;
86    public static final int SCOPE_SINGLETON = 2;
87    public static String JavaDoc[] scopeStrings = {"per-access",
88                                           "per-request",
89                                           "singleton"};
90
91    private static Logger log = Logger.getLogger(WSDDDeployableItem.class.getName());
92
93    /**
94     * Our parameters
95     */

96    protected LockableHashtable parameters;
97
98    /**
99     * Our name
100     */

101    protected QName JavaDoc qname;
102
103    /**
104     * Our type
105     */

106    protected QName JavaDoc type;
107
108    /**
109     * Scope for this item (default is singleton)
110     */

111    protected int scope = SCOPE_SINGLETON;
112
113    /**
114     * Placeholder for hanging on to singleton object
115     */

116    protected Handler singletonInstance = null;
117
118    /**
119     * Default constructor
120     */

121    public WSDDDeployableItem()
122    {
123    }
124
125    /**
126     * @param e (Element) XXX
127     * @throws WSDDException XXX
128     */

129    public WSDDDeployableItem(Element JavaDoc e)
130            throws WSDDException
131    {
132       super(e);
133
134       String JavaDoc name = e.getAttribute(ATTR_NAME);
135       if (name != null && !name.equals(""))
136       {
137 // qname = XMLUtils.getQNameFromString(name, e);
138
qname = new QName JavaDoc("", name);
139       }
140
141       String JavaDoc typeStr = e.getAttribute(ATTR_TYPE);
142       if (typeStr != null && !typeStr.equals(""))
143       {
144          type = XMLUtils.getQNameFromString(typeStr, e);
145       }
146
147       // Figure out our scope - right now if a non-recognized scope
148
// attribute appears, we will ignore it and use the default
149
// scope. Is this right, or should we throw an error?
150
String JavaDoc scopeStr = e.getAttribute(JavaProvider.OPTION_SCOPE);
151       if (scopeStr != null)
152       {
153          for (int i = 0; i < scopeStrings.length; i++)
154          {
155             if (scopeStr.equals(scopeStrings[i]))
156             {
157                scope = i;
158                break;
159             }
160          }
161       }
162
163       if (parameters == null)
164          parameters = new LockableHashtable();
165
166       // Load up our params
167
Element JavaDoc[] paramElements = getChildElements(e, ELEM_WSDD_PARAM);
168       for (int i = 0; i < paramElements.length; i++)
169       {
170          Element JavaDoc param = paramElements[i];
171          String JavaDoc pname = param.getAttribute(ATTR_NAME);
172          String JavaDoc value = param.getAttribute(ATTR_VALUE);
173          String JavaDoc locked = param.getAttribute(ATTR_LOCKED);
174          parameters.put(pname, value, JavaUtils.isTrueExplicitly(locked));
175       }
176    }
177
178    /**
179     * @param name XXX
180     */

181    public void setName(String JavaDoc name)
182    {
183       qname = new QName JavaDoc(null, name);
184    }
185
186    public void setQName(QName JavaDoc qname)
187    {
188       this.qname = qname;
189    }
190
191    /**
192     * @return XXX
193     */

194    public QName JavaDoc getQName()
195    {
196       return qname;
197    }
198
199    /**
200     * @return XXX
201     */

202    public QName JavaDoc getType()
203    {
204       return type;
205    }
206
207    /**
208     * @param type XXX
209     */

210    public void setType(QName JavaDoc type)
211    {
212       this.type = type;
213    }
214
215    /**
216     * Set a parameter
217     */

218    public void setParameter(String JavaDoc name, String JavaDoc value)
219    {
220       if (parameters == null)
221          parameters = new LockableHashtable();
222       parameters.put(name, value);
223    }
224
225    /**
226     * Get the value of one of our parameters
227     */

228    public String JavaDoc getParameter(String JavaDoc name)
229    {
230       if (name == null)
231          return null;
232
233       return (String JavaDoc)parameters.get(name);
234    }
235
236    /**
237     * Returns the config parameters as a hashtable (lockable)
238     *
239     * @return XXX
240     */

241    public LockableHashtable getParametersTable()
242    {
243       return parameters;
244    }
245
246    /**
247     * Convenience method for using old deployment XML with WSDD.
248     * This allows us to set the options directly after the Admin class
249     * has parsed them out of the old format.
250     */

251    public void setOptionsHashtable(Hashtable hashtable)
252    {
253       if (hashtable == null)
254          return;
255
256       parameters = new LockableHashtable(hashtable);
257    }
258
259    public void writeParamsToContext(SerializationContext context)
260            throws IOException JavaDoc
261    {
262       if (parameters == null)
263          return;
264
265       Set JavaDoc entries = parameters.entrySet();
266       Iterator JavaDoc i = entries.iterator();
267       while (i.hasNext())
268       {
269          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
270          String JavaDoc name = (String JavaDoc)entry.getKey();
271          AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
272
273          attrs.addAttribute("", ATTR_NAME, ATTR_NAME, "CDATA", name);
274          attrs.addAttribute("", ATTR_VALUE, ATTR_VALUE, "CDATA",
275                  entry.getValue().toString());
276          if (parameters.isKeyLocked(name))
277          {
278             attrs.addAttribute("", ATTR_LOCKED, ATTR_LOCKED, "CDATA", "true");
279          }
280
281          context.startElement(QNAME_PARAM, attrs);
282          context.endElement();
283       }
284    }
285
286    /**
287     * @param name XXX
288     */

289    public void removeParameter(String JavaDoc name)
290    {
291       parameters.remove(name);
292    }
293
294    /**
295     * @param registry XXX
296     * @return XXX
297     * @throws ConfigurationException XXX
298     */

299    public final Handler getInstance(EngineConfiguration registry)
300            throws ConfigurationException
301    {
302       if (scope == SCOPE_SINGLETON)
303       {
304          synchronized (this)
305          {
306             if (singletonInstance == null)
307                singletonInstance = getNewInstance(registry);
308          }
309          return singletonInstance;
310       }
311
312       return getNewInstance(registry);
313    }
314
315    private Handler getNewInstance(EngineConfiguration registry)
316            throws ConfigurationException
317    {
318       QName JavaDoc type = getType();
319       if (type == null ||
320               WSDDConstants.URI_WSDD_JAVA.equals(type.getNamespaceURI()))
321       {
322          return makeNewInstance(registry);
323       }
324       else
325       {
326          return registry.getHandler(type);
327       }
328    }
329
330    /**
331     * Creates a new instance of this deployable. if the
332     * java class is not found, the registry is queried to
333     * find a suitable item
334     *
335     * @param registry XXX
336     * @return XXX
337     * @throws ConfigurationException XXX
338     */

339    protected Handler makeNewInstance(EngineConfiguration registry)
340            throws ConfigurationException
341    {
342       Class JavaDoc c = null;
343       Handler h = null;
344
345       try
346       {
347          c = getJavaClass();
348       }
349       catch (ClassNotFoundException JavaDoc e)
350       {
351          throw new ConfigurationException(e);
352       }
353
354       if (c != null)
355       {
356          try
357          {
358             h = (Handler)createInstance(c);
359          }
360          catch (Exception JavaDoc e)
361          {
362             throw new ConfigurationException(e);
363          }
364
365          if (h != null)
366          {
367             if (qname != null)
368                h.setName(qname.getLocalPart());
369             h.setOptions(getParametersTable());
370             try
371             {
372                h.init();
373             }
374             catch (Exception JavaDoc e)
375             {
376                String JavaDoc msg = e + JavaUtils.LS + JavaUtils.stackToString(e);
377                log.debug(msg);
378                throw new ConfigurationException(e);
379             }
380             catch (Error JavaDoc e)
381             {
382                String JavaDoc msg = e + JavaUtils.LS + JavaUtils.stackToString(e);
383                log.debug(msg);
384                throw new ConfigurationException(msg);
385             }
386          }
387       }
388       else
389       {
390          // !!! Should never get here!
391
h = registry.getHandler(getType());
392       }
393
394       return h;
395    }
396
397    /**
398     * @param _class XXX
399     * @return XXX
400     */

401    Object JavaDoc createInstance(Class JavaDoc _class)
402            throws InstantiationException JavaDoc, IllegalAccessException JavaDoc
403    {
404       return _class.newInstance();
405    }
406
407    /**
408     * @return XXX
409     * @throws ClassNotFoundException XXX
410     */

411    public Class JavaDoc getJavaClass()
412            throws ClassNotFoundException JavaDoc
413    {
414       QName JavaDoc type = getType();
415       if (type != null &&
416               URI_WSDD_JAVA.equals(type.getNamespaceURI()))
417       {
418          return ClassUtils.forName(type.getLocalPart());
419       }
420       return null;
421    }
422 }
423
Popular Tags