Modified: geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/Compiler.java URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/Compiler.java?view=diff&r1=158861&r2=158862 ============================================================================== --- geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/Compiler.java (original) +++ geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/Compiler.java Wed Mar 23 18:30:30 2005 @@ -18,74 +18,97 @@ package org.apache.geronimo.interop.rmi.iiop.compiler; import java.lang.reflect.Method; -import java.util.*; -import java.io.File; +import java.util.HashMap; import org.apache.geronimo.interop.generator.GenOptions; import org.apache.geronimo.interop.generator.JParameter; import org.apache.geronimo.interop.generator.JVariable; -public class Compiler { - protected GenOptions genOptions; - - private ClassLoader classLoader; - private static HashMap readMethods; - private static HashMap writeMethods; - private static HashMap overloadTypes; +public class Compiler { + protected Class _riClass; + protected GenOptions _genOptions; + protected ClassLoader _cl; + protected boolean _simpleIDL = false; + + protected static HashMap _readMethods; + protected static HashMap _writeMethods; + protected static HashMap _overloadTypes; static { - readMethods = new HashMap(); - readMethods.put("boolean", "readBoolean"); - readMethods.put("char", "readChar"); - readMethods.put("byte", "readByte"); - readMethods.put("short", "readShort"); - readMethods.put("int", "readInt"); - readMethods.put("long", "readLong"); - readMethods.put("float", "readFloat"); - readMethods.put("double", "readDouble"); - - writeMethods = new HashMap(); - writeMethods.put("boolean", "writeBoolean"); - writeMethods.put("char", "writeChar"); - writeMethods.put("byte", "writeByte"); - writeMethods.put("short", "writeShort"); - writeMethods.put("int", "writeInt"); - writeMethods.put("long", "writeLong"); - writeMethods.put("float", "writeFloat"); - writeMethods.put("double", "writeDouble"); - - overloadTypes = new HashMap(); - overloadTypes.put("boolean", "boolean"); - overloadTypes.put("byte", "octet"); - overloadTypes.put("char", "wchar"); - overloadTypes.put("double", "double"); - overloadTypes.put("float", "float"); - overloadTypes.put("int", "long"); - overloadTypes.put("long", "long_long"); - overloadTypes.put("short", "short"); - overloadTypes.put("java.lang.Class", "javax_rmi_CORBA.ClassDesc"); - overloadTypes.put("java.lang.String", "CORBA.WStringValue"); - overloadTypes.put("org.omg.CORBA.Object", "Object"); - overloadTypes.put("org.omg.CORBA.Any", "org_omg_boxedIDL_CORBA.Any"); - overloadTypes.put("org.omg.CORBA.TypeCode", "org_omg_boxedIDL_CORBA.TypeCode"); - } + _readMethods = new HashMap(); + _readMethods.put("boolean", "readBoolean"); + _readMethods.put("char", "readChar"); + _readMethods.put("byte", "readByte"); + _readMethods.put("short", "readShort"); + _readMethods.put("int", "readInt"); + _readMethods.put("long", "readLong"); + _readMethods.put("float", "readFloat"); + _readMethods.put("double", "readDouble"); + + _writeMethods = new HashMap(); + _writeMethods.put("boolean", "writeBoolean"); + _writeMethods.put("char", "writeChar"); + _writeMethods.put("byte", "writeByte"); + _writeMethods.put("short", "writeShort"); + _writeMethods.put("int", "writeInt"); + _writeMethods.put("long", "writeLong"); + _writeMethods.put("float", "writeFloat"); + _writeMethods.put("double", "writeDouble"); + + _overloadTypes = new HashMap(); + _overloadTypes.put("boolean", "boolean"); + _overloadTypes.put("byte", "octet"); + _overloadTypes.put("char", "wchar"); + _overloadTypes.put("double", "double"); + _overloadTypes.put("float", "float"); + _overloadTypes.put("int", "long"); + _overloadTypes.put("long", "long_long"); + _overloadTypes.put("short", "short"); + _overloadTypes.put("java.lang.Class", "javax_rmi_CORBA.ClassDesc"); + _overloadTypes.put("java.lang.String", "CORBA.WStringValue"); + _overloadTypes.put("org.omg.CORBA.Object", "Object"); + _overloadTypes.put("org.omg.CORBA.Any", "org_omg_boxedIDL_CORBA.Any"); + _overloadTypes.put("org.omg.CORBA.TypeCode", "org_omg_boxedIDL_CORBA.TypeCode"); + } + + public Compiler(Class remoteInterface) { + this(remoteInterface, null); + } + + public Compiler(Class riClass, GenOptions go) { + _riClass = riClass; + + _cl = _riClass.getClassLoader(); + if (_cl == null) { + _cl = ClassLoader.getSystemClassLoader(); + } + + if (go == null) { + go = new GenOptions(); + } + + _genOptions = go; + } + + // + // Properties + // - public Compiler(GenOptions go, ClassLoader cl) { - classLoader = cl; - if (classLoader == null) { - classLoader = ClassLoader.getSystemClassLoader(); - } + public boolean isSimpleIDL() { + return _simpleIDL; + } - genOptions = go; + public void setSimpleIDL(boolean simpleIDL) { + _simpleIDL = simpleIDL; } public GenOptions getGenOptions() { - return genOptions; + return _genOptions; } - public ClassLoader getClassLoader() { - return classLoader; + public void setGenOptions(GenOptions genOptions) { + _genOptions = genOptions; } public JParameter[] getMethodParms(Method m) { @@ -108,7 +131,7 @@ String rc = null; if (v != null) { - rc = (String) readMethods.get(v.getTypeDecl()); + rc = (String) _readMethods.get(v.getTypeDecl()); } return rc; @@ -118,191 +141,11 @@ String rc = null; if (v != null) { - rc = (String) writeMethods.get(v.getTypeDecl()); + rc = (String) _writeMethods.get(v.getTypeDecl()); } return rc; } - protected static void error( String errMsg ) { - System.err.println( "Error: " + errMsg ); - System.exit(1); - } - - protected void error(String msg, Throwable t) { - error(msg); - t.printStackTrace(); - } - - protected static void warn( String warnMsg ) { - System.out.println( "Warning: " + warnMsg ); - } - - protected String adjustPath( String path ) - { - // Maybe it would be easier if GenOptions just made sure that platform path - // separators and file separators were as required on the platform? - - if (File.separatorChar == '/') { - // We're under Unix, change '\\' to '/' - return path.replace( '\\', '/' ); - } else { - // We're under Windows, change '/' to '\\' - return path.replace( '/', '\\' ); - } - } - - protected void addMethodsToList( ArrayList list, Method[] methods ) - { - for(int i=0; list != null && methods != null && i < methods.length; i++ ) - { - list.add( methods[i] ); - } - } - - protected void collectInterfaceMethods( ArrayList list, Class intfClass, boolean simpleIdl ) - { - Method myMethods[] = intfClass.getDeclaredMethods(); - - if (!simpleIdl) - { - addMethodsToList( list, myMethods ); - } - - Class myInterfaces[] = intfClass.getInterfaces(); - if (myInterfaces != null && myInterfaces.length > 0) - { - String opsName = intfClass.getName() + "Operations"; - - for (int i = 0; i < myInterfaces.length; i++) - { - if (simpleIdl) - { - // add interface and its Operations, only if there is a coresponding Operations - if (myInterfaces[i].getName().equals(opsName)) - { - addMethodsToList( list, myMethods ); - addMethodsToList( list, myInterfaces[i].getDeclaredMethods() ); - continue; - } - else - { - collectInterfaceMethods( list, myInterfaces[i], simpleIdl ); - } - } - else - { - // Collect the interface methods for all interfaces .. - collectInterfaceMethods( list, myInterfaces[i], simpleIdl ); - } - } - } - } - - protected Method[] getMethods( Class intfClass, GenOptions go ) - { - Method myMethods[] = intfClass.getDeclaredMethods(); - ArrayList list = new ArrayList( myMethods.length * 2 ); - - collectInterfaceMethods( list, intfClass, go.isSimpleIdl() ); - - Object[] objs = list.toArray(); - Method[] methods = new Method[objs.length]; - System.arraycopy( objs, 0, methods, 0, objs.length ); - return methods; - } - - public MethodOverload[] getMethodOverloads( Method methods[] ) - { - HashMap hm = new HashMap( methods.length ); - - // Put all the methods into the hashmap - for( int i=0; methods != null && i < methods.length; i++ ) - { - ArrayList al = (ArrayList)hm.get( methods[i].getName() ); - if (al == null) - { - al = new ArrayList( methods.length ); - al.add( methods[i] ); - hm.put( methods[i].getName(), al ); - } - else - { - al.add( methods[i] ); - } - } - - Set keySet = hm.keySet(); - ArrayList overloadList = new ArrayList( methods.length ); - for (Iterator keyIt = keySet.iterator(); keyIt != null && keyIt.hasNext(); ) - { - ArrayList al = (ArrayList)hm.get( keyIt.next() ); - if (al.size() == 1) - { - Method m = (Method)al.remove(0); - overloadList.add( new MethodOverload( m.getName(), m ) ); - } - else - { - for( int i=0; i<=al.size(); i++ ) - { - Method m = (Method)al.remove(0); - overloadList.add( new MethodOverload( overloadMethodName(m), m ) ); - } - } - } - - Object obj[] = overloadList.toArray(); - MethodOverload m[] = new MethodOverload[ obj.length ]; - System.arraycopy( obj, 0, m, 0, obj.length ); - - return m; - } - - protected String overloadMethodName( Method m ) - { - Class parms[] = m.getParameterTypes(); - String name = m.getName() + "_"; - for( int i=0; i<parms.length; i++ ) - { - name += "_" + parms[i].getName(); - } - return name.replace( '.', '_' ); - } - - class MethodOverload - { - public Method method; - public String iiop_name; - - public MethodOverload( String iiop_name, Method method ) - { - this.method = method; - this.iiop_name = iiop_name; - } - - public int hashCode() - { - return iiop_name.hashCode(); - } - - public boolean equals( Object other ) - { - if (other instanceof MethodOverload) - { - MethodOverload mother = (MethodOverload)other; - if (iiop_name != null) - { - return iiop_name.equals( mother.iiop_name ); - } - else - { - return iiop_name == mother.iiop_name; - } - } - - return false; - } - } }
Modified: geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/SkelCompiler.java URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/SkelCompiler.java?view=diff&r1=158861&r2=158862 ============================================================================== --- geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/SkelCompiler.java (original) +++ geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/SkelCompiler.java Wed Mar 23 18:30:30 2005 @@ -19,45 +19,55 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.io.File; -import java.util.*; -import org.apache.geronimo.interop.generator.*; -import org.apache.geronimo.interop.util.JavaClass; -import org.apache.geronimo.interop.util.ProcessUtil; -import org.apache.geronimo.interop.adapter.Adapter; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -public class SkelCompiler extends Compiler { - private final Log log = LogFactory.getLog(SkelCompiler.class); - - private ValueTypeContext vtc = new ValueTypeContext(); - - private static JParameter objInputVar = new JParameter(org.apache.geronimo.interop.rmi.iiop.ObjectInputStream.class, "input"); - private static JParameter objOutputVar = new JParameter(org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream.class, "output"); - - private String inStreamName = "getInputStream"; - private String outStreamName = "getOutputStream"; - - private HashMap packages = new HashMap(); - - public SkelCompiler(GenOptions go, ClassLoader cl) { - super(go, cl); - } +import org.apache.geronimo.interop.generator.GenOptions; +import org.apache.geronimo.interop.generator.JCaseStatement; +import org.apache.geronimo.interop.generator.JCatchStatement; +import org.apache.geronimo.interop.generator.JClass; +import org.apache.geronimo.interop.generator.JCodeStatement; +import org.apache.geronimo.interop.generator.JConstructor; +import org.apache.geronimo.interop.generator.JDeclareStatement; +import org.apache.geronimo.interop.generator.JExpression; +import org.apache.geronimo.interop.generator.JField; +import org.apache.geronimo.interop.generator.JLocalVariable; +import org.apache.geronimo.interop.generator.JMethod; +import org.apache.geronimo.interop.generator.JPackage; +import org.apache.geronimo.interop.generator.JParameter; +import org.apache.geronimo.interop.generator.JReturnType; +import org.apache.geronimo.interop.generator.JSwitchStatement; +import org.apache.geronimo.interop.generator.JTryCatchFinallyStatement; +import org.apache.geronimo.interop.generator.JTryStatement; +import org.apache.geronimo.interop.generator.JVariable; +import org.apache.geronimo.interop.generator.JavaGenerator; + + +public class SkelCompiler + extends Compiler { + protected ValueTypeContext _vtc = new ValueTypeContext(); + protected static JParameter _objInputVar = new JParameter(org.apache.geronimo.interop.rmi.iiop.ObjectInputStream.class, "input"); + protected static JParameter _objOutputVar = new JParameter(org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream.class, "output"); + + public SkelCompiler(Class remoteInterface) { + super(remoteInterface); + } + + public SkelCompiler(Class remoteInterface, GenOptions go) { + super(remoteInterface, go); + } + + // + // Methods + // public void addMethodGetIds(JClass jc) { - // - // Method Template: - // - // public String[] getIds() - // { - // return _ids; - // } - // + /* + public String[] getIds() + { + return _ids; + } + */ - JMethod jm = jc.newMethod(new JReturnType(String[].class), + JMethod jm = jc.newMethod(new JReturnType(String.class, true), "getIds", (JParameter[]) null, (Class[]) null); @@ -66,14 +76,12 @@ } public void addMethodRegisterMethod(JClass jc) { - // - // Method Template: - // - // public void registerMethod( String name, int id ) - // { - // _methodMap.put( name, new Integer(id) ); - // } - // + /* + public void registerMethod( String name, int id ) + { + _methodMap.put( name, new Integer(id) ); + } + */ JMethod jm = jc.newMethod(new JReturnType(void.class), "registerMethod", @@ -85,20 +93,18 @@ } public void addMethodGetObjectRef(JClass jc, Class c) { - // - // Method Template: - // - // public ObjectRef getObjectRef() - // { - // ObjectRef or = new ObjectRef(); - // or.$setID("RMI:mark.comps.Add:0000000000000000"); - // or.$setObjectKey( "mark.comps.Add" ); - // return or; - // } - // + /* + public ObjectRef $getObjectRef() + { + ObjectRef or = new ObjectRef(); + or.$setID("RMI:mark.comps.Add:0000000000000000"); + or.$setObjectKey( "mark.comps.Add" ); + return or; + } + */ - JMethod jm = jc.newMethod(new JReturnType(org.apache.geronimo.interop.rmi.iiop.ObjectRef.class), - "getObjectRef", + JMethod jm = jc.newMethod(new JReturnType("ObjectRef"), + "$getObjectRef", (JParameter[]) null, (Class[]) null); @@ -109,16 +115,14 @@ } public void addMethodGetSkeleton(JClass jc) { - // - // Method Template - // - // public RemoteInterface $getSkeleton() - // { - // return this; - // } - // + /* + public RemoteInterface $getSkeleton() + { + return this; + } + */ - JMethod jm = jc.newMethod(new JReturnType(org.apache.geronimo.interop.rmi.iiop.RemoteInterface.class), + JMethod jm = jc.newMethod(new JReturnType("RemoteInterface"), "$getSkeleton", (JParameter[]) null, (Class[]) null); @@ -139,20 +143,17 @@ return rc; } - public void addMethod(MethodOverload mo, JClass jc, GenOptions go) { + public void addMethod(Method m, JClass jc) { String invokeCall; - Method m = mo.method; String name = m.getName(); JParameter[] sparms = getMethodParms(m); - JParameter[] iparms = new JParameter[]{objInputVar, objOutputVar}; - String vtVarName = null; - JCodeStatement codeStmt = null; + JParameter[] iparms = new JParameter[]{_objInputVar, _objOutputVar}; - if (!go.isSimpleIdl() && !throwsAnRMIRemoteException(m)) { + if (!isSimpleIDL() && !throwsAnRMIRemoteException(m)) { error("Method " + m.getName() + " does not throw java.rmi.RemoteException or subclass, unable to generate its skeleton method."); } - JMethod jm = jc.newMethod(new JReturnType(void.class), mo.iiop_name, iparms, null); + JMethod jm = jc.newMethod(new JReturnType(void.class), name, iparms, null); JVariable jrc = null; String rc = m.getReturnType().getName(); @@ -160,8 +161,8 @@ jrc = jm.newLocalVariable(m.getReturnType(), "rc"); } - ArrayList declareStatementList = new ArrayList( 20 ); - JStatement invokeStatement = null; + JTryCatchFinallyStatement tcfs = new JTryCatchFinallyStatement(); + JTryStatement ts = tcfs.getTryStatement(); invokeCall = "_servant." + name + "("; @@ -177,7 +178,7 @@ jcs = new JCodeStatement("input." + readMethod + "()"); } else { - vtVarName = vtc.getValueTypeVarName(jc, sparms[i]); + String vtVarName = _vtc.getValueTypeVarName(jc, sparms[i]); if (vtVarName != null) { jcs = new JCodeStatement("(" + sparms[i].getTypeDecl() + ") input.readObject( " + vtVarName + " )"); } else { @@ -185,7 +186,7 @@ } } - declareStatementList.add(new JDeclareStatement(sparms[i], new JExpression(jcs))); + ts.addStatement(new JDeclareStatement(sparms[i], new JExpression(jcs))); invokeCall += " " + sparms[i].getName(); if (i + 1 < sparms.length) { @@ -202,118 +203,37 @@ invokeCall = invokeCall + ";"; - invokeStatement = new JCodeStatement(invokeCall); + ts.addStatement(new JCodeStatement(invokeCall)); + + JVariable jv = new JVariable(java.lang.Exception.class, "ex"); + JCatchStatement cs = tcfs.newCatch(jv); + cs.addStatement(new JCodeStatement(jv.getName() + ".printStackTrace();")); + + jv = new JVariable(java.lang.Error.class, "er"); + cs = tcfs.newCatch(jv); + cs.addStatement(new JCodeStatement(jv.getName() + ".printStackTrace();")); + + jm.addStatement(tcfs); - JStatement writeResultStatement = null; if (jrc != null) { String writeMethod = getWriteMethod(jrc); - codeStmt = null; + JCodeStatement jcs = null; if (writeMethod != null) { // Primitive Type // Cast not needed since each method returns the primitive datatype. - codeStmt = new JCodeStatement("output." + writeMethod + "( " + jrc.getName() + " );"); + jcs = new JCodeStatement("output." + writeMethod + "( " + jrc.getName() + " );"); } else { - vtVarName = vtc.getValueTypeVarName(jc, jrc); - if (vtVarName != null) { - codeStmt = new JCodeStatement("output.writeObject( " + vtVarName + ", " + jrc.getName() + " );"); - } else { - codeStmt = new JCodeStatement("// Code Gen Error: Class '" + jrc.getTypeDecl() + " is not a valid value type."); - } - } - - writeResultStatement = codeStmt; - } - - // - // The exception handling block: - // - // try - // { - // invoke method() - // } - // catch (java.lang.Exception $ex_1) - // { - // Listed here are the individual catches that the method can throw - // if ($ex_1 instanceof com.sybase.djc.org.omg.CosNaming.NamingContextPackage.NotFound) - // { - // $output.writeException(type$4, $ex_1); - // return; - // } - // if ($ex_1 instanceof com.sybase.djc.org.omg.CosNaming.NamingContextPackage.CannotProceed) - // { - // $output.writeException(type$5, $ex_1); - // return; - // } - // throw $ex_1; - // } - - Class[] excepts = m.getExceptionTypes(); - JVariable jvExcept = null; - JVariable jvTmp = null; - - JCatchStatement catchStmt = null; - - if (excepts != null && excepts.length > 0) - { - JTryCatchFinallyStatement tcfs = new JTryCatchFinallyStatement(); - JTryStatement ts = tcfs.getTryStatement(); - - if (declareStatementList.size() > 0) - { - for( int i=0; i<declareStatementList.size(); i++ ) - { - ts.addStatement( (JStatement)declareStatementList.get(i) ); - } - } - - ts.addStatement( invokeStatement ); - - jvExcept = new JVariable(java.lang.Exception.class, "ex"); - catchStmt = tcfs.newCatch(jvExcept); - - for( int i=0; excepts != null && i < excepts.length; i++ ) - { - jvTmp = new JVariable( excepts[i], "exvar" ); - vtVarName = vtc.getValueTypeVarName(jc, jvTmp); - codeStmt = null; + String vtVarName = _vtc.getValueTypeVarName(jc, jrc); if (vtVarName != null) { - codeStmt = new JCodeStatement("output.writeException( " + vtVarName + ", " + jvExcept.getName() + ");" ); + jcs = new JCodeStatement("output.writeObject( " + vtVarName + ", " + jrc.getName() + " );"); } else { - codeStmt = new JCodeStatement("// Code Gen Error: Class '" + sparms[i].getTypeDecl() + " is not a valid value type."); - } - - JIfStatement ifs = new JIfStatement( new JExpression( - new JCodeStatement( jvExcept.getName() + " instanceof " + excepts[i].getName() ) )); - ifs.addStatement( codeStmt ); - ifs.addStatement( new JCodeStatement( "return;" )); - catchStmt.addStatement( ifs ); - } - - if (writeResultStatement != null) - { - ts.addStatement( writeResultStatement ); - } - - jm.addStatement(tcfs); - } - else - { - if (declareStatementList.size() > 0) - { - for( int i=0; i<declareStatementList.size(); i++ ) - { - jm.addStatement( (JStatement)declareStatementList.get(i) ); + jcs = new JCodeStatement("// Code Gen Error: Class '" + jrc.getTypeDecl() + " is not a valid value type."); } } - jm.addStatement( invokeStatement ); - - if (writeResultStatement != null) - { - jm.addStatement( writeResultStatement ); - } + ts.addStatement(jcs); } } @@ -368,203 +288,132 @@ return rc; } - public void generate() throws GenException { - - GenOptions go = getGenOptions(); - List interfaces = go.getInterfaces(); - Iterator intf = null; + protected void error(String msg) { + System.out.println("Error: " + msg); + } - if (interfaces != null) { - intf = interfaces.iterator(); - } + protected void error(String msg, Throwable t) { + error(msg); + t.printStackTrace(); + } - JavaGenerator jg = new JavaGenerator(genOptions); + public void generate() + throws Exception { + _vtc.clear(); - if (go.isSimpleIdl()) { - inStreamName = "getSimpleInputStream"; - outStreamName = "getSimpleOutputStream"; - } else { - inStreamName = "getInputStream"; - outStreamName = "getOutputStream"; + if (!isSimpleIDL() && !isClassARMIRemote(_riClass)) { + error("Class '" + _riClass.getName() + "' must be an instance of either java.rmi.Remote or of a subclass."); } - String riClassName = ""; - Class riClass = null; - String skelClassName = ""; - String pkgName = ""; - JPackage pkg = null; - - while (intf != null && intf.hasNext() ) { - // Clear the value type cache. - vtc.clear(); - - riClassName = (String)intf.next(); - - - try { - riClass = getClassLoader().loadClass( riClassName ); - } catch (Exception ex) { - throw new GenException( "Generate Skels Failed:", ex ); - } - - if (!go.isSimpleIdl() && !isClassARMIRemote(riClass)) { - error("Class '" + riClass.getName() + "' must be an instance of either java.rmi.Remote or of a subclass."); - } - - pkgName = JavaClass.getNamePrefix(riClassName); - skelClassName = JavaClass.getNameSuffix(riClassName); - pkg = (JPackage) packages.get( pkgName ); - if (pkg == null) - { - pkg = new JPackage( pkgName ); - packages.put( pkgName, pkg ); - } - - JClass jc = pkg.newClass(skelClassName + "_Skeleton"); - - jc.addImport("org.apache.geronimo.interop.rmi.iiop", "RemoteInterface"); - jc.addImport("org.apache.geronimo.interop.rmi.iiop", "ObjectRef"); - jc.addImport("org.apache.geronimo.interop.rmi.iiop", "RemoteObject"); - - jc.setExtends("RemoteObject"); - jc.addImplements("RemoteInterface"); - - JField idsField = jc.newField(String[].class, "_ids", new JExpression(new JCodeStatement("{ \"" + riClass.getName() + "\", \"RMI:" + riClass.getName() + ":0000000000000000\"}")), true); - idsField.setModifiers(Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL); - - JField servantField = jc.newField(riClass, "_servant", new JExpression(new JCodeStatement("null"))); - servantField.setModifiers(Modifier.PRIVATE); - - JConstructor jcCon = jc.newConstructor((JParameter[]) null, (Class[]) null); - jcCon.addStatement(new JCodeStatement("super();")); - - // Replaced with the method in the RemoteObject parent class - //addMethodRegisterMethod(jc); - - addMethodGetIds(jc); - - // Not used anymore - //addMethodGetSkeleton(jc); - - addMethodGetObjectRef(jc, riClass); - - JMethod jmInvoke = jc.newMethod(new JReturnType(void.class), - "invoke", - new JParameter[]{new JParameter(String.class, "methodName"), - new JParameter(byte[].class, "objectKey"), - new JParameter(Adapter.class, "adapter"), - objInputVar, - objOutputVar}, - (Class[]) null); + ClassLoader cl = _riClass.getClassLoader(); + if (cl == null) { + cl = ClassLoader.getSystemClassLoader(); + } - jmInvoke.setModifier(Modifier.PUBLIC); + String fullGenDir = _genOptions.getGenDir(); - JLocalVariable jvM = jmInvoke.newLocalVariable(Integer.class, "m", new JExpression(new JCodeStatement("getMethodId(methodName); // (Integer)_methods.get(methodName)"))); + JavaGenerator jg = new JavaGenerator(_genOptions); - JIfStatement jis = new JIfStatement(new JExpression(new JCodeStatement(jvM.getName() + " == null"))); - jis.addStatement(new JCodeStatement("throw new org.omg.CORBA.BAD_OPERATION(methodName);")); - jmInvoke.addStatement(jis); + String className = _riClass.getName(); - jmInvoke.addStatement(new JCodeStatement("_servant = (" + riClass.getName() + ")adapter.getServant(); //instance;")); + JPackage p = new JPackage(""); + if (_riClass.getPackage() != null) { + p = new JPackage(_riClass.getPackage().getName()); + className = className.substring(className.lastIndexOf(".") + 1); + } - JIfStatement jis2 = new JIfStatement(new JExpression(new JCodeStatement(jvM.getName() + ".intValue() < 0"))); - jis2.addStatement(new JCodeStatement("super.invoke( " + jvM.getName() + ".intValue(), objectKey, adapter, input, output );")); - jmInvoke.addStatement(jis2); + JClass jc = p.newClass(className + "_Skeleton"); - JTryCatchFinallyStatement tcfs = new JTryCatchFinallyStatement(); - JTryStatement ts = tcfs.getTryStatement(); + /* + jw.comment( "" ); + jw.comment( "CORBA RMI-IIOP Skeleton Generator" ); + jw.comment( " Interface: " + c.getName() ); + jw.comment( " Date: " + (new Date(System.currentTimeMillis())).toString() ); + jw.comment( "" ); + */ + + jc.addImport("org.apache.geronimo.interop.rmi.iiop", "ObjectInputStream"); + jc.addImport("org.apache.geronimo.interop.rmi.iiop", "ObjectOutputStream"); + jc.addImport("org.apache.geronimo.interop.rmi.iiop", "RemoteInterface"); + jc.addImport("org.apache.geronimo.interop.rmi.iiop", "RemoteInterface"); + jc.addImport("org.apache.geronimo.interop.rmi.iiop", "ObjectRef"); + jc.addImport("org.apache.geronimo.interop.rmi.iiop", "RemoteObject"); + jc.addImport("org.apache.geronimo.interop.rmi.iiop.server", "Adapter"); + jc.addImport("java.util", "HashMap"); + + jc.setExtends("RemoteObject"); + jc.addImplements("RemoteInterface"); + + JField idsField = jc.newField(String[].class, "_ids", new JExpression(new JCodeStatement("{ \"" + _riClass.getName() + "\", \"RMI:" + _riClass.getName() + ":0000000000000000\"}")), true); + JField methodsField = jc.newField(java.util.HashMap.class, "_methods", new JExpression(new JCodeStatement("new HashMap(10)"))); + JField servantField = jc.newField(_riClass, "_servant", new JExpression(new JCodeStatement("null"))); + + JConstructor jcCon = jc.newConstructor((JParameter[]) null, (Class[]) null); + jcCon.addStatement(new JCodeStatement("super();")); + + addMethodRegisterMethod(jc); + addMethodGetIds(jc); + addMethodGetSkeleton(jc); + addMethodGetObjectRef(jc, _riClass); + + JMethod jmInvoke = jc.newMethod(new JReturnType(void.class), + "$invoke", + new JParameter[]{new JParameter(String.class, "methodName"), + new JParameter(byte[].class, "objectKey"), + new JParameter(Object.class, "instance"), + _objInputVar, + _objOutputVar}, + (Class[]) null); + + jmInvoke.setModifier(Modifier.PUBLIC, true); + + JLocalVariable jvM = jmInvoke.newLocalVariable(Integer.class, "m", new JExpression(new JCodeStatement("(Integer)_methods.get(methodName)"))); + + jmInvoke.addStatement(new JCodeStatement("if (m == null)")); + jmInvoke.addStatement(new JCodeStatement("{")); + jmInvoke.addStatement(new JCodeStatement(" throw new org.omg.CORBA.BAD_OPERATION(methodName);")); + jmInvoke.addStatement(new JCodeStatement("}")); + jmInvoke.addStatement(new JCodeStatement("")); + jmInvoke.addStatement(new JCodeStatement("_servant = (" + _riClass.getName() + ")instance;")); + jmInvoke.addStatement(new JCodeStatement("")); + jmInvoke.addStatement(new JCodeStatement("if (m.intValue() < 0)")); + jmInvoke.addStatement(new JCodeStatement("{")); + jmInvoke.addStatement(new JCodeStatement(" super.invoke( m.intValue(), objectKey, instance, input, output );")); + jmInvoke.addStatement(new JCodeStatement("}")); + jmInvoke.addStatement(new JCodeStatement("")); + + JSwitchStatement ss = new JSwitchStatement(new JExpression(new JCodeStatement("m.intValue()"))); + JCaseStatement cs = null; + jmInvoke.addStatement(ss); - JSwitchStatement switchStmt = new JSwitchStatement(new JExpression(new JCodeStatement("m.intValue()"))); - JCaseStatement caseStmt = null; - ts.addStatement(switchStmt); + Method m[] = null; - Method m[] = getMethods( riClass, go ); - MethodOverload mo[] = null; - mo = getMethodOverloads( m ); + if (isSimpleIDL()) { + m = _riClass.getMethods(); + } else { + m = _riClass.getDeclaredMethods(); + } - for (int i = 0; mo != null && i < mo.length; i++) - { + if (m != null && m.length > 0) { + int i; + for (i = 0; i < m.length; i++) { // Enter a new method id in the _methods hashtable. - jcCon.addStatement(new JCodeStatement("registerMethod( \"" + mo[i].iiop_name + "\", " + i + ");")); + jcCon.addStatement(new JCodeStatement("registerMethod( \"" + m[i].getName() + "\", " + i + ");")); // Add a new case statement to the invoke swtich - caseStmt = switchStmt.newCase(new JExpression(new JCodeStatement("" + i))); - caseStmt.addStatement(new JCodeStatement(mo[i].iiop_name + "(input,output);")); + cs = ss.newCase(new JExpression(new JCodeStatement("" + i))); + cs.addStatement(new JCodeStatement(m[i].getName() + "(input,output);")); // Generate the method wrapper - addMethod(mo[i], jc, go); + addMethod(m[i], jc); } - - JCatchStatement catchStmt = null; - JVariable jvExcept = null; - - jvExcept = new JVariable(java.lang.Error.class, "erEx"); - catchStmt = tcfs.newCatch(jvExcept); - catchStmt.addStatement(new JCodeStatement( "throw new org.apache.geronimo.interop.SystemException( " + jvExcept.getName() + " );" ) ); - - jvExcept = new JVariable(java.lang.RuntimeException.class, "rtEx"); - catchStmt = tcfs.newCatch(jvExcept); - catchStmt.addStatement(new JCodeStatement( "throw " + jvExcept.getName() + ";" ) ); - - jvExcept = new JVariable(java.lang.Exception.class, "exEx"); - catchStmt = tcfs.newCatch(jvExcept); - catchStmt.addStatement(new JCodeStatement( "throw new org.apache.geronimo.interop.SystemException( " + jvExcept.getName() + " );" ) ); - - jmInvoke.addStatement( tcfs ); } - Set pkgSet = packages.keySet(); - Iterator pkgIt = pkgSet.iterator(); - String skelPkg = ""; - - while (pkgIt.hasNext()) - { - skelPkg = (String) pkgIt.next(); - pkg = (JPackage)packages.get(skelPkg); - System.out.println("Generating Package: " + skelPkg); - jg.generate(pkg); - } + jg.generate(p); } public void compile() throws Exception { - - Set pkg = packages.keySet(); - Iterator pkgIt = pkg.iterator(); - String skelPkg = ""; - - /* - * Each of the packages were generated under go.getGenSrcDir(). - * - * Go through all the packages and run the compiler on *.java - */ - - GenOptions go = getGenOptions(); - String classpath = adjustPath(go.getClasspath()); - String srcpath = adjustPath(go.getGenSrcDir()); - - String filesToCompile = ""; - String javacCmd = ""; - - while (pkgIt.hasNext()) - { - skelPkg = (String) pkgIt.next(); - skelPkg = skelPkg.replace( '.', File.separatorChar ); - filesToCompile = adjustPath(go.getGenSrcDir() + File.separator + skelPkg + File.separator + "*.java"); - - System.out.println("Compiling Package: " + filesToCompile); - - javacCmd = "javac -d " + go.getGenClassDir() + - ( go.isCompileDebug() ? " -g" : "" ) + - " -classpath " + classpath + " " + - " -sourcepath " + srcpath + " " + filesToCompile; - - System.out.println( "Lauching: " + javacCmd ); - - ProcessUtil pu = ProcessUtil.getInstance(); - pu.setEcho(System.out); - pu.run(javacCmd, (String[]) null, "./" ); - } } public Class getSkelClass() { @@ -580,27 +429,62 @@ return c; } - public static void main(String args[]) throws Exception { - GenOptions go = null; - - try - { - go = new GenOptions( "./skels", args ); - } - catch( GenWarning gw ) - { - gw.printStackTrace(); + public static void main(String args[]) + throws Exception { + boolean generate = false; + boolean compile = false; + boolean simpleIDL = false; + String ri = ""; + GenOptions go = new GenOptions(); + + go.setGenDir("./src"); + go.setOverwrite(false); + go.setVerbose(false); + + for (int i = 0; i < args.length; i++) { + if (args[i].equals("-g")) { + generate = true; + } else if (args[i].equals("-c")) { + compile = true; + } else if (args[i].equals("-d") && ((i + 1) < args.length)) { + go.setGenDir(args[++i]); + } else if (args[i].equals("-v")) { + go.setVerbose(true); + } else if (args[i].equals("-o")) { + go.setOverwrite(true); + } else if (args[i].equals("-s")) { + simpleIDL = true; + } else if (args[i].startsWith("-")) { + System.out.println("Warning: Ignoring unrecognized options: '" + args[i] + "'"); + } else { + ri = args[i]; + } } - ClassLoader cl = ClassLoader.getSystemClassLoader(); - SkelCompiler sg = new SkelCompiler( go, cl ); + Class riClass = Class.forName(ri); + + SkelCompiler sg = new SkelCompiler(riClass, go); + + sg.setSimpleIDL(simpleIDL); + + if (generate) { + if (go.isVerbose()) { + System.out.println("Generating: " + ri); + } - if (go.isGenerate()) { sg.generate(); } - if (go.isCompile()) { + if (compile) { + if (go.isVerbose()) { + System.out.println("Compiling: " + ri); + } + sg.compile(); } + + // sg.setSimpleIDL( true ); + // sg.generate( "org.apache.geronimo.interop.rmi.iiop.NameServiceOperations"); } } + Modified: geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/SkelFactory.java URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/SkelFactory.java?view=diff&r1=158861&r2=158862 ============================================================================== --- geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/SkelFactory.java (original) +++ geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/SkelFactory.java Wed Mar 23 18:30:30 2005 @@ -23,27 +23,56 @@ import org.apache.geronimo.interop.rmi.iiop.RemoteObject; import org.apache.geronimo.interop.util.ThreadContext; + public class SkelFactory { - private static SkelFactory sf = new SkelFactory(); + protected static SkelFactory _sf = new SkelFactory(); protected SkelFactory() { } public static SkelFactory getInstance() { - return sf; + return _sf; } - private static HashMap skelClassMap; + // private data + + private static HashMap _skelClassMap; + + // internal methods protected void init() { - skelClassMap = new HashMap(); + _skelClassMap = new HashMap(); } - /* protected Class loadStub(Class remoteInterface) { String className = remoteInterface.getName(); String skelClassName = className + "_Skeleton"; + /* + StubClass sc = new StubClass(); + if (sc.skelClass == null) + { + // Try generating skel class now. + System.out.println( "TODO: StubFactory.loadStub(): className = " + className ); + StubCompiler skelCompiler = StubCompiler.getInstance(remoteInterface); + sc.skelClass = skelCompiler.getStubClass(); + } + + if (sc.skelClass != null) + { + try + { + sc.getInstance = sc.skelClass.getMethod("$getInstance", ArrayUtil.EMPTY_CLASS_ARRAY); + } + catch (Exception ex) + { + throw new SystemException(ex); + } + } + + return sc; + */ + Class sc = null; try { sc = Class.forName(skelClassName); @@ -55,19 +84,19 @@ return sc; } - */ - /* + // public methods + public RemoteObject getSkel(Class remoteInterface) { System.out.println("SkelFactory.getSkel(): remoteInterface: " + remoteInterface); try { - Class sc = (Class) skelClassMap.get(remoteInterface); + Class sc = (Class) _skelClassMap.get(remoteInterface); if (sc == null) { - synchronized (skelClassMap) { - sc = (Class) skelClassMap.get(remoteInterface); + synchronized (_skelClassMap) { + sc = (Class) _skelClassMap.get(remoteInterface); if (sc == null) { sc = loadStub(remoteInterface); - skelClassMap.put(remoteInterface, sc); + _skelClassMap.put(remoteInterface, sc); } } } @@ -77,11 +106,8 @@ throw new SystemException(ex); } } - */ - /* public Object getSkel(String remoteInterface) { return getSkel(ThreadContext.loadClass(remoteInterface)); } - */ } Modified: geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubClass.java URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubClass.java?view=diff&r1=158861&r2=158862 ============================================================================== --- geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubClass.java (original) +++ geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubClass.java Wed Mar 23 18:30:30 2005 @@ -21,22 +21,7 @@ public class StubClass { - private Class stubClass; - private Method getInstanceMethod; + public Class stubClass; - public StubClass( Class stubClass, Method getInstanceMethod ) - { - this.stubClass = stubClass; - this.getInstanceMethod = getInstanceMethod; - } - - public Class getStubClass() - { - return stubClass; - } - - public Method getGetInstanceMethod() - { - return getInstanceMethod; - } + public Method getInstance; } Modified: geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubCompiler.java URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubCompiler.java?view=diff&r1=158861&r2=158862 ============================================================================== --- geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubCompiler.java (original) +++ geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubCompiler.java Wed Mar 23 18:30:30 2005 @@ -18,65 +18,93 @@ package org.apache.geronimo.interop.rmi.iiop.compiler; import java.lang.reflect.Method; -import java.io.File; -import java.util.*; - -import org.apache.geronimo.interop.generator.*; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import org.apache.geronimo.interop.generator.GenOptions; +import org.apache.geronimo.interop.generator.JBlockStatement; +import org.apache.geronimo.interop.generator.JCatchStatement; +import org.apache.geronimo.interop.generator.JClass; +import org.apache.geronimo.interop.generator.JCodeStatement; +import org.apache.geronimo.interop.generator.JConstructor; +import org.apache.geronimo.interop.generator.JExpression; +import org.apache.geronimo.interop.generator.JField; +import org.apache.geronimo.interop.generator.JForStatement; +import org.apache.geronimo.interop.generator.JIfElseIfElseStatement; +import org.apache.geronimo.interop.generator.JIfStatement; +import org.apache.geronimo.interop.generator.JLocalVariable; +import org.apache.geronimo.interop.generator.JMethod; +import org.apache.geronimo.interop.generator.JPackage; +import org.apache.geronimo.interop.generator.JParameter; +import org.apache.geronimo.interop.generator.JReturnType; +import org.apache.geronimo.interop.generator.JTryCatchFinallyStatement; +import org.apache.geronimo.interop.generator.JTryStatement; +import org.apache.geronimo.interop.generator.JVariable; +import org.apache.geronimo.interop.generator.JavaGenerator; import org.apache.geronimo.interop.util.JavaClass; import org.apache.geronimo.interop.util.ProcessUtil; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -public class StubCompiler extends Compiler { - private final Log log = LogFactory.getLog(StubCompiler.class); - private ValueTypeContext vtc = new ValueTypeContext(); +public class StubCompiler + extends Compiler { + protected ValueTypeContext _vtc = new ValueTypeContext(); + protected static JParameter _objInputVar = new JParameter(org.apache.geronimo.interop.rmi.iiop.ObjectInputStream.class, "input"); + protected static JParameter _objOutputVar = new JParameter(org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream.class, "output"); + + protected String _stubClassName = ""; + protected Class _stubClass = null; + + protected String _inStreamName = "getInputStream"; + protected String _outStreamName = "getOutputStream"; + + public StubCompiler(Class riClass) { + super(riClass); + init(); + } + + public StubCompiler(Class riClass, GenOptions go) { + super(riClass, go); + init(); + } + + protected void init() { + String className = _riClass.getName(); + _stubClassName = JavaClass.addPackageSuffix(className, "iiop_stubs") + "_Stub"; + } - private String inStreamName = "getInputStream"; - private String outStreamName = "getOutputStream"; - - private HashMap packages = new HashMap(); - - public StubCompiler(GenOptions go, ClassLoader cl) { - super(go, cl); - } - - protected void addMethod(JClass jc, String iiopMethodName, JReturnType jrc, - String name, JParameter[] jparms, Class[] excepts) { - // - // Method Template: - // - // java.lang.Object $key_1 = $getRequestKey(); - // for (int $retry = 0; ; $retry++) - // { - // try - // { - // org.apache.geronimo.interop.rmi.iiop.client.Connection $connection_2 = this.$connect(); - // org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream $output_3 = $connection_2.getSimpleOutputStream(); // simple idl - // org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream $output_3 = $connection_2.getOutputStream(); // rmi-iiop - // $output_3.writeObject(type$1, p1); - // $connection_2.invoke(this, "_is_a", $key_1, $retry); - // org.apache.geronimo.interop.rmi.iiop.ObjectInputStream $input_4 = $connection_2.getSimpleInputStream(); // simple idl - // org.apache.geronimo.interop.rmi.iiop.ObjectInputStream $input_4 = $connection_2.getInputStream(); // rmi-iiop - // $connection_2.forget($key_1); - // $connection_2.close(); - // java.lang.String $et_5 = $connection_2.getExceptionType(); - // if ($et_5 != null) - // { - // throw org.apache.geronimo.interop.rmi.iiop.SystemExceptionFactory.getException($connection_2.getException()); - // } - // boolean $djc_result; - // $djc_result = $input_4.readBoolean(); - // return $djc_result; - // } - // catch (org.apache.geronimo.interop.rmi.iiop.client.RetryInvokeException $ex_6) - // { - // if ($retry == 3) - // { - // throw $ex_6.getRuntimeException(); - // } - // } - // } + protected void addMethod(JClass jc, JReturnType jrc, String name, JParameter[] jparms, Class[] excepts) { +// java.lang.Object $key_1 = $getRequestKey(); +// for (int $retry = 0; ; $retry++) +// { +// try +// { +// org.apache.geronimo.interop.rmi.iiop.client.Connection $connection_2 = this.$connect(); +// org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream $output_3 = $connection_2.getSimpleOutputStream(); // simple idl +// org.apache.geronimo.interop.rmi.iiop.ObjectOutputStream $output_3 = $connection_2.getOutputStream(); // rmi-iiop +// $output_3.writeObject(type$1, p1); +// $connection_2.invoke(this, "_is_a", $key_1, $retry); +// org.apache.geronimo.interop.rmi.iiop.ObjectInputStream $input_4 = $connection_2.getSimpleInputStream(); // simple idl +// org.apache.geronimo.interop.rmi.iiop.ObjectInputStream $input_4 = $connection_2.getInputStream(); // rmi-iiop +// $connection_2.forget($key_1); +// $connection_2.close(); +// java.lang.String $et_5 = $connection_2.getExceptionType(); +// if ($et_5 != null) +// { +// throw org.apache.geronimo.interop.rmi.iiop.SystemExceptionFactory.getException($connection_2.getException()); +// } +// boolean $djc_result; +// $djc_result = $input_4.readBoolean(); +// return $djc_result; +// } +// catch (org.apache.geronimo.interop.rmi.iiop.client.RetryInvokeException $ex_6) +// { +// if ($retry == 3) +// { +// throw $ex_6.getRuntimeException(); +// } +// } +// } //JParameter jpID = new JParameter( java.lang.String.class, "id" ); JMethod jm = jc.newMethod(jrc, name, jparms, excepts); @@ -107,7 +135,7 @@ } jbs.addStatement(new JCodeStatement(jlvConn.getName() + " = this.$connect();")); - jbs.addStatement(new JCodeStatement(jlvOutput.getName() + " = " + jlvConn.getName() + "." + outStreamName + "();")); + jbs.addStatement(new JCodeStatement(jlvOutput.getName() + " = " + jlvConn.getName() + "." + _outStreamName + "();")); String writeMethod = null; String writeCall = ""; @@ -117,15 +145,15 @@ if (writeMethod != null) { writeCall = writeMethod + "( " + jparms[i].getName() + " )"; } else { - writeCall = "writeObject( " + vtc.getValueTypeVarName(jc, jparms[i]) + ", " + jparms[i].getName() + ")"; + writeCall = "writeObject( " + _vtc.getValueTypeVarName(jc, jparms[i]) + ", " + jparms[i].getName() + ")"; } jbs.addStatement(new JCodeStatement(jlvOutput.getName() + "." + writeCall + ";")); } - jbs.addStatement(new JCodeStatement(jlvConn.getName() + ".invoke(this, \"" + iiopMethodName + "\", " + jlvKey.getName() + ", $retry);")); + jbs.addStatement(new JCodeStatement(jlvConn.getName() + ".invoke(this, \"" + name + "\", " + jlvKey.getName() + ", $retry);")); if (jlvRc != null) { - jbs.addStatement(new JCodeStatement(jlvInput.getName() + " = " + jlvConn.getName() + "." + inStreamName + "();")); + jbs.addStatement(new JCodeStatement(jlvInput.getName() + " = " + jlvConn.getName() + "." + _inStreamName + "();")); } jbs.addStatement(new JCodeStatement(jlvConn.getName() + ".forget(" + jlvKey.getName() + ");")); jbs.addStatement(new JCodeStatement(jlvConn.getName() + ".close();")); @@ -143,7 +171,7 @@ if (readMethod != null) { readCall = jlvInput.getName() + "." + readMethod + "()"; } else { - readCall = "(" + jlvRc.getTypeDecl() + ")" + jlvInput.getName() + "." + "readObject( " + vtc.getValueTypeVarName(jc, jlvRc) + ")"; + readCall = "(" + jlvRc.getTypeDecl() + ")" + jlvInput.getName() + "." + "readObject( " + _vtc.getValueTypeVarName(jc, jlvRc) + ")"; } jbs.addStatement(new JCodeStatement(jlvRc.getName() + " = " + readCall + ";")); @@ -163,167 +191,200 @@ jfs.addStatement(tcfs); } - protected void addMethod_is_a(JClass jc) { + // public methods + + public void addMethod_is_a(JClass jc) { JParameter jpID = new JParameter(java.lang.String.class, "id"); - addMethod(jc, "_is_a", new JReturnType(boolean.class), + addMethod(jc, new JReturnType(boolean.class), "_is_a", new JParameter[]{jpID}, (Class[]) null); } - protected void addMethod(MethodOverload mo, JClass jc) { - Method m = mo.method; - + public void addMethod(Method m, JClass jc) { String name = m.getName(); JParameter[] sparms = getMethodParms(m); - addMethod(jc, mo.iiop_name, new JReturnType(m.getReturnType()), + addMethod(jc, new JReturnType(m.getReturnType()), name, sparms, m.getExceptionTypes()); } - public void generate() throws GenException { - GenOptions go = getGenOptions(); - List interfaces = go.getInterfaces(); - Iterator intf = null; - - if (interfaces != null) { - intf = interfaces.iterator(); + protected Method[] getMethods() { + Method myMethods[] = _riClass.getDeclaredMethods(); + Method myOpsMethods[] = null; + Class myInterfaces[] = _riClass.getInterfaces(); + + if (myInterfaces != null && myInterfaces.length > 0) { + String opsName = _riClass.getName() + "Operations"; + + for (int i = 0; i < myInterfaces.length; i++) { + if (myInterfaces[i].getName().equals(opsName)) { + myOpsMethods = myInterfaces[i].getDeclaredMethods(); + break; + } + } } - JavaGenerator jg = new JavaGenerator(genOptions); + Method m[] = null; - if (go.isSimpleIdl()) { - inStreamName = "getSimpleInputStream"; - outStreamName = "getSimpleOutputStream"; + if (myOpsMethods == null) { + m = myMethods; } else { - inStreamName = "getInputStream"; - outStreamName = "getOutputStream"; + m = new Method[myMethods.length + myOpsMethods.length]; + System.arraycopy(myMethods, 0, m, 0, myMethods.length); + System.arraycopy(myOpsMethods, 0, m, myMethods.length, myOpsMethods.length); } - String riClassName = ""; - Class riClass = null; - String stubClassName = ""; - JPackage pkg = null; + return m; + } - while (intf != null && intf.hasNext() ) { - // Clear the value type cache. - vtc.clear(); + public void generate() + throws Exception { + _vtc.clear(); - riClassName = (String)intf.next(); - stubClassName = JavaClass.addPackageSuffix(riClassName, "iiop_stubs") + "_Stub"; + if (_simpleIDL) { + _inStreamName = "getSimpleInputStream"; + _outStreamName = "getSimpleOutputStream"; + } - try { - riClass = getClassLoader().loadClass( riClassName ); - } catch (Exception ex) { - throw new GenException( "Generate Stubs Failed:", ex ); - } + JavaGenerator jg = new JavaGenerator(_genOptions); - String pkgName = JavaClass.getNamePrefix(stubClassName); - pkg = (JPackage) packages.get( pkgName ); - if (pkg == null) - { - pkg = new JPackage( pkgName ); - packages.put( pkgName, pkg ); - } + String className; + JPackage p = new JPackage(JavaClass.getNamePrefix(_stubClassName)); + className = JavaClass.getNameSuffix(_stubClassName); - String className = JavaClass.getNameSuffix(stubClassName); - JClass jc = pkg.newClass(className); - jc.addImport("org.apache.geronimo.interop.rmi.iiop", "ObjectRef"); - jc.setExtends("ObjectRef"); - jc.addImplements(riClass.getName()); - - JConstructor jcCon = jc.newConstructor((JParameter[]) null, (Class[]) null); - jcCon.addStatement(new JCodeStatement("super();")); - - addMethod_is_a(jc); - - Method m[] = getMethods( riClass, go ); - MethodOverload mo[] = null; - mo = getMethodOverloads( m ); - for (int i = 0; mo != null && i < mo.length; i++) { - addMethod( mo[i], jc ); - } - } + JClass jc = p.newClass(className); + jc.addImport("org.apache.geronimo.interop.rmi.iiop", "ObjectRef"); + jc.setExtends("ObjectRef"); + jc.addImplements(_riClass.getName()); - Set pkgSet = packages.keySet(); - Iterator pkgIt = pkgSet.iterator(); - String stubPkg = ""; - - while (pkgIt.hasNext()) - { - stubPkg = (String) pkgIt.next(); - pkg = (JPackage)packages.get(stubPkg); - System.out.println("Generating Package: " + stubPkg); - jg.generate(pkg); - } - } + JField idsField = jc.newField(String[].class, "_ids", new JExpression(new JCodeStatement("{ \"" + _riClass.getName() + "\", \"RMI:" + _riClass.getName() + ":0000000000000000\"}")), true); - public void compile() - throws Exception { + JConstructor jcCon = jc.newConstructor((JParameter[]) null, (Class[]) null); + jcCon.addStatement(new JCodeStatement("super();")); - Set pkg = packages.keySet(); - Iterator pkgIt = pkg.iterator(); - String stubPkg = ""; + addMethod_is_a(jc); - /* - * Each of the packages were generated under go.getGenSrcDir(). - * - * Go through all the packages and run the compiler on *.java - */ + Method m[] = null; + m = getMethods(); + for (int i = 0; m != null && i < m.length; i++) { + addMethod(m[i], jc); + } - GenOptions go = getGenOptions(); - String classpath = adjustPath(go.getClasspath()); - String srcpath = adjustPath(go.getGenSrcDir()); + jg.generate(p); + } - String filesToCompile = ""; - String javacCmd = ""; + public void compile() + throws Exception { + String className = _riClass.getName(); + String stubClassName = JavaClass.addPackageSuffix(className, "iiop_stubs"); + String stubPackage = JavaClass.getNamePrefix(stubClassName); + + System.out.println("Compiling Package: " + stubPackage); + System.out.println("Compiling Stub: " + stubClassName); + + String javac = "javac -d ../classes -classpath ../classes;D:/Dev/3rdparty.jag/ejb21/ejb-2_1-api.jar " + stubPackage.replace('.', '/') + "/*.java"; + + ProcessUtil pu = ProcessUtil.getInstance(); + pu.setEcho(System.out); + pu.run(javac, (String[]) null, "./src"); + } - while (pkgIt.hasNext()) - { - stubPkg = (String) pkgIt.next(); - stubPkg = stubPkg.replace( '.', File.separatorChar ); - filesToCompile = adjustPath(go.getGenSrcDir() + File.separator + stubPkg + File.separator + "*.java"); + public Class getStubClass() { + System.out.println("StubCompiler.getStubClass(): riClass: " + _riClass); - System.out.println("Compiling Package: " + filesToCompile); + if (_stubClass == null) { + try { + _stubClass = Class.forName(_stubClassName); + } catch (Exception e) { + e.printStackTrace(); + } - javacCmd = "javac -d " + go.getGenClassDir() + - ( go.isCompileDebug() ? " -g" : "" ) + - " -classpath " + classpath + " " + - " -sourcepath " + srcpath + " " + filesToCompile; + try { + if (_stubClass == null) { + generate(); + compile(); + _stubClass = Class.forName(_stubClassName); + } + } catch (Exception e) { + e.printStackTrace(); + } + } - System.out.println( "Lauching: " + javacCmd ); + return _stubClass; + } - ProcessUtil pu = ProcessUtil.getInstance(); - pu.setEcho(System.out); - pu.run(javacCmd, (String[]) null, "./" ); + public static void main(String args[]) + throws Exception { + boolean generate = false; + boolean compile = false; + boolean loadclass = false; + boolean simpleidl = false; + List interfaces = new LinkedList(); + GenOptions go = new GenOptions(); + + go.setGenDir("./"); + go.setOverwrite(false); + go.setVerbose(false); + + for (int i = 0; i < args.length; i++) { + if (args[i].equals("-g")) { + generate = true; + } else if (args[i].equals("-c")) { + compile = true; + } else if (args[i].equals("-l")) { + loadclass = true; + } else if (args[i].equals("-s")) { + simpleidl = true; + } else if (args[i].equals("-d") && ((i + 1) < args.length)) { + go.setGenDir(args[++i]); + } else if (args[i].equals("-v")) { + go.setVerbose(true); + } else if (args[i].equals("-o")) { + go.setOverwrite(true); + } else if (args[i].startsWith("-")) { + System.out.println("Warning: Ignoring unrecognized options: '" + args[i] + "'"); + } else { + interfaces.add(args[i]); + } } - } - public static void main(String args[]) throws Exception { - GenOptions go = null; + Iterator i = interfaces.iterator(); + while (i != null && i.hasNext()) { + String intfName = (String) i.next(); + + if (intfName.startsWith("RMI:")) { + simpleidl = false; + intfName = intfName.substring(4); + } else if (intfName.startsWith("IDL:")) { + simpleidl = true; + intfName = intfName.substring(4); + } - try - { - go = new GenOptions( "./stubs", args ); - } - catch( GenWarning gw ) - { - gw.printStackTrace(); - } + Class riClass = Class.forName(intfName); + StubCompiler sg = new StubCompiler(riClass, go); + sg.setSimpleIDL(simpleidl); - ClassLoader cl = ClassLoader.getSystemClassLoader(); - StubCompiler sg = new StubCompiler( go, cl ); + if (generate) { + sg.generate(); + } - if (go.isGenerate()) { - sg.generate(); - } + if (compile) { + sg.compile(); + } - if (go.isCompile()) { - sg.compile(); + if (loadclass) { + Class c = sg.getStubClass(); + System.out.println("StubClass: " + c); + } } + + // sg.setSimpleIDL( true ); + // sg.generate( "org.apache.geronimo.interop.rmi.iiop.NameServiceOperations"); } + } Modified: geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubFactory.java URL: http://svn.apache.org/viewcvs/geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubFactory.java?view=diff&r1=158861&r2=158862 ============================================================================== --- geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubFactory.java (original) +++ geronimo/trunk/modules/interop/src/java/org/apache/geronimo/interop/rmi/iiop/compiler/StubFactory.java Wed Mar 23 18:30:30 2005 @@ -24,29 +24,34 @@ import org.apache.geronimo.interop.util.JavaClass; import org.apache.geronimo.interop.util.ThreadContext; + public class StubFactory { - protected static StubFactory sf; + protected static StubFactory _sf; protected StubFactory() { } public static StubFactory getInstance() { - if (sf == null) { + if (_sf == null) { synchronized (StubFactory.class) { - if (sf == null) { - sf = new StubFactory(); - sf.init(); + if (_sf == null) { + _sf = new StubFactory(); + _sf.init(); } } } - return sf; + return _sf; } - private static HashMap stubClassMap; + // private data + + private static HashMap _stubClassMap; + + // internal methods protected void init() { - stubClassMap = new HashMap(); + _stubClassMap = new HashMap(); } protected Class loadStub(Class remoteInterface) { @@ -71,9 +76,9 @@ // Try generating stub class now. // - //StubCompiler stubCompiler = new StubCompiler(remoteInterface); - //System.out.println("StubFactory.loadStub(): stubCompiler: " + stubCompiler); - //sc = stubCompiler.getStubClass(); + StubCompiler stubCompiler = new StubCompiler(remoteInterface); + System.out.println("StubFactory.loadStub(): stubCompiler: " + stubCompiler); + sc = stubCompiler.getStubClass(); System.out.println("StubFactory.loadStub(): sc: " + sc); } @@ -94,18 +99,20 @@ return sc; } + // public methods + public ObjectRef getStub(Class remoteInterface) { System.out.println("StubFactory.getStub(): remoteInterface: " + remoteInterface); try { - Class sc = (Class) stubClassMap.get(remoteInterface); + Class sc = (Class) _stubClassMap.get(remoteInterface); System.out.println("StubFactory.getStub(): sc: " + sc); if (sc == null) { - synchronized (stubClassMap) { - sc = (Class) stubClassMap.get(remoteInterface); + synchronized (_stubClassMap) { + sc = (Class) _stubClassMap.get(remoteInterface); if (sc == null) { sc = loadStub(remoteInterface); System.out.println("StubFactory.getStub(): sc: " + sc); - stubClassMap.put(remoteInterface, sc); + _stubClassMap.put(remoteInterface, sc); } } }