1 28 29 package com.caucho.ejb.hessian; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.java.WorkDir; 33 import com.caucho.make.ClassDependency; 34 import com.caucho.vfs.MergePath; 35 import com.caucho.vfs.PersistentDependency; 36 37 import java.io.IOException ; 38 import java.lang.reflect.Method ; 39 import java.util.ArrayList ; 40 41 44 class StubGenerator extends MarshalGenerator { 45 private ArrayList <PersistentDependency> _dependList; 46 47 StubGenerator() 48 { 49 setClassDir(WorkDir.getLocalWorkDir().lookup("ejb")); 50 } 51 52 Class createHomeStub(Class cl) 53 throws ConfigException 54 { 55 return makeClient(cl, "__HessianStub", true); 56 } 57 58 Class createObjectStub(Class cl) 59 throws ConfigException 60 { 61 return makeClient(cl, "__HessianStub", false); 62 } 63 64 Class createStub(Class cl) 65 throws ConfigException 66 { 67 return makeClient(cl, "__HessianStub", true); 68 } 69 70 76 Class makeClient(Class cl, String genSuffix, boolean isHome) 77 throws ConfigException 78 { 79 _cl = cl; 80 81 try { 82 setFullClassName("_ejb." + cl.getName() + genSuffix); 83 84 if (cl.getClassLoader() != null) 85 setParentLoader(cl.getClassLoader()); 86 87 MergePath mergePath = new MergePath(); 88 if (cl.getClassLoader() != null) 89 mergePath.addClassPath(cl.getClassLoader()); 90 else 91 mergePath.addClassPath(Thread.currentThread().getContextClassLoader()); 92 setSearchPath(mergePath); 93 94 _dependList = new ArrayList <PersistentDependency>(); 95 96 _dependList.add(new ClassDependency(cl)); 97 98 Class stubClass = preload(); 99 if (stubClass != null) 100 return stubClass; 101 102 generate(); 103 104 return compile(); 105 } catch (Exception e) { 106 throw new ConfigException(e); 107 } 108 } 109 110 public void generateJava() 111 throws IOException 112 { 113 generateJava(_cl.getMethods()); 114 } 115 116 117 122 private void generateJava(Method []methods) 123 throws IOException 124 { 125 if (javax.ejb.EJBHome .class.isAssignableFrom(_cl)) 126 printHeader("HomeStub"); 127 else if (javax.ejb.EJBObject .class.isAssignableFrom(_cl)) 128 printHeader("ObjectStub"); 129 else 130 printHeader("ObjectStub"); 131 132 println("public String getHessianType()"); 133 println("{"); 134 println(" return \"" + _cl.getName() + "\";"); 135 println("}"); 136 137 for (int i = 0; i < methods.length; i++) { 138 Method method = methods[i]; 139 Class declaringClass = method.getDeclaringClass(); 140 String prefix = ""; 141 142 if (declaringClass.getName().startsWith("javax.ejb.")) 143 prefix = "_ejb_"; 144 145 154 printMethod(prefix + method.getName(), method); 155 } 156 157 printDependList(_dependList); 158 159 printFooter(); 160 } 161 162 165 void printHeader(String stubClassName) 166 throws IOException 167 { 168 if (getPackageName() != null) 169 println("package " + getPackageName() + ";"); 170 171 println(); 172 println("import java.io.*;"); 173 println("import java.rmi.*;"); 174 println("import com.caucho.vfs.*;"); 175 println("import com.caucho.util.*;"); 176 println("import com.caucho.ejb.hessian.*;"); 177 println("import com.caucho.hessian.io.*;"); 178 println("import " + _cl.getName() + ";"); 179 180 println(); 181 println("public class " + getClassName() + " extends " + stubClassName); 182 print(" implements " + _cl.getName()); 183 184 println(" {"); 185 pushDepth(); 186 } 187 188 194 void printMethod(String name, Method method) 195 throws IOException 196 { 197 Class ret = method.getReturnType(); 198 Class []params = method.getParameterTypes(); 199 200 printMethodDeclaration(name, method); 201 202 println("{"); 203 pushDepth(); 204 205 println("HessianWriter out = _hessian_openWriter();"); 206 println("try {"); 207 pushDepth(); 208 209 String mangleName = mangleMethodName(method.getName(), method, false); 210 211 println("out.startCall();"); 212 println("_hessian_writeHeaders(out);"); 213 println("out.writeMethod(\"" + mangleName + "\");"); 214 215 for (int i = 0; i < params.length; i++) 216 printMarshalType(params[i], "_arg" + i); 217 218 println("HessianInput in = out.doCall();"); 219 220 if (! void.class.equals(ret)) { 221 printClass(ret); 222 println(" _ret;"); 223 print("_ret = "); 224 printUnmarshalType(ret); 225 } 226 else { 227 println("in.readNull();"); 228 } 229 230 println("in.completeReply();"); 231 232 println("_hessian_freeWriter(out);"); 233 println("out = null;"); 234 235 if (! void.class.equals(ret)) 236 println("return _ret;"); 237 238 popDepth(); 239 240 Class []exn = method.getExceptionTypes(); 241 242 boolean hasThrowable = false; 243 boolean hasRuntimeException = false; 244 245 loop: 246 for (int i = 0; i < exn.length; i++) { 247 for (int j = 0; j < i; j++) { 248 if (exn[j].isAssignableFrom(exn[i])) 249 continue loop; 250 } 251 252 if (! hasThrowable) { 253 println("} catch (" + exn[i].getName() + " e) {"); 254 println(" throw e;"); 255 } 256 257 if (exn[i].equals(Throwable .class)) { 258 hasThrowable = true; 259 hasRuntimeException = true; 260 } 261 262 if (exn[i].equals(Exception .class)) 263 hasRuntimeException = true; 264 if (exn[i].equals(RuntimeException .class)) 265 hasRuntimeException = true; 266 } 267 268 if (! hasRuntimeException) { 269 println("} catch (RuntimeException e) {"); 270 println(" throw e;"); 271 } 272 273 if (! hasThrowable) { 274 println("} catch (Throwable e) {"); 275 println(" throw new com.caucho.ejb.EJBExceptionWrapper(\"stub exception\", e);"); 276 } 277 278 println("} finally {"); 279 println(" if (out != null) out.close();"); 280 println("}"); 281 282 popDepth(); 283 println("}"); 284 } 285 286 289 void printFooter() 290 throws IOException 291 { 292 println(); 293 println("public String toString()"); 294 println("{"); 295 pushDepth(); 296 println("return \"[HessianStub " + _cl.getName() + " \" + _urlPath + \"]\";"); 297 popDepth(); 298 println("}"); 299 300 popDepth(); 301 println("}"); 302 } 303 304 307 protected void printVersionChange() 308 throws IOException 309 { 310 println("if (com.caucho.ejb.Version.getVersionId() != " + 311 com.caucho.ejb.Version.getVersionId() + ")"); 312 println(" return true;"); 313 } 314 } 315 | Popular Tags |