001/* ActivationID.java -- the object activation identifier
002   Copyright (c) 1996, 1997, 1998, 1999, 2006 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.rmi.activation;
040
041import java.io.IOException;
042import java.io.ObjectInputStream;
043import java.io.ObjectOutputStream;
044import java.io.Serializable;
045import java.rmi.Remote;
046import java.rmi.RemoteException;
047import java.rmi.server.UID;
048
049/**
050 * Denotes the object that can be activated over time. The instance of the
051 * ActivationID for the given object can be obtained in the following ways:
052 * <ul>
053 * <li>via {@link Activatable#register(ActivationDesc)}</li>
054 * <li>via Activatable constructor</li>
055 * <li>via Activatable.exportObject
056 * <li>
057 * </ul>
058 * An instance of the ActivationID has the {@link UID} as its component and
059 * hence is globally unique.
060 *
061 * @author Audrius Meskauskas (audriusa@bioinformatics.org) (from stub)
062 */
063public class ActivationID
064    implements Serializable
065{
066  /**
067   * Use SVUID for interoperability.
068   */
069  static final long serialVersionUID = - 4608673054848209235L;
070
071  /**
072   * The activator.
073   */
074  transient Activator activator;
075
076  /**
077   * The UID, making this instance unique.
078   */
079  transient UID uid;
080
081  /**
082   * The activation group that has activated the object with this
083   * activation id. The field is filled in inside the group and is used
084   * to notify the group about the request to inactivated the object.
085   */
086  transient ActivationGroup group;
087
088  /**
089   * Create a new instance with the given activator.
090   *
091   * @param an_activator tha activator that should activate the object.
092   */
093  public ActivationID(Activator an_activator)
094  {
095    activator = an_activator;
096    uid = new UID();
097  }
098
099  /**
100   * Activate the object.
101   *
102   * @param force if true, always contact the group. Otherwise, the cached value
103   *          may be returned.
104   * @return the activated object
105   * @throws UnknownObjectException if the object is unknown
106   * @throws ActivationException if the activation has failed
107   * @throws RemoteException if the remote call has failed
108   */
109  public Remote activate(boolean force) throws ActivationException,
110      UnknownObjectException, RemoteException
111  {
112        try
113          {
114            return (Remote) activator.activate(this, force).get();
115          }
116        catch (IOException e)
117          {
118            ActivationException acex = new ActivationException("id "+uid, e);
119            throw acex;
120          }
121        catch (ClassNotFoundException e)
122          {
123            ActivationException acex = new ActivationException("id "+uid, e);
124            throw acex;
125          }
126  }
127
128  /**
129   * Returns the hash code of the activator.
130   */
131  public int hashCode()
132  {
133    return uid == null ? 0 : uid.hashCode();
134  }
135
136  /**
137   * Compares the activators for equality.
138   */
139  public boolean equals(Object obj)
140  {
141    if (obj instanceof ActivationID)
142      {
143        ActivationID that = (ActivationID) obj;
144        return eq(uid, that.uid);
145      }
146    else
147      return false;
148  }
149
150  /**
151   * Read the object from the input stream.
152   *
153   * @param in the stream to read from
154   *
155   * @throws IOException if thrown by the stream
156   * @throws ClassNotFoundException
157   */
158  private void readObject(ObjectInputStream in) throws IOException,
159      ClassNotFoundException
160  {
161     uid = (UID) in.readObject();
162     activator = (Activator) in.readObject();
163  }
164
165  /**
166   * Write the object to the output stream.
167   *
168   * @param out the stream to write int
169   * @throws IOException if thrown by the stream
170   * @throws ClassNotFoundException
171   */
172  private void writeObject(ObjectOutputStream out) throws IOException,
173      ClassNotFoundException
174  {
175    out.writeObject(uid);
176    out.writeObject(activator);
177  }
178
179  /**
180   * Compare by .equals if both a and b are not null, compare directly if at
181   * least one of them is null.
182   */
183  static final boolean eq(Object a, Object b)
184  {
185    if (a == null || b == null)
186      return a == b;
187    else
188      return a.equals(b);
189  }
190
191  /**
192   * Return the content based string representation.
193   */
194  public String toString()
195  {
196    return uid.toString();
197  }
198
199}