Source for java.rmi.server.UnicastRemoteObject

   1: /* UnicastRemoteObject.java --
   2:    Copyright (c) 1996, 1997, 1998, 1999, 2002, 2003, 2006
   3:    Free Software Foundation, Inc.
   4: 
   5: This file is part of GNU Classpath.
   6: 
   7: GNU Classpath is free software; you can redistribute it and/or modify
   8: it under the terms of the GNU General Public License as published by
   9: the Free Software Foundation; either version 2, or (at your option)
  10: any later version.
  11: 
  12: GNU Classpath is distributed in the hope that it will be useful, but
  13: WITHOUT ANY WARRANTY; without even the implied warranty of
  14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15: General Public License for more details.
  16: 
  17: You should have received a copy of the GNU General Public License
  18: along with GNU Classpath; see the file COPYING.  If not, write to the
  19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20: 02110-1301 USA.
  21: 
  22: Linking this library statically or dynamically with other modules is
  23: making a combined work based on this library.  Thus, the terms and
  24: conditions of the GNU General Public License cover the whole
  25: combination.
  26: 
  27: As a special exception, the copyright holders of this library give you
  28: permission to link this library with independent modules to produce an
  29: executable, regardless of the license terms of these independent
  30: modules, and to copy and distribute the resulting executable under
  31: terms of your choice, provided that you also meet, for each linked
  32: independent module, the terms and conditions of the license of that
  33: module.  An independent module is a module which is not derived from
  34: or based on this library.  If you modify this library, you may extend
  35: this exception to your version of the library, but you are not
  36: obligated to do so.  If you do not wish to do so, delete this
  37: exception statement from your version. */
  38: 
  39: 
  40: package java.rmi.server;
  41: 
  42: import gnu.java.rmi.server.UnicastServerRef;
  43: 
  44: import java.rmi.NoSuchObjectException;
  45: import java.rmi.Remote;
  46: import java.rmi.RemoteException;
  47: 
  48: /**
  49:  * This class obtains stub that communicates with the remote object.
  50:  */
  51: public class UnicastRemoteObject extends RemoteServer
  52: {
  53:   /**
  54:    * Use SVUID for interoperability.
  55:    */
  56:   private static final long serialVersionUID = 4974527148936298033L;
  57: 
  58:   //The following serialized fields are from Java API Documentation
  59:   // "Serialized form"
  60: 
  61:   /**
  62:    * The port, on that the created remote object becomes available,
  63:    * zero meaning the anonymous port.
  64:    */
  65:   private int port;
  66: 
  67:   /**
  68:    * The client socket factory for producing client sockets, used by this
  69:    * object.
  70:    */
  71:   private RMIClientSocketFactory csf;
  72: 
  73:   /**
  74:    * The server socket factory for producing server sockets, used by this
  75:    * object.
  76:    */
  77:   private RMIServerSocketFactory ssf;
  78: 
  79:   /**
  80:    * Create and export new remote object without specifying the port value.
  81:    *
  82:    * @throws RemoteException if the attempt to export the object failed.
  83:    */
  84:   protected UnicastRemoteObject()
  85:     throws RemoteException
  86:   {
  87:         this(0);
  88:   }
  89: 
  90:   /**
  91:    * Create and export the new remote object, making it available at the
  92:    * given port, local host.
  93:    *
  94:    * @param port the port, on that the object should become available.
  95:    * Zero means anonymous port.
  96:    *
  97:    * @throws RemoteException if the attempt to export the object failed.
  98:    */
  99:   protected UnicastRemoteObject(int port)
 100:     throws RemoteException
 101:   {
 102:         this(port, RMISocketFactory.getSocketFactory(),
 103:          RMISocketFactory.getSocketFactory());
 104:   }
 105: 
 106:   /**
 107:    * Create and export the new remote object, making it available at the
 108:    * given port, using sockets, produced by the specified factories.
 109:    *
 110:    * @param port the port, on that the object should become available.
 111:    * Zero means anonymous port.
 112:    *
 113:    * @param clientSocketFactory the client socket factory
 114:    * @param serverSocketFactory the server socket factory
 115:    *
 116:    * @throws RemoteException if the attempt to export the object failed.
 117:    */
 118:   protected UnicastRemoteObject(int port,
 119:                                 RMIClientSocketFactory clientSocketFactory,
 120:                                 RMIServerSocketFactory serverSocketFactory)
 121:     throws RemoteException
 122:   {
 123:     this.port = port;
 124:     //Is RMIXXXSocketFactory serializable
 125:     //this.csf = csf;
 126:     //this.ssf = ssf;
 127:     this.ref = new UnicastServerRef(new ObjID(), port, serverSocketFactory);
 128:     exportObject(this, port);
 129:   }
 130: 
 131:   protected UnicastRemoteObject(RemoteRef ref)
 132:     throws RemoteException
 133:   {
 134:         super((UnicastServerRef) ref);
 135:         exportObject(this, 0);
 136:   }
 137: 
 138:   public Object clone()
 139:     throws CloneNotSupportedException
 140:   {
 141:         throw new Error("Not implemented");
 142:   }
 143: 
 144:   /**
 145:    * Export object, making it available for the remote calls at the
 146:    * anonymous port.
 147:    *
 148:    * This method returns the instance of the abstract class, not an interface.
 149:    * Hence it will not work with the proxy stubs that are supported since
 150:    * jdk 1.5 (such stubs cannot be derived from the RemoteStub). Only use
 151:    * this method if you are sure that the stub class will be accessible.
 152:    *
 153:    * @param obj the object being exported.
 154:    *
 155:    * @return the remote object stub
 156:    *
 157:    * @throws RemoteException if the attempt to export the object failed.
 158:    */
 159:   public static RemoteStub exportObject(Remote obj)
 160:     throws RemoteException
 161:   {
 162:         return (RemoteStub) exportObject(obj, 0);
 163:   }
 164: 
 165:   /**
 166:    * Export object, making it available for the remote calls at the
 167:    * specified port.
 168:    *
 169:    * Since jdk 1.5 this method does not longer require the stub class to be
 170:    * present. If such class is not found, the stub is replaced by the
 171:    * dynamically constructed proxy class. No attempt to find and load the stubs
 172:    * is made if the system property java.rmi.server.ignoreStubClasses
 173:    * is set to true (set to reduce the starting time if the stubs are
 174:    * surely not present and exclusively 1.2 RMI is used).
 175:    *
 176:    * @param obj the object being exported.
 177:    * @param port the remote object port
 178:    *
 179:    * @return the remote object stub
 180:    *
 181:    * @throws RemoteException if the attempt to export the object failed.
 182:    */
 183:   public static Remote exportObject(Remote obj, int port)
 184:     throws RemoteException
 185:   {
 186:     return exportObject(obj, port, null);
 187:   }
 188: 
 189:   /**
 190:    * Create and export the new remote object, making it available at the
 191:    * given port, using sockets, produced by the specified factories.
 192:    *
 193:    * Since jdk 1.5 this method does not longer require the stub class to be
 194:    * present. If such class is not found, the stub is replaced by the
 195:    * dynamically constructed proxy class. No attempt to find and load the stubs
 196:    * is made if the system property java.rmi.server.ignoreStubClasses
 197:    * is set to true (set to reduce the starting time if the stubs are
 198:    * surely not present and exclusively 1.2 RMI is used).
 199:    *
 200:    * @param port the port, on that the object should become available.
 201:    * Zero means anonymous port.
 202:    *
 203:    * @param serverSocketFactory the server socket factory
 204:    */
 205:   static Remote exportObject(Remote obj, int port,
 206:                              RMIServerSocketFactory serverSocketFactory)
 207:     throws RemoteException
 208:   {
 209:     UnicastServerRef sref = null;
 210:     if (obj instanceof RemoteObject)
 211:       sref = (UnicastServerRef) ((RemoteObject) obj).getRef();
 212: 
 213:     if (sref == null)
 214:       sref = new UnicastServerRef(new ObjID(), port, serverSocketFactory);
 215: 
 216:     Remote stub = sref.exportObject(obj);
 217:     addStub(obj, stub);
 218:     return stub;
 219:   }
 220: 
 221:   /**
 222:    * FIXME
 223:    */
 224:   public static Remote exportObject(Remote obj, int port,
 225:                                     RMIClientSocketFactory csf,
 226:                                     RMIServerSocketFactory ssf)
 227:     throws RemoteException
 228:   {
 229:     return (exportObject(obj, port, ssf));
 230:   }
 231: 
 232:   public static boolean unexportObject(Remote obj, boolean force)
 233:     throws NoSuchObjectException
 234:   {
 235:     if (obj instanceof RemoteObject)
 236:       {
 237:         deleteStub(obj);
 238:         UnicastServerRef sref =
 239:           (UnicastServerRef) ((RemoteObject) obj).getRef();
 240:         return sref.unexportObject(obj, force);
 241:       }
 242:     // FIXME
 243:     /* else
 244:       {
 245:         ;
 246:       }
 247:     */
 248:     return true;
 249:   }
 250: 
 251: }