1 57 58 package org.apache.wsif.providers.ejb; 59 60 import java.lang.reflect.Method ; 61 import java.util.HashMap ; 62 import java.util.Hashtable ; 63 import java.util.Iterator ; 64 import java.util.Map ; 65 66 import javax.ejb.EJBHome ; 67 import javax.ejb.EJBObject ; 68 import javax.naming.InitialContext ; 69 import javax.rmi.PortableRemoteObject ; 70 import javax.wsdl.BindingOperation; 71 import javax.wsdl.Definition; 72 import javax.wsdl.Port; 73 import javax.wsdl.extensions.ExtensibilityElement; 74 75 import org.apache.wsif.WSIFException; 76 import org.apache.wsif.WSIFOperation; 77 import org.apache.wsif.logging.Trc; 78 import org.apache.wsif.providers.WSIFDynamicTypeMap; 79 import org.apache.wsif.util.WSIFUtils; 80 import org.apache.wsif.wsdl.extensions.ejb.EJBAddress; 81 82 90 public class WSIFPort_EJB 91 extends org.apache.wsif.base.WSIFDefaultPort 92 implements java.io.Serializable { 93 94 private static final long serialVersionUID = 1L; 95 96 private javax.wsdl.Definition fieldDefinition = null; 97 private javax.wsdl.Port fieldPortModel = null; 98 private EJBHome fieldEjbHome = null; private EJBObject fieldEjbObject = null; 101 protected Map operationInstances = new HashMap (); 102 103 public WSIFPort_EJB(Definition def, Port port, WSIFDynamicTypeMap typeMap) { 104 Trc.entry(this, def, port, typeMap); 105 106 fieldDefinition = def; 107 fieldPortModel = port; 108 109 Trc.exit(); 110 } 111 112 public Definition getDefinition() { 113 return fieldDefinition; 114 } 115 116 public WSIFOperation_EJB getDynamicWSIFOperation( 117 String name, 118 String inputName, 119 String outputName) 120 throws WSIFException { 121 Trc.entry(this, name, inputName, outputName); 122 123 WSIFOperation_EJB tempOp = 124 (WSIFOperation_EJB) operationInstances.get(getKey(name, inputName, outputName)); 125 126 WSIFOperation_EJB operation = null; 127 if (tempOp != null) { 128 operation = tempOp.copy(); 129 } 130 131 if (operation == null) { 132 BindingOperation bindingOperationModel = 133 WSIFUtils.getBindingOperation( 134 fieldPortModel.getBinding(), name, inputName, outputName ); 135 136 if (bindingOperationModel != null) { 137 operation = new WSIFOperation_EJB(fieldPortModel, bindingOperationModel, this); 138 setDynamicWSIFOperation(name, inputName, outputName, operation); 139 } 140 } 141 142 Trc.exit(operation); 143 return operation; 144 } 145 146 public EJBHome getEjbHome() throws WSIFException { 147 Trc.entry(this); 148 149 if (fieldEjbHome == null) { 150 EJBAddress address = null; 151 152 try { 153 ExtensibilityElement portExtension = 154 (ExtensibilityElement) fieldPortModel.getExtensibilityElements().get(0); 155 156 if (portExtension == null) { 157 throw new WSIFException("missing port extension"); 158 } 159 160 address = (EJBAddress) portExtension; 161 162 Hashtable hash = new Hashtable (); 163 164 String icf = address.getInitialContextFactory(); 166 if (icf != null) { 167 hash.put(InitialContext.INITIAL_CONTEXT_FACTORY, icf); 168 } 169 170 String providerURL = address.getJndiProviderURL(); 172 if (providerURL != null) { 173 hash.put(InitialContext.PROVIDER_URL, providerURL); 174 } 175 176 hash.put(InitialContext.AUTHORITATIVE, "true"); 178 179 InitialContext initContext; 180 initContext = new InitialContext (hash); 181 182 Class homeClass = null; 183 try { 184 if (address.getClassName() != null) { 185 homeClass = 186 Class.forName( 187 address.getClassName(), 188 true, 189 Thread.currentThread().getContextClassLoader()); 190 if (!(EJBHome .class.isAssignableFrom(homeClass))) { 191 homeClass = null; 192 } 193 } 194 } catch (ClassNotFoundException cnf) { 195 Trc.ignoredException(cnf); 196 } 197 198 if (homeClass != null) { 199 fieldEjbHome = (EJBHome ) PortableRemoteObject.narrow(initContext.lookup(address.getJndiName()),homeClass); 200 } else { 201 fieldEjbHome = (EJBHome ) initContext.lookup(address.getJndiName()); 202 } 203 204 } catch (Exception ex) { 205 Trc.exception(ex); 206 throw new WSIFException( 207 "Failed to lookup EJB home using JNDI name '" + address.getJndiName() + "'", 208 ex); 209 } 210 } 211 Trc.exit(fieldEjbHome); 212 return fieldEjbHome; 213 } 214 215 public EJBObject getEjbObject() throws WSIFException { 216 Trc.entry(this); 217 218 if (fieldEjbObject == null) { 219 EJBHome ejbHome = getEjbHome(); 220 try { 221 Method createMethod = ejbHome.getClass().getDeclaredMethod("create", null); 222 fieldEjbObject = (EJBObject ) createMethod.invoke(ejbHome, null); 223 } catch (Exception ex) { 224 Trc.exception(ex); 225 throw new WSIFException( 226 "Could not create instance for home '" + ejbHome + "'", 227 ex); 228 } 229 } 230 Trc.exit(fieldEjbObject); 231 return fieldEjbObject; 232 } 233 234 public Port getPortModel() { 235 Trc.entry(this); 236 Trc.exit(fieldPortModel); 237 return fieldPortModel; 238 } 239 240 public void setDefinition(Definition value) { 241 Trc.entry(this, value); 242 fieldDefinition = value; 243 Trc.exit(); 244 } 245 246 public void setDynamicWSIFOperation( 248 String name, 249 String inputName, 250 String outputName, 251 WSIFOperation_EJB value) { 252 Trc.entry(this, name, inputName, outputName); 253 254 operationInstances.put(getKey(name, inputName, outputName), value); 255 Trc.exit(); 256 } 257 258 public void setEjbHome(EJBHome newEjbHome) { 259 Trc.entry(this, newEjbHome); 260 fieldEjbHome = newEjbHome; 261 Trc.exit(); 262 } 263 264 public void setEjbObject(EJBObject newEjbObject) { 265 Trc.entry(this, newEjbObject); 266 fieldEjbObject = newEjbObject; 267 Trc.exit(); 268 } 269 270 public void setPortModel(Port value) { 271 Trc.entry(this, value); 272 fieldPortModel = value; 273 Trc.exit(); 274 } 275 276 public WSIFOperation createOperation(String operationName) 277 throws WSIFException { 278 Trc.entry(this, operationName); 279 WSIFOperation wo = createOperation(operationName, null, null); 280 Trc.exit(wo); 281 return wo; 282 } 283 284 public WSIFOperation createOperation( 285 String operationName, 286 String inputName, 287 String outputName) 288 throws WSIFException { 289 Trc.entry(this, operationName, inputName, outputName); 290 WSIFOperation_EJB op = 291 getDynamicWSIFOperation(operationName, inputName, outputName); 292 if (op == null) { 293 throw new WSIFException( 294 "Could not create operation: " 295 + operationName 296 + ":" 297 + inputName 298 + ":" 299 + outputName); 300 } 301 WSIFOperation wo = op.copy(); 302 Trc.exit(wo); 303 return wo; 304 } 305 306 public String deep() { 307 String buff = ""; 308 try { 309 buff = new String (super.toString() + ":\n"); 310 311 buff += "definition:" + Trc.brief(fieldDefinition); 312 buff += " portModel:" + Trc.brief(fieldPortModel); 313 buff += " ejbHome:" + fieldEjbHome; 314 buff += " ejbObject" + fieldEjbObject; 315 316 buff += " operationInstances:"; 317 if (operationInstances == null) 318 buff += "null"; 319 else { 320 buff += " size:" + operationInstances.size(); 321 Iterator it = operationInstances.keySet().iterator(); 322 int i = 0; 323 while (it.hasNext()) { 324 String key = (String ) it.next(); 325 WSIFOperation_EJB woejb = (WSIFOperation_EJB) operationInstances.get(key); 326 buff += "\noperationInstances[" + i + "]:" + key + " " + woejb + " "; 327 i++; 328 } 329 } 330 } catch (Exception e) { 331 Trc.exceptionInTrace(e); 332 } 333 334 return buff; 335 } 336 } | Popular Tags |