Java Source Code: org.openejb.ri.server.EjbHomeIntfcProcessor


   1: 
   2: /**
   3:  * Redistribution and use of this software and associated documentation
   4:  * ("Software"), with or without modification, are permitted provided
   5:  * that the following conditions are met:
   6:  *
   7:  * 1. Redistributions of source code must retain copyright
   8:  *    statements and notices.  Redistributions must also contain a
   9:  *    copy of this document.
  10:  *
  11:  * 2. Redistributions in binary form must reproduce the
  12:  *    above copyright notice, this list of conditions and the
  13:  *    following disclaimer in the documentation and/or other
  14:  *    materials provided with the distribution.
  15:  *
  16:  * 3. The name "Exolab" must not be used to endorse or promote
  17:  *    products derived from this Software without prior written
  18:  *    permission of Exoffice Technologies.  For written permission,
  19:  *    please contact info@exolab.org.
  20:  *
  21:  * 4. Products derived from this Software may not be called "Exolab"
  22:  *    nor may "Exolab" appear in their names without prior written
  23:  *    permission of Exoffice Technologies. Exolab is a registered
  24:  *    trademark of Exoffice Technologies.
  25:  *
  26:  * 5. Due credit should be given to the Exolab Project
  27:  *    (http://www.exolab.org/).
  28:  *
  29:  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
  30:  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
  31:  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  32:  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
  33:  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  34:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  35:  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  36:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  40:  * OF THE POSSIBILITY OF SUCH DAMAGE.
  41:  *
  42:  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
  43:  *
  44:  * $Id: EjbHomeIntfcProcessor.java,v 1.2 2002/02/25 20:30:54 dblevins Exp $
  45:  */
  46: 
  47: 
  48: package org.openejb.ri.server;
  49: 
  50: import java.io.FileInputStream;
  51: import java.io.ObjectInputStream;
  52: import java.io.ObjectOutputStream;
  53: import java.lang.reflect.Method;
  54: import java.net.ServerSocket;
  55: import java.net.Socket;
  56: import java.rmi.RemoteException;
  57: import java.util.HashMap;
  58: import java.util.Properties;
  59: import java.util.Vector;
  60: import javax.ejb.EJBHome;
  61: import javax.ejb.EJBObject;
  62: import org.openejb.Container;
  63: import org.openejb.DeploymentInfo;
  64: import org.openejb.EnvProps;
  65: import org.openejb.InvalidateReferenceException;
  66: import org.openejb.OpenEJB;
  67: import org.openejb.OpenEJBException;
  68: import org.openejb.ProxyInfo;
  69: import org.openejb.util.ArrayEnumeration;
  70: import org.openejb.util.SafeProperties;
  71: import org.openejb.util.SafeToolkit;
  72: import org.openejb.util.proxy.InvocationHandler;
  73: import org.openejb.util.proxy.ProxyManager;
  74: 
  75: /**
  76:  * Represents the EJB Server's responsibility in handling the finder and create 
  77:  * methods that are declared in the bean's home interface.
  78:  * 
  79:  * @see javax.ejb.EJBHome
  80:  */
  81:	  public class EjbHomeIntfcProcessor {
  82:
  83:    /**
  84:     * Internally processes the finder and create methods from the bean's home 
  85:     * interface.
  86:     * 
  87:     * @param mi
  88:     * @param securityToken
  89:     * @param server
  90:     * @return 
  91:     * @exception OpenEJBException
  92:     */
  93:	      public static Object processMethod(MethodInvocation mi, String securityToken, Server server) throws OpenEJBException {
  94:
  95:        if ( mi.getMethod().getName().equals("create") )        return create(mi, securityToken, server);
  96:        else if ( mi.getMethod().getName().startsWith("find") ) return find(mi, securityToken, server);
  97:
  98:        return server.invokeMethod(mi);
  99:    }
 100:
 101:    /**
 102:     * Invokes the finder method on the OpenEJB container system and then generates
 103:     * the appropriate EJB Server specific EJBObjects and EJBHomes to return to the 
 104:     * client.
 105:     * 
 106:     * @param mi
 107:     * @param securityToken
 108:     * @param server
 109:     * @return 
 110:     * @exception OpenEJBException
 111:     */
 112:	      protected static Object find(MethodInvocation mi, String securityToken, Server server) throws OpenEJBException {
 113:        Object retValue = server.invokeMethod(mi);
 114:        Class proxyClass = null;
 115:        DeploymentInfo di = mi.getDeploymentInfo();
 116:
 117:	          if ( retValue instanceof Collection ) {
 118:
 119:            Object [] elements = ((Collection)retValue).toArray();
 120:            Vector proxies = new Vector();
 121:	              for ( int i = 0; i < elements.length; i++ ) {
 122:                proxies.addElement( createProxy(elements[i], di.getRemoteInterface(), securityToken, server, new javax.ejb.FinderException()) );
 123:            }
 124:
 125:	              if ( mi.getMethod().getReturnType() == java.util.Enumeration.class ) {
 126:                return new ArrayEnumeration(proxies);
 127:            } else
 128:                return proxies;// vector is a type of Collection.
 129:
 130:
 131:        } else {
 132:            return createProxy(retValue, di.getRemoteInterface(), securityToken, server, new javax.ejb.FinderException()); 
 133:        }
 134:    }
 135:
 136:    /**
 137:     * Invokes the create method on the OpenEJB container system and then generates
 138:     * the appropriate EJB Server specific EJBObjects to return to the
 139:     * client.
 140:     * 
 141:     * @param mi
 142:     * @param securityToken
 143:     * @param server
 144:     * @return 
 145:     * @exception OpenEJBException
 146:     */
 147:	      protected static Object create( MethodInvocation mi, String securityToken, Server server) throws OpenEJBException{
 148:        Object retValue = server.invokeMethod(mi);
 149:        Exception exception = new RemoteException();
 150:        DeploymentInfo di = mi.getDeploymentInfo();
 151:        return createProxy(retValue,di.getRemoteInterface(), securityToken, server, exception);
 152:    }
 153:
 154:    /**
 155:     * Utility method to aid this EJB Server implementation in creating EjbProxyHandlers 
 156:     * to represent the EJBObject and EJBHome to the client.
 157:     * 
 158:     * @param retValue
 159:     * @param proxyClass
 160:     * @param securityToken
 161:     * @param server
 162:     * @param exception
 163:     * @return 
 164:     */
 165:	      private static Object createProxy(Object retValue, Class interfce, String securityToken, Server server, Exception exception) {
 166:        ProxyInfo pInfo = (ProxyInfo)retValue;
 167:	          try{
 168:            InvocationHandler handler = new EjbProxyHandler(server.port, server.ip, pInfo.getPrimaryKey(),pInfo.getDeploymentInfo().getDeploymentID(), securityToken);
 169:            return ProxyManager.newProxyInstance(interfce, handler);
 170:        } catch (Exception e){
 171:            return exception;
 172:        }
 173:    }
 174:}