1 57 58 package org.apache.soap.providers; 59 60 import java.io.* ; 61 import java.util.* ; 62 import javax.servlet.* ; 63 import javax.servlet.http.* ; 64 import org.apache.soap.* ; 65 import org.apache.soap.rpc.* ; 66 import org.apache.soap.server.* ; 67 import org.apache.soap.util.* ; 68 69 import java.lang.reflect.*; 70 import javax.rmi.*; 71 import javax.ejb.*; 72 import javax.naming.*; 73 import javax.naming.Context .*; 74 75 public class StatelessEJBProvider implements org.apache.soap.util.Provider { 76 77 private DeploymentDescriptor dd ; 78 private Envelope envelope ; 79 private Call call ; 80 private String methodName ; 81 private String targetObjectURI ; 82 private HttpServlet servlet ; 83 private HttpSession session ; 84 85 private javax.naming.Context contxt = null; 86 private EJBObject remoteObjRef = null; 87 public static java.lang.String CNTXT_PROVIDER_URL = "iiop://localhost:900"; 88 public static java.lang.String CNTXT_FACTORY_NAME = "com.ibm.ejs.ns.jndi.CNInitialContextFactory"; 89 private Vector methodParameters = null; 90 private String respEncStyle = null; 91 94 public StatelessEJBProvider() { 95 super(); 96 } 97 98 private void initialize() throws SOAPException { 99 100 if(contxt == null) { 101 102 java.util.Properties properties = new java.util.Properties (); 103 properties.put(javax.naming.Context.PROVIDER_URL, CNTXT_PROVIDER_URL); 104 properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, CNTXT_FACTORY_NAME); 105 try { 106 contxt = new javax.naming.InitialContext (properties); 107 } catch (NamingException ne) { 108 System.out.println("Naming Exception caught during InitialContext creation @ " + CNTXT_PROVIDER_URL); 110 throw new SOAPException(Constants.FAULT_CODE_SERVER, "Unable to initialize context" ); 111 } 112 } 113 114 } 115 116 private void initialize(String url, String factory) 117 throws SOAPException 118 { 119 if(contxt == null) 120 { 121 java.util.Properties properties = new java.util.Properties (); 122 123 if ((url != null) && (!url.trim().equals(""))) 124 { 125 properties.put(javax.naming.Context.PROVIDER_URL, url); 126 } 127 if ((factory != null) && (!factory.trim().equals(""))) 128 { 129 properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, factory); 130 } 131 132 try 133 { 134 contxt = new javax.naming.InitialContext (properties); 135 } 136 catch (NamingException ne) 137 { 138 System.out.println("Naming Exception caught during InitialContext creation @ " + url); 140 throw new SOAPException(Constants.FAULT_CODE_SERVER, "Unable to initialize context"); 141 } 142 } 143 } 144 145 146 149 public void invoke(SOAPContext reqContext, SOAPContext resContext) 150 throws SOAPException { 151 System.err.println( "=============================================" ); 152 System.err.println("In TemplateProvider.invoke()" ); 153 154 Parameter ret = null; 155 Object [] args = null; 156 Class [] argTypes = null; 157 158 respEncStyle = call.getEncodingStyleURI(); 159 160 if (methodParameters != null) { 161 int parametersCount = methodParameters.size (); 162 args = new Object [parametersCount]; 163 argTypes = new Class [parametersCount]; 164 165 for (int i = 0; i < parametersCount; i++) { 166 Parameter param = (Parameter) methodParameters.elementAt (i); 167 args[i] = param.getValue (); 168 argTypes[i] = param.getType (); 169 170 if (respEncStyle == null) { 171 respEncStyle = param.getEncodingStyleURI (); 172 } 173 } 174 } 175 176 if (respEncStyle == null) { 177 respEncStyle = Constants.NS_URI_SOAP_ENC; 178 } 179 180 try { 181 182 Method m = MethodUtils.getMethod (remoteObjRef, methodName, argTypes); 183 Bean result = new Bean (m.getReturnType (), m.invoke (remoteObjRef, args)); 184 185 186 if (result.type != void.class) { 187 ret = new Parameter (RPCConstants.ELEM_RETURN, result.type, 188 result.value, null); 189 } 190 191 192 } catch (InvocationTargetException e) { 193 System.err.println("Exception Caught upon method invocation attempt: " + e.getMessage()); 194 Throwable t = e.getTargetException (); 195 throw new SOAPException (Constants.FAULT_CODE_SERVER, t.getMessage(), t); 196 } 197 catch (Throwable t) { 198 System.err.println("Exception Caught upon method invocation attempt: " + t.toString()); 199 throw new SOAPException (Constants.FAULT_CODE_SERVER, t.getMessage(), t); 200 } 201 202 try { 203 Response resp = new Response( targetObjectURI, call.getMethodName(), (Parameter) ret, null, null, respEncStyle, resContext ); Envelope env = resp.buildEnvelope(); 211 StringWriter sw = new StringWriter(); 212 env.marshall( sw, call.getSOAPMappingRegistry(), resContext ); 213 resContext.setRootPart( sw.toString(), Constants.HEADERVAL_CONTENT_TYPE_UTF8); 214 } 215 catch( Exception e ) { 216 if ( e instanceof SOAPException ) throw (SOAPException ) e ; 217 throw new SOAPException( Constants.FAULT_CODE_SERVER, e.toString() ); 218 } 219 } 220 221 224 public void locate(DeploymentDescriptor dd, 225 Envelope env, 226 Call call, 227 String methodName, 228 String targetObjectURI, 229 SOAPContext reqContext) 230 throws org.apache.soap.SOAPException { 231 232 HttpServlet servlet = (HttpServlet) reqContext.getProperty( Constants.BAG_HTTPSERVLET ); 233 HttpSession session = (HttpSession) reqContext.getProperty( Constants.BAG_HTTPSESSION ); 234 235 System.err.println( "=============================================" ); 236 System.err.println( "In TemplateProvider.locate()" ); 237 System.err.println( "URI: " + targetObjectURI ); 238 System.err.println( "DD.ServiceClass: " + dd.getServiceClass() ); 239 System.err.println( "DD.ProviderClass: " + dd.getProviderClass() ); 240 System.err.println( "Call.MethodName: " + call.getMethodName() ); 241 242 this.dd = dd ; 243 this.envelope = env ; 244 this.call = call ; 245 this.methodName = methodName ; 246 this.targetObjectURI = targetObjectURI ; 247 this.servlet = servlet ; 248 this.session = session ; 249 250 Hashtable props = dd.getProps(); 251 252 253 String ContxtProviderURL = (String ) props.get("ContextProviderURL"); 254 String ContxtFactoryName = (String ) props.get("FullContextFactoryName"); 255 256 if ((ContxtProviderURL != null) || (ContxtFactoryName != null)) 257 initialize(ContxtProviderURL, ContxtFactoryName); 258 else 259 initialize(); 260 261 String homeInterfaceName = (String ) props.get("FullHomeInterfaceName"); 262 if (homeInterfaceName == null) 263 throw new SOAPException(Constants.FAULT_CODE_SERVER, "Error in Deployment Descriptor Property Settings"); 264 265 String jndiName = (String ) props.get("JNDIName"); 267 if ( jndiName == null ) jndiName = dd.getProviderClass(); 268 269 if ((jndiName != null) && (contxt != null)) { 270 271 try { 272 273 EJBHome home = (EJBHome) PortableRemoteObject.narrow(contxt.lookup(jndiName), Class.forName(homeInterfaceName)); 276 Method createMethod = home.getClass().getMethod("create", new Class [0]); 279 remoteObjRef = (EJBObject) createMethod.invoke((Object ) home, new Object [0]); 280 281 } catch (Exception e) { 282 283 System.out.println("Exception caught: " + e.toString()); 284 throw new SOAPException(Constants.FAULT_CODE_SERVER,"Error in connecting to EJB", e); 285 } 286 } 287 288 methodName = call.getMethodName(); 291 methodParameters = call.getParams(); 292 293 294 } 295 } 296 | Popular Tags |