1 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 ; 68 import org.xml.sax.helpers.AttributesImpl ; 69 70 import javax.xml.namespace.QName ; 71 import java.io.IOException ; 72 import java.util.Hashtable ; 73 import java.util.Iterator ; 74 import java.util.Map ; 75 import java.util.Set ; 76 77 78 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 [] scopeStrings = {"per-access", 88 "per-request", 89 "singleton"}; 90 91 private static Logger log = Logger.getLogger(WSDDDeployableItem.class.getName()); 92 93 96 protected LockableHashtable parameters; 97 98 101 protected QName qname; 102 103 106 protected QName type; 107 108 111 protected int scope = SCOPE_SINGLETON; 112 113 116 protected Handler singletonInstance = null; 117 118 121 public WSDDDeployableItem() 122 { 123 } 124 125 129 public WSDDDeployableItem(Element e) 130 throws WSDDException 131 { 132 super(e); 133 134 String name = e.getAttribute(ATTR_NAME); 135 if (name != null && !name.equals("")) 136 { 137 qname = new QName ("", name); 139 } 140 141 String typeStr = e.getAttribute(ATTR_TYPE); 142 if (typeStr != null && !typeStr.equals("")) 143 { 144 type = XMLUtils.getQNameFromString(typeStr, e); 145 } 146 147 String 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 Element [] paramElements = getChildElements(e, ELEM_WSDD_PARAM); 168 for (int i = 0; i < paramElements.length; i++) 169 { 170 Element param = paramElements[i]; 171 String pname = param.getAttribute(ATTR_NAME); 172 String value = param.getAttribute(ATTR_VALUE); 173 String locked = param.getAttribute(ATTR_LOCKED); 174 parameters.put(pname, value, JavaUtils.isTrueExplicitly(locked)); 175 } 176 } 177 178 181 public void setName(String name) 182 { 183 qname = new QName (null, name); 184 } 185 186 public void setQName(QName qname) 187 { 188 this.qname = qname; 189 } 190 191 194 public QName getQName() 195 { 196 return qname; 197 } 198 199 202 public QName getType() 203 { 204 return type; 205 } 206 207 210 public void setType(QName type) 211 { 212 this.type = type; 213 } 214 215 218 public void setParameter(String name, String value) 219 { 220 if (parameters == null) 221 parameters = new LockableHashtable(); 222 parameters.put(name, value); 223 } 224 225 228 public String getParameter(String name) 229 { 230 if (name == null) 231 return null; 232 233 return (String )parameters.get(name); 234 } 235 236 241 public LockableHashtable getParametersTable() 242 { 243 return parameters; 244 } 245 246 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 261 { 262 if (parameters == null) 263 return; 264 265 Set entries = parameters.entrySet(); 266 Iterator i = entries.iterator(); 267 while (i.hasNext()) 268 { 269 Map.Entry entry = (Map.Entry )i.next(); 270 String name = (String )entry.getKey(); 271 AttributesImpl attrs = new AttributesImpl (); 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 289 public void removeParameter(String name) 290 { 291 parameters.remove(name); 292 } 293 294 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 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 339 protected Handler makeNewInstance(EngineConfiguration registry) 340 throws ConfigurationException 341 { 342 Class c = null; 343 Handler h = null; 344 345 try 346 { 347 c = getJavaClass(); 348 } 349 catch (ClassNotFoundException 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 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 e) 375 { 376 String msg = e + JavaUtils.LS + JavaUtils.stackToString(e); 377 log.debug(msg); 378 throw new ConfigurationException(e); 379 } 380 catch (Error e) 381 { 382 String msg = e + JavaUtils.LS + JavaUtils.stackToString(e); 383 log.debug(msg); 384 throw new ConfigurationException(msg); 385 } 386 } 387 } 388 else 389 { 390 h = registry.getHandler(getType()); 392 } 393 394 return h; 395 } 396 397 401 Object createInstance(Class _class) 402 throws InstantiationException , IllegalAccessException 403 { 404 return _class.newInstance(); 405 } 406 407 411 public Class getJavaClass() 412 throws ClassNotFoundException 413 { 414 QName 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 |