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:     actId = (ActivationID) in.readObject();
 116:     String type = in.readUTF();
 117:     // XXX handle type.equals("") (null reference)
 118:     super.readExternal(in);
 119:   }
 120: 
 121:   /**
 122:    * Write the content to the output stream.
 123:    */
 124:   public void writeExternal(ObjectOutput out) throws IOException
 125:   {
 126:     out.writeObject(actId);
 127:     // XXX write a "" if the "nested" reference is a null reference
 128:     out.writeUTF("UnicastRef2");
 129:     super.writeExternal(out);
 130:   }
 131: 
 132:   /**
 133:    * Invoke the remote method on the given object and try to activate the object
 134:    * if it is not reacheable with the current manager.
 135:    */
 136:   protected Object invokeCommon(Remote obj, Method method, Object[] params,
 137:                                 int opnum, long hash) throws Exception
 138:   {
 139:     UnicastConnection conn;
 140:     try
 141:       {
 142:         conn = manager.getConnection();
 143:       }
 144:     catch (IOException e1)
 145:       {
 146:         // Connection failed: try to activate.
 147:         Remote reactivated = actId.activate(false);
 148: 
 149:         if (reactivated instanceof RemoteObject)
 150:           {
 151:             RemoteRef ref = ((RemoteObject) reactivated).getRef();
 152:             manager = ((UnicastRef) ref).manager;
 153:           }
 154:         else if (Proxy.isProxyClass(reactivated.getClass()))
 155:           {
 156:             RemoteObjectInvocationHandler hander =
 157:               (RemoteObjectInvocationHandler)
 158:                 Proxy.getInvocationHandler(reactivated);
 159: 
 160:             RemoteRef ref = hander.getRef();
 161:             manager = ((UnicastRef) ref).manager;
 162:           }
 163:         else
 164:           throw new ActivationException("Activating into unsupported class "
 165:                                         + reactivated.getClass());
 166: 
 167:         try
 168:           {
 169:             conn = manager.getConnection();
 170:           }
 171:         catch (IOException e2)
 172:           {
 173:             throw new RemoteException("connection failed to host: "
 174:                                       + manager.serverName, e1);
 175:           }
 176:       }
 177:     return invokeCommon(conn, obj, method, params, opnum, hash);
 178:   }
 179: }