Source code: org/apache/axis/deployment/wsdd/WSDDDeployableItem.java
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.deployment.wsdd;
17
18 import org.apache.axis.ConfigurationException;
19 import org.apache.axis.EngineConfiguration;
20 import org.apache.axis.Handler;
21 import org.apache.axis.components.logger.LogFactory;
22 import org.apache.axis.encoding.SerializationContext;
23 import org.apache.axis.providers.java.JavaProvider;
24 import org.apache.axis.utils.ClassUtils;
25 import org.apache.axis.utils.JavaUtils;
26 import org.apache.axis.utils.LockableHashtable;
27 import org.apache.axis.utils.XMLUtils;
28 import org.apache.commons.logging.Log;
29 import org.w3c.dom.Element;
30 import org.xml.sax.helpers.AttributesImpl;
31
32 import javax.xml.namespace.QName;
33 import java.io.IOException;
34 import java.util.Hashtable;
35 import java.util.Iterator;
36 import java.util.Map;
37 import java.util.Set;
38
39
40 /**
41 * WSDD DeployableItem complexType
42 *
43 */
44 public abstract class WSDDDeployableItem
45 extends WSDDElement
46 {
47 public static final int SCOPE_PER_ACCESS = 0;
48 public static final int SCOPE_PER_REQUEST = 1;
49 public static final int SCOPE_SINGLETON = 2;
50 public static String [] scopeStrings = { "per-access",
51 "per-request",
52 "singleton" };
53
54 protected static Log log =
55 LogFactory.getLog(WSDDDeployableItem.class.getName());
56
57 /** Our parameters */
58 protected LockableHashtable parameters;
59
60 /** Our name */
61 protected QName qname;
62
63 /** Our type */
64 protected QName type;
65
66 /** Scope for this item (default is singleton) */
67 protected int scope = SCOPE_SINGLETON;
68
69 /** Placeholder for hanging on to singleton object */
70 protected Handler singletonInstance = null;
71
72 /**
73 * Default constructor
74 */
75 public WSDDDeployableItem()
76 {
77 }
78
79 /**
80 *
81 * @param e (Element) XXX
82 * @throws WSDDException XXX
83 */
84 public WSDDDeployableItem(Element e)
85 throws WSDDException
86 {
87 super(e);
88
89 String name = e.getAttribute(ATTR_NAME);
90 if (name != null && !name.equals("")) {
91 // qname = XMLUtils.getQNameFromString(name, e);
92 qname = new QName("", name);
93 }
94
95 String typeStr = e.getAttribute(ATTR_TYPE);
96 if (typeStr != null && !typeStr.equals("")) {
97 type = XMLUtils.getQNameFromString(typeStr, e);
98 }
99
100 // Figure out our scope - right now if a non-recognized scope
101 // attribute appears, we will ignore it and use the default
102 // scope. Is this right, or should we throw an error?
103 String scopeStr = e.getAttribute(JavaProvider.OPTION_SCOPE);
104 if (scopeStr != null) {
105 for (int i = 0; i < scopeStrings.length; i++) {
106 if (scopeStr.equals(scopeStrings[i])) {
107 scope = i;
108 break;
109 }
110 }
111 }
112
113 parameters = new LockableHashtable();
114
115 // Load up our params
116 Element [] paramElements = getChildElements(e, ELEM_WSDD_PARAM);
117 for (int i = 0; i < paramElements.length; i++) {
118 Element param = paramElements[i];
119 String pname = param.getAttribute(ATTR_NAME);
120 String value = param.getAttribute(ATTR_VALUE);
121 String locked = param.getAttribute(ATTR_LOCKED);
122 parameters.put(pname, value, JavaUtils.isTrueExplicitly(locked));
123 }
124 }
125
126 /**
127 *
128 * @param name XXX
129 */
130 public void setName(String name)
131 {
132 qname = new QName(null, name);
133 }
134
135 public void setQName(QName qname)
136 {
137 this.qname = qname;
138 }
139
140 /**
141 *
142 * @return XXX
143 */
144 public QName getQName()
145 {
146 return qname;
147 }
148
149 /**
150 *
151 * @return XXX
152 */
153 public QName getType()
154 {
155 return type;
156 }
157
158 /**
159 *
160 * @param type XXX
161 */
162 public void setType(QName type)
163 {
164 this.type = type;
165 }
166
167 /**
168 * Set a parameter
169 */
170 public void setParameter(String name, String value)
171 {
172 if (parameters == null) {
173 parameters = new LockableHashtable();
174 }
175 parameters.put(name, value);
176 }
177
178 /**
179 * Get the value of one of our parameters
180 */
181 public String getParameter(String name)
182 {
183 if (name == null || parameters == null) {
184 return null;
185 }
186
187 return (String)parameters.get(name);
188 }
189
190 /**
191 * Returns the config parameters as a hashtable (lockable)
192 * @return XXX
193 */
194 public LockableHashtable getParametersTable()
195 {
196 return parameters;
197 }
198
199 /**
200 * Convenience method for using old deployment XML with WSDD.
201 * This allows us to set the options directly after the Admin class
202 * has parsed them out of the old format.
203 */
204 public void setOptionsHashtable(Hashtable hashtable)
205 {
206 if (hashtable == null)
207 return;
208
209 parameters = new LockableHashtable(hashtable);
210 }
211
212 public void writeParamsToContext(SerializationContext context)
213 throws IOException
214 {
215 if (parameters == null)
216 return;
217
218 Set entries = parameters.entrySet();
219 Iterator i = entries.iterator();
220 while (i.hasNext()) {
221 Map.Entry entry = (Map.Entry) i.next();
222 String name = (String) entry.getKey();
223 AttributesImpl attrs = new AttributesImpl();
224
225 attrs.addAttribute("", ATTR_NAME, ATTR_NAME, "CDATA", name);
226 attrs.addAttribute("", ATTR_VALUE, ATTR_VALUE, "CDATA",
227 entry.getValue().toString());
228 if (parameters.isKeyLocked(name)) {
229 attrs.addAttribute("", ATTR_LOCKED, ATTR_LOCKED, "CDATA", "true");
230 }
231
232 context.startElement(QNAME_PARAM, attrs);
233 context.endElement();
234 }
235 }
236
237 /**
238 *
239 * @param name XXX
240 */
241 public void removeParameter(String name)
242 {
243 if (parameters != null) {
244 parameters.remove(name);
245 }
246 }
247
248 /**
249 *
250 * @param registry XXX
251 * @return XXX
252 * @throws ConfigurationException XXX
253 */
254 public final Handler getInstance(EngineConfiguration registry)
255 throws ConfigurationException
256 {
257 if (scope == SCOPE_SINGLETON) {
258 synchronized (this) {
259 if (singletonInstance == null)
260 singletonInstance = getNewInstance(registry);
261 }
262 return singletonInstance;
263 }
264
265 return getNewInstance(registry);
266 }
267
268 private Handler getNewInstance(EngineConfiguration registry)
269 throws ConfigurationException
270 {
271 QName type = getType();
272 if (type == null ||
273 WSDDConstants.URI_WSDD_JAVA.equals(type.getNamespaceURI())) {
274 return makeNewInstance(registry);
275 } else {
276 return registry.getHandler(type);
277 }
278 }
279
280 /**
281 * Creates a new instance of this deployable. if the
282 * java class is not found, the registry is queried to
283 * find a suitable item
284 * @param registry XXX
285 * @return XXX
286 * @throws ConfigurationException XXX
287 */
288 protected Handler makeNewInstance(EngineConfiguration registry)
289 throws ConfigurationException
290 {
291 Class c = null;
292 Handler h = null;
293
294 try {
295 c = getJavaClass();
296 } catch (ClassNotFoundException e) {
297 throw new ConfigurationException(e);
298 }
299
300 if (c != null) {
301 try {
302 h = (Handler)createInstance(c);
303 } catch (Exception e) {
304 throw new ConfigurationException(e);
305 }
306
307 if (h != null) {
308 if ( qname != null )
309 h.setName(qname.getLocalPart());
310 h.setOptions(getParametersTable());
311 try{
312 h.init();
313 }catch(Exception e){
314 String msg=e + JavaUtils.LS + JavaUtils.stackToString(e);
315 log.debug(msg);
316 throw new ConfigurationException(e);
317 }catch(Error e){
318 String msg=e + JavaUtils.LS + JavaUtils.stackToString(e);
319 log.debug(msg);
320 throw new ConfigurationException(msg);
321 }
322 }
323 } else {
324 // !!! Should never get here!
325 h = registry.getHandler(getType());
326 }
327
328 return h;
329 }
330
331 /**
332 *
333 * @param _class XXX
334 * @return XXX
335 */
336 Object createInstance(Class _class)
337 throws InstantiationException, IllegalAccessException
338 {
339 return _class.newInstance();
340 }
341
342 /**
343 *
344 * @return XXX
345 * @throws ClassNotFoundException XXX
346 */
347 public Class getJavaClass()
348 throws ClassNotFoundException
349 {
350 QName type = getType();
351 if (type != null &&
352 URI_WSDD_JAVA.equals(type.getNamespaceURI())) {
353 return ClassUtils.forName(type.getLocalPart());
354 }
355 return null;
356 }
357 }