maguro      2005/03/02 00:23:10

  Modified:    modules/core/src/java/org/openejb/corba Adapter.java
                        AdapterEntity.java AdapterStateful.java
                        AdapterStateless.java
  Added:       modules/core/src/java/org/openejb/corba AdapterDelegate.java
                        AdapterProxyFactory.java
  Log:

  Added CGLIB enhancer to catch EJB exceptions and map them into CORBA 
exceptions.
  
  Revision  Changes    Path
  1.4       +8 -5      
openejb/modules/core/src/java/org/openejb/corba/Adapter.java
  
  Index: Adapter.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/corba/Adapter.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Adapter.java      18 Jan 2005 21:50:39 -0000      1.3
  +++ Adapter.java      2 Mar 2005 05:23:10 -0000       1.4
  @@ -44,6 +44,7 @@
    */
   package org.openejb.corba;
   
  +import java.rmi.Remote;
   import javax.rmi.CORBA.Tie;
   
   import org.omg.CORBA.ORB;
  @@ -66,6 +67,7 @@
    * @version $Revision$ $Date$
    */
   public abstract class Adapter implements RefGenerator {
  +
       private final EJBContainer container;
       private final POA parentPOA;
       private final NamingContextExt initialContext;
  @@ -79,14 +81,16 @@
           this.tieLoader = tieLoader;
           this.home_id = container.getContainerID().toString().getBytes();
   
  -
           try {
               org.omg.CORBA.Object obj = 
orb.resolve_initial_references("NameService");
               initialContext = NamingContextExtHelper.narrow(obj);
   
               Servant servant = 
tieLoader.loadTieClass(container.getProxyInfo().getHomeInterface(), 
container.getProxyInfo());
  +            AdapterProxyFactory factory = new 
AdapterProxyFactory(container.getProxyInfo().getHomeInterface(), 
container.getClassLoader());
  +            Remote remote = (Remote) 
factory.create(container.getEJBObject(null));
  +
               if (servant instanceof Tie) {
  -                ((Tie) servant).setTarget(container.getEJBHome());
  +                ((Tie) servant).setTarget(remote);
               }
               parentPOA.activate_object_with_id(home_id, servant);
               homeReference = parentPOA.servant_to_reference(servant);
  @@ -157,5 +161,4 @@
       public org.omg.CORBA.Object genHomeReference(ProxyInfo proxyInfo) throws 
CORBAException {
           return this.getHomeReference();
       }
  -
   }
  
  
  
  1.4       +9 -3      
openejb/modules/core/src/java/org/openejb/corba/AdapterEntity.java
  
  Index: AdapterEntity.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/corba/AdapterEntity.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AdapterEntity.java        19 Feb 2005 09:46:38 -0000      1.3
  +++ AdapterEntity.java        2 Mar 2005 05:23:10 -0000       1.4
  @@ -49,6 +49,7 @@
   import java.io.IOException;
   import java.io.ObjectInputStream;
   import java.io.ObjectOutputStream;
  +import java.rmi.Remote;
   import javax.rmi.CORBA.Tie;
   
   import org.apache.commons.logging.Log;
  @@ -81,6 +82,7 @@
   
       private final POA poa;
       private final String referenceInterface;
  +    private final AdapterProxyFactory factory;
   
       public AdapterEntity(EJBContainer container, ORB orb, POA parentPOA, 
TieLoader tieLoader) throws CORBAException {
           super(container, orb, parentPOA, tieLoader);
  @@ -100,6 +102,8 @@
   
               Servant servant = 
getTieLoader().loadTieClass(container.getProxyInfo().getRemoteInterface(), 
container.getProxyInfo());
               referenceInterface = servant._all_interfaces(null, null)[0];
  +
  +            factory = new 
AdapterProxyFactory(container.getProxyInfo().getRemoteInterface(), 
container.getClassLoader());
           } catch (Exception e) {
               throw new CORBAException(e);
           }
  @@ -145,8 +149,10 @@
   
                   EJBContainer container = getContainer();
                   Servant servant = 
getTieLoader().loadTieClass(container.getProxyInfo().getRemoteInterface(), 
container.getProxyInfo());
  +                Remote remote = (Remote) 
factory.create(container.getEJBObject(pk));
  +
                   if (servant instanceof Tie) {
  -                    ((Tie) servant).setTarget(container.getEJBObject(pk));
  +                    ((Tie) servant).setTarget(remote);
                   }
                   return servant;
               } catch (IOException e) {
  
  
  
  1.4       +9 -3      
openejb/modules/core/src/java/org/openejb/corba/AdapterStateful.java
  
  Index: AdapterStateful.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/corba/AdapterStateful.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AdapterStateful.java      19 Feb 2005 09:46:38 -0000      1.3
  +++ AdapterStateful.java      2 Mar 2005 05:23:10 -0000       1.4
  @@ -49,6 +49,7 @@
   import java.io.IOException;
   import java.io.ObjectInputStream;
   import java.io.ObjectOutputStream;
  +import java.rmi.Remote;
   import javax.rmi.CORBA.Tie;
   
   import org.apache.commons.logging.Log;
  @@ -81,6 +82,7 @@
   
       private final POA poa;
       private final String referenceInterface;
  +    private final AdapterProxyFactory factory;
   
       public AdapterStateful(EJBContainer container, ORB orb, POA parentPOA, 
TieLoader tieLoader) throws CORBAException {
           super(container, orb, parentPOA, tieLoader);
  @@ -100,6 +102,8 @@
   
               Servant servant = 
getTieLoader().loadTieClass(container.getProxyInfo().getRemoteInterface(), 
container.getProxyInfo());
               referenceInterface = servant._all_interfaces(null, null)[0];
  +
  +            factory = new 
AdapterProxyFactory(container.getProxyInfo().getRemoteInterface(), 
container.getClassLoader());
           } catch (Exception e) {
               throw new CORBAException(e);
           }
  @@ -145,8 +149,10 @@
   
                   EJBContainer container = getContainer();
                   Servant servant = 
getTieLoader().loadTieClass(container.getProxyInfo().getRemoteInterface(), 
container.getProxyInfo());
  +                Remote remote = (Remote) 
factory.create(container.getEJBObject(pk));
  +
                   if (servant instanceof Tie) {
  -                    ((Tie) servant).setTarget(container.getEJBObject(pk));
  +                    ((Tie) servant).setTarget(remote);
                   }
                   return servant;
               } catch (IOException e) {
  
  
  
  1.4       +7 -4      
openejb/modules/core/src/java/org/openejb/corba/AdapterStateless.java
  
  Index: AdapterStateless.java
  ===================================================================
  RCS file: 
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/corba/AdapterStateless.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AdapterStateless.java     19 Feb 2005 09:46:38 -0000      1.3
  +++ AdapterStateless.java     2 Mar 2005 05:23:10 -0000       1.4
  @@ -89,14 +89,17 @@
               poa.the_POAManager().activate();
   
               Servant servant = 
tieLoader.loadTieClass(container.getProxyInfo().getRemoteInterface(), 
container.getProxyInfo());
  -            Remote obj = container.getEJBObject(null);
  +            AdapterProxyFactory factory = new 
AdapterProxyFactory(container.getProxyInfo().getRemoteInterface(), 
container.getClassLoader());
  +            Remote remote = (Remote) 
factory.create(container.getEJBObject(null));
  +
               if (servant instanceof Tie) {
  -                ((Tie) servant).setTarget(obj);
  +                ((Tie) servant).setTarget(remote);
               }
   
               poa.activate_object_with_id(object_id = 
container.getContainerID().toString().getBytes(), servant);
               objectReference = poa.servant_to_reference(servant);
           } catch (Exception e) {
  +            e.printStackTrace();
               throw new CORBAException(e);
           }
       }
  
  
  
  1.1                  
openejb/modules/core/src/java/org/openejb/corba/AdapterDelegate.java
  
  Index: AdapterDelegate.java
  ===================================================================
  /**
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "OpenEJB" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of The OpenEJB Group.  For written permission,
   *    please contact [EMAIL PROTECTED]
   *
   * 4. Products derived from this Software may not be called "OpenEJB"
   *    nor may "OpenEJB" appear in their names without prior written
   *    permission of The OpenEJB Group. OpenEJB is a registered
   *    trademark of The OpenEJB Group.
   *
   * 5. Due credit should be given to the OpenEJB Project
   *    (http://openejb.sf.net/).
   *
   * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * Copyright 2005 (C) The OpenEJB Group. All Rights Reserved.
   *
   * $Id: AdapterDelegate.java,v 1.1 2005/03/02 05:23:10 maguro Exp $
   */
  package org.openejb.corba;
  
  import java.rmi.Remote;
  
  
  /**
   * @version $Revision: 1.1 $ $Date: 2005/03/02 05:23:10 $
   */
  public class AdapterDelegate {
  
      private final Remote delegate;
  
      public AdapterDelegate(Remote delegate) {
          this.delegate = delegate;
      }
  
      final public Remote getDelegate() {
          return delegate;
      }
  }
  
  
  
  1.1                  
openejb/modules/core/src/java/org/openejb/corba/AdapterProxyFactory.java
  
  Index: AdapterProxyFactory.java
  ===================================================================
  /**
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "OpenEJB" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of The OpenEJB Group.  For written permission,
   *    please contact [EMAIL PROTECTED]
   *
   * 4. Products derived from this Software may not be called "OpenEJB"
   *    nor may "OpenEJB" appear in their names without prior written
   *    permission of The OpenEJB Group. OpenEJB is a registered
   *    trademark of The OpenEJB Group.
   *
   * 5. Due credit should be given to the OpenEJB Project
   *    (http://openejb.sf.net/).
   *
   * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * Copyright 2005 (C) The OpenEJB Group. All Rights Reserved.
   *
   * $Id: AdapterProxyFactory.java,v 1.1 2005/03/02 05:23:10 maguro Exp $
   */
  package org.openejb.corba;
  
  import java.lang.reflect.Method;
  import java.rmi.AccessException;
  import java.rmi.MarshalException;
  import java.rmi.NoSuchObjectException;
  import java.rmi.Remote;
  import java.rmi.RemoteException;
  import javax.transaction.InvalidTransactionException;
  import javax.transaction.TransactionRequiredException;
  import javax.transaction.TransactionRolledbackException;
  
  import net.sf.cglib.proxy.Callback;
  import net.sf.cglib.proxy.Enhancer;
  import net.sf.cglib.proxy.MethodInterceptor;
  import net.sf.cglib.proxy.MethodProxy;
  import org.omg.CORBA.INVALID_TRANSACTION;
  import org.omg.CORBA.MARSHAL;
  import org.omg.CORBA.NO_PERMISSION;
  import org.omg.CORBA.OBJECT_NOT_EXIST;
  import org.omg.CORBA.TRANSACTION_REQUIRED;
  import org.omg.CORBA.TRANSACTION_ROLLEDBACK;
  import org.omg.CORBA.UNKNOWN;
  
  
  /**
   * @version $Revision: 1.1 $ $Date: 2005/03/02 05:23:10 $
   */
  public class AdapterProxyFactory {
  
      private final static AdapterMethodInterceptor interceptor = new 
AdapterMethodInterceptor();
      private final Enhancer enhancer;
  
      public AdapterProxyFactory(Class clientInterface) {
          this(clientInterface, clientInterface.getClassLoader());
      }
  
      public AdapterProxyFactory(Class clientInterface, ClassLoader 
classLoader) {
          this(new Class[]{clientInterface}, classLoader);
      }
  
  
      public AdapterProxyFactory(Class[] clientInterfaces, ClassLoader 
classLoader) {
          assert clientInterfaces != null;
          assert areAllInterfaces(clientInterfaces);
  
          enhancer = new Enhancer();
          enhancer.setClassLoader(classLoader);
          enhancer.setSuperclass(AdapterDelegate.class);
          enhancer.setInterfaces(clientInterfaces);
          enhancer.setCallbackType(MethodInterceptor.class);
          enhancer.setUseFactory(false);
      }
  
      private static boolean areAllInterfaces(Class[] clientInterfaces) {
          for (int i = 0; i < clientInterfaces.length; i++) {
              if (clientInterfaces[i] == null || 
!clientInterfaces[i].isInterface()) {
                  return false;
              }
          }
          return true;
      }
  
      public Object create(Remote delegate) {
          return create(new Class[]{Remote.class}, new Object[]{delegate});
      }
  
      public synchronized Object create(Class[] types, Object[] arguments) {
          enhancer.setCallbacks(new Callback[]{interceptor});
          return enhancer.create(types, arguments);
      }
  
      private final static class AdapterMethodInterceptor implements 
MethodInterceptor {
  
          public final Object intercept(Object o, Method method, Object[] args, 
MethodProxy methodProxy) throws Throwable {
              try {
                  return methodProxy.invoke(((AdapterDelegate) 
o).getDelegate(), args);
              } catch (TransactionRolledbackException e) {
                  throw new TRANSACTION_ROLLEDBACK(e.toString());
              } catch (TransactionRequiredException e) {
                  throw new TRANSACTION_REQUIRED(e.toString());
              } catch (InvalidTransactionException e) {
                  throw new INVALID_TRANSACTION(e.toString());
              } catch (NoSuchObjectException e) {
                  throw new OBJECT_NOT_EXIST(e.toString());
              } catch (AccessException e) {
                  throw new NO_PERMISSION(e.toString());
              } catch (MarshalException e) {
                  throw new MARSHAL(e.toString());
              } catch (RemoteException e) {
                  throw new UNKNOWN(e.toString());
              } catch (Throwable t) {
                  t.printStackTrace();
                  throw new UNKNOWN(t.toString());
              }
          }
      }
  }
  
  
  
  

Reply via email to