1 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 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 58 protected LockableHashtable parameters; 59 60 61 protected QName qname; 62 63 64 protected QName type; 65 66 67 protected int scope = SCOPE_SINGLETON; 68 69 70 protected Handler singletonInstance = null; 71 72 75 public WSDDDeployableItem() 76 { 77 } 78 79 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 = 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 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 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 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 144 public QName getQName() 145 { 146 return qname; 147 } 148 149 153 public QName getType() 154 { 155 return type; 156 } 157 158 162 public void setType(QName type) 163 { 164 this.type = type; 165 } 166 167 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 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 194 public LockableHashtable getParametersTable() 195 { 196 return parameters; 197 } 198 199 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 241 public void removeParameter(String name) 242 { 243 if (parameters != null) { 244 parameters.remove(name); 245 } 246 } 247 248 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 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 h = registry.getHandler(getType()); 326 } 327 328 return h; 329 } 330 331 336 Object createInstance(Class _class) 337 throws InstantiationException , IllegalAccessException 338 { 339 return _class.newInstance(); 340 } 341 342 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 } 358 | Popular Tags |