1 23 package com.sun.ejb.codegen; 24 25 import java.lang.reflect.Method ; 26 import java.io.*; 27 import java.util.*; 28 import java.util.logging.*; 29 import com.sun.logging.*; 30 31 import sun.rmi.rmic.IndentingWriter; 32 33 import static java.lang.reflect.Modifier .*; 34 import static com.sun.corba.ee.spi.codegen.Wrapper.*; 35 import com.sun.corba.ee.spi.codegen.Type; 36 import com.sun.corba.ee.spi.codegen.Expression; 37 import com.sun.corba.ee.impl.codegen.ClassGenerator; 38 39 import javax.ejb.EnterpriseBean ; 40 import javax.ejb.SessionBean ; 41 import javax.ejb.EntityBean ; 42 import com.sun.ejb.EJBUtils; 43 import com.sun.ejb.containers.InternalEJBContainerException; 44 import com.sun.enterprise.deployment.*; 45 import com.sun.enterprise.util.JarClassLoader; 46 import com.sun.enterprise.util.LocalStringManagerImpl; 47 48 51 52 public class Remote30WrapperGenerator extends Generator 53 implements ClassGeneratorFactory { 54 55 private static LocalStringManagerImpl localStrings = 56 new LocalStringManagerImpl(WrapperGenerator.class); 57 private static Logger _logger=null; 58 static{ 59 _logger=LogDomains.getLogger(LogDomains.DPL_LOGGER); 60 } 61 62 private String remoteInterfaceName; 63 private Class businessInterface; 64 private String remoteClientClassName; 65 private String remoteClientPackageName; 66 private String remoteClientSimpleName; 67 private Method [] bizMethods; 68 69 private ClassLoader loader; 70 71 public String getGeneratedClass() { 72 return remoteClientClassName; 73 } 74 75 public String className() { 77 return getGeneratedClass(); 78 } 79 80 85 public Remote30WrapperGenerator 86 (ClassLoader cl, String businessIntfName, String remoteIntfName) 87 throws GeneratorException 88 { 89 super(); 90 91 remoteInterfaceName = remoteIntfName; 92 93 loader = cl; 94 95 try { 96 this.businessInterface = cl.loadClass(businessIntfName); 97 } catch (ClassNotFoundException ex) { 98 throw new InvalidBean( 99 localStrings.getLocalString( 100 "generator.remote_interface_not_found", 101 "Business interface " + businessInterface + " not found ")); 102 } 103 104 if( javax.ejb.EJBObject .class.isAssignableFrom(businessInterface) ) { 105 throw new GeneratorException("Invalid Remote Business Interface " + 106 businessInterface + ". A Remote Business interface MUST " + 107 "not extend javax.ejb.EJBObject."); 108 } 109 110 remoteClientClassName = EJBUtils. 111 getGeneratedRemoteWrapperName(businessInterface.getName()); 112 113 remoteClientPackageName = getPackageName(remoteClientClassName); 114 remoteClientSimpleName = getBaseName(remoteClientClassName); 115 116 bizMethods = removeDups(businessInterface.getMethods()); 117 118 121 } 122 123 public ClassGenerator evaluate() { 124 125 _clear(); 126 127 if (remoteClientPackageName != null) { 128 _package(remoteClientPackageName); 129 } else { 130 _package(); 132 } 133 134 _class(PUBLIC, remoteClientSimpleName, 135 _t("com.sun.ejb.containers.RemoteBusinessWrapperBase"), 136 _t(businessInterface.getName())); 137 138 _data(PRIVATE, _t(remoteInterfaceName), "delegate_"); 139 140 _constructor( PUBLIC ) ; 141 _arg(_t(remoteInterfaceName), "stub"); 142 _arg(_String(), "busIntf"); 143 144 _body(); 145 _expr(_super(_s(_void(), _t("java.rmi.Remote"), _String()), 146 _v("stub"), _v("busIntf"))) ; 147 _assign(_v("delegate_"), _v("stub")); 148 _end(); 149 150 for(int i = 0; i < bizMethods.length; i++) { 151 printMethodImpl(bizMethods[i]); 152 } 153 154 _end(); 155 156 169 170 return _classGenerator() ; 171 172 } 173 174 175 181 public void generate(OutputStream out) 182 throws GeneratorException, IOException 183 { 184 IndentingWriter p = new IndentingWriter(new OutputStreamWriter(out)); 185 186 if (remoteClientPackageName != null) { 187 p.pln("package " + remoteClientPackageName + ";"); 188 } 189 190 p.plnI("public final class " + remoteClientSimpleName + 191 " extends com.sun.ejb.containers.RemoteBusinessWrapperBase" + 192 " implements " + businessInterface.getName() + " {"); 193 194 p.pln(""); 195 p.pln("private " + remoteInterfaceName + " delegate_;"); 196 p.pln(""); 197 198 p.plnI("public " + remoteClientSimpleName + "(" + 200 remoteInterfaceName + " stub" + " , " + 201 "java.lang.String busIntf" + ")" + " {"); 202 p.pln("super(stub, busIntf);"); 203 p.pln(""); 204 p.pln("delegate_ = stub;"); 205 p.pOln("}"); 206 p.pln(""); 207 208 for(int i = 0; i < bizMethods.length; i++) { 210 printMethodImpl(p, bizMethods[i]); 211 } 212 213 p.pOln("}"); 214 p.close(); 215 } 216 217 218 private void printMethodImpl(Method m) 219 { 220 221 List<Type> exceptionList = new LinkedList<Type>(); 222 for(Class exception : m.getExceptionTypes()) { 223 exceptionList.add(Type.type(exception)); 224 } 225 226 _method( PUBLIC, Type.type(m.getReturnType()), 227 m.getName(), exceptionList); 228 229 int i = 0; 230 List<Type> expressionListTypes = new LinkedList<Type>(); 231 List<Expression> expressionList = new LinkedList<Expression>(); 232 for(Class param : m.getParameterTypes()) { 233 String paramName = "param" + i; 234 _arg(Type.type(param), paramName); 235 i++; 236 expressionListTypes.add(Type.type(param)); 237 expressionList.add(_v(paramName)); 238 } 239 240 _body(); 241 242 243 _try(); 244 245 Class returnType = m.getReturnType(); 246 247 if( returnType == void.class ) { 248 _expr( _call( _v("delegate_"), m.getName(), 249 _s(Type.type(returnType), expressionListTypes), 250 expressionList)); 251 } else { 252 _return( _call( _v("delegate_"), m.getName(), 253 _s(Type.type(returnType), expressionListTypes), 254 expressionList) ); 255 } 256 257 boolean doExceptionTranslation = 258 !java.rmi.Remote .class.isAssignableFrom(businessInterface); 259 if( doExceptionTranslation ) { 260 261 _catch( _t("javax.transaction.TransactionRolledbackException"), 262 "trex"); 263 264 _define( _t("java.lang.RuntimeException"), "r", 265 _new( _t("javax.ejb.EJBTransactionRolledbackException"), 266 _s(_void()))); 267 _expr( _call( _v("r"), "initCause", 268 _s(_t("java.lang.Throwable"), 269 _t("java.lang.Throwable")), 270 _v("trex"))); 271 _throw(_v("r")); 272 273 _catch( _t("javax.transaction.TransactionRequiredException"), 274 "treqex"); 275 276 _define( _t("java.lang.RuntimeException"), "r", 277 _new( _t("javax.ejb.EJBTransactionRequiredException"), 278 _s(_void()))); 279 _expr( _call( _v("r"), "initCause", 280 _s(_t("java.lang.Throwable"), 281 _t("java.lang.Throwable")), 282 _v("treqex"))); 283 _throw(_v("r")); 284 285 _catch( _t("java.rmi.NoSuchObjectException"), 286 "nsoe"); 287 288 _define( _t("java.lang.RuntimeException"), "r", 289 _new( _t("javax.ejb.NoSuchEJBException"), 290 _s(_void()))); 291 _expr( _call( _v("r"), "initCause", 292 _s(_t("java.lang.Throwable"), 293 _t("java.lang.Throwable")), 294 _v("nsoe"))); 295 _throw(_v("r")); 296 297 _catch(_t("com.sun.ejb.containers.InternalEJBContainerException"), 298 "iejbcEx"); 299 _define(_t("com.sun.ejb.containers.ParallelAccessException"), 302 "paEx", 303 _cast(_t("com.sun.ejb.containers.ParallelAccessException"), 304 _v("iejbcEx"))); 305 306 _define(_t("javax.ejb.ConcurrentAccessException"), "r", _new( 307 _t("javax.ejb.ConcurrentAccessException"), _s(_void()))); 308 _expr(_call(_v("r"), "initCause", _s(_t("java.lang.Throwable"), 309 _t("java.lang.Throwable")), _v("paEx"))); 310 _throw(_v("r")); 311 312 _catch( _t("java.rmi.RemoteException"), "re"); 313 314 _throw( _new( _t("javax.ejb.EJBException"), 315 _s(_void(), _t("java.lang.Exception")), 316 _v("re"))); 317 _end(); 318 319 } else { 320 _catch(_t("com.sun.ejb.containers.InternalEJBContainerException"), "iejbcEx"); 321 _throw( _new( _t("com.sun.ejb.containers.InternalRemoteException"), 322 _s(_void(), _t("com.sun.ejb.containers.InternalEJBContainerException")), 323 _v("iejbcEx"))); 324 _end(); 325 } 326 327 _end(); 328 } 329 330 336 private void printMethodImpl(IndentingWriter p, Method m) 337 throws IOException 338 { 339 340 p.p("public " + printType(m.getReturnType()) + " " 342 + m.getName() + "("); 343 Class [] params = m.getParameterTypes(); 344 for(int i = 0; i < params.length; i++) { 345 if (i != 0) 346 p.p(", "); 347 p.p(printType(params[i]) + " param" + i); 348 } 349 p.p(") "); 350 Class [] exceptions = m.getExceptionTypes(); 351 for(int i = 0; i < exceptions.length; i++) { 352 if (i == 0) 353 p.p("throws "); 354 else 355 p.p(", "); 356 p.p(exceptions[i].getName()); 357 } 358 p.plnI("{"); 359 360 if( !java.rmi.Remote .class.isAssignableFrom(businessInterface) ) { 361 p.pln("try {"); 362 } 363 364 if (m.getReturnType() != void.class) { 365 p.p(" return "); 366 } 367 368 370 p.p(" delegate_." + m.getName() + "("); 371 for(int i = 0; i < params.length; i++) { 372 if( i != 0 ) { 373 p.p(" , "); 374 } 375 p.p("param" + i); 376 } 377 p.pln(");"); 378 379 if( !java.rmi.Remote .class.isAssignableFrom(businessInterface) ) { 380 381 p.pln("} catch(javax.transaction.TransactionRolledbackException trex) {"); 382 p.pln("\tjava.lang.RuntimeException r = new javax.ejb.EJBTransactionRolledbackException();"); 383 p.pln("\tr.initCause(trex);"); 384 p.pln("\tthrow r;"); 385 386 p.pln("} catch(java.rmi.NoSuchObjectException nsoe) {"); 387 p.pln("\tjava.lang.RuntimeException r = new javax.ejb.NoSuchEJBException();"); 388 p.pln("\tr.initCause(nsoe);"); 389 p.pln("\tthrow r;"); 390 391 p.pln("} catch(javax.transaction.TransactionRequiredException txre) {"); 392 p.pln("\tjava.lang.RuntimeException r = new javax.ejb.EJBTransactionRequiredException();"); 393 p.pln("\tr.initCause(txre);"); 394 p.pln("\tthrow r;"); 395 396 p.pln("} catch(java.rmi.RemoteException re) {"); 397 p.pln("\tthrow new javax.ejb.EJBException(re);"); 398 p.pln("} "); 399 400 } 401 402 p.pln(""); 403 p.pOln("} "); 404 p.pln(""); 405 406 } 407 408 } 409 | Popular Tags |