1. /*
  2. * @(#)ActivationGroup.java 1.44 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.rmi.activation;
  8. import java.lang.reflect.Constructor;
  9. import java.net.URL;
  10. import java.net.MalformedURLException;
  11. import java.rmi.MarshalledObject;
  12. import java.rmi.Naming;
  13. import java.rmi.activation.UnknownGroupException;
  14. import java.rmi.activation.UnknownObjectException;
  15. import java.rmi.Remote;
  16. import java.rmi.RemoteException;
  17. import java.rmi.server.UnicastRemoteObject;
  18. import java.rmi.server.RMIClassLoader;
  19. import java.security.PrivilegedExceptionAction;
  20. import java.security.PrivilegedActionException;
  21. import sun.security.action.GetIntegerAction;
  22. /**
  23. * An <code>ActivationGroup</code> is responsible for creating new
  24. * instances of "activatable" objects in its group, informing its
  25. * <code>ActivationMonitor</code> when either: its object's become
  26. * active or inactive, or the group as a whole becomes inactive. <p>
  27. *
  28. * An <code>ActivationGroup</code> is <i>initially</i> created in one
  29. * of several ways: <ul>
  30. * <li>as a side-effect of creating an <code>ActivationDesc</code>
  31. * without an explicit <code>ActivationGroupID</code> for the
  32. * first activatable object in the group, or
  33. * <li>via the <code>ActivationGroup.createGroup</code> method
  34. * <li>as a side-effect of activating the first object in a group
  35. * whose <code>ActivationGroupDesc</code> was only registered.</ul><p>
  36. *
  37. * Only the activator can <i>recreate</i> an
  38. * <code>ActivationGroup</code>. The activator spawns, as needed, a
  39. * separate VM (as a child process, for example) for each registered
  40. * activation group and directs activation requests to the appropriate
  41. * group. It is implementation specific how VMs are spawned. An
  42. * activation group is created via the
  43. * <code>ActivationGroup.createGroup</code> static method. The
  44. * <code>createGroup</code> method has two requirements on the group
  45. * to be created: 1) the group must be a concrete subclass of
  46. * <code>ActivationGroup</code>, and 2) the group must have a
  47. * constructor that takes two arguments:
  48. *
  49. * <ul>
  50. * <li> the group's <code>ActivationGroupID</code>, and
  51. * <li> the group's initialization data (in a
  52. * <code>java.rmi.MarshalledObject</code>)</ul><p>
  53. *
  54. * When created, the default implementation of
  55. * <code>ActivationGroup</code> will override the system properties
  56. * with the properties requested when its
  57. * <code>ActivationGroupDesc</code> was created, and will set a
  58. * <code>java.rmi.RMISecurityManager</code> as the default system
  59. * security manager. If your application requires specific properties
  60. * to be set when objects are activated in the group, the application
  61. * should create a special <code>Properties</code> object containing
  62. * these properties, then create an <code>ActivationGroupDesc</code>
  63. * with the <code>Properties</code> object, and use
  64. * <code>ActivationGroup.createGroup</code> before creating any
  65. * <code>ActivationDesc</code>s (before the default
  66. * <code>ActivationGroupDesc</code> is created). If your application
  67. * requires the use of a security manager other than
  68. * <code>java.rmi.RMISecurityManager</code>, in the
  69. * ActivativationGroupDescriptor properties list you can set
  70. * <code>java.security.manager</code> property to the name of the security
  71. * manager you would like to install.
  72. *
  73. * @author Ann Wollrath
  74. * @version 1.44, 03/12/19
  75. * @see ActivationInstantiator
  76. * @see ActivationGroupDesc
  77. * @see ActivationGroupID
  78. * @since 1.2
  79. */
  80. public abstract class ActivationGroup
  81. extends UnicastRemoteObject
  82. implements ActivationInstantiator
  83. {
  84. /**
  85. * @serial the group's identifier
  86. */
  87. private ActivationGroupID groupID;
  88. /**
  89. * @serial the group's monitor
  90. */
  91. private ActivationMonitor monitor;
  92. /**
  93. * @serial the group's incarnation number
  94. */
  95. private long incarnation;
  96. /** the current activation group for this VM */
  97. private static ActivationGroup currGroup;
  98. /** the current group's identifier */
  99. private static ActivationGroupID currGroupID;
  100. /** the current group's activation system */
  101. private static ActivationSystem currSystem;
  102. /** used to control a group being created only once */
  103. private static boolean canCreate = true;
  104. /** formal parameters for constructing an activation group */
  105. private static Class[] groupConstrParams = {
  106. ActivationGroupID.class, MarshalledObject.class
  107. };
  108. /** indicate compatibility with the Java 2 SDK v1.2 version of class */
  109. private static final long serialVersionUID = -7696947875314805420L;
  110. /**
  111. * Constructs an activation group with the given activation group
  112. * identifier. The group is exported as a
  113. * <code>java.rmi.server.UnicastRemoteObject</code>.
  114. *
  115. * @param groupID the group's identifier
  116. * @throws RemoteException if this group could not be exported
  117. * @since 1.2
  118. */
  119. protected ActivationGroup(ActivationGroupID groupID)
  120. throws RemoteException
  121. {
  122. // call super constructor to export the object
  123. super();
  124. this.groupID = groupID;
  125. }
  126. /**
  127. * The group's <code>inactiveObject</code> method is called
  128. * indirectly via a call to the <code>Activatable.inactive</code>
  129. * method. A remote object implementation must call
  130. * <code>Activatable</code>'s <code>inactive</code> method when
  131. * that object deactivates (the object deems that it is no longer
  132. * active). If the object does not call
  133. * <code>Activatable.inactive</code> when it deactivates, the
  134. * object will never be garbage collected since the group keeps
  135. * strong references to the objects it creates. <p>
  136. *
  137. * <p>The group's <code>inactiveObject</code> method unexports the
  138. * remote object from the RMI runtime so that the object can no
  139. * longer receive incoming RMI calls. An object will only be unexported
  140. * if the object has no pending or executing calls.
  141. * The subclass of <code>ActivationGroup</code> must override this
  142. * method and unexport the object. <p>
  143. *
  144. * <p>After removing the object from the RMI runtime, the group
  145. * must inform its <code>ActivationMonitor</code> (via the monitor's
  146. * <code>inactiveObject</code> method) that the remote object is
  147. * not currently active so that the remote object will be
  148. * re-activated by the activator upon a subsequent activation
  149. * request.<p>
  150. *
  151. * <p>This method simply informs the group's monitor that the object
  152. * is inactive. It is up to the concrete subclass of ActivationGroup
  153. * to fulfill the additional requirement of unexporting the object. <p>
  154. *
  155. * @param id the object's activation identifier
  156. * @return true if the object was successfully deactivated; otherwise
  157. * returns false.
  158. * @exception UnknownObjectException if object is unknown (may already
  159. * be inactive)
  160. * @exception RemoteException if call informing monitor fails
  161. * @exception ActivationException if group is inactive
  162. * @since 1.2
  163. */
  164. public boolean inactiveObject(ActivationID id)
  165. throws ActivationException, UnknownObjectException, RemoteException
  166. {
  167. getMonitor().inactiveObject(id);
  168. return true;
  169. }
  170. /**
  171. * The group's <code>activeObject</code> method is called when an
  172. * object is exported (either by <code>Activatable</code> object
  173. * construction or an explicit call to
  174. * <code>Activatable.exportObject</code>. The group must inform its
  175. * <code>ActivationMonitor</code> that the object is active (via
  176. * the monitor's <code>activeObject</code> method) if the group
  177. * hasn't already done so.
  178. *
  179. * @param id the object's identifier
  180. * @param obj the remote object implementation
  181. * @exception UnknownObjectException if object is not registered
  182. * @exception RemoteException if call informing monitor fails
  183. * @exception ActivationException if group is inactive
  184. * @since 1.2
  185. */
  186. public abstract void activeObject(ActivationID id, Remote obj)
  187. throws ActivationException, UnknownObjectException, RemoteException;
  188. /**
  189. * Create and set the activation group for the current VM. The
  190. * activation group can only be set if it is not currently set.
  191. * An activation group is set using the <code>createGroup</code>
  192. * method when the <code>Activator</code> initiates the
  193. * re-creation of an activation group in order to carry out
  194. * incoming <code>activate</code> requests. A group must first be
  195. * registered with the <code>ActivationSystem</code> before it can
  196. * be created via this method.
  197. *
  198. * <p>The group class specified by the
  199. * <code>ActivationGroupDesc</code> must be a concrete subclass of
  200. * <code>ActivationGroup</code> and have a public constructor that
  201. * takes two arguments: the <code>ActivationGroupID</code> for the
  202. * group and the <code>MarshalledObject</code> containing the
  203. * group's initialization data (obtained from the
  204. * <code>ActivationGroupDesc</code>.
  205. *
  206. * <p>If the group class name specified in the
  207. * <code>ActivationGroupDesc</code> is <code>null</code>, then
  208. * this method will behave as if the group descriptor contained
  209. * the name of the default activation group implementation class.
  210. *
  211. * <p>Note that if your application creates its own custom
  212. * activation group, a security manager must be set for that
  213. * group. Otherwise objects cannot be activated in the group.
  214. * <code>java.rmi.RMISecurityManager</code> is set by default.
  215. *
  216. * <p>If a security manager is already set in the group VM, this
  217. * method first calls the security manager's
  218. * <code>checkSetFactory</code> method. This could result in a
  219. * <code>SecurityException</code>. If your application needs to
  220. * set a different security manager, you must ensure that the
  221. * policy file specified by the group's
  222. * <code>ActivationGroupDesc</code> grants the group the necessary
  223. * permissions to set a new security manager. (Note: This will be
  224. * necessary if your group downloads and sets a security manager).
  225. *
  226. * <p>After the group is created, the
  227. * <code>ActivationSystem</code> is informed that the group is
  228. * active by calling the <code>activeGroup</code> method which
  229. * returns the <code>ActivationMonitor</code> for the group. The
  230. * application need not call <code>activeGroup</code>
  231. * independently since it is taken care of by this method.
  232. *
  233. * <p>Once a group is created, subsequent calls to the
  234. * <code>currentGroupID</code> method will return the identifier
  235. * for this group until the group becomes inactive.
  236. *
  237. * @param id the activation group's identifier
  238. * @param desc the activation group's descriptor
  239. * @param incarnation the group's incarnation number (zero on group's
  240. * initial creation)
  241. * @return the activation group for the VM
  242. * @exception ActivationException if group already exists or if error
  243. * occurs during group creation
  244. * @exception SecurityException if permission to create group is denied.
  245. * (Note: The default implementation of the security manager
  246. * <code>checkSetFactory</code>
  247. * method requires the RuntimePermission "setFactory")
  248. * @see SecurityManager#checkSetFactory
  249. * @since 1.2
  250. */
  251. public static synchronized
  252. ActivationGroup createGroup(ActivationGroupID id,
  253. final ActivationGroupDesc desc,
  254. long incarnation)
  255. throws ActivationException
  256. {
  257. SecurityManager security = System.getSecurityManager();
  258. if (security != null)
  259. security.checkSetFactory();
  260. if (currGroup != null)
  261. throw new ActivationException("group already exists");
  262. if (canCreate == false)
  263. throw new ActivationException("group deactivated and " +
  264. "cannot be recreated");
  265. try {
  266. // load group's class
  267. String groupClassName = desc.getClassName();
  268. /*
  269. * Fix for 4252236: resolution of the default
  270. * activation group implementation name should be
  271. * delayed until now.
  272. */
  273. if (groupClassName == null) {
  274. groupClassName = sun.rmi.server.ActivationGroupImpl.class.getName();
  275. }
  276. final String className = groupClassName;
  277. /*
  278. * Fix for 4170955: Because the default group
  279. * implementation is a sun.* class, the group class
  280. * needs to be loaded in a privileged block of code.
  281. */
  282. Class cl;
  283. try {
  284. cl = (Class) java.security.AccessController.
  285. doPrivileged(new PrivilegedExceptionAction() {
  286. public Object run() throws ClassNotFoundException,
  287. MalformedURLException
  288. {
  289. return RMIClassLoader.
  290. loadClass(desc.getLocation(), className);
  291. }
  292. });
  293. } catch (PrivilegedActionException pae) {
  294. throw new ActivationException("Could not load default group " +
  295. "implementation class",
  296. pae.getException());
  297. }
  298. // create group
  299. Constructor constructor = cl.getConstructor(groupConstrParams);
  300. Object[] params = new Object[] { id, desc.getData() };
  301. Object obj = constructor.newInstance(params);
  302. if (obj instanceof ActivationGroup) {
  303. ActivationGroup newGroup = (ActivationGroup) obj;
  304. currSystem = id.getSystem();
  305. newGroup.incarnation = incarnation;
  306. newGroup.monitor =
  307. currSystem.activeGroup(id, newGroup, incarnation);
  308. currGroup = newGroup;
  309. currGroupID = id;
  310. canCreate = false;
  311. } else {
  312. throw new ActivationException("group not correct class: " +
  313. obj.getClass().getName());
  314. }
  315. } catch (java.lang.reflect.InvocationTargetException e) {
  316. e.getTargetException().printStackTrace();
  317. throw new ActivationException("exception in group constructor",
  318. e.getTargetException());
  319. } catch (ActivationException e) {
  320. throw e;
  321. } catch (Exception e) {
  322. throw new ActivationException("exception creating group", e);
  323. }
  324. return currGroup;
  325. }
  326. /**
  327. * Returns the current activation group's identifier. Returns null
  328. * if no group is currently active for this VM.
  329. * @return the activation group's identifier
  330. * @since 1.2
  331. */
  332. public static synchronized ActivationGroupID currentGroupID() {
  333. return currGroupID;
  334. }
  335. /**
  336. * Returns the activation group identifier for the VM. If an
  337. * activation group does not exist for this VM, a default
  338. * activation group is created. A group can be created only once,
  339. * so if a group has already become active and deactivated.
  340. *
  341. * @return the activation group identifier
  342. * @exception ActivationException if error occurs during group
  343. * creation, if security manager is not set, or if the group
  344. * has already been created and deactivated.
  345. */
  346. static synchronized ActivationGroupID internalCurrentGroupID()
  347. throws ActivationException
  348. {
  349. if (currGroupID == null)
  350. throw new ActivationException("nonexistent group");
  351. return currGroupID;
  352. }
  353. /**
  354. * Set the activation system for the VM. The activation system can
  355. * only be set it if no group is currently active. If the activation
  356. * system is not set via this call, then the <code>getSystem</code>
  357. * method attempts to obtain a reference to the
  358. * <code>ActivationSystem</code> by looking up the name
  359. * "java.rmi.activation.ActivationSystem" in the Activator's
  360. * registry. By default, the port number used to look up the
  361. * activation system is defined by
  362. * <code>ActivationSystem.SYSTEM_PORT</code>. This port can be overridden
  363. * by setting the property <code>java.rmi.activation.port</code>.
  364. *
  365. * <p>If there is a security manager, this method first
  366. * calls the security manager's <code>checkSetFactory</code> method.
  367. * This could result in a SecurityException.
  368. *
  369. * @param system remote reference to the <code>ActivationSystem</code>
  370. * @exception ActivationException if activation system is already set
  371. * @exception SecurityException if permission to set the activation system is denied.
  372. * (Note: The default implementation of the security manager
  373. * <code>checkSetFactory</code>
  374. * method requires the RuntimePermission "setFactory")
  375. * @see #getSystem
  376. * @see SecurityManager#checkSetFactory
  377. * @since 1.2
  378. */
  379. public static synchronized void setSystem(ActivationSystem system)
  380. throws ActivationException
  381. {
  382. SecurityManager security = System.getSecurityManager();
  383. if (security != null)
  384. security.checkSetFactory();
  385. if (currSystem != null)
  386. throw new ActivationException("activation system already set");
  387. currSystem = system;
  388. }
  389. /**
  390. * Returns the activation system for the VM. The activation system
  391. * may be set by the <code>setSystem</code> method. If the
  392. * activation system is not set via the <code>setSystem</code>
  393. * method, then the <code>getSystem</code> method attempts to
  394. * obtain a reference to the <code>ActivationSystem</code> by
  395. * looking up the name "java.rmi.activation.ActivationSystem" in
  396. * the Activator's registry. By default, the port number used to
  397. * look up the activation system is defined by
  398. * <code>ActivationSystem.SYSTEM_PORT</code>. This port can be
  399. * overridden by setting the property
  400. * <code>java.rmi.activation.port</code>.
  401. *
  402. * @return the activation system for the VM/group
  403. * @exception ActivationException if activation system cannot be
  404. * obtained or is not bound
  405. * (means that it is not running)
  406. * @see #setSystem
  407. * @since 1.2
  408. */
  409. public static synchronized ActivationSystem getSystem()
  410. throws ActivationException
  411. {
  412. if (currSystem == null) {
  413. try {
  414. int port;
  415. port = ((Integer)java.security.AccessController.doPrivileged(
  416. new GetIntegerAction("java.rmi.activation.port",
  417. ActivationSystem.SYSTEM_PORT))).intValue();
  418. currSystem = (ActivationSystem)
  419. Naming.lookup("//:" + port +
  420. "/java.rmi.activation.ActivationSystem");
  421. } catch (Exception e) {
  422. throw new ActivationException(
  423. "unable to obtain ActivationSystem", e);
  424. }
  425. }
  426. return currSystem;
  427. }
  428. /**
  429. * This protected method is necessary for subclasses to
  430. * make the <code>activeObject</code> callback to the group's
  431. * monitor. The call is simply forwarded to the group's
  432. * <code>ActivationMonitor</code>.
  433. *
  434. * @param id the object's identifier
  435. * @param mobj a marshalled object containing the remote object's stub
  436. * @exception UnknownObjectException if object is not registered
  437. * @exception RemoteException if call informing monitor fails
  438. * @exception ActivationException if an activation error occurs
  439. * @since 1.2
  440. */
  441. protected void activeObject(ActivationID id, MarshalledObject mobj)
  442. throws ActivationException, UnknownObjectException, RemoteException
  443. {
  444. getMonitor().activeObject(id, mobj);
  445. }
  446. /**
  447. * This protected method is necessary for subclasses to
  448. * make the <code>inactiveGroup</code> callback to the group's
  449. * monitor. The call is simply forwarded to the group's
  450. * <code>ActivationMonitor</code>. Also, the current group
  451. * for the VM is set to null.
  452. *
  453. * @exception UnknownGroupException if group is not registered
  454. * @exception RemoteException if call informing monitor fails
  455. * @since 1.2
  456. */
  457. protected void inactiveGroup()
  458. throws UnknownGroupException, RemoteException
  459. {
  460. try {
  461. getMonitor().inactiveGroup(groupID, incarnation);
  462. } finally {
  463. destroyGroup();
  464. }
  465. }
  466. /**
  467. * Returns the monitor for the activation group.
  468. */
  469. private ActivationMonitor getMonitor() throws RemoteException {
  470. synchronized (ActivationGroup.class) {
  471. if (monitor != null) {
  472. return monitor;
  473. }
  474. }
  475. throw new RemoteException("monitor not received");
  476. }
  477. /**
  478. * Destroys the current group.
  479. */
  480. private static synchronized void destroyGroup() {
  481. currGroup = null;
  482. currGroupID = null;
  483. // NOTE: don't set currSystem to null since it may be needed
  484. }
  485. /**
  486. * Returns the current group for the VM.
  487. * @exception ActivationException if current group is null (not active)
  488. */
  489. static synchronized ActivationGroup currentGroup()
  490. throws ActivationException
  491. {
  492. if (currGroup == null) {
  493. throw new ActivationException("group is not active");
  494. }
  495. return currGroup;
  496. }
  497. }