1 18 package org.apache.geronimo.interop.rmi.iiop.compiler; 19 20 import java.lang.reflect.Method ; 21 import java.io.File ; 22 import java.util.*; 23 24 import org.apache.geronimo.interop.generator.*; 25 import org.apache.geronimo.interop.util.JavaClass; 26 import org.apache.geronimo.interop.util.ProcessUtil; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 public class StubCompiler extends Compiler { 31 private final Log log = LogFactory.getLog(StubCompiler.class); 32 33 private ValueTypeContext vtc = new ValueTypeContext(); 34 35 private String inStreamName = "getInputStream"; 36 private String outStreamName = "getOutputStream"; 37 38 private HashMap packages = new HashMap(); 39 40 public StubCompiler(GenOptions go, ClassLoader cl) { 41 super(go, cl); 42 } 43 44 protected void addMethod(JClass jc, String iiopMethodName, JReturnType jrc, 45 String name, JParameter[] jparms, Class [] excepts) { 46 81 JMethod jm = jc.newMethod(jrc, name, jparms, excepts); 83 84 JLocalVariable jlvKey = jm.newLocalVariable(Object .class, "$key", new JExpression(new JCodeStatement("$getRequestKey()"))); 85 JLocalVariable jlvRetry = jm.newLocalVariable(int.class, "$retry"); 86 87 JForStatement jfs = new JForStatement(new JCodeStatement(jlvRetry.getName() + " = 0"), 88 new JExpression(new JCodeStatement(" ; ")), 89 new JCodeStatement(jlvRetry.getName() + "++")); 90 91 jm.addStatement(jfs); 92 93 JTryCatchFinallyStatement tcfs = new JTryCatchFinallyStatement(); 94 JTryStatement ts = tcfs.getTryStatement(); 95 96 JBlockStatement jbs = (JBlockStatement) ts; 97 98 JLocalVariable jlvConn = jbs.newLocalVariable(org.apache.geronimo.interop.rmi.iiop.client.Connection.class, "$conn"); 99 JLocalVariable jlvOutput = jbs.newLocalVariable(org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream.class, "$out"); 100 JLocalVariable jlvEt = jbs.newLocalVariable(java.lang.String .class, "$et"); 101 JLocalVariable jlvInput = null; 102 103 JLocalVariable jlvRc = null; 104 if (jrc != null && jrc.getType() != void.class) { 105 jlvRc = jbs.newLocalVariable(jrc.getType(), "$rc"); 106 jlvInput = jbs.newLocalVariable(org.apache.geronimo.interop.rmi.iiop.ObjectInputStream.class, "$in"); 107 } 108 109 jbs.addStatement(new JCodeStatement(jlvConn.getName() + " = this.$connect();")); 110 jbs.addStatement(new JCodeStatement(jlvOutput.getName() + " = " + jlvConn.getName() + "." + outStreamName + "();")); 111 112 String writeMethod = null; 113 String writeCall = ""; 114 for (int i = 0; i < jparms.length; i++) { 115 writeMethod = getWriteMethod(jparms[i]); 116 117 if (writeMethod != null) { 118 writeCall = writeMethod + "( " + jparms[i].getName() + " )"; 119 } else { 120 writeCall = "writeObject( " + vtc.getValueTypeVarName(jc, jparms[i]) + ", " + jparms[i].getName() + ")"; 121 } 122 123 jbs.addStatement(new JCodeStatement(jlvOutput.getName() + "." + writeCall + ";")); 124 } 125 126 jbs.addStatement(new JCodeStatement(jlvConn.getName() + ".invoke(this, \"" + iiopMethodName + "\", " + jlvKey.getName() + ", $retry);")); 127 if (jlvRc != null) { 128 jbs.addStatement(new JCodeStatement(jlvInput.getName() + " = " + jlvConn.getName() + "." + inStreamName + "();")); 129 } 130 jbs.addStatement(new JCodeStatement(jlvConn.getName() + ".forget(" + jlvKey.getName() + ");")); 131 jbs.addStatement(new JCodeStatement(jlvConn.getName() + ".close();")); 132 jbs.addStatement(new JCodeStatement(jlvEt.getName() + " = " + jlvConn.getName() + ".getExceptionType();")); 133 134 JIfElseIfElseStatement jiefs = new JIfElseIfElseStatement(new JExpression(new JCodeStatement(jlvEt.getName() + " != null"))); 135 JIfStatement jis = jiefs.getIfStatement(); 136 jis.addStatement(new JCodeStatement("throw org.apache.geronimo.interop.rmi.iiop.SystemExceptionFactory.getException(" + jlvConn.getName() + ".getException());")); 137 jbs.addStatement(jiefs); 138 139 if (jlvRc != null) { 140 String readMethod = getReadMethod(jlvRc); 141 String readCall = ""; 142 143 if (readMethod != null) { 144 readCall = jlvInput.getName() + "." + readMethod + "()"; 145 } else { 146 readCall = "(" + jlvRc.getTypeDecl() + ")" + jlvInput.getName() + "." + "readObject( " + vtc.getValueTypeVarName(jc, jlvRc) + ")"; 147 } 148 149 jbs.addStatement(new JCodeStatement(jlvRc.getName() + " = " + readCall + ";")); 150 jbs.addStatement(new JCodeStatement("return " + jlvRc.getName() + ";")); 151 } 152 153 ts.addStatement(jbs); 154 155 JVariable jv = new JVariable(org.apache.geronimo.interop.rmi.iiop.client.RetryInvokeException.class, "$ex"); 156 JCatchStatement cs = tcfs.newCatch(jv); 157 158 jiefs = new JIfElseIfElseStatement(new JExpression(new JCodeStatement(jlvRetry.getName() + " == 3"))); 159 jis = jiefs.getIfStatement(); 160 jis.addStatement(new JCodeStatement("throw " + jv.getName() + ".getRuntimeException();")); 161 cs.addStatement(jiefs); 162 163 jfs.addStatement(tcfs); 164 } 165 166 protected void addMethod_is_a(JClass jc) { 167 JParameter jpID = new JParameter(java.lang.String .class, "id"); 168 addMethod(jc, "_is_a", new JReturnType(boolean.class), 169 "_is_a", 170 new JParameter[]{jpID}, 171 (Class []) null); 172 173 } 174 175 protected void addMethod(MethodOverload mo, JClass jc) { 176 Method m = mo.method; 177 178 String name = m.getName(); 179 JParameter[] sparms = getMethodParms(m); 180 181 addMethod(jc, mo.iiop_name, new JReturnType(m.getReturnType()), 182 name, 183 sparms, 184 m.getExceptionTypes()); 185 186 } 187 188 public void generate() throws GenException { 189 GenOptions go = getGenOptions(); 190 List interfaces = go.getInterfaces(); 191 Iterator intf = null; 192 193 if (interfaces != null) { 194 intf = interfaces.iterator(); 195 } 196 197 JavaGenerator jg = new JavaGenerator(genOptions); 198 199 if (go.isSimpleIdl()) { 200 inStreamName = "getSimpleInputStream"; 201 outStreamName = "getSimpleOutputStream"; 202 } else { 203 inStreamName = "getInputStream"; 204 outStreamName = "getOutputStream"; 205 } 206 207 String riClassName = ""; 208 Class riClass = null; 209 String stubClassName = ""; 210 JPackage pkg = null; 211 212 while (intf != null && intf.hasNext() ) { 213 vtc.clear(); 215 216 riClassName = (String )intf.next(); 217 stubClassName = JavaClass.addPackageSuffix(riClassName, "iiop_stubs") + "_Stub"; 218 219 try { 220 riClass = getClassLoader().loadClass( riClassName ); 221 } catch (Exception ex) { 222 throw new GenException( "Generate Stubs Failed:", ex ); 223 } 224 225 String pkgName = JavaClass.getNamePrefix(stubClassName); 226 pkg = (JPackage) packages.get( pkgName ); 227 if (pkg == null) 228 { 229 pkg = new JPackage( pkgName ); 230 packages.put( pkgName, pkg ); 231 } 232 233 String className = JavaClass.getNameSuffix(stubClassName); 234 JClass jc = pkg.newClass(className); 235 jc.addImport("org.apache.geronimo.interop.rmi.iiop", "ObjectRef"); 236 jc.setExtends("ObjectRef"); 237 jc.addImplements(riClass.getName()); 238 239 JConstructor jcCon = jc.newConstructor((JParameter[]) null, (Class []) null); 240 jcCon.addStatement(new JCodeStatement("super();")); 241 242 addMethod_is_a(jc); 243 244 Method m[] = getMethods( riClass, go.isSimpleIdl()); 245 MethodOverload mo[] = null; 246 mo = getMethodOverloads( m ); 247 for (int i = 0; mo != null && i < mo.length; i++) { 248 addMethod( mo[i], jc ); 249 } 250 } 251 252 Set pkgSet = packages.keySet(); 253 Iterator pkgIt = pkgSet.iterator(); 254 String stubPkg = ""; 255 256 while (pkgIt.hasNext()) 257 { 258 stubPkg = (String ) pkgIt.next(); 259 pkg = (JPackage)packages.get(stubPkg); 260 System.out.println("Generating Package: " + stubPkg); 261 jg.generate(pkg); 262 } 263 } 264 265 public void compile() 266 throws Exception { 267 268 Set pkg = packages.keySet(); 269 Iterator pkgIt = pkg.iterator(); 270 String stubPkg = ""; 271 272 277 278 GenOptions go = getGenOptions(); 279 String classpath = adjustPath(go.getClasspath()); 280 String srcpath = adjustPath(go.getGenSrcDir()); 281 282 String filesToCompile = ""; 283 String javacCmd = ""; 284 285 while (pkgIt.hasNext()) 286 { 287 stubPkg = (String ) pkgIt.next(); 288 stubPkg = stubPkg.replace( '.', File.separatorChar ); 289 filesToCompile = adjustPath(go.getGenSrcDir() + File.separator + stubPkg + File.separator + "*.java"); 290 291 System.out.println("Compiling Package: " + filesToCompile); 292 293 javacCmd = "javac -d " + go.getGenClassDir() + 294 ( go.isCompileDebug() ? " -g" : "" ) + 295 " -classpath " + classpath + " " + 296 " -sourcepath " + srcpath + " " + filesToCompile; 297 298 System.out.println( "Lauching: " + javacCmd ); 299 300 ProcessUtil pu = ProcessUtil.getInstance(); 301 pu.setEcho(System.out); 302 pu.run(javacCmd, (String []) null, "./" ); 303 } 304 } 305 306 public static void main(String args[]) throws Exception { 307 GenOptions go = null; 308 309 try 310 { 311 go = new GenOptions( "./stubs", args ); 312 } 313 catch( GenWarning gw ) 314 { 315 gw.printStackTrace(); 316 } 317 318 ClassLoader cl = ClassLoader.getSystemClassLoader(); 319 StubCompiler sg = new StubCompiler( go, cl ); 320 321 if (go.isGenerate()) { 322 sg.generate(); 323 } 324 325 if (go.isCompile()) { 326 sg.compile(); 327 } 328 } 329 } 330 | Popular Tags |