1 28 29 package com.caucho.ejb.gen; 30 31 import com.caucho.java.AbstractGenerator; 32 33 import java.io.IOException ; 34 import java.lang.reflect.Method ; 35 36 39 class EJBHomeGenerator extends AbstractGenerator { 40 protected Class skelClass; 41 protected Class remoteClass; 42 protected String prefix = "_ejb_lh_"; 43 44 47 EJBHomeGenerator(Class remoteClass, Class skelClass) 48 { 49 this.skelClass = skelClass; 50 this.remoteClass = remoteClass; 51 52 setFullClassName("_ejb." + remoteClass.getName() + "__Stub"); 53 } 54 55 60 public void generateJava() 61 throws IOException 62 { 63 printHeader(); 64 65 Method []methods = remoteClass.getMethods(); 66 for (int i = 0; i < methods.length; i++) { 67 Method method = methods[i]; 68 String methodName = method.getName(); 69 Class declaringClass = method.getDeclaringClass(); 70 71 if (declaringClass.getName().startsWith("javax.ejb.") && 72 ! methodName.equals("getPrimaryKey") && 73 ! methodName.equals("getEJBMetaData")) 74 continue; 75 76 if (methodName.startsWith("create")) { 77 printCreateMethod(method.getName(), method); 78 continue; 79 } 80 81 printMethod(method.getName(), method); 82 } 83 84 printFooter(); 85 } 86 87 90 protected void printHeader() 91 throws IOException 92 { 93 if (getPackageName() != null) 94 println("package " + getPackageName() + ";"); 95 96 println(); 97 println("import java.io.*;"); 98 println("import java.rmi.*;"); 99 println("import " + skelClass.getName() + ";"); 100 println("import " + remoteClass.getName() + ";"); 101 102 println(); 103 println("public class " + getClassName()); 104 println(" extends com.caucho.ejb.SessionHome"); 105 println(" implements " + remoteClass.getName()); 106 107 println("{"); 108 pushDepth(); 109 110 println("private " + skelClass.getName() + " obj;"); 111 112 println(); 113 println("protected void _init(com.caucho.ejb.SessionServer server, javax.ejb.SessionBean obj)"); 114 println("{"); 115 println(" this.server = server;"); 116 println(" this.obj = (" + skelClass.getName() + ") obj;"); 117 println("}"); 118 } 119 120 125 protected void printMethod(String name, Method method) 126 throws IOException 127 { 128 Class ret = method.getReturnType(); 129 Class []params = method.getParameterTypes(); 130 131 printMethodHeader(name, method); 132 133 println("{"); 134 pushDepth(); 135 136 if (! ret.getName().equals("void")) { 137 printClass(ret); 138 println(" _ret;"); 139 } 140 141 if (! ret.getName().equals("void")) 142 print("_ret = "); 143 144 print("obj." + prefix + method.getName() + "("); 145 for (int i = 0; i < params.length; i++) { 146 if (i != 0) 147 print(", "); 148 print("a" + i); 149 } 150 println(");"); 151 152 if (! ret.getName().equals("void")) 153 println("return _ret;"); 154 155 popDepth(); 156 println("}"); 157 } 158 159 164 protected void printCreateMethod(String name, Method method) 165 throws IOException 166 { 167 Class ret = method.getReturnType(); 168 Class []params = method.getParameterTypes(); 169 170 printMethodHeader(name, method); 171 172 println("{"); 173 pushDepth(); 174 175 println("com.caucho.ejb.AbstractContext cxt;"); 176 print("cxt = "); 177 178 print("obj." + prefix + method.getName() + "("); 179 for (int i = 0; i < params.length; i++) { 180 if (i != 0) 181 print(", "); 182 print("a" + i); 183 } 184 println(");"); 185 186 println("return (" + ret.getName() + ") cxt.getEJBLocalObject();"); 187 188 popDepth(); 189 println("}"); 190 } 191 192 195 void printFooter() 196 throws IOException 197 { 198 popDepth(); 199 println("}"); 200 } 201 } 202 | Popular Tags |