0N/A/*
2362N/A * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.rmi.activation;
0N/A
0N/Aimport java.rmi.MarshalledObject;
0N/Aimport java.rmi.NoSuchObjectException;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.activation.UnknownGroupException;
0N/Aimport java.rmi.activation.UnknownObjectException;
0N/Aimport java.rmi.server.RMIClientSocketFactory;
0N/Aimport java.rmi.server.RMIServerSocketFactory;
0N/Aimport java.rmi.server.RemoteServer;
0N/Aimport sun.rmi.server.ActivatableServerRef;
0N/A
0N/A/**
0N/A * The <code>Activatable</code> class provides support for remote
0N/A * objects that require persistent access over time and that
0N/A * can be activated by the system.
0N/A *
0N/A * <p>For the constructors and static <code>exportObject</code> methods,
0N/A * the stub for a remote object being exported is obtained as described in
0N/A * {@link java.rmi.server.UnicastRemoteObject}.
0N/A *
0N/A * <p>An attempt to serialize explicitly an instance of this class will
0N/A * fail.
0N/A *
0N/A * @author Ann Wollrath
0N/A * @since 1.2
0N/A * @serial exclude
0N/A */
0N/Apublic abstract class Activatable extends RemoteServer {
0N/A
0N/A private ActivationID id;
0N/A /** indicate compatibility with the Java 2 SDK v1.2 version of class */
0N/A private static final long serialVersionUID = -3120617863591563455L;
0N/A
0N/A /**
0N/A * Constructs an activatable remote object by registering
0N/A * an activation descriptor (with the specified location, data, and
0N/A * restart mode) for this object, and exporting the object with the
0N/A * specified port.
0N/A *
0N/A * <p><strong>Note:</strong> Using the <code>Activatable</code>
0N/A * constructors that both register and export an activatable remote
0N/A * object is strongly discouraged because the actions of registering
0N/A * and exporting the remote object are <i>not</i> guaranteed to be
0N/A * atomic. Instead, an application should register an activation
0N/A * descriptor and export a remote object separately, so that exceptions
0N/A * can be handled properly.
0N/A *
0N/A * <p>This method invokes the {@link
2015N/A * #exportObject(Remote,String,MarshalledObject,boolean,int)
0N/A * exportObject} method with this object, and the specified location,
0N/A * data, restart mode, and port. Subsequent calls to {@link #getID}
0N/A * will return the activation identifier returned from the call to
0N/A * <code>exportObject</code>.
0N/A *
0N/A * @param location the location for classes for this object
0N/A * @param data the object's initialization data
0N/A * @param port the port on which the object is exported (an anonymous
0N/A * port is used if port=0)
0N/A * @param restart if true, the object is restarted (reactivated) when
0N/A * either the activator is restarted or the object's activation group
0N/A * is restarted after an unexpected crash; if false, the object is only
0N/A * activated on demand. Specifying <code>restart</code> to be
0N/A * <code>true</code> does not force an initial immediate activation of
0N/A * a newly registered object; initial activation is lazy.
0N/A * @exception ActivationException if object registration fails.
0N/A * @exception RemoteException if either of the following fails:
0N/A * a) registering the object with the activation system or b) exporting
0N/A * the object to the RMI runtime.
0N/A * @since 1.2
0N/A **/
0N/A protected Activatable(String location,
0N/A MarshalledObject<?> data,
0N/A boolean restart,
0N/A int port)
0N/A throws ActivationException, RemoteException
0N/A {
0N/A super();
0N/A id = exportObject(this, location, data, restart, port);
0N/A }
0N/A
0N/A /**
0N/A * Constructs an activatable remote object by registering
0N/A * an activation descriptor (with the specified location, data, and
0N/A * restart mode) for this object, and exporting the object with the
0N/A * specified port, and specified client and server socket factories.
0N/A *
0N/A * <p><strong>Note:</strong> Using the <code>Activatable</code>
0N/A * constructors that both register and export an activatable remote
0N/A * object is strongly discouraged because the actions of registering
0N/A * and exporting the remote object are <i>not</i> guaranteed to be
0N/A * atomic. Instead, an application should register an activation
0N/A * descriptor and export a remote object separately, so that exceptions
0N/A * can be handled properly.
0N/A *
0N/A * <p>This method invokes the {@link
2015N/A * #exportObject(Remote,String,MarshalledObject,boolean,int,RMIClientSocketFactory,RMIServerSocketFactory)
0N/A * exportObject} method with this object, and the specified location,
0N/A * data, restart mode, port, and client and server socket factories.
0N/A * Subsequent calls to {@link #getID} will return the activation
0N/A * identifier returned from the call to <code>exportObject</code>.
0N/A *
0N/A * @param location the location for classes for this object
0N/A * @param data the object's initialization data
0N/A * @param restart if true, the object is restarted (reactivated) when
0N/A * either the activator is restarted or the object's activation group
0N/A * is restarted after an unexpected crash; if false, the object is only
0N/A * activated on demand. Specifying <code>restart</code> to be
0N/A * <code>true</code> does not force an initial immediate activation of
0N/A * a newly registered object; initial activation is lazy.
0N/A * @param port the port on which the object is exported (an anonymous
0N/A * port is used if port=0)
0N/A * @param csf the client-side socket factory for making calls to the
0N/A * remote object
0N/A * @param ssf the server-side socket factory for receiving remote calls
0N/A * @exception ActivationException if object registration fails.
0N/A * @exception RemoteException if either of the following fails:
0N/A * a) registering the object with the activation system or b) exporting
0N/A * the object to the RMI runtime.
0N/A * @since 1.2
0N/A **/
0N/A protected Activatable(String location,
0N/A MarshalledObject<?> data,
0N/A boolean restart,
0N/A int port,
0N/A RMIClientSocketFactory csf,
0N/A RMIServerSocketFactory ssf)
0N/A throws ActivationException, RemoteException
0N/A {
0N/A super();
0N/A id = exportObject(this, location, data, restart, port, csf, ssf);
0N/A }
0N/A
0N/A /**
0N/A * Constructor used to activate/export the object on a specified
0N/A * port. An "activatable" remote object must have a constructor that
0N/A * takes two arguments: <ul>
0N/A * <li>the object's activation identifier (<code>ActivationID</code>), and
0N/A * <li>the object's initialization data (a <code>MarshalledObject</code>).
0N/A * </ul><p>
0N/A *
0N/A * A concrete subclass of this class must call this constructor when it is
0N/A * <i>activated</i> via the two parameter constructor described above. As
0N/A * a side-effect of construction, the remote object is "exported"
0N/A * to the RMI runtime (on the specified <code>port</code>) and is
0N/A * available to accept incoming calls from clients.
0N/A *
0N/A * @param id activation identifier for the object
0N/A * @param port the port number on which the object is exported
0N/A * @exception RemoteException if exporting the object to the RMI
0N/A * runtime fails
0N/A * @since 1.2
0N/A */
0N/A protected Activatable(ActivationID id, int port)
0N/A throws RemoteException
0N/A {
0N/A super();
0N/A this.id = id;
0N/A exportObject(this, id, port);
0N/A }
0N/A
0N/A /**
0N/A * Constructor used to activate/export the object on a specified
0N/A * port. An "activatable" remote object must have a constructor that
0N/A * takes two arguments: <ul>
0N/A * <li>the object's activation identifier (<code>ActivationID</code>), and
0N/A * <li>the object's initialization data (a <code>MarshalledObject</code>).
0N/A * </ul><p>
0N/A *
0N/A * A concrete subclass of this class must call this constructor when it is
0N/A * <i>activated</i> via the two parameter constructor described above. As
0N/A * a side-effect of construction, the remote object is "exported"
0N/A * to the RMI runtime (on the specified <code>port</code>) and is
0N/A * available to accept incoming calls from clients.
0N/A *
0N/A * @param id activation identifier for the object
0N/A * @param port the port number on which the object is exported
0N/A * @param csf the client-side socket factory for making calls to the
0N/A * remote object
0N/A * @param ssf the server-side socket factory for receiving remote calls
0N/A * @exception RemoteException if exporting the object to the RMI
0N/A * runtime fails
0N/A * @since 1.2
0N/A */
0N/A protected Activatable(ActivationID id, int port,
0N/A RMIClientSocketFactory csf,
0N/A RMIServerSocketFactory ssf)
0N/A throws RemoteException
0N/A {
0N/A super();
0N/A this.id = id;
0N/A exportObject(this, id, port, csf, ssf);
0N/A }
0N/A
0N/A /**
0N/A * Returns the object's activation identifier. The method is
0N/A * protected so that only subclasses can obtain an object's
0N/A * identifier.
0N/A * @return the object's activation identifier
0N/A * @since 1.2
0N/A */
0N/A protected ActivationID getID() {
0N/A return id;
0N/A }
0N/A
0N/A /**
0N/A * Register an object descriptor for an activatable remote
0N/A * object so that is can be activated on demand.
0N/A *
0N/A * @param desc the object's descriptor
0N/A * @return the stub for the activatable remote object
0N/A * @exception UnknownGroupException if group id in <code>desc</code>
0N/A * is not registered with the activation system
0N/A * @exception ActivationException if activation system is not running
0N/A * @exception RemoteException if remote call fails
0N/A * @since 1.2
0N/A */
0N/A public static Remote register(ActivationDesc desc)
0N/A throws UnknownGroupException, ActivationException, RemoteException
0N/A {
0N/A // register object with activator.
0N/A ActivationID id =
0N/A ActivationGroup.getSystem().registerObject(desc);
0N/A return sun.rmi.server.ActivatableRef.getStub(desc, id);
0N/A }
0N/A
0N/A /**
0N/A * Informs the system that the object with the corresponding activation
0N/A * <code>id</code> is currently inactive. If the object is currently
0N/A * active, the object is "unexported" from the RMI runtime (only if
0N/A * there are no pending or in-progress calls)
0N/A * so the that it can no longer receive incoming calls. This call
0N/A * informs this VM's ActivationGroup that the object is inactive,
0N/A * that, in turn, informs its ActivationMonitor. If this call
0N/A * completes successfully, a subsequent activate request to the activator
0N/A * will cause the object to reactivate. The operation may still
0N/A * succeed if the object is considered active but has already
0N/A * unexported itself.
0N/A *
0N/A * @param id the object's activation identifier
0N/A * @return true if the operation succeeds (the operation will
0N/A * succeed if the object in currently known to be active and is
0N/A * either already unexported or is currently exported and has no
0N/A * pending/executing calls); false is returned if the object has
0N/A * pending/executing calls in which case it cannot be deactivated
0N/A * @exception UnknownObjectException if object is not known (it may
0N/A * already be inactive)
0N/A * @exception ActivationException if group is not active
0N/A * @exception RemoteException if call informing monitor fails
0N/A * @since 1.2
0N/A */
0N/A public static boolean inactive(ActivationID id)
0N/A throws UnknownObjectException, ActivationException, RemoteException
0N/A {
0N/A return ActivationGroup.currentGroup().inactiveObject(id);
0N/A }
0N/A
0N/A /**
0N/A * Revokes previous registration for the activation descriptor
0N/A * associated with <code>id</code>. An object can no longer be
0N/A * activated via that <code>id</code>.
0N/A *
0N/A * @param id the object's activation identifier
0N/A * @exception UnknownObjectException if object (<code>id</code>) is unknown
0N/A * @exception ActivationException if activation system is not running
0N/A * @exception RemoteException if remote call to activation system fails
0N/A * @since 1.2
0N/A */
0N/A public static void unregister(ActivationID id)
0N/A throws UnknownObjectException, ActivationException, RemoteException
0N/A {
0N/A ActivationGroup.getSystem().unregisterObject(id);
0N/A }
0N/A
0N/A /**
0N/A * Registers an activation descriptor (with the specified location,
0N/A * data, and restart mode) for the specified object, and exports that
0N/A * object with the specified port.
0N/A *
0N/A * <p><strong>Note:</strong> Using this method (as well as the
0N/A * <code>Activatable</code> constructors that both register and export
0N/A * an activatable remote object) is strongly discouraged because the
0N/A * actions of registering and exporting the remote object are
0N/A * <i>not</i> guaranteed to be atomic. Instead, an application should
0N/A * register an activation descriptor and export a remote object
0N/A * separately, so that exceptions can be handled properly.
0N/A *
0N/A * <p>This method invokes the {@link
2015N/A * #exportObject(Remote,String,MarshalledObject,boolean,int,RMIClientSocketFactory,RMIServerSocketFactory)
0N/A * exportObject} method with the specified object, location, data,
0N/A * restart mode, and port, and <code>null</code> for both client and
0N/A * server socket factories, and then returns the resulting activation
0N/A * identifier.
0N/A *
0N/A * @param obj the object being exported
0N/A * @param location the object's code location
0N/A * @param data the object's bootstrapping data
0N/A * @param restart if true, the object is restarted (reactivated) when
0N/A * either the activator is restarted or the object's activation group
0N/A * is restarted after an unexpected crash; if false, the object is only
0N/A * activated on demand. Specifying <code>restart</code> to be
0N/A * <code>true</code> does not force an initial immediate activation of
0N/A * a newly registered object; initial activation is lazy.
0N/A * @param port the port on which the object is exported (an anonymous
0N/A * port is used if port=0)
0N/A * @return the activation identifier obtained from registering the
0N/A * descriptor, <code>desc</code>, with the activation system
0N/A * the wrong group
0N/A * @exception ActivationException if activation group is not active
0N/A * @exception RemoteException if object registration or export fails
0N/A * @since 1.2
0N/A **/
0N/A public static ActivationID exportObject(Remote obj,
0N/A String location,
0N/A MarshalledObject<?> data,
0N/A boolean restart,
0N/A int port)
0N/A throws ActivationException, RemoteException
0N/A {
0N/A return exportObject(obj, location, data, restart, port, null, null);
0N/A }
0N/A
0N/A /**
0N/A * Registers an activation descriptor (with the specified location,
0N/A * data, and restart mode) for the specified object, and exports that
0N/A * object with the specified port, and the specified client and server
0N/A * socket factories.
0N/A *
0N/A * <p><strong>Note:</strong> Using this method (as well as the
0N/A * <code>Activatable</code> constructors that both register and export
0N/A * an activatable remote object) is strongly discouraged because the
0N/A * actions of registering and exporting the remote object are
0N/A * <i>not</i> guaranteed to be atomic. Instead, an application should
0N/A * register an activation descriptor and export a remote object
0N/A * separately, so that exceptions can be handled properly.
0N/A *
0N/A * <p>This method first registers an activation descriptor for the
0N/A * specified object as follows. It obtains the activation system by
0N/A * invoking the method {@link ActivationGroup#getSystem
0N/A * ActivationGroup.getSystem}. This method then obtains an {@link
0N/A * ActivationID} for the object by invoking the activation system's
0N/A * {@link ActivationSystem#registerObject registerObject} method with
0N/A * an {@link ActivationDesc} constructed with the specified object's
0N/A * class name, and the specified location, data, and restart mode. If
0N/A * an exception occurs obtaining the activation system or registering
0N/A * the activation descriptor, that exception is thrown to the caller.
0N/A *
0N/A * <p>Next, this method exports the object by invoking the {@link
0N/A * #exportObject(Remote,ActivationID,int,RMIClientSocketFactory,RMIServerSocketFactory)
0N/A * exportObject} method with the specified remote object, the
0N/A * activation identifier obtained from registration, the specified
0N/A * port, and the specified client and server socket factories. If an
0N/A * exception occurs exporting the object, this method attempts to
0N/A * unregister the activation identifier (obtained from registration) by
0N/A * invoking the activation system's {@link
0N/A * ActivationSystem#unregisterObject unregisterObject} method with the
0N/A * activation identifier. If an exception occurs unregistering the
0N/A * identifier, that exception is ignored, and the original exception
0N/A * that occurred exporting the object is thrown to the caller.
0N/A *
0N/A * <p>Finally, this method invokes the {@link
0N/A * ActivationGroup#activeObject activeObject} method on the activation
0N/A * group in this VM with the activation identifier and the specified
0N/A * remote object, and returns the activation identifier to the caller.
0N/A *
0N/A * @param obj the object being exported
0N/A * @param location the object's code location
0N/A * @param data the object's bootstrapping data
0N/A * @param restart if true, the object is restarted (reactivated) when
0N/A * either the activator is restarted or the object's activation group
0N/A * is restarted after an unexpected crash; if false, the object is only
0N/A * activated on demand. Specifying <code>restart</code> to be
0N/A * <code>true</code> does not force an initial immediate activation of
0N/A * a newly registered object; initial activation is lazy.
0N/A * @param port the port on which the object is exported (an anonymous
0N/A * port is used if port=0)
0N/A * @param csf the client-side socket factory for making calls to the
0N/A * remote object
0N/A * @param ssf the server-side socket factory for receiving remote calls
0N/A * @return the activation identifier obtained from registering the
0N/A * descriptor with the activation system
0N/A * @exception ActivationException if activation group is not active
0N/A * @exception RemoteException if object registration or export fails
0N/A * @since 1.2
0N/A **/
0N/A public static ActivationID exportObject(Remote obj,
0N/A String location,
0N/A MarshalledObject<?> data,
0N/A boolean restart,
0N/A int port,
0N/A RMIClientSocketFactory csf,
0N/A RMIServerSocketFactory ssf)
0N/A throws ActivationException, RemoteException
0N/A {
0N/A ActivationDesc desc = new ActivationDesc(obj.getClass().getName(),
0N/A location, data, restart);
0N/A /*
0N/A * Register descriptor.
0N/A */
0N/A ActivationSystem system = ActivationGroup.getSystem();
0N/A ActivationID id = system.registerObject(desc);
0N/A
0N/A /*
0N/A * Export object.
0N/A */
0N/A try {
0N/A exportObject(obj, id, port, csf, ssf);
0N/A } catch (RemoteException e) {
0N/A /*
0N/A * Attempt to unregister activation descriptor because export
0N/A * failed and register/export should be atomic (see 4323621).
0N/A */
0N/A try {
0N/A system.unregisterObject(id);
0N/A } catch (Exception ex) {
0N/A }
0N/A /*
0N/A * Report original exception.
0N/A */
0N/A throw e;
0N/A }
0N/A
0N/A /*
0N/A * This call can't fail (it is a local call, and the only possible
0N/A * exception, thrown if the group is inactive, will not be thrown
0N/A * because the group is not inactive).
0N/A */
0N/A ActivationGroup.currentGroup().activeObject(id, obj);
0N/A
0N/A return id;
0N/A }
0N/A
0N/A /**
0N/A * Export the activatable remote object to the RMI runtime to make
0N/A * the object available to receive incoming calls. The object is
0N/A * exported on an anonymous port, if <code>port</code> is zero. <p>
0N/A *
0N/A * During activation, this <code>exportObject</code> method should
0N/A * be invoked explicitly by an "activatable" object, that does not
0N/A * extend the <code>Activatable</code> class. There is no need for objects
0N/A * that do extend the <code>Activatable</code> class to invoke this
0N/A * method directly because the object is exported during construction.
0N/A *
0N/A * @return the stub for the activatable remote object
0N/A * @param obj the remote object implementation
0N/A * @param id the object's activation identifier
0N/A * @param port the port on which the object is exported (an anonymous
0N/A * port is used if port=0)
0N/A * @exception RemoteException if object export fails
0N/A * @since 1.2
0N/A */
0N/A public static Remote exportObject(Remote obj,
0N/A ActivationID id,
0N/A int port)
0N/A throws RemoteException
0N/A {
0N/A return exportObject(obj, new ActivatableServerRef(id, port));
0N/A }
0N/A
0N/A /**
0N/A * Export the activatable remote object to the RMI runtime to make
0N/A * the object available to receive incoming calls. The object is
0N/A * exported on an anonymous port, if <code>port</code> is zero. <p>
0N/A *
0N/A * During activation, this <code>exportObject</code> method should
0N/A * be invoked explicitly by an "activatable" object, that does not
0N/A * extend the <code>Activatable</code> class. There is no need for objects
0N/A * that do extend the <code>Activatable</code> class to invoke this
0N/A * method directly because the object is exported during construction.
0N/A *
0N/A * @return the stub for the activatable remote object
0N/A * @param obj the remote object implementation
0N/A * @param id the object's activation identifier
0N/A * @param port the port on which the object is exported (an anonymous
0N/A * port is used if port=0)
0N/A * @param csf the client-side socket factory for making calls to the
0N/A * remote object
0N/A * @param ssf the server-side socket factory for receiving remote calls
0N/A * @exception RemoteException if object export fails
0N/A * @since 1.2
0N/A */
0N/A public static Remote exportObject(Remote obj,
0N/A ActivationID id,
0N/A int port,
0N/A RMIClientSocketFactory csf,
0N/A RMIServerSocketFactory ssf)
0N/A throws RemoteException
0N/A {
0N/A return exportObject(obj, new ActivatableServerRef(id, port, csf, ssf));
0N/A }
0N/A
0N/A /**
0N/A * Remove the remote object, obj, from the RMI runtime. If
0N/A * successful, the object can no longer accept incoming RMI calls.
0N/A * If the force parameter is true, the object is forcibly unexported
0N/A * even if there are pending calls to the remote object or the
0N/A * remote object still has calls in progress. If the force
0N/A * parameter is false, the object is only unexported if there are
0N/A * no pending or in progress calls to the object.
0N/A *
0N/A * @param obj the remote object to be unexported
0N/A * @param force if true, unexports the object even if there are
0N/A * pending or in-progress calls; if false, only unexports the object
0N/A * if there are no pending or in-progress calls
0N/A * @return true if operation is successful, false otherwise
0N/A * @exception NoSuchObjectException if the remote object is not
0N/A * currently exported
0N/A * @since 1.2
0N/A */
0N/A public static boolean unexportObject(Remote obj, boolean force)
0N/A throws NoSuchObjectException
0N/A {
0N/A return sun.rmi.transport.ObjectTable.unexportObject(obj, force);
0N/A }
0N/A
0N/A /**
0N/A * Exports the specified object using the specified server ref.
0N/A */
0N/A private static Remote exportObject(Remote obj, ActivatableServerRef sref)
0N/A throws RemoteException
0N/A {
0N/A // if obj extends Activatable, set its ref.
0N/A if (obj instanceof Activatable) {
0N/A ((Activatable) obj).ref = sref;
0N/A
0N/A }
0N/A return sref.exportObject(obj, null, false);
0N/A }
0N/A}