1 7 8 package java.rmi.activation; 9 10 import java.lang.reflect.Constructor ; 11 12 import java.net.URL ; 13 import java.net.MalformedURLException ; 14 import java.rmi.MarshalledObject ; 15 import java.rmi.Naming ; 16 import java.rmi.activation.UnknownGroupException ; 17 import java.rmi.activation.UnknownObjectException ; 18 import java.rmi.Remote ; 19 import java.rmi.RemoteException ; 20 import java.rmi.server.UnicastRemoteObject ; 21 import java.rmi.server.RMIClassLoader ; 22 import java.security.PrivilegedExceptionAction ; 23 import java.security.PrivilegedActionException ; 24 25 import sun.security.action.GetIntegerAction; 26 27 85 public abstract class ActivationGroup 86 extends UnicastRemoteObject 87 implements ActivationInstantiator 88 { 89 92 private ActivationGroupID groupID; 93 94 97 private ActivationMonitor monitor; 98 99 102 private long incarnation; 103 104 105 private static ActivationGroup currGroup; 106 107 private static ActivationGroupID currGroupID; 108 109 private static ActivationSystem currSystem; 110 111 private static boolean canCreate = true; 112 113 private static Class [] groupConstrParams = { 114 ActivationGroupID .class, MarshalledObject .class 115 }; 116 117 118 private static final long serialVersionUID = -7696947875314805420L; 119 120 129 protected ActivationGroup(ActivationGroupID groupID) 130 throws RemoteException 131 { 132 super(); 134 this.groupID = groupID; 135 } 136 137 175 public boolean inactiveObject(ActivationID id) 176 throws ActivationException , UnknownObjectException , RemoteException 177 { 178 getMonitor().inactiveObject(id); 179 return true; 180 } 181 182 198 public abstract void activeObject(ActivationID id, Remote obj) 199 throws ActivationException , UnknownObjectException , RemoteException ; 200 201 264 public static synchronized 265 ActivationGroup createGroup(ActivationGroupID id, 266 final ActivationGroupDesc desc, 267 long incarnation) 268 throws ActivationException 269 { 270 SecurityManager security = System.getSecurityManager(); 271 if (security != null) 272 security.checkSetFactory(); 273 274 if (currGroup != null) 275 throw new ActivationException ("group already exists"); 276 277 if (canCreate == false) 278 throw new ActivationException ("group deactivated and " + 279 "cannot be recreated"); 280 281 try { 282 String groupClassName = desc.getClassName(); 284 285 290 if (groupClassName == null) { 291 groupClassName = sun.rmi.server.ActivationGroupImpl.class.getName(); 292 } 293 294 final String className = groupClassName; 295 296 301 Class cl; 302 try { 303 cl = (Class ) java.security.AccessController. 304 doPrivileged(new PrivilegedExceptionAction () { 305 public Object run() throws ClassNotFoundException , 306 MalformedURLException 307 { 308 return RMIClassLoader. 309 loadClass(desc.getLocation(), className); 310 } 311 }); 312 } catch (PrivilegedActionException pae) { 313 throw new ActivationException ("Could not load default group " + 314 "implementation class", 315 pae.getException()); 316 } 317 318 Constructor constructor = cl.getConstructor(groupConstrParams); 320 Object [] params = new Object [] { id, desc.getData() }; 321 322 Object obj = constructor.newInstance(params); 323 if (obj instanceof ActivationGroup ) { 324 ActivationGroup newGroup = (ActivationGroup ) obj; 325 currSystem = id.getSystem(); 326 newGroup.incarnation = incarnation; 327 newGroup.monitor = 328 currSystem.activeGroup(id, newGroup, incarnation); 329 currGroup = newGroup; 330 currGroupID = id; 331 canCreate = false; 332 } else { 333 throw new ActivationException ("group not correct class: " + 334 obj.getClass().getName()); 335 } 336 } catch (java.lang.reflect.InvocationTargetException e) { 337 e.getTargetException().printStackTrace(); 338 throw new ActivationException ("exception in group constructor", 339 e.getTargetException()); 340 341 } catch (ActivationException e) { 342 throw e; 343 344 } catch (Exception e) { 345 throw new ActivationException ("exception creating group", e); 346 } 347 348 return currGroup; 349 } 350 351 357 public static synchronized ActivationGroupID currentGroupID() { 358 return currGroupID; 359 } 360 361 372 static synchronized ActivationGroupID internalCurrentGroupID() 373 throws ActivationException 374 { 375 if (currGroupID == null) 376 throw new ActivationException ("nonexistent group"); 377 378 return currGroupID; 379 } 380 381 407 public static synchronized void setSystem(ActivationSystem system) 408 throws ActivationException 409 { 410 SecurityManager security = System.getSecurityManager(); 411 if (security != null) 412 security.checkSetFactory(); 413 414 if (currSystem != null) 415 throw new ActivationException ("activation system already set"); 416 417 currSystem = system; 418 } 419 420 440 public static synchronized ActivationSystem getSystem() 441 throws ActivationException 442 { 443 if (currSystem == null) { 444 try { 445 int port; 446 port = ((Integer )java.security.AccessController.doPrivileged( 447 new GetIntegerAction("java.rmi.activation.port", 448 ActivationSystem.SYSTEM_PORT))).intValue(); 449 currSystem = (ActivationSystem ) 450 Naming.lookup("//:" + port + 451 "/java.rmi.activation.ActivationSystem"); 452 } catch (Exception e) { 453 throw new ActivationException ( 454 "unable to obtain ActivationSystem", e); 455 } 456 } 457 return currSystem; 458 } 459 460 473 protected void activeObject(ActivationID id, MarshalledObject mobj) 474 throws ActivationException , UnknownObjectException , RemoteException 475 { 476 getMonitor().activeObject(id, mobj); 477 } 478 479 490 protected void inactiveGroup() 491 throws UnknownGroupException , RemoteException 492 { 493 try { 494 getMonitor().inactiveGroup(groupID, incarnation); 495 } finally { 496 destroyGroup(); 497 } 498 } 499 500 503 private ActivationMonitor getMonitor() throws RemoteException { 504 synchronized (ActivationGroup .class) { 505 if (monitor != null) { 506 return monitor; 507 } 508 } 509 throw new RemoteException ("monitor not received"); 510 } 511 512 515 private static synchronized void destroyGroup() { 516 currGroup = null; 517 currGroupID = null; 518 } 520 521 525 static synchronized ActivationGroup currentGroup() 526 throws ActivationException 527 { 528 if (currGroup == null) { 529 throw new ActivationException ("group is not active"); 530 } 531 return currGroup; 532 } 533 534 } 535 | Popular Tags |