Source for gnu.java.rmi.server.ActivatableRef

   1: /* ActivatableRef.java -- Activatable server reference
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.rmi.server;
  40: 
  41: import java.io.IOException;
  42: import java.io.ObjectInput;
  43: import java.io.ObjectOutput;
  44: import java.lang.reflect.Method;
  45: import java.lang.reflect.Proxy;
  46: import java.rmi.Remote;
  47: import java.rmi.RemoteException;
  48: import java.rmi.activation.ActivationException;
  49: import java.rmi.activation.ActivationID;
  50: import java.rmi.server.ObjID;
  51: import java.rmi.server.RMIClientSocketFactory;
  52: import java.rmi.server.RemoteObject;
  53: import java.rmi.server.RemoteObjectInvocationHandler;
  54: import java.rmi.server.RemoteRef;
  55: 
  56: /**
  57:  * The activatable reference works like UnicastRef, but if the remote object
  58:  * appears to be not accessible, it tries to reactivate it before reporting
  59:  * any errors. Apart the fields of the UnicastRef, the activatable reference
  60:  * contains the ActivationID that is used for this activation.
  61:  * 
  62:  * @author Audrius Meskauskas (Audriusa@Bioinformatics.org)
  63:  */
  64: public class ActivatableRef extends UnicastRef
  65: {
  66:   /**
  67:    * Use serial version UID for iteroperability
  68:    */
  69:   private static final long serialVersionUID = 1;
  70:   
  71:   /**
  72:    * The activation id.
  73:    */
  74:   ActivationID actId;
  75:   
  76:   /**
  77:    * Delegate call to the superclass.
  78:    */
  79:   public ActivatableRef()
  80:   {
  81:     super();
  82:   }
  83: 
  84:   /**
  85:    * Delegate call to the superclass.
  86:    */
  87:   public ActivatableRef(ObjID objid, String host, int port,
  88:                         RMIClientSocketFactory csf)
  89:   {
  90:     super(objid, host, port, csf);
  91:   }
  92: 
  93:   /**
  94:    * Delegate call to the superclass.
  95:    */
  96:   public ActivatableRef(ObjID objid)
  97:   {
  98:     super(objid);
  99:   }
 100: 
 101:   /**
 102:    * Get the referencing class.
 103:    */
 104:   public String getRefClass(ObjectOutput out)
 105:   {
 106:     return "ActivatableRef";
 107:   }
 108: 
 109:   /**
 110:    * Read the content from the input stream.
 111:    */
 112:   public void readExternal(ObjectInput in) throws IOException,
 113:       ClassNotFoundException
 114:   {
 115:     super.readExternal(in);
 116:     actId = (ActivationID) in.readObject();
 117:   }
 118: 
 119:   /**
 120:    * Write the content to the output stream.
 121:    */
 122:   public void writeExternal(ObjectOutput out) throws IOException
 123:   {
 124:     super.writeExternal(out);
 125:     out.writeObject(actId);
 126:   }
 127:   
 128:   /**
 129:    * Invoke the remote method on the given object and try to activate the object
 130:    * if it is not reacheable with the current manager.
 131:    */
 132:   protected Object invokeCommon(Remote obj, Method method, Object[] params,
 133:                                 int opnum, long hash) throws Exception
 134:   {
 135:     UnicastConnection conn;
 136:     try
 137:       {
 138:         conn = manager.getConnection();
 139:       }
 140:     catch (IOException e1)
 141:       {
 142:         // Connection failed: try to activate.
 143:         Remote reactivated = actId.activate(false);
 144: 
 145:         if (reactivated instanceof RemoteObject)
 146:           {
 147:             RemoteRef ref = ((RemoteObject) reactivated).getRef();
 148:             manager = ((UnicastRef) ref).manager;
 149:           }
 150:         else if (Proxy.isProxyClass(reactivated.getClass()))
 151:           {
 152:             RemoteObjectInvocationHandler hander = 
 153:               (RemoteObjectInvocationHandler) 
 154:                 Proxy.getInvocationHandler(reactivated);
 155:             
 156:             RemoteRef ref = hander.getRef();
 157:             manager = ((UnicastRef) ref).manager;
 158:           }
 159:         else
 160:           throw new ActivationException("Activating into unsupported class "
 161:                                         + reactivated.getClass());
 162: 
 163:         try
 164:           {
 165:             conn = manager.getConnection();
 166:           }
 167:         catch (IOException e2)
 168:           {
 169:             throw new RemoteException("connection failed to host: "
 170:                                       + manager.serverName, e1);
 171:           }
 172:       }
 173:     return invokeCommon(conn, obj, method, params, opnum, hash);
 174:   }
 175: }