001    /* ActivationGroup.java -- the RMI activation group.
002       Copyright (c) 1996, 1997, 1998, 1999, 2006 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package java.rmi.activation;
040    
041    import gnu.java.rmi.activation.DefaultActivationGroup;
042    import gnu.java.rmi.activation.DefaultActivationSystem;
043    
044    import java.lang.reflect.Constructor;
045    import java.rmi.MarshalledObject;
046    import java.rmi.Remote;
047    import java.rmi.RemoteException;
048    import java.rmi.server.UnicastRemoteObject;
049    
050    /**
051     * The entity that receives the request to activate object and activates it.
052     * Frequently there is one activation group per virtual machine.
053     *
054     * @author Audrius Meskauskas (audriusa@Bioinformatics.org) (from stub)
055     */
056    public abstract class ActivationGroup
057        extends UnicastRemoteObject
058        implements ActivationInstantiator
059    {
060    
061      /**
062       * Use the SVUID for interoperability.
063       */
064      static final long serialVersionUID = - 7696947875314805420L;
065    
066      /**
067       * The Id of the current group on this VM (null if none).
068       */
069      static ActivationGroupID currentGroupId = null;
070    
071      /**
072       * The groups identifier.
073       */
074      final ActivationGroupID groupId;
075    
076      /**
077       * The groups activation monitor.
078       */
079      ActivationMonitor monitor;
080    
081      /**
082       * The groups incarnation number.
083       */
084      long incarnation;
085    
086      /**
087       * The groups activation system.
088       */
089      static ActivationSystem system;
090    
091      /**
092       * Used during the group creation (required constructor).
093       */
094      static final Class[] cConstructorTypes = new Class[]
095                                                       {
096                                                        ActivationGroupID.class,
097                                                        MarshalledObject.class
098                                                       };
099    
100      /**
101       * Create the new activation group with the given group id.
102       *
103       * @param aGroupId the group Id.
104       *
105       * @throws RemoteException if the group export fails.
106       */
107      protected ActivationGroup(ActivationGroupID aGroupId) throws RemoteException
108      {
109        groupId = aGroupId;
110      }
111    
112      /**
113       * The method is called when the object is exported. The group must notify
114       * the activation monitor, if this was not already done before.
115       *
116       * @param id the object activation id
117       * @param obj the remote object implementation
118       *
119       * @throws ActivationException if the group is inactive
120       * @throws UnknownObjectException if such object is not known
121       * @throws RemoteException if the call to monitor fails
122       */
123      public abstract void activeObject(ActivationID id, Remote obj)
124          throws ActivationException, UnknownObjectException, RemoteException;
125    
126      /**
127       * Notifies the monitor about the object being inactivated.
128       *
129       * @param id the object being inactivated.
130       * @return true always (must be overridden to return other values).
131       * @throws ActivationException never
132       * @throws UnknownObjectException if the object is not known
133       * @throws RemoteException if the remote call to monitor fails
134       */
135      public boolean inactiveObject(ActivationID id) throws ActivationException,
136          UnknownObjectException, RemoteException
137      {
138        if (monitor != null)
139          monitor.inactiveObject(id);
140        return true;
141      }
142    
143      /**
144       * Create the new instance of the activation group, using the class name and
145       * location information, stored in the passed descriptor. The method expects
146       * the group class to have the two parameter constructor, the first parameter
147       * being the {@link ActivationGroupID} and the second the
148       * {@link MarshalledObject}. The group must be first be registered with the
149       * ActivationSystem. Once a group is created, the currentGroupID method
150       * returns the identifier for this group until the group becomes inactive.
151       *
152       * @param id the activation group id
153       * @param desc the group descriptor, providing the information, necessary to
154       *          create the group
155       * @param incarnation the incarnation number
156       * @return the created group instance
157       * @throws ActivationException if the activation fails due any reason
158       */
159      public static ActivationGroup createGroup(ActivationGroupID id,
160                                                ActivationGroupDesc desc,
161                                                long incarnation)
162          throws ActivationException
163      {
164        // If the activation system is not yet set, set it to the system.
165        // passed in the group id.
166        if (system == null)
167          system = id.system;
168    
169        ActivationGroup group = null;
170    
171        // TODO at the moment all groups are created on the current jre and the
172        // group class must be reachable via thread context class loader.
173        Class groupClass;
174    
175        if (desc.className != null)
176          {
177            try
178              {
179                ClassLoader loader = Thread.currentThread().getContextClassLoader();
180                groupClass = loader.loadClass(desc.className);
181              }
182            catch (ClassNotFoundException e)
183              {
184                ActivationException acex = new ActivationException(
185                  "Cannot load " + desc.className);
186                acex.detail = e;
187                throw acex;
188              }
189          }
190        else
191          groupClass = DefaultActivationGroup.class;
192    
193        try
194          {
195            Constructor constructor = groupClass.getConstructor(cConstructorTypes);
196            group = (ActivationGroup) constructor.newInstance(
197              new Object[] { id, desc.data });
198          }
199        catch (Exception e)
200          {
201            ActivationException acex = new ActivationException(
202              "Cannot instantiate " + desc.className);
203            acex.detail = e;
204            throw acex;
205          }
206    
207        currentGroupId = id;
208        try
209          {
210            group.monitor = getSystem().activeGroup(id, group, incarnation);
211            return group;
212          }
213        catch (RemoteException e)
214          {
215            ActivationException acex = new ActivationException("createGroup");
216            acex.detail = e;
217            throw acex;
218          }
219      }
220    
221      /**
222       * Get the id of current activation group.
223       *
224       * @return the id of the current activation group or null if none exists.
225       */
226      public static ActivationGroupID currentGroupID()
227      {
228        try
229          {
230            if (currentGroupId==null)
231              {
232                // This will also assing the currentGroupId to the current
233                // (default) group of the default system.
234                setSystem(DefaultActivationSystem.get());
235              }
236          }
237        catch (ActivationException e)
238          {
239            InternalError ierr = new InternalError("Unable to activate AS");
240            ierr.initCause(e);
241            throw ierr;
242          }
243    
244        return currentGroupId;
245      }
246    
247      /**
248       * Set the activation system for this virtual machine. The system can only
249       * be set if no group is active.
250       *
251       * @param aSystem the system to set
252       *
253       * @throws ActivationException if some group is active now.
254       */
255      public static void setSystem(ActivationSystem aSystem)
256          throws ActivationException
257      {
258        if (currentGroupId!=null)
259          throw new ActivationException("Group active");
260        else
261          {
262            try
263              {
264                // Register the default transient activation system and group.
265                system = aSystem;
266                ActivationGroupDesc def = new ActivationGroupDesc(
267                   DefaultActivationGroup.class.getName(),
268                  "",
269                  null,
270                  null,
271                  null);
272                currentGroupId = system.registerGroup(def);
273              }
274            catch (Exception ex)
275            {
276              InternalError ierr = new InternalError("Unable to start default AG");
277              ierr.initCause(ex);
278              throw ierr;
279            }
280          }
281      }
282    
283      /**
284       * Get the current activation system. If the system is not set via
285       * {@link #setSystem} method, the default system for this virtual machine is
286       * returned. The default system is first searched by name
287       * "java.rmi.activation.ActivationSystem" on the activation registry port. The
288       * default value of the activation registry port is
289       * {@link ActivationSystem#SYSTEM_PORT}, but it can be changed by putting the
290       * system property java.rmi.activation.port. Both activation system and
291       * activation registry are provided by the RMI daemon tool, RMID, if it is
292       * running on the local host. If the RMID is not running, the internal
293       * transient activation system will be created and returned. This internal
294       * system is highly limited in in capabilities and is not intended to be used
295       * anywhere apart automated testing.
296       *
297       * @return the activation system for this virtual machine
298       * @throws ActivationException
299       */
300      public static ActivationSystem getSystem() throws ActivationException
301      {
302        if (system == null)
303          system = DefaultActivationSystem.get();
304        return system;
305      }
306    
307      /**
308       * Makes the call back to the groups {@link ActivationMonitor}.
309       *
310       * @param id the id obj the object being activated
311       * @param mObject the marshalled object, contains the activated remote object
312       *          stub.
313       * @throws ActivationException on activation error
314       * @throws UnknownObjectException if such object is not registered
315       * @throws RemoteException on remote call (to monitor) error
316       */
317      protected void activeObject(ActivationID id,
318                                  MarshalledObject<? extends Remote> mObject)
319          throws ActivationException, UnknownObjectException, RemoteException
320      {
321        if (monitor!=null)
322          monitor.activeObject(id, mObject);
323    
324        id.group = this;
325      }
326    
327      /**
328       * Makes the call back to the groups {@link ActivationMonitor} and sets
329       * the current group to null.
330       */
331      protected void inactiveGroup() throws UnknownGroupException, RemoteException
332      {
333        if (monitor!=null)
334          monitor.inactiveGroup(groupId, incarnation);
335    
336        if (currentGroupId!=null && currentGroupId.equals(groupId))
337          currentGroupId = null;
338      }
339    
340    }