1 28 29 package com.caucho.ejb.burlap; 30 31 import com.caucho.make.ClassDependency; 32 import com.caucho.util.IntMap; 33 import com.caucho.vfs.MergePath; 34 import com.caucho.vfs.Path; 35 import com.caucho.vfs.PersistentDependency; 36 37 import java.io.IOException ; 38 import java.lang.reflect.Method ; 39 import java.util.ArrayList ; 40 import java.util.Iterator ; 41 42 45 class BurlapSkeletonGenerator extends MarshalGenerator { 46 Class _cl; 47 String _objClass; 48 String _fullName; 49 String _pkg; 50 String _className; 51 52 protected int _unique; 53 54 protected ArrayList <Class > _marshallClasses; 55 protected ArrayList <Class > _unmarshallClasses; 56 57 protected ArrayList <Class > _marshallArrays; 58 protected ArrayList <Class > _unmarshallArrays; 59 protected Class _interfaceClass; 60 protected ArrayList <PersistentDependency> _dependList; 61 62 65 BurlapSkeletonGenerator(Class interfaceClass) 66 { 67 if (interfaceClass == null) 68 throw new IllegalArgumentException ("interface can't be null"); 69 70 _interfaceClass = interfaceClass; 71 72 _dependList = new ArrayList <PersistentDependency>(); 73 74 MergePath mergePath = new MergePath(); 75 mergePath.addClassPath(interfaceClass.getClassLoader()); 76 77 setSearchPath(mergePath); 78 79 _dependList.add(new ClassDependency(interfaceClass)); 80 } 81 82 85 static Class generate(Class interfaceClass, Path workDir) 86 throws Exception 87 { 88 BurlapSkeletonGenerator gen = new BurlapSkeletonGenerator(interfaceClass); 89 gen.setClassDir(workDir); 90 91 gen.setFullClassName("_ejb." + interfaceClass.getName() + "__BurlapSkel"); 92 93 Class cl = gen.preload(); 94 if (cl != null) 95 return cl; 96 97 gen.generate(); 98 return gen.compile(); 99 } 100 101 104 public void generateJava() 105 throws IOException 106 { 107 ArrayList <Method > methodList = new ArrayList <Method >(); 108 109 Method []methods = _interfaceClass.getMethods(); 110 for (int i = 0; i < methods.length; i++) { 111 String className = methods[i].getDeclaringClass().getName(); 112 String methodName = methods[i].getName(); 113 114 methodList.add(methods[i]); 115 } 116 117 printHeader(); 118 119 IntMap methodMap = printDispatcher(methodList); 120 121 printDependList(_dependList); 122 123 printFooter(methodMap); 124 } 125 126 129 void printHeader() 130 throws IOException 131 { 132 if (getPackageName() != null) 133 println("package " + getPackageName() + ";"); 134 135 println(); 136 println("import java.io.*;"); 137 println("import com.caucho.util.*;"); 138 println("import com.caucho.burlap.*;"); 139 println("import com.caucho.burlap.io.BurlapInput;"); 140 println("import com.caucho.burlap.io.BurlapOutput;"); 141 println("import " + _interfaceClass.getName() + ";"); 142 143 println(); 144 print("public class " + getClassName() + " extends com.caucho.ejb.burlap.BurlapSkeleton"); 145 println(" {"); 146 147 pushDepth(); 148 149 println("private " + _interfaceClass.getName() + " obj;"); 150 println(); 151 println("protected void _setObject(Object o)"); 152 println("{"); 153 println(" obj = (" + _interfaceClass.getName() + ") o;"); 154 println("}"); 155 } 156 157 162 protected IntMap printDispatcher(ArrayList <Method > methods) 163 throws IOException 164 { 165 IntMap map = new IntMap(); 166 167 println(); 168 print("protected void _execute(CharBuffer method, BurlapInput in, BurlapOutput out)"); 169 println(" throws Throwable"); 170 println("{"); 171 pushDepth(); 172 println("switch (methods.get(method)) {"); 173 174 int i = 0; 175 for (; i < methods.size(); i++) { 176 Method method = methods.get(i); 177 178 if (map.get(method.getName()) < 0) 179 map.put(method.getName(), i); 180 181 map.put(mangleMethodName(method.getName(), method, false), i); 182 183 if (i > 0) 184 println(); 185 println("case " + i + ":"); 186 pushDepth(); 187 println("{"); 188 pushDepth(); 189 190 printUnmarshal(method.getName(), method.getParameterTypes(), 191 method.getReturnType()); 192 193 println("break;"); 194 popDepth(); 195 println("}"); 196 popDepth(); 197 } 198 199 println("default:"); 200 println(" _executeUnknown(method, in, out);"); 201 println(" break;"); 202 println("}"); 203 popDepth(); 204 println("}"); 205 206 return map; 207 } 208 209 214 void printUnmarshal(String methodName, Class []param, Class retType) 215 throws IOException 216 { 217 for (int i = 0; i < param.length; i++) { 218 String var = "_arg" + i; 219 printClass(param[i]); 220 print(" " + var + " = "); 221 printUnmarshalType(param[i]); 222 } 223 224 println("in.completeCall();"); 225 226 if (! retType.getName().equals("void")) { 227 printClass(retType); 228 print(" _ret = "); 229 } 230 231 printCall("obj", methodName, param); 232 233 println("out.startReply();"); 234 235 if (! retType.equals(void.class)) 236 printMarshalType(retType, "_ret"); 237 else 238 println("out.writeNull();"); 239 240 println("out.completeReply();"); 241 } 242 243 249 void printCall(String objName, String methodName, Class []param) 250 throws IOException 251 { 252 print(objName); 253 print("."); 254 print(methodName); 255 print("("); 256 for (int i = 0; i < param.length; i++) { 257 if (i != 0) 258 print(", "); 259 print("_arg" + i); 260 } 261 println(");"); 262 } 263 264 267 String getMethodName(Method method) 268 { 269 return method.getName(); 270 } 271 272 275 protected void printFooter(IntMap methodMap) 276 throws IOException 277 { 278 println("static private IntMap methods = new IntMap();"); 279 println("static {"); 280 pushDepth(); 281 282 Iterator keys = methodMap.iterator(); 283 while (keys.hasNext()) { 284 String name = (String ) keys.next(); 285 int value = methodMap.get(name); 286 287 println("methods.put(new CharBuffer(\"" + name + "\")," + value + ");"); 288 } 289 popDepth(); 290 println("}"); 291 popDepth(); 292 println("}"); 293 } 294 } 295 | Popular Tags |