001    /* UnicastRemoteObject.java --
002       Copyright (c) 1996, 1997, 1998, 1999, 2002, 2003, 2006 
003       Free Software Foundation, Inc.
004    
005    This file is part of GNU Classpath.
006    
007    GNU Classpath is free software; you can redistribute it and/or modify
008    it under the terms of the GNU General Public License as published by
009    the Free Software Foundation; either version 2, or (at your option)
010    any later version.
011     
012    GNU Classpath is distributed in the hope that it will be useful, but
013    WITHOUT ANY WARRANTY; without even the implied warranty of
014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015    General Public License for more details.
016    
017    You should have received a copy of the GNU General Public License
018    along with GNU Classpath; see the file COPYING.  If not, write to the
019    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
020    02110-1301 USA.
021    
022    Linking this library statically or dynamically with other modules is
023    making a combined work based on this library.  Thus, the terms and
024    conditions of the GNU General Public License cover the whole
025    combination.
026    
027    As a special exception, the copyright holders of this library give you
028    permission to link this library with independent modules to produce an
029    executable, regardless of the license terms of these independent
030    modules, and to copy and distribute the resulting executable under
031    terms of your choice, provided that you also meet, for each linked
032    independent module, the terms and conditions of the license of that
033    module.  An independent module is a module which is not derived from
034    or based on this library.  If you modify this library, you may extend
035    this exception to your version of the library, but you are not
036    obligated to do so.  If you do not wish to do so, delete this
037    exception statement from your version. */
038    
039    
040    package java.rmi.server;
041    
042    import gnu.java.rmi.server.UnicastServerRef;
043    
044    import java.rmi.NoSuchObjectException;
045    import java.rmi.Remote;
046    import java.rmi.RemoteException;
047    
048    /**
049     * This class obtains stub that communicates with the remote object. 
050     */
051    public class UnicastRemoteObject extends RemoteServer
052    {
053      /**
054       * Use SVUID for interoperability.
055       */
056      private static final long serialVersionUID = 4974527148936298033L;
057    
058      //The following serialized fields are from Java API Documentation
059      // "Serialized form"
060      
061      /**
062       * The port, on that the created remote object becomes available,
063       * zero meaning the anonymous port.
064       */
065      private int port;
066      
067      /**
068       * The client socket factory for producing client sockets, used by this
069       * object.
070       */
071      private RMIClientSocketFactory csf;
072      
073      /**
074       * The server socket factory for producing server sockets, used by this
075       * object.
076       */
077      private RMIServerSocketFactory ssf;
078    
079      /**
080       * Create and export new remote object without specifying the port value.
081       * 
082       * @throws RemoteException if the attempt to export the object failed.
083       */
084      protected UnicastRemoteObject()
085        throws RemoteException
086      {
087            this(0);
088      }
089      
090      /**
091       * Create and export the new remote object, making it available at the
092       * given port, local host.
093       * 
094       * @param port the port, on that the object should become available.
095       * Zero means anonymous port.
096       * 
097       * @throws RemoteException if the attempt to export the object failed.
098       */
099      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    }