dain        2005/06/22 20:56:43

  Modified:    modules/core/src/java/org/openejb/corba/compiler
                        PortableStubCompiler.java
  Removed:     modules/core/src/java/org/openejb/corba/compiler
                        PortableStubGenerator.java
  Log:

  Removed PortableStubGenerator since it is totally broken.  We now use 
DynamicStubClassLoader instead.
  Added StubDelegate to handle assigning ClientDelegate objects to new stubs
  Removed ClienContextHolder since it is no longer used
  Fixed several marshalling errors in the StubMethodInterceptor
  
  Revision  Changes    Path
  1.3       +2 -397    
openejb/modules/core/src/java/org/openejb/corba/compiler/PortableStubCompiler.java
  
  Index: PortableStubCompiler.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/corba/compiler/PortableStubCompiler.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- PortableStubCompiler.java 21 Jun 2005 08:40:45 -0000      1.2
  +++ PortableStubCompiler.java 23 Jun 2005 00:56:43 -0000      1.3
  @@ -44,110 +44,21 @@
    */
   package org.openejb.corba.compiler;
   
  -import java.io.Serializable;
   import java.lang.reflect.Method;
  -import java.lang.reflect.Modifier;
  -import java.rmi.RemoteException;
  -import java.rmi.Remote;
   import java.util.ArrayList;
   import java.util.Arrays;
   import java.util.HashMap;
   import java.util.HashSet;
   import java.util.Iterator;
  +import java.util.LinkedHashSet;
   import java.util.LinkedList;
   import java.util.List;
   import java.util.Map;
   import java.util.Set;
  -import java.util.LinkedHashSet;
   
  -import org.apache.geronimo.interop.generator.GenException;
  -import org.apache.geronimo.interop.generator.GenOptions;
  -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.JExpression;
  -import org.apache.geronimo.interop.generator.JField;
  -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.JType;
  -import org.apache.geronimo.interop.generator.JVariable;
  -import org.apache.geronimo.interop.generator.JavaGenerator;
  -import org.omg.CORBA.SystemException;
  -import org.omg.CORBA.portable.ApplicationException;
   import org.omg.CORBA.portable.IDLEntity;
  -import org.omg.CORBA.portable.RemarshalException;
   
   public class PortableStubCompiler {
  -    private HashMap packages = new HashMap();
  -    private final GenOptions genOptions;
  -    private final ClassLoader classLoader;
  -
  -    public PortableStubCompiler(GenOptions genOptions, ClassLoader 
classLoader) {
  -        this.genOptions = genOptions;
  -        this.classLoader = classLoader;
  -    }
  -
  -    public void generate() throws GenException {
  -        JavaGenerator javaGenerator = new JavaGenerator(genOptions);
  -
  -        List interfaces = genOptions.getInterfaces();
  -        for (Iterator iterator = interfaces.iterator(); iterator.hasNext();) 
{
  -            String interfaceName = (String) iterator.next();
  -
  -            // load the interface class
  -            Class interfaceClass = null;
  -            try {
  -                interfaceClass = classLoader.loadClass(interfaceName);
  -            } catch (Exception ex) {
  -                throw new GenException("Generate Stubs Failed:", ex);
  -            }
  -
  -            // get the package object
  -            String packageName = getPackageName(interfaceName);
  -            if (packageName.length() > 0) {
  -                packageName = "org.omg.stub." + packageName;
  -            } else {
  -                packageName = "org.omg.stub";
  -            }
  -            JPackage jpackage = (JPackage) packages.get(packageName);
  -            if (jpackage == null) {
  -                jpackage = new JPackage(packageName);
  -                packages.put(packageName, jpackage);
  -            }
  -
  -            // build the basic class object
  -            String className = "_" + getClassName(interfaceClass) + "_Stub";
  -            JClass jclass = jpackage.newClass(className);
  -            jclass.addImport("javax.rmi.CORBA", "Stub");
  -            jclass.setExtends("Stub");
  -            jclass.addImplements(interfaceClass.getName());
  -            jclass.addImplements("org.openejb.corba.ClientContextHolder");
  -
  -            addClientContextMethods(jclass);
  -            addIdsMethod(jclass, interfaceClass);
  -
  -            IiopOperation[] iiopOperations = 
createIiopOperations(interfaceClass);
  -            for (int i = 0; iiopOperations != null && i < 
iiopOperations.length; i++) {
  -                addMethod(iiopOperations[i], jclass);
  -            }
  -        }
  -
  -        for (Iterator iterator = packages.entrySet().iterator(); 
iterator.hasNext();) {
  -            Map.Entry entry = (Map.Entry) iterator.next();
  -            String packageName = (String) entry.getKey();
  -            JPackage jpackage = (JPackage) entry.getValue();
  -
  -            System.out.println("Generating Package: " + packageName);
  -            javaGenerator.generate(jpackage);
  -        }
  -    }
  -
       private static String getClassName(Class type) {
           if (type.isArray()) {
               throw new IllegalArgumentException("type is an array: " + type);
  @@ -370,303 +281,12 @@
           return className.toString();
       }
   
  -    private void addMethod(JClass jclass, String iiopMethodName, JReturnType 
jreturnType, String name, JParameter[] jparameters, Class[] exceptions) {
  -        //
  -        // Method Template:
  -        //
  -        // if (!Util.isLocal(this)) {
  -        //     org.openejb.corba.ClientContext saved = 
org.openejb.corba.ClientContextManager.getClientContext();
  -        //     try {
  -        //         org.omg.CORBA_2_3.portable.InputStream in = null;
  -        //         try {
  -        //             org.omg.CORBA_2_3.portable.OutputStream out =
  -        //                 (org.omg.CORBA_2_3.portable.OutputStream)
  -        //                 
_request("passAndReturnCheese__org_apache_geronimo_interop_rmi_iiop_compiler_Cheese",
 true);
  -        //             out.write_value((Serializable)arg0,Cheese.class);
  -        //             in = 
(org.omg.CORBA_2_3.portable.InputStream)_invoke(out);
  -        //
  -        //             Object result = 
javax.rmi.PortableRemoteObject.narrow( _input.read_Object(), Cheese.class );
  -        //             if (result instanceof 
org.openejb.corba.ClientContextHolder) {
  -        //                 
((org.openejb.corba.ClientContextHolder)result).setClientContext(_context);
  -        //             }
  -        //             return (Cheese) result;
  -        //         } catch (ApplicationException ex) {
  -        //             in = (org.omg.CORBA_2_3.portable.InputStream) 
ex.getInputStream();
  -        //             String id = in.read_string();
  -        //             throw new UnexpectedException(id);
  -        //         } catch (RemarshalException ex) {
  -        //             return passAndReturnCheese(arg0);
  -        //         } finally {
  -        //             _releaseReply(in);
  -        //         }
  -        //     } catch (SystemException ex) {
  -        //         throw Util.mapSystemException(ex);
  -        //     }
  -        // } else {
  -        //     ServantObject so = 
_servant_preinvoke("passAndReturnCheese__org_apache_geronimo_interop_rmi_iiop_compiler_Cheese",Foo.class);
  -        //     if (so == null) {
  -        //         return passAndReturnCheese(arg0);
  -        //     }
  -        //     try {
  -        //         Cheese arg0Copy = (Cheese) Util.copyObject(arg0,_orb());
  -        //         Cheese result = 
((Foo)so.servant).passAndReturnCheese(arg0Copy);
  -        //         return (Cheese)Util.copyObject(result,_orb());
  -        //     } catch (Throwable ex) {
  -        //         Throwable exCopy = (Throwable)Util.copyObject(ex,_orb());
  -        //         throw Util.wrapException(exCopy);
  -        //     } finally {
  -        //         _servant_postinvoke(so);
  -        //     }
  -        // }
  -
  -        JMethod jmethod = jclass.newMethod(jreturnType, name, jparameters, 
exceptions);
  -
  -        JVariable saved = 
jmethod.newLocalVariable(org.openejb.corba.ClientContext.class, "saved", new 
JExpression(new JCodeStatement(
  -                
"org.openejb.corba.ClientContextManager.getClientContext()")));
  -
  -
  -        JTryCatchFinallyStatement outerTryCatchFinally = new 
JTryCatchFinallyStatement();
  -        jmethod.addStatement(outerTryCatchFinally);
  -        JTryStatement outerTry = outerTryCatchFinally.getTryStatement();
  -
  -        JLocalVariable inVar = 
outerTry.newLocalVariable(org.omg.CORBA_2_3.portable.InputStream.class,
  -                "in",
  -                new JExpression(new JCodeStatement("null")));
  -
  -        outerTry.addStatement(new 
JCodeStatement("org.openejb.corba.ClientContextManager.setClientContext(_context);"));
  -
  -        JTryCatchFinallyStatement innterTryCatchFinally = new 
JTryCatchFinallyStatement();
  -        outerTry.addStatement(innterTryCatchFinally);
  -        JTryStatement innerTry = innterTryCatchFinally.getTryStatement();
  -
  -        JLocalVariable outVar = 
innerTry.newLocalVariable(org.omg.CORBA_2_3.portable.OutputStream.class,
  -                "out",
  -                new JExpression(new JCodeStatement("(" + 
org.omg.CORBA_2_3.portable.OutputStream.class.getName() + ") _request(\"" + 
iiopMethodName + "\", true)")));
  -
  -        // Write the variables
  -        for (int i = 0; i < jparameters.length; i++) {
  -            JParameter jparameter = jparameters[i];
  -
  -            String writeMethod = getWriteMethod(jparameter);
  -            String writeCall;
  -            if (writeMethod != null) {
  -                writeCall = writeMethod + "( " + jparameter.getName() + " )";
  -            } else {
  -                String cast = "";
  -                if 
(!Serializable.class.isAssignableFrom(jparameter.getType())) {
  -                    cast = "(java.io.Serializable)";
  -                }
  -                writeCall = "write_value(" + cast + jparameter.getName() + 
", " + jparameter.getTypeDecl() + ".class)";
  -            }
  -
  -            innerTry.addStatement(new JCodeStatement(outVar.getName() + "." 
+ writeCall + ";"));
  -        }
  -
  -        // invoke the method
  -        String invoke = "_invoke(" + outVar.getName() + ");";
  -        if (jreturnType.getType() != Void.TYPE) {
  -            invoke = inVar.getName() + " = (" + inVar.getTypeDecl() + ")" + 
invoke;
  -        }
  -        innerTry.addStatement(new JCodeStatement(invoke));
  -
  -        // read the return value
  -        if (jreturnType.getType() != Void.TYPE) {
  -            String readMethod = getReadMethod(jreturnType);
  -            if (readMethod != null) {
  -                String readCall = inVar.getName() + "." + readMethod + "()";
  -                innerTry.addStatement(new JCodeStatement("return " + 
readCall + ";"));
  -            } else {
  -                // Object result = javax.rmi.PortableRemoteObject.narrow( 
_input.read_Object(), Cheese.class );
  -                // if (result instanceof 
org.openejb.corba.ClientContextHolder) {
  -                //     
((org.openejb.corba.ClientContextHolder)result).setClientContext(_context);
  -                // }
  -                // return (Cheese) result;
  -//                String readCall = "(" + jreturnType.getTypeDecl() + ")" + 
inVar.getName() + ".read_value( " + jreturnType.getTypeDecl() + ".class)";
  -
  -                // can not use a local variable here since the java 
generator will reorder the fields
  -                String resultName = "result";
  -                innerTry.addStatement(new JCodeStatement("Object " + 
resultName + " = " +
  -                        "javax.rmi.PortableRemoteObject.narrow(" + 
inVar.getName() + ".read_Object(), " + jreturnType.getTypeDecl() + ".class);"
  -                ));
  -
  -                JIfStatement jif = new JIfStatement(new JExpression(new 
JCodeStatement(resultName + " instanceof 
org.openejb.corba.ClientContextHolder")));
  -                jif.addStatement(new 
JCodeStatement("((org.openejb.corba.ClientContextHolder)" + resultName + 
").setClientContext(_context);"));
  -                innerTry.addStatement(jif);
  -
  -                innerTry.addStatement(new JCodeStatement("return (" + 
jreturnType.getTypeDecl() + ") " + resultName + ";"));
  -            }
  -        }
  -
  -        JVariable exVar = new JVariable(ApplicationException.class, "ex");
  -        JCatchStatement jcatchStatement = 
innterTryCatchFinally.newCatch(exVar);
  -
  -        jcatchStatement.addStatement(new JCodeStatement(inVar.getName() + " 
= (" + inVar.getTypeDecl() + ") " + exVar.getName() + ".getInputStream();"));
  -        JLocalVariable idVar = jcatchStatement.newLocalVariable(String.class,
  -                "id",
  -                new JExpression(new JCodeStatement(inVar.getName() + 
".read_string()")));
  -//        if 
(id.equals("IDL:org/apache/geronimo/interop/rmi/iiop/compiler/other/BlahEx:1.0"))
 {
  -//            throw (BlahException) in.read_value(BlahException.class);
  -//        }
  -//        if 
(id.equals("IDL:org/apache/geronimo/interop/rmi/iiop/compiler/BooEx:1.0")) {
  -//            throw (BooException) in.read_value(BooException.class);
  -//        }
  -        for (int i = 0; i < exceptions.length; i++) {
  -            Class exception = exceptions[i];
  -            if (RemoteException.class.isAssignableFrom(exception) ||
  -                    RuntimeException.class.isAssignableFrom(exception) ) {
  -                continue;
  -            }
  -            String exceptionName = exception.getName().replace('.', '/');
  -            if (exceptionName.endsWith("Exception")) {
  -                exceptionName = exceptionName.substring(0, 
exceptionName.length() - "Exception".length());
  -            }
  -            exceptionName += "Ex";
  -            JIfStatement jif = new JIfStatement(new JExpression(new 
JCodeStatement(idVar.getName() + ".equals(\"IDL:" + exceptionName + 
":1.0\")")));
  -            jif.addStatement(new JCodeStatement("throw (" + 
exception.getName() + ") in.read_value(" + exception.getName() + ".class);"));
  -            jcatchStatement.addStatement(jif);
  -
  -        }
  -        jcatchStatement.addStatement(new JCodeStatement("throw new 
java.rmi.UnexpectedException(" + idVar.getName() + ");"));
  -
  -        //         } catch (RemarshalException ex) {
  -        //             return passAndReturnCheese(arg0);
  -        exVar = new JVariable(RemarshalException.class, "ex");
  -        jcatchStatement = innterTryCatchFinally.newCatch(exVar);
  -
  -        String remarshal = name + "(";
  -        for (int i = 0; i < jparameters.length; i++) {
  -            JParameter jparameter = jparameters[i];
  -            if (i > 0) {
  -                remarshal += ", ";
  -            }
  -            remarshal += jparameter.getName();
  -        }
  -        remarshal += ");";
  -        if (jreturnType.getType() != Void.TYPE) {
  -            remarshal = "return " + remarshal;
  -        }
  -        jcatchStatement.addStatement(new JCodeStatement(remarshal));
  -
  -        //         } finally {
  -        //             _releaseReply(in);
  -        innterTryCatchFinally.addFinallyStatement(new 
JCodeStatement("org.openejb.corba.ClientContextManager.setClientContext(" + 
saved.getName() + ");"));
  -        innterTryCatchFinally.addFinallyStatement(new 
JCodeStatement("_releaseReply(" + inVar.getName() + ");"));
  -
  -        //     } catch (SystemException ex) {
  -        //         throw Util.mapSystemException(ex);
  -        exVar = new JVariable(SystemException.class, "ex");
  -        jcatchStatement = outerTryCatchFinally.newCatch(exVar);
  -        jcatchStatement.addStatement(new JCodeStatement("throw 
javax.rmi.CORBA.Util.mapSystemException(" + exVar.getName() + ");"));
  -    }
  -
  -    private void addClientContextMethods(JClass jclass) {
  -        //
  -        // Method Template:
  -        //
  -        // private org.openejb.corba.ClientContext _context;
  -        //
  -        // public org.openejb.corba.ClientContext getClientContext() {
  -        //     return _context;
  -        // }
  -        //
  -        // public void setClientContext(org.openejb.corba.ClientContext 
context) {
  -        //     _context = context;
  -        // }
  -
  -        JField typesField = 
jclass.newField(org.openejb.corba.ClientContext.class, "_context");
  -        typesField.setModifiers(Modifier.PRIVATE);
  -
  -        JMethod getterMethod = jclass.newMethod(new 
JReturnType(org.openejb.corba.ClientContext.class), "getClientContext", null, 
null);
  -        getterMethod.addStatement(new JCodeStatement("return " + 
typesField.getName() + ";"));
  -
  -        JParameter jparameter = new 
JParameter(org.openejb.corba.ClientContext.class, typesField.getName());
  -        JMethod setterMethod = jclass.newMethod(new JReturnType(Void.TYPE), 
"setClientContext", new JParameter[] {jparameter}, null);
  -        setterMethod.addStatement(new JCodeStatement("this." + 
typesField.getName() + " = " + jparameter.getName() +  ";"));
  -    }
  -
  -    private void addIdsMethod(JClass jclass, Class iface) {
  -        //
  -        // Method Template:
  -        //
  -        // private static final String[] _type_ids = {
  -        //     "RMI:org.openejb.corba.compiler.Foo:0000000000000000"
  -        //     "RMI:org.openejb.corba.compiler.All:0000000000000000"
  -        // };
  -        //
  -        // public String[] getIds()
  -        // {
  -        //     return _type_ids;
  -        // }
  -        //
  -
  -        String ids = "";
  -        for (Iterator iterator = getAllInterfaces(iface).iterator(); 
iterator.hasNext();) {
  -            Class superInterface = (Class) iterator.next();
  -            if (Remote.class.isAssignableFrom(superInterface) && 
superInterface != Remote.class) {
  -                if (ids.length() > 0) {
  -                    ids += ", ";
  -                }
  -                ids += "\"RMI:" + superInterface.getName() + 
":0000000000000000\"";
  -            }
  -        }
  -        ids = "{ " + ids + " }";
  -
  -        JField typesField = jclass.newField(String[].class, "_type_ids", new 
JExpression(new JCodeStatement(ids)));
  -        typesField.setModifiers(Modifier.PRIVATE | Modifier.STATIC | 
Modifier.FINAL);
  -
  -        JMethod jmethod = jclass.newMethod(new JReturnType(String[].class), 
"_ids", null, null);
  -        jmethod.addStatement(new JCodeStatement("return _type_ids;"));
  -    }
  -
  -
  -    private void addMethod(IiopOperation iiopOperation, JClass jclass) {
  -        Method method = iiopOperation.getMethod();
  -
  -        JReturnType jreturnType = new JReturnType(method.getReturnType());
   
  -        Class[] parameterTypes = method.getParameterTypes();
  -        JParameter[] jparameters = new JParameter[parameterTypes.length];
  -        for (int i = 0; i < parameterTypes.length; i++) {
  -            jparameters[i] = new JParameter(parameterTypes[i], "arg" + i);
  -        }
  -
  -        addMethod(jclass,
  -                iiopOperation.getName(),
  -                jreturnType,
  -                method.getName(),
  -                jparameters,
  -                method.getExceptionTypes());
  -    }
  -
  -    private static final Map readMethods;
  -    private static final Map writeMethods;
       private static final Map specialTypeNames;
       private static final Map specialTypePackages;
       private static final Set keywords;
   
       static {
  -        readMethods = new HashMap();
  -        readMethods.put("boolean", "read_boolean");
  -        readMethods.put("char", "read_wchar");
  -        readMethods.put("byte", "read_octet");
  -        readMethods.put("short", "read_short");
  -        readMethods.put("int", "read_long");
  -        readMethods.put("long", "read_longlong");
  -        readMethods.put("float", "read_float");
  -        readMethods.put("double", "read_double");
  -        readMethods.put("org.omg.CORBA.Object", "read_Object");
  -
  -        writeMethods = new HashMap();
  -        writeMethods.put("boolean", "write_boolean");
  -        writeMethods.put("char", "write_wchar");
  -        writeMethods.put("byte", "write_octet");
  -        writeMethods.put("short", "write_short");
  -        writeMethods.put("int", "write_long");
  -        writeMethods.put("long", "write_longlong");
  -        writeMethods.put("float", "write_float");
  -        writeMethods.put("double", "write_double");
  -        writeMethods.put("org.omg.CORBA.Object", "write_Object");
  -
           specialTypeNames = new HashMap();
           specialTypeNames.put("boolean", "boolean");
           specialTypeNames.put("char", "wchar");
  @@ -742,19 +362,4 @@
           keywords.add("wchar");
           keywords.add("wstring");
       }
  -
  -    protected String getWriteMethod(JVariable jvariable) {
  -        if (jvariable != null) {
  -            return (String) writeMethods.get(jvariable.getTypeDecl());
  -        }
  -        return null;
  -    }
  -
  -    protected String getReadMethod(JType jtype) {
  -        if (jtype != null) {
  -            return (String) readMethods.get(jtype.getTypeDecl());
  -        }
  -        return null;
  -    }
  -
   }
  
  
  

Reply via email to