Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/sun/rmi/server/Activation.java
+++ new/src/share/classes/sun/rmi/server/Activation.java
1 1 /*
2 2 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 package sun.rmi.server;
27 27
28 28 import java.io.ByteArrayOutputStream;
29 29 import java.io.File;
30 30 import java.io.FileOutputStream;
31 31 import java.io.IOException;
32 32 import java.io.InputStream;
33 33 import java.io.ObjectInputStream;
34 34 import java.io.OutputStream;
35 35 import java.io.PrintStream;
36 36 import java.io.PrintWriter;
37 37 import java.io.Serializable;
38 38 import java.lang.Process;
39 39 import java.lang.reflect.InvocationTargetException;
40 40 import java.lang.reflect.Method;
41 41 import java.net.InetAddress;
42 42 import java.net.ServerSocket;
43 43 import java.net.Socket;
44 44 import java.net.SocketAddress;
45 45 import java.net.SocketException;
46 46 import java.nio.channels.Channel;
47 47 import java.nio.channels.ServerSocketChannel;
48 48 import java.rmi.AccessException;
49 49 import java.rmi.AlreadyBoundException;
50 50 import java.rmi.ConnectException;
51 51 import java.rmi.ConnectIOException;
52 52 import java.rmi.MarshalledObject;
53 53 import java.rmi.NoSuchObjectException;
54 54 import java.rmi.NotBoundException;
55 55 import java.rmi.Remote;
56 56 import java.rmi.RemoteException;
57 57 import java.rmi.activation.ActivationDesc;
58 58 import java.rmi.activation.ActivationException;
59 59 import java.rmi.activation.ActivationGroupDesc;
60 60 import java.rmi.activation.ActivationGroup;
61 61 import java.rmi.activation.ActivationGroupID;
62 62 import java.rmi.activation.ActivationID;
63 63 import java.rmi.activation.ActivationInstantiator;
64 64 import java.rmi.activation.ActivationMonitor;
65 65 import java.rmi.activation.ActivationSystem;
66 66 import java.rmi.activation.Activator;
67 67 import java.rmi.activation.UnknownGroupException;
68 68 import java.rmi.activation.UnknownObjectException;
69 69 import java.rmi.registry.Registry;
70 70 import java.rmi.server.ObjID;
71 71 import java.rmi.server.RMIClassLoader;
72 72 import java.rmi.server.RMIClientSocketFactory;
73 73 import java.rmi.server.RMIServerSocketFactory;
74 74 import java.rmi.server.RemoteObject;
75 75 import java.rmi.server.RemoteServer;
76 76 import java.rmi.server.UnicastRemoteObject;
77 77 import java.security.AccessControlException;
78 78 import java.security.AccessController;
79 79 import java.security.AllPermission;
80 80 import java.security.CodeSource;
81 81 import java.security.Permission;
82 82 import java.security.PermissionCollection;
83 83 import java.security.Permissions;
84 84 import java.security.Policy;
85 85 import java.security.PrivilegedAction;
86 86 import java.security.PrivilegedExceptionAction;
87 87 import java.security.cert.Certificate;
88 88 import java.text.MessageFormat;
89 89 import java.util.ArrayList;
90 90 import java.util.Arrays;
91 91 import java.util.Date;
92 92 import java.util.Enumeration;
93 93 import java.util.HashMap;
94 94 import java.util.HashSet;
95 95 import java.util.Iterator;
96 96 import java.util.List;
97 97 import java.util.Map;
98 98 import java.util.MissingResourceException;
99 99 import java.util.Properties;
100 100 import java.util.ResourceBundle;
101 101 import java.util.Set;
102 102 import java.util.concurrent.ConcurrentHashMap;
103 103 import sun.rmi.log.LogHandler;
104 104 import sun.rmi.log.ReliableLog;
105 105 import sun.rmi.registry.RegistryImpl;
106 106 import sun.rmi.runtime.NewThreadAction;
107 107 import sun.rmi.server.UnicastServerRef;
108 108 import sun.rmi.transport.LiveRef;
109 109 import sun.security.action.GetBooleanAction;
110 110 import sun.security.action.GetIntegerAction;
111 111 import sun.security.action.GetPropertyAction;
112 112 import sun.security.provider.PolicyFile;
113 113 import com.sun.rmi.rmid.ExecPermission;
114 114 import com.sun.rmi.rmid.ExecOptionPermission;
115 115
116 116 /**
117 117 * The Activator facilitates remote object activation. A "faulting"
118 118 * remote reference calls the activator's <code>activate</code> method
119 119 * to obtain a "live" reference to a activatable remote object. Upon
120 120 * receiving a request for activation, the activator looks up the
121 121 * activation descriptor for the activation identifier, id, determines
122 122 * the group in which the object should be activated and invokes the
123 123 * activate method on the object's activation group (described by the
124 124 * remote interface <code>ActivationInstantiator</code>). The
125 125 * activator initiates the execution of activation groups as
126 126 * necessary. For example, if an activation group for a specific group
127 127 * identifier is not already executing, the activator will spawn a
128 128 * child process for the activation group. <p>
129 129 *
130 130 * The activator is responsible for monitoring and detecting when
↓ open down ↓ |
130 lines elided |
↑ open up ↑ |
131 131 * activation groups fail so that it can remove stale remote references
132 132 * from its internal tables. <p>
133 133 *
134 134 * @author Ann Wollrath
135 135 * @since 1.2
136 136 */
137 137 public class Activation implements Serializable {
138 138
139 139 /** indicate compatibility with JDK 1.2 version of class */
140 140 private static final long serialVersionUID = 2921265612698155191L;
141 -
142 141 private static final byte MAJOR_VERSION = 1;
143 142 private static final byte MINOR_VERSION = 0;
144 143
145 144 /** exec policy object */
146 145 private static Object execPolicy;
147 146 private static Method execPolicyMethod;
148 147 private static boolean debugExec;
149 148
150 149 /** maps activation id to its respective group id */
151 150 private Map<ActivationID,ActivationGroupID> idTable =
152 151 new ConcurrentHashMap<>();
153 152 /** maps group id to its GroupEntry groups */
154 153 private Map<ActivationGroupID,GroupEntry> groupTable =
155 154 new ConcurrentHashMap<>();
156 155
157 156 private byte majorVersion = MAJOR_VERSION;
158 157 private byte minorVersion = MINOR_VERSION;
159 158
160 159 /** number of simultaneous group exec's */
161 160 private transient int groupSemaphore;
162 161 /** counter for numbering groups */
163 162 private transient int groupCounter;
164 163 /** reliable log to hold descriptor table */
165 164 private transient ReliableLog log;
166 165 /** number of updates since last snapshot */
167 166 private transient int numUpdates;
168 167
169 168 /** the java command */
170 169 // accessed by GroupEntry
171 170 private transient String[] command;
172 171 /** timeout on wait for child process to be created or destroyed */
173 172 private static final long groupTimeout =
174 173 getInt("sun.rmi.activation.groupTimeout", 60000);
175 174 /** take snapshot after this many updates */
176 175 private static final int snapshotInterval =
177 176 getInt("sun.rmi.activation.snapshotInterval", 200);
178 177 /** timeout on wait for child process to be created */
179 178 private static final long execTimeout =
180 179 getInt("sun.rmi.activation.execTimeout", 30000);
181 180
182 181 private static final Object initLock = new Object();
183 182 private static boolean initDone = false;
184 183
185 184 // this should be a *private* method since it is privileged
186 185 private static int getInt(String name, int def) {
187 186 return AccessController.doPrivileged(new GetIntegerAction(name, def));
188 187 }
189 188
190 189 private transient Activator activator;
191 190 private transient Activator activatorStub;
192 191 private transient ActivationSystem system;
193 192 private transient ActivationSystem systemStub;
194 193 private transient ActivationMonitor monitor;
195 194 private transient Registry registry;
196 195 private transient volatile boolean shuttingDown = false;
197 196 private transient volatile Object startupLock;
198 197 private transient Thread shutdownHook;
199 198
200 199 private static ResourceBundle resources = null;
201 200
202 201 /**
203 202 * Create an uninitialized instance of Activation that can be
204 203 * populated with log data. This is only called when the initial
205 204 * snapshot is taken during the first incarnation of rmid.
206 205 */
207 206 private Activation() {}
208 207
209 208 /**
210 209 * Recover activation state from the reliable log and initialize
211 210 * activation services.
212 211 */
213 212 private static void startActivation(int port,
214 213 RMIServerSocketFactory ssf,
215 214 String logName,
216 215 String[] childArgs)
217 216 throws Exception
218 217 {
219 218 ReliableLog log = new ReliableLog(logName, new ActLogHandler());
220 219 Activation state = (Activation) log.recover();
221 220 state.init(port, ssf, log, childArgs);
222 221 }
223 222
224 223 /**
225 224 * Initialize the Activation instantiation; start activation
226 225 * services.
227 226 */
228 227 private void init(int port,
229 228 RMIServerSocketFactory ssf,
230 229 ReliableLog log,
231 230 String[] childArgs)
232 231 throws Exception
233 232 {
234 233 // initialize
235 234 this.log = log;
236 235 numUpdates = 0;
237 236 shutdownHook = new ShutdownHook();
238 237 groupSemaphore = getInt("sun.rmi.activation.groupThrottle", 3);
239 238 groupCounter = 0;
240 239 Runtime.getRuntime().addShutdownHook(shutdownHook);
241 240
242 241 // Use array size of 0, since the value from calling size()
243 242 // may be out of date by the time toArray() is called.
244 243 ActivationGroupID[] gids =
245 244 groupTable.keySet().toArray(new ActivationGroupID[0]);
246 245
247 246 synchronized (startupLock = new Object()) {
248 247 // all the remote methods briefly synchronize on startupLock
249 248 // (via checkShutdown) to make sure they don't happen in the
250 249 // middle of this block. This block must not cause any such
251 250 // incoming remote calls to happen, or deadlock would result!
252 251 activator = new ActivatorImpl(port, ssf);
253 252 activatorStub = (Activator) RemoteObject.toStub(activator);
254 253 system = new ActivationSystemImpl(port, ssf);
255 254 systemStub = (ActivationSystem) RemoteObject.toStub(system);
256 255 monitor = new ActivationMonitorImpl(port, ssf);
257 256 initCommand(childArgs);
258 257 registry = new SystemRegistryImpl(port, null, ssf, systemStub);
259 258
260 259 if (ssf != null) {
261 260 synchronized (initLock) {
262 261 initDone = true;
263 262 initLock.notifyAll();
264 263 }
265 264 }
266 265 }
267 266 startupLock = null;
268 267
269 268 // restart services
270 269 for (int i = gids.length; --i >= 0; ) {
271 270 try {
272 271 getGroupEntry(gids[i]).restartServices();
273 272 } catch (UnknownGroupException e) {
274 273 System.err.println(
275 274 getTextResource("rmid.restart.group.warning"));
276 275 e.printStackTrace();
277 276 }
278 277 }
279 278 }
280 279
281 280 /**
282 281 * Previous versions used HashMap instead of ConcurrentHashMap.
283 282 * Replace any HashMaps found during deserialization with
284 283 * ConcurrentHashMaps.
285 284 */
286 285 private void readObject(ObjectInputStream ois)
287 286 throws IOException, ClassNotFoundException
288 287 {
289 288 ois.defaultReadObject();
290 289 if (! (groupTable instanceof ConcurrentHashMap)) {
↓ open down ↓ |
139 lines elided |
↑ open up ↑ |
291 290 groupTable = new ConcurrentHashMap<>(groupTable);
292 291 }
293 292 if (! (idTable instanceof ConcurrentHashMap)) {
294 293 idTable = new ConcurrentHashMap<>(idTable);
295 294 }
296 295 }
297 296
298 297 private static class SystemRegistryImpl extends RegistryImpl {
299 298
300 299 private static final String NAME = ActivationSystem.class.getName();
300 + private static final long serialVersionUID = 4877330021609408794L;
301 301 private final ActivationSystem systemStub;
302 302
303 303 SystemRegistryImpl(int port,
304 304 RMIClientSocketFactory csf,
305 305 RMIServerSocketFactory ssf,
306 306 ActivationSystem systemStub)
307 307 throws RemoteException
308 308 {
309 309 super(port, csf, ssf);
310 310 this.systemStub = systemStub;
311 311 }
312 312
313 313 /**
314 314 * Returns the activation system stub if the specified name
315 315 * matches the activation system's class name, otherwise
316 316 * returns the result of invoking super.lookup with the specified
317 317 * name.
318 318 */
319 319 public Remote lookup(String name)
320 320 throws RemoteException, NotBoundException
321 321 {
322 322 if (name.equals(NAME)) {
323 323 return systemStub;
324 324 } else {
325 325 return super.lookup(name);
326 326 }
327 327 }
328 328
329 329 public String[] list() throws RemoteException {
330 330 String[] list1 = super.list();
331 331 int length = list1.length;
332 332 String[] list2 = new String[length + 1];
333 333 if (length > 0) {
334 334 System.arraycopy(list1, 0, list2, 0, length);
335 335 }
336 336 list2[length] = NAME;
337 337 return list2;
338 338 }
339 339
340 340 public void bind(String name, Remote obj)
341 341 throws RemoteException, AlreadyBoundException, AccessException
342 342 {
343 343 if (name.equals(NAME)) {
344 344 throw new AccessException(
345 345 "binding ActivationSystem is disallowed");
346 346 } else {
347 347 super.bind(name, obj);
348 348 }
349 349 }
350 350
351 351 public void unbind(String name)
352 352 throws RemoteException, NotBoundException, AccessException
353 353 {
354 354 if (name.equals(NAME)) {
355 355 throw new AccessException(
356 356 "unbinding ActivationSystem is disallowed");
357 357 } else {
358 358 super.unbind(name);
359 359 }
360 360 }
361 361
362 362
363 363 public void rebind(String name, Remote obj)
364 364 throws RemoteException, AccessException
365 365 {
366 366 if (name.equals(NAME)) {
367 367 throw new AccessException(
368 368 "binding ActivationSystem is disallowed");
369 369 } else {
370 370 super.rebind(name, obj);
371 371 }
372 372 }
373 373 }
374 374
375 375
376 376 class ActivatorImpl extends RemoteServer implements Activator {
377 377 // Because ActivatorImpl has a fixed ObjID, it can be
378 378 // called by clients holding stale remote references. Each of
379 379 // its remote methods, then, must check startupLock (calling
380 380 // checkShutdown() is easiest).
381 381
382 382 private static final long serialVersionUID = -3654244726254566136L;
383 383
384 384 /**
385 385 * Construct a new Activator on a specified port.
386 386 */
387 387 ActivatorImpl(int port, RMIServerSocketFactory ssf)
388 388 throws RemoteException
389 389 {
390 390 /* Server ref must be created and assigned before remote object
391 391 * 'this' can be exported.
392 392 */
393 393 LiveRef lref =
394 394 new LiveRef(new ObjID(ObjID.ACTIVATOR_ID), port, null, ssf);
395 395 UnicastServerRef uref = new UnicastServerRef(lref);
396 396 ref = uref;
397 397 uref.exportObject(this, null, false);
398 398 }
399 399
400 400 public MarshalledObject<? extends Remote> activate(ActivationID id,
401 401 boolean force)
402 402 throws ActivationException, UnknownObjectException, RemoteException
403 403 {
404 404 checkShutdown();
405 405 return getGroupEntry(id).activate(id, force);
406 406 }
407 407 }
408 408
409 409 class ActivationMonitorImpl extends UnicastRemoteObject
410 410 implements ActivationMonitor
411 411 {
412 412 private static final long serialVersionUID = -6214940464757948867L;
413 413
414 414 ActivationMonitorImpl(int port, RMIServerSocketFactory ssf)
415 415 throws RemoteException
416 416 {
417 417 super(port, null, ssf);
418 418 }
419 419
420 420 public void inactiveObject(ActivationID id)
421 421 throws UnknownObjectException, RemoteException
422 422 {
423 423 try {
424 424 checkShutdown();
425 425 } catch (ActivationException e) {
426 426 return;
427 427 }
428 428 RegistryImpl.checkAccess("Activator.inactiveObject");
429 429 getGroupEntry(id).inactiveObject(id);
430 430 }
431 431
432 432 public void activeObject(ActivationID id,
433 433 MarshalledObject<? extends Remote> mobj)
434 434 throws UnknownObjectException, RemoteException
435 435 {
436 436 try {
437 437 checkShutdown();
438 438 } catch (ActivationException e) {
439 439 return;
440 440 }
441 441 RegistryImpl.checkAccess("ActivationSystem.activeObject");
442 442 getGroupEntry(id).activeObject(id, mobj);
443 443 }
444 444
445 445 public void inactiveGroup(ActivationGroupID id,
446 446 long incarnation)
447 447 throws UnknownGroupException, RemoteException
448 448 {
449 449 try {
450 450 checkShutdown();
451 451 } catch (ActivationException e) {
452 452 return;
453 453 }
454 454 RegistryImpl.checkAccess("ActivationMonitor.inactiveGroup");
455 455 getGroupEntry(id).inactiveGroup(incarnation, false);
456 456 }
457 457 }
458 458
459 459
460 460 class ActivationSystemImpl
461 461 extends RemoteServer
462 462 implements ActivationSystem
463 463 {
464 464 private static final long serialVersionUID = 9100152600327688967L;
465 465
466 466 // Because ActivationSystemImpl has a fixed ObjID, it can be
467 467 // called by clients holding stale remote references. Each of
468 468 // its remote methods, then, must check startupLock (calling
469 469 // checkShutdown() is easiest).
470 470 ActivationSystemImpl(int port, RMIServerSocketFactory ssf)
471 471 throws RemoteException
472 472 {
473 473 /* Server ref must be created and assigned before remote object
474 474 * 'this' can be exported.
475 475 */
476 476 LiveRef lref = new LiveRef(new ObjID(4), port, null, ssf);
477 477 UnicastServerRef uref = new UnicastServerRef(lref);
478 478 ref = uref;
479 479 uref.exportObject(this, null);
480 480 }
481 481
482 482 public ActivationID registerObject(ActivationDesc desc)
483 483 throws ActivationException, UnknownGroupException, RemoteException
484 484 {
485 485 checkShutdown();
486 486 RegistryImpl.checkAccess("ActivationSystem.registerObject");
487 487
488 488 ActivationGroupID groupID = desc.getGroupID();
489 489 ActivationID id = new ActivationID(activatorStub);
490 490 getGroupEntry(groupID).registerObject(id, desc, true);
491 491 return id;
492 492 }
493 493
494 494 public void unregisterObject(ActivationID id)
495 495 throws ActivationException, UnknownObjectException, RemoteException
496 496 {
497 497 checkShutdown();
498 498 RegistryImpl.checkAccess("ActivationSystem.unregisterObject");
499 499 getGroupEntry(id).unregisterObject(id, true);
500 500 }
501 501
502 502 public ActivationGroupID registerGroup(ActivationGroupDesc desc)
503 503 throws ActivationException, RemoteException
504 504 {
505 505 checkShutdown();
506 506 RegistryImpl.checkAccess("ActivationSystem.registerGroup");
507 507 checkArgs(desc, null);
508 508
509 509 ActivationGroupID id = new ActivationGroupID(systemStub);
510 510 GroupEntry entry = new GroupEntry(id, desc);
511 511 // table insertion must take place before log update
512 512 groupTable.put(id, entry);
513 513 addLogRecord(new LogRegisterGroup(id, desc));
514 514 return id;
515 515 }
516 516
517 517 public ActivationMonitor activeGroup(ActivationGroupID id,
518 518 ActivationInstantiator group,
519 519 long incarnation)
520 520 throws ActivationException, UnknownGroupException, RemoteException
521 521 {
522 522 checkShutdown();
523 523 RegistryImpl.checkAccess("ActivationSystem.activeGroup");
524 524
525 525 getGroupEntry(id).activeGroup(group, incarnation);
526 526 return monitor;
527 527 }
528 528
529 529 public void unregisterGroup(ActivationGroupID id)
530 530 throws ActivationException, UnknownGroupException, RemoteException
531 531 {
532 532 checkShutdown();
533 533 RegistryImpl.checkAccess("ActivationSystem.unregisterGroup");
534 534
535 535 // remove entry before unregister so state is updated before
536 536 // logged
537 537 removeGroupEntry(id).unregisterGroup(true);
538 538 }
539 539
540 540 public ActivationDesc setActivationDesc(ActivationID id,
541 541 ActivationDesc desc)
542 542 throws ActivationException, UnknownObjectException, RemoteException
543 543 {
544 544 checkShutdown();
545 545 RegistryImpl.checkAccess("ActivationSystem.setActivationDesc");
546 546
547 547 if (!getGroupID(id).equals(desc.getGroupID())) {
548 548 throw new ActivationException(
549 549 "ActivationDesc contains wrong group");
550 550 }
551 551 return getGroupEntry(id).setActivationDesc(id, desc, true);
552 552 }
553 553
554 554 public ActivationGroupDesc setActivationGroupDesc(ActivationGroupID id,
555 555 ActivationGroupDesc desc)
556 556 throws ActivationException, UnknownGroupException, RemoteException
557 557 {
558 558 checkShutdown();
559 559 RegistryImpl.checkAccess(
560 560 "ActivationSystem.setActivationGroupDesc");
561 561
562 562 checkArgs(desc, null);
563 563 return getGroupEntry(id).setActivationGroupDesc(id, desc, true);
564 564 }
565 565
566 566 public ActivationDesc getActivationDesc(ActivationID id)
567 567 throws ActivationException, UnknownObjectException, RemoteException
568 568 {
569 569 checkShutdown();
570 570 RegistryImpl.checkAccess("ActivationSystem.getActivationDesc");
571 571
572 572 return getGroupEntry(id).getActivationDesc(id);
573 573 }
574 574
575 575 public ActivationGroupDesc getActivationGroupDesc(ActivationGroupID id)
576 576 throws ActivationException, UnknownGroupException, RemoteException
577 577 {
578 578 checkShutdown();
579 579 RegistryImpl.checkAccess
580 580 ("ActivationSystem.getActivationGroupDesc");
581 581
582 582 return getGroupEntry(id).desc;
583 583 }
584 584
585 585 /**
586 586 * Shutdown the activation system. Destroys all groups spawned by
587 587 * the activation daemon and exits the activation daemon.
588 588 */
589 589 public void shutdown() throws AccessException {
590 590 RegistryImpl.checkAccess("ActivationSystem.shutdown");
591 591
592 592 Object lock = startupLock;
593 593 if (lock != null) {
594 594 synchronized (lock) {
595 595 // nothing
596 596 }
597 597 }
598 598
599 599 synchronized (Activation.this) {
600 600 if (!shuttingDown) {
601 601 shuttingDown = true;
602 602 (new Shutdown()).start();
603 603 }
604 604 }
605 605 }
606 606 }
607 607
608 608 private void checkShutdown() throws ActivationException {
609 609 // if the startup critical section is running, wait until it
610 610 // completes/fails before continuing with the remote call.
611 611 Object lock = startupLock;
612 612 if (lock != null) {
613 613 synchronized (lock) {
614 614 // nothing
615 615 }
616 616 }
617 617
618 618 if (shuttingDown == true) {
619 619 throw new ActivationException(
620 620 "activation system shutting down");
621 621 }
622 622 }
623 623
624 624 private static void unexport(Remote obj) {
625 625 for (;;) {
626 626 try {
627 627 if (UnicastRemoteObject.unexportObject(obj, false) == true) {
628 628 break;
629 629 } else {
630 630 Thread.sleep(100);
631 631 }
632 632 } catch (Exception e) {
633 633 continue;
634 634 }
635 635 }
636 636 }
637 637
638 638 /**
639 639 * Thread to shutdown rmid.
640 640 */
641 641 private class Shutdown extends Thread {
642 642 Shutdown() {
643 643 super("rmid Shutdown");
644 644 }
645 645
646 646 public void run() {
647 647 try {
648 648 /*
649 649 * Unexport activation system services
650 650 */
651 651 unexport(activator);
652 652 unexport(system);
653 653
654 654 // destroy all child processes (groups)
655 655 for (GroupEntry groupEntry : groupTable.values()) {
656 656 groupEntry.shutdown();
657 657 }
658 658
659 659 Runtime.getRuntime().removeShutdownHook(shutdownHook);
660 660
661 661 /*
662 662 * Unexport monitor safely since all processes are destroyed.
663 663 */
664 664 unexport(monitor);
665 665
666 666 /*
667 667 * Close log file, fix for 4243264: rmid shutdown thread
668 668 * interferes with remote calls in progress. Make sure
669 669 * the log file is only closed when it is impossible for
670 670 * its closure to interfere with any pending remote calls.
671 671 * We close the log when all objects in the rmid VM are
672 672 * unexported.
673 673 */
674 674 try {
675 675 synchronized (log) {
676 676 log.close();
677 677 }
678 678 } catch (IOException e) {
679 679 }
680 680
681 681 } finally {
682 682 /*
683 683 * Now exit... A System.exit should only be done if
684 684 * the RMI activation system daemon was started up
685 685 * by the main method below (in which should always
686 686 * be the case since the Activation contructor is private).
687 687 */
688 688 System.err.println(getTextResource("rmid.daemon.shutdown"));
689 689 System.exit(0);
690 690 }
691 691 }
692 692 }
693 693
694 694 /** Thread to destroy children in the event of abnormal termination. */
695 695 private class ShutdownHook extends Thread {
696 696 ShutdownHook() {
697 697 super("rmid ShutdownHook");
698 698 }
699 699
700 700 public void run() {
701 701 synchronized (Activation.this) {
702 702 shuttingDown = true;
703 703 }
704 704
705 705 // destroy all child processes (groups) quickly
706 706 for (GroupEntry groupEntry : groupTable.values()) {
707 707 groupEntry.shutdownFast();
708 708 }
709 709 }
710 710 }
711 711
712 712 /**
713 713 * Returns the groupID for a given id of an object in the group.
714 714 * Throws UnknownObjectException if the object is not registered.
715 715 */
716 716 private ActivationGroupID getGroupID(ActivationID id)
717 717 throws UnknownObjectException
718 718 {
719 719 ActivationGroupID groupID = idTable.get(id);
720 720 if (groupID != null) {
721 721 return groupID;
722 722 }
723 723 throw new UnknownObjectException("unknown object: " + id);
724 724 }
725 725
726 726 /**
727 727 * Returns the group entry for the group id, optionally removing it.
728 728 * Throws UnknownGroupException if the group is not registered.
729 729 */
730 730 private GroupEntry getGroupEntry(ActivationGroupID id, boolean rm)
731 731 throws UnknownGroupException
732 732 {
733 733 if (id.getClass() == ActivationGroupID.class) {
734 734 GroupEntry entry;
735 735 if (rm) {
736 736 entry = groupTable.remove(id);
737 737 } else {
738 738 entry = groupTable.get(id);
739 739 }
740 740 if (entry != null && !entry.removed) {
741 741 return entry;
742 742 }
743 743 }
744 744 throw new UnknownGroupException("group unknown");
745 745 }
746 746
747 747 /**
748 748 * Returns the group entry for the group id. Throws
749 749 * UnknownGroupException if the group is not registered.
750 750 */
751 751 private GroupEntry getGroupEntry(ActivationGroupID id)
752 752 throws UnknownGroupException
753 753 {
754 754 return getGroupEntry(id, false);
755 755 }
756 756
757 757 /**
758 758 * Removes and returns the group entry for the group id. Throws
759 759 * UnknownGroupException if the group is not registered.
760 760 */
761 761 private GroupEntry removeGroupEntry(ActivationGroupID id)
762 762 throws UnknownGroupException
763 763 {
764 764 return getGroupEntry(id, true);
765 765 }
766 766
767 767 /**
768 768 * Returns the group entry for the object's id. Throws
769 769 * UnknownObjectException if the object is not registered or the
770 770 * object's group is not registered.
771 771 */
772 772 private GroupEntry getGroupEntry(ActivationID id)
773 773 throws UnknownObjectException
774 774 {
775 775 ActivationGroupID gid = getGroupID(id);
776 776 GroupEntry entry = groupTable.get(gid);
777 777 if (entry != null && !entry.removed) {
778 778 return entry;
779 779 }
780 780 throw new UnknownObjectException("object's group removed");
781 781 }
782 782
783 783 /**
784 784 * Container for group information: group's descriptor, group's
785 785 * instantiator, flag to indicate pending group creation, and
786 786 * table of the group's actived objects.
787 787 *
788 788 * WARNING: GroupEntry objects should not be written into log file
789 789 * updates. GroupEntrys are inner classes of Activation and they
790 790 * can not be serialized independent of this class. If the
791 791 * complete Activation system is written out as a log update, the
792 792 * point of having updates is nullified.
793 793 */
794 794 private class GroupEntry implements Serializable {
795 795
796 796 /** indicate compatibility with JDK 1.2 version of class */
↓ open down ↓ |
486 lines elided |
↑ open up ↑ |
797 797 private static final long serialVersionUID = 7222464070032993304L;
798 798 private static final int MAX_TRIES = 2;
799 799 private static final int NORMAL = 0;
800 800 private static final int CREATING = 1;
801 801 private static final int TERMINATE = 2;
802 802 private static final int TERMINATING = 3;
803 803
804 804 ActivationGroupDesc desc = null;
805 805 ActivationGroupID groupID = null;
806 806 long incarnation = 0;
807 - Map<ActivationID,ObjectEntry> objects =
808 - new HashMap<ActivationID,ObjectEntry>();
809 - Set<ActivationID> restartSet = new HashSet<ActivationID>();
807 + Map<ActivationID,ObjectEntry> objects = new HashMap<>();
808 + Set<ActivationID> restartSet = new HashSet<>();
810 809
811 810 transient ActivationInstantiator group = null;
812 811 transient int status = NORMAL;
813 812 transient long waitTime = 0;
814 813 transient String groupName = null;
815 814 transient Process child = null;
816 815 transient boolean removed = false;
817 816 transient Watchdog watchdog = null;
818 817
819 818 GroupEntry(ActivationGroupID groupID, ActivationGroupDesc desc) {
820 819 this.groupID = groupID;
821 820 this.desc = desc;
822 821 }
823 822
824 823 void restartServices() {
825 824 Iterator<ActivationID> iter = null;
826 825
827 826 synchronized (this) {
828 827 if (restartSet.isEmpty()) {
829 828 return;
830 829 }
831 830
832 831 /*
833 832 * Clone the restartSet so the set does not have to be locked
834 833 * during iteration. Locking the restartSet could cause
835 834 * deadlock if an object we are restarting caused another
836 835 * object in this group to be activated.
837 836 */
838 837 iter = (new HashSet<ActivationID>(restartSet)).iterator();
839 838 }
840 839
841 840 while (iter.hasNext()) {
842 841 ActivationID id = iter.next();
843 842 try {
844 843 activate(id, true);
845 844 } catch (Exception e) {
846 845 if (shuttingDown) {
847 846 return;
848 847 }
849 848 System.err.println(
850 849 getTextResource("rmid.restart.service.warning"));
851 850 e.printStackTrace();
852 851 }
853 852 }
854 853 }
855 854
856 855 synchronized void activeGroup(ActivationInstantiator inst,
857 856 long instIncarnation)
858 857 throws ActivationException, UnknownGroupException
859 858 {
860 859 if (incarnation != instIncarnation) {
861 860 throw new ActivationException("invalid incarnation");
862 861 }
863 862
864 863 if (group != null) {
865 864 if (group.equals(inst)) {
866 865 return;
867 866 } else {
868 867 throw new ActivationException("group already active");
869 868 }
870 869 }
871 870
872 871 if (child != null && status != CREATING) {
873 872 throw new ActivationException("group not being created");
874 873 }
875 874
876 875 group = inst;
877 876 status = NORMAL;
878 877 notifyAll();
879 878 }
880 879
881 880 private void checkRemoved() throws UnknownGroupException {
882 881 if (removed) {
883 882 throw new UnknownGroupException("group removed");
884 883 }
885 884 }
886 885
887 886 private ObjectEntry getObjectEntry(ActivationID id)
888 887 throws UnknownObjectException
889 888 {
890 889 if (removed) {
891 890 throw new UnknownObjectException("object's group removed");
892 891 }
893 892 ObjectEntry objEntry = objects.get(id);
894 893 if (objEntry == null) {
895 894 throw new UnknownObjectException("object unknown");
896 895 }
897 896 return objEntry;
898 897 }
899 898
900 899 synchronized void registerObject(ActivationID id,
901 900 ActivationDesc desc,
902 901 boolean addRecord)
903 902 throws UnknownGroupException, ActivationException
904 903 {
905 904 checkRemoved();
906 905 objects.put(id, new ObjectEntry(desc));
907 906 if (desc.getRestartMode() == true) {
908 907 restartSet.add(id);
909 908 }
910 909
911 910 // table insertion must take place before log update
912 911 idTable.put(id, groupID);
913 912
914 913 if (addRecord) {
915 914 addLogRecord(new LogRegisterObject(id, desc));
916 915 }
917 916 }
918 917
919 918 synchronized void unregisterObject(ActivationID id, boolean addRecord)
920 919 throws UnknownGroupException, ActivationException
921 920 {
922 921 ObjectEntry objEntry = getObjectEntry(id);
923 922 objEntry.removed = true;
924 923 objects.remove(id);
925 924 if (objEntry.desc.getRestartMode() == true) {
926 925 restartSet.remove(id);
927 926 }
928 927
929 928 // table removal must take place before log update
930 929 idTable.remove(id);
931 930 if (addRecord) {
932 931 addLogRecord(new LogUnregisterObject(id));
933 932 }
934 933 }
935 934
936 935 synchronized void unregisterGroup(boolean addRecord)
937 936 throws UnknownGroupException, ActivationException
938 937 {
939 938 checkRemoved();
940 939 removed = true;
941 940 for (Map.Entry<ActivationID,ObjectEntry> entry :
942 941 objects.entrySet())
943 942 {
944 943 ActivationID id = entry.getKey();
945 944 idTable.remove(id);
946 945 ObjectEntry objEntry = entry.getValue();
947 946 objEntry.removed = true;
948 947 }
949 948 objects.clear();
950 949 restartSet.clear();
951 950 reset();
952 951 childGone();
953 952
954 953 // removal should be recorded before log update
955 954 if (addRecord) {
956 955 addLogRecord(new LogUnregisterGroup(groupID));
957 956 }
958 957 }
959 958
960 959 synchronized ActivationDesc setActivationDesc(ActivationID id,
961 960 ActivationDesc desc,
962 961 boolean addRecord)
963 962 throws UnknownObjectException, UnknownGroupException,
964 963 ActivationException
965 964 {
966 965 ObjectEntry objEntry = getObjectEntry(id);
967 966 ActivationDesc oldDesc = objEntry.desc;
968 967 objEntry.desc = desc;
969 968 if (desc.getRestartMode() == true) {
970 969 restartSet.add(id);
971 970 } else {
972 971 restartSet.remove(id);
973 972 }
974 973 // restart information should be recorded before log update
975 974 if (addRecord) {
976 975 addLogRecord(new LogUpdateDesc(id, desc));
977 976 }
978 977
979 978 return oldDesc;
980 979 }
981 980
982 981 synchronized ActivationDesc getActivationDesc(ActivationID id)
983 982 throws UnknownObjectException, UnknownGroupException
984 983 {
985 984 return getObjectEntry(id).desc;
986 985 }
987 986
988 987 synchronized ActivationGroupDesc setActivationGroupDesc(
989 988 ActivationGroupID id,
990 989 ActivationGroupDesc desc,
991 990 boolean addRecord)
992 991 throws UnknownGroupException, ActivationException
993 992 {
994 993 checkRemoved();
995 994 ActivationGroupDesc oldDesc = this.desc;
996 995 this.desc = desc;
997 996 // state update should occur before log update
998 997 if (addRecord) {
999 998 addLogRecord(new LogUpdateGroupDesc(id, desc));
1000 999 }
1001 1000 return oldDesc;
1002 1001 }
1003 1002
1004 1003 synchronized void inactiveGroup(long incarnation, boolean failure)
1005 1004 throws UnknownGroupException
1006 1005 {
1007 1006 checkRemoved();
1008 1007 if (this.incarnation != incarnation) {
1009 1008 throw new UnknownGroupException("invalid incarnation");
1010 1009 }
1011 1010
1012 1011 reset();
1013 1012 if (failure) {
1014 1013 terminate();
1015 1014 } else if (child != null && status == NORMAL) {
1016 1015 status = TERMINATE;
1017 1016 watchdog.noRestart();
1018 1017 }
1019 1018 }
1020 1019
1021 1020 synchronized void activeObject(ActivationID id,
1022 1021 MarshalledObject<? extends Remote> mobj)
1023 1022 throws UnknownObjectException
1024 1023 {
1025 1024 getObjectEntry(id).stub = mobj;
1026 1025 }
1027 1026
1028 1027 synchronized void inactiveObject(ActivationID id)
1029 1028 throws UnknownObjectException
1030 1029 {
1031 1030 getObjectEntry(id).reset();
1032 1031 }
1033 1032
1034 1033 private synchronized void reset() {
1035 1034 group = null;
1036 1035 for (ObjectEntry objectEntry : objects.values()) {
1037 1036 objectEntry.reset();
1038 1037 }
1039 1038 }
1040 1039
1041 1040 private void childGone() {
1042 1041 if (child != null) {
1043 1042 child = null;
1044 1043 watchdog.dispose();
1045 1044 watchdog = null;
1046 1045 status = NORMAL;
1047 1046 notifyAll();
1048 1047 }
1049 1048 }
↓ open down ↓ |
230 lines elided |
↑ open up ↑ |
1050 1049
1051 1050 private void terminate() {
1052 1051 if (child != null && status != TERMINATING) {
1053 1052 child.destroy();
1054 1053 status = TERMINATING;
1055 1054 waitTime = System.currentTimeMillis() + groupTimeout;
1056 1055 notifyAll();
1057 1056 }
1058 1057 }
1059 1058
1059 + /*
1060 + * Fallthrough from TERMINATE to TERMINATING
1061 + * is intentional
1062 + */
1063 + @SuppressWarnings("fallthrough")
1060 1064 private void await() {
1061 1065 while (true) {
1062 1066 switch (status) {
1063 1067 case NORMAL:
1064 1068 return;
1065 1069 case TERMINATE:
1066 1070 terminate();
1067 1071 case TERMINATING:
1068 1072 try {
1069 1073 child.exitValue();
1070 1074 } catch (IllegalThreadStateException e) {
1071 1075 long now = System.currentTimeMillis();
1072 1076 if (waitTime > now) {
1073 1077 try {
1074 1078 wait(waitTime - now);
1075 1079 } catch (InterruptedException ee) {
1076 1080 }
1077 1081 continue;
1078 1082 }
1079 1083 // REMIND: print message that group did not terminate?
1080 1084 }
1081 1085 childGone();
1082 1086 return;
1083 1087 case CREATING:
1084 1088 try {
1085 1089 wait();
1086 1090 } catch (InterruptedException e) {
1087 1091 }
1088 1092 }
1089 1093 }
1090 1094 }
1091 1095
1092 1096 // no synchronization to avoid delay wrt getInstantiator
1093 1097 void shutdownFast() {
1094 1098 Process p = child;
1095 1099 if (p != null) {
1096 1100 p.destroy();
1097 1101 }
1098 1102 }
1099 1103
1100 1104 synchronized void shutdown() {
1101 1105 reset();
1102 1106 terminate();
1103 1107 await();
1104 1108 }
1105 1109
1106 1110 MarshalledObject<? extends Remote> activate(ActivationID id,
1107 1111 boolean force)
1108 1112 throws ActivationException
1109 1113 {
1110 1114 Exception detail = null;
1111 1115
1112 1116 /*
1113 1117 * Attempt to activate object and reattempt (several times)
1114 1118 * if activation fails due to communication problems.
1115 1119 */
1116 1120 for (int tries = MAX_TRIES; tries > 0; tries--) {
1117 1121 ActivationInstantiator inst;
1118 1122 long currentIncarnation;
1119 1123
1120 1124 // look up object to activate
1121 1125 ObjectEntry objEntry;
1122 1126 synchronized (this) {
1123 1127 objEntry = getObjectEntry(id);
1124 1128 // if not forcing activation, return cached stub
1125 1129 if (!force && objEntry.stub != null) {
1126 1130 return objEntry.stub;
1127 1131 }
1128 1132 inst = getInstantiator(groupID);
1129 1133 currentIncarnation = incarnation;
1130 1134 }
1131 1135
1132 1136 boolean groupInactive = false;
1133 1137 boolean failure = false;
1134 1138 // activate object
1135 1139 try {
1136 1140 return objEntry.activate(id, force, inst);
1137 1141 } catch (NoSuchObjectException e) {
1138 1142 groupInactive = true;
1139 1143 detail = e;
1140 1144 } catch (ConnectException e) {
1141 1145 groupInactive = true;
1142 1146 failure = true;
1143 1147 detail = e;
1144 1148 } catch (ConnectIOException e) {
1145 1149 groupInactive = true;
1146 1150 failure = true;
1147 1151 detail = e;
1148 1152 } catch (InactiveGroupException e) {
1149 1153 groupInactive = true;
1150 1154 detail = e;
1151 1155 } catch (RemoteException e) {
1152 1156 // REMIND: wait some here before continuing?
1153 1157 if (detail == null) {
1154 1158 detail = e;
1155 1159 }
1156 1160 }
1157 1161
1158 1162 if (groupInactive) {
1159 1163 // group has failed or is inactive; mark inactive
1160 1164 try {
1161 1165 System.err.println(
1162 1166 MessageFormat.format(
1163 1167 getTextResource("rmid.group.inactive"),
1164 1168 detail.toString()));
1165 1169 detail.printStackTrace();
1166 1170 getGroupEntry(groupID).
1167 1171 inactiveGroup(currentIncarnation, failure);
1168 1172 } catch (UnknownGroupException e) {
1169 1173 // not a problem
1170 1174 }
1171 1175 }
1172 1176 }
1173 1177
1174 1178 /**
1175 1179 * signal that group activation failed, nested exception
1176 1180 * specifies what exception occurred when the group did not
1177 1181 * activate
1178 1182 */
1179 1183 throw new ActivationException("object activation failed after " +
1180 1184 MAX_TRIES + " tries", detail);
1181 1185 }
1182 1186
1183 1187 /**
1184 1188 * Returns the instantiator for the group specified by id and
1185 1189 * entry. If the group is currently inactive, exec some
1186 1190 * bootstrap code to create the group.
1187 1191 */
1188 1192 private ActivationInstantiator getInstantiator(ActivationGroupID id)
1189 1193 throws ActivationException
1190 1194 {
1191 1195 assert Thread.holdsLock(this);
1192 1196
1193 1197 await();
1194 1198 if (group != null) {
1195 1199 return group;
1196 1200 }
1197 1201 checkRemoved();
1198 1202 boolean acquired = false;
1199 1203
1200 1204 try {
1201 1205 groupName = Pstartgroup();
1202 1206 acquired = true;
1203 1207 String[] argv = activationArgs(desc);
1204 1208 checkArgs(desc, argv);
1205 1209
1206 1210 if (debugExec) {
1207 1211 StringBuffer sb = new StringBuffer(argv[0]);
1208 1212 int j;
1209 1213 for (j = 1; j < argv.length; j++) {
1210 1214 sb.append(' ');
1211 1215 sb.append(argv[j]);
1212 1216 }
1213 1217 System.err.println(
1214 1218 MessageFormat.format(
1215 1219 getTextResource("rmid.exec.command"),
1216 1220 sb.toString()));
1217 1221 }
1218 1222
1219 1223 try {
1220 1224 child = Runtime.getRuntime().exec(argv);
↓ open down ↓ |
151 lines elided |
↑ open up ↑ |
1221 1225 status = CREATING;
1222 1226 ++incarnation;
1223 1227 watchdog = new Watchdog();
1224 1228 watchdog.start();
1225 1229 addLogRecord(new LogGroupIncarnation(id, incarnation));
1226 1230
1227 1231 // handle child I/O streams before writing to child
1228 1232 PipeWriter.plugTogetherPair
1229 1233 (child.getInputStream(), System.out,
1230 1234 child.getErrorStream(), System.err);
1235 + try (MarshalOutputStream out =
1236 + new MarshalOutputStream(child.getOutputStream())) {
1237 + out.writeObject(id);
1238 + out.writeObject(desc);
1239 + out.writeLong(incarnation);
1240 + out.flush();
1241 + }
1231 1242
1232 - MarshalOutputStream out =
1233 - new MarshalOutputStream(child.getOutputStream());
1234 - out.writeObject(id);
1235 - out.writeObject(desc);
1236 - out.writeLong(incarnation);
1237 - out.flush();
1238 - out.close();
1239 1243
1240 -
1241 1244 } catch (IOException e) {
1242 1245 terminate();
1243 1246 throw new ActivationException(
1244 1247 "unable to create activation group", e);
1245 1248 }
1246 1249
1247 1250 try {
1248 1251 long now = System.currentTimeMillis();
1249 1252 long stop = now + execTimeout;
1250 1253 do {
1251 1254 wait(stop - now);
1252 1255 if (group != null) {
1253 1256 return group;
1254 1257 }
1255 1258 now = System.currentTimeMillis();
1256 1259 } while (status == CREATING && now < stop);
1257 1260 } catch (InterruptedException e) {
1258 1261 }
1259 1262
1260 1263 terminate();
1261 1264 throw new ActivationException(
1262 1265 (removed ?
1263 1266 "activation group unregistered" :
1264 1267 "timeout creating child process"));
1265 1268 } finally {
1266 1269 if (acquired) {
1267 1270 Vstartgroup();
1268 1271 }
1269 1272 }
1270 1273 }
1271 1274
1272 1275 /**
1273 1276 * Waits for process termination and then restarts services.
1274 1277 */
1275 1278 private class Watchdog extends Thread {
1276 1279 private final Process groupProcess = child;
1277 1280 private final long groupIncarnation = incarnation;
1278 1281 private boolean canInterrupt = true;
1279 1282 private boolean shouldQuit = false;
1280 1283 private boolean shouldRestart = true;
1281 1284
1282 1285 Watchdog() {
1283 1286 super("WatchDog-" + groupName + "-" + incarnation);
1284 1287 setDaemon(true);
1285 1288 }
1286 1289
1287 1290 public void run() {
1288 1291
1289 1292 if (shouldQuit) {
1290 1293 return;
1291 1294 }
1292 1295
1293 1296 /*
1294 1297 * Wait for the group to crash or exit.
1295 1298 */
1296 1299 try {
1297 1300 groupProcess.waitFor();
1298 1301 } catch (InterruptedException exit) {
1299 1302 return;
1300 1303 }
1301 1304
1302 1305 boolean restart = false;
1303 1306 synchronized (GroupEntry.this) {
1304 1307 if (shouldQuit) {
1305 1308 return;
1306 1309 }
1307 1310 canInterrupt = false;
1308 1311 interrupted(); // clear interrupt bit
1309 1312 /*
1310 1313 * Since the group crashed, we should
1311 1314 * reset the entry before activating objects
1312 1315 */
1313 1316 if (groupIncarnation == incarnation) {
1314 1317 restart = shouldRestart && !shuttingDown;
1315 1318 reset();
1316 1319 childGone();
1317 1320 }
1318 1321 }
1319 1322
1320 1323 /*
1321 1324 * Activate those objects that require restarting
1322 1325 * after a crash.
1323 1326 */
1324 1327 if (restart) {
1325 1328 restartServices();
1326 1329 }
1327 1330 }
1328 1331
1329 1332 /**
1330 1333 * Marks this thread as one that is no longer needed.
1331 1334 * If the thread is in a state in which it can be interrupted,
1332 1335 * then the thread is interrupted.
1333 1336 */
1334 1337 void dispose() {
1335 1338 shouldQuit = true;
1336 1339 if (canInterrupt) {
1337 1340 interrupt();
1338 1341 }
1339 1342 }
1340 1343
1341 1344 /**
1342 1345 * Marks this thread as no longer needing to restart objects.
1343 1346 */
1344 1347 void noRestart() {
↓ open down ↓ |
94 lines elided |
↑ open up ↑ |
1345 1348 shouldRestart = false;
1346 1349 }
1347 1350 }
1348 1351 }
1349 1352
1350 1353 private String[] activationArgs(ActivationGroupDesc desc) {
1351 1354 ActivationGroupDesc.CommandEnvironment cmdenv;
1352 1355 cmdenv = desc.getCommandEnvironment();
1353 1356
1354 1357 // argv is the literal command to exec
1355 - List<String> argv = new ArrayList<String>();
1358 + List<String> argv = new ArrayList<>();
1356 1359
1357 1360 // Command name/path
1358 1361 argv.add((cmdenv != null && cmdenv.getCommandPath() != null)
1359 1362 ? cmdenv.getCommandPath()
1360 1363 : command[0]);
1361 1364
1362 1365 // Group-specific command options
1363 1366 if (cmdenv != null && cmdenv.getCommandOptions() != null) {
1364 1367 argv.addAll(Arrays.asList(cmdenv.getCommandOptions()));
1365 1368 }
1366 1369
1367 1370 // Properties become -D parameters
1368 1371 Properties props = desc.getPropertyOverrides();
1369 1372 if (props != null) {
1370 1373 for (Enumeration<?> p = props.propertyNames();
1371 1374 p.hasMoreElements();)
1372 1375 {
1373 1376 String name = (String) p.nextElement();
1374 1377 /* Note on quoting: it would be wrong
1375 1378 * here, since argv will be passed to
1376 1379 * Runtime.exec, which should not parse
1377 1380 * arguments or split on whitespace.
1378 1381 */
1379 1382 argv.add("-D" + name + "=" + props.getProperty(name));
1380 1383 }
1381 1384 }
1382 1385
1383 1386 /* Finally, rmid-global command options (e.g. -C options)
1384 1387 * and the classname
1385 1388 */
1386 1389 for (int i = 1; i < command.length; i++) {
1387 1390 argv.add(command[i]);
1388 1391 }
1389 1392
1390 1393 String[] realArgv = new String[argv.size()];
1391 1394 System.arraycopy(argv.toArray(), 0, realArgv, 0, realArgv.length);
1392 1395
1393 1396 return realArgv;
1394 1397 }
1395 1398
1396 1399 private void checkArgs(ActivationGroupDesc desc, String[] cmd)
1397 1400 throws SecurityException, ActivationException
1398 1401 {
1399 1402 /*
1400 1403 * Check exec command using execPolicy object
1401 1404 */
1402 1405 if (execPolicyMethod != null) {
1403 1406 if (cmd == null) {
1404 1407 cmd = activationArgs(desc);
1405 1408 }
1406 1409 try {
1407 1410 execPolicyMethod.invoke(execPolicy, desc, cmd);
1408 1411 } catch (InvocationTargetException e) {
1409 1412 Throwable targetException = e.getTargetException();
1410 1413 if (targetException instanceof SecurityException) {
1411 1414 throw (SecurityException) targetException;
1412 1415 } else {
1413 1416 throw new ActivationException(
1414 1417 execPolicyMethod.getName() + ": unexpected exception",
1415 1418 e);
1416 1419 }
1417 1420 } catch (Exception e) {
1418 1421 throw new ActivationException(
1419 1422 execPolicyMethod.getName() + ": unexpected exception", e);
1420 1423 }
1421 1424 }
1422 1425 }
1423 1426
1424 1427 private static class ObjectEntry implements Serializable {
1425 1428
1426 1429 private static final long serialVersionUID = -5500114225321357856L;
1427 1430
1428 1431 /** descriptor for object */
1429 1432 ActivationDesc desc;
1430 1433 /** the stub (if active) */
1431 1434 volatile transient MarshalledObject<? extends Remote> stub = null;
1432 1435 volatile transient boolean removed = false;
1433 1436
1434 1437 ObjectEntry(ActivationDesc desc) {
1435 1438 this.desc = desc;
1436 1439 }
1437 1440
1438 1441 synchronized MarshalledObject<? extends Remote>
1439 1442 activate(ActivationID id,
1440 1443 boolean force,
1441 1444 ActivationInstantiator inst)
1442 1445 throws RemoteException, ActivationException
1443 1446 {
1444 1447 MarshalledObject<? extends Remote> nstub = stub;
1445 1448 if (removed) {
1446 1449 throw new UnknownObjectException("object removed");
1447 1450 } else if (!force && nstub != null) {
1448 1451 return nstub;
1449 1452 }
1450 1453
1451 1454 nstub = inst.newInstance(id, desc);
1452 1455 stub = nstub;
1453 1456 /*
1454 1457 * stub could be set to null by a group reset, so return
1455 1458 * the newstub here to prevent returning null.
1456 1459 */
1457 1460 return nstub;
1458 1461 }
1459 1462
1460 1463 void reset() {
1461 1464 stub = null;
1462 1465 }
1463 1466 }
1464 1467
1465 1468 /**
1466 1469 * Add a record to the activation log. If the number of updates
1467 1470 * passes a predetermined threshold, record a snapshot.
1468 1471 */
1469 1472 private void addLogRecord(LogRecord rec) throws ActivationException {
1470 1473 synchronized (log) {
1471 1474 checkShutdown();
1472 1475 try {
1473 1476 log.update(rec, true);
1474 1477 } catch (Exception e) {
1475 1478 numUpdates = snapshotInterval;
1476 1479 System.err.println(getTextResource("rmid.log.update.warning"));
1477 1480 e.printStackTrace();
1478 1481 }
1479 1482 if (++numUpdates < snapshotInterval) {
1480 1483 return;
1481 1484 }
1482 1485 try {
1483 1486 log.snapshot(this);
1484 1487 numUpdates = 0;
1485 1488 } catch (Exception e) {
1486 1489 System.err.println(
1487 1490 getTextResource("rmid.log.snapshot.warning"));
1488 1491 e.printStackTrace();
1489 1492 try {
1490 1493 // shutdown activation system because snapshot failed
1491 1494 system.shutdown();
1492 1495 } catch (RemoteException ignore) {
1493 1496 // can't happen
1494 1497 }
1495 1498 // warn the client of the original update problem
1496 1499 throw new ActivationException("log snapshot failed", e);
1497 1500 }
1498 1501 }
1499 1502 }
1500 1503
1501 1504 /**
1502 1505 * Handler for the log that knows how to take the initial snapshot
1503 1506 * and apply an update (a LogRecord) to the current state.
1504 1507 */
1505 1508 private static class ActLogHandler extends LogHandler {
1506 1509
1507 1510 ActLogHandler() {
1508 1511 }
1509 1512
1510 1513 public Object initialSnapshot()
1511 1514 {
1512 1515 /**
1513 1516 * Return an empty Activation object. Log will update
1514 1517 * this object with recovered state.
1515 1518 */
1516 1519 return new Activation();
1517 1520 }
1518 1521
1519 1522 public Object applyUpdate(Object update, Object state)
1520 1523 throws Exception
1521 1524 {
1522 1525 return ((LogRecord) update).apply(state);
1523 1526 }
1524 1527
1525 1528 }
1526 1529
1527 1530 /**
1528 1531 * Abstract class for all log records. The subclass contains
1529 1532 * specific update information and implements the apply method
1530 1533 * that applys the update information contained in the record
1531 1534 * to the current state.
1532 1535 */
1533 1536 private static abstract class LogRecord implements Serializable {
1534 1537 /** indicate compatibility with JDK 1.2 version of class */
1535 1538 private static final long serialVersionUID = 8395140512322687529L;
1536 1539 abstract Object apply(Object state) throws Exception;
1537 1540 }
1538 1541
1539 1542 /**
1540 1543 * Log record for registering an object.
1541 1544 */
1542 1545 private static class LogRegisterObject extends LogRecord {
1543 1546 /** indicate compatibility with JDK 1.2 version of class */
1544 1547 private static final long serialVersionUID = -6280336276146085143L;
1545 1548 private ActivationID id;
1546 1549 private ActivationDesc desc;
1547 1550
1548 1551 LogRegisterObject(ActivationID id, ActivationDesc desc) {
1549 1552 this.id = id;
1550 1553 this.desc = desc;
1551 1554 }
1552 1555
1553 1556 Object apply(Object state) {
1554 1557 try {
1555 1558 ((Activation) state).getGroupEntry(desc.getGroupID()).
1556 1559 registerObject(id, desc, false);
1557 1560 } catch (Exception ignore) {
1558 1561 System.err.println(
1559 1562 MessageFormat.format(
1560 1563 getTextResource("rmid.log.recover.warning"),
1561 1564 "LogRegisterObject"));
1562 1565 ignore.printStackTrace();
1563 1566 }
1564 1567 return state;
1565 1568 }
1566 1569 }
1567 1570
1568 1571 /**
1569 1572 * Log record for unregistering an object.
1570 1573 */
1571 1574 private static class LogUnregisterObject extends LogRecord {
1572 1575 /** indicate compatibility with JDK 1.2 version of class */
1573 1576 private static final long serialVersionUID = 6269824097396935501L;
1574 1577 private ActivationID id;
1575 1578
1576 1579 LogUnregisterObject(ActivationID id) {
1577 1580 this.id = id;
1578 1581 }
1579 1582
1580 1583 Object apply(Object state) {
1581 1584 try {
1582 1585 ((Activation) state).getGroupEntry(id).
1583 1586 unregisterObject(id, false);
1584 1587 } catch (Exception ignore) {
1585 1588 System.err.println(
1586 1589 MessageFormat.format(
1587 1590 getTextResource("rmid.log.recover.warning"),
1588 1591 "LogUnregisterObject"));
1589 1592 ignore.printStackTrace();
1590 1593 }
1591 1594 return state;
1592 1595 }
1593 1596 }
1594 1597
1595 1598 /**
1596 1599 * Log record for registering a group.
1597 1600 */
1598 1601 private static class LogRegisterGroup extends LogRecord {
1599 1602 /** indicate compatibility with JDK 1.2 version of class */
1600 1603 private static final long serialVersionUID = -1966827458515403625L;
1601 1604 private ActivationGroupID id;
1602 1605 private ActivationGroupDesc desc;
1603 1606
1604 1607 LogRegisterGroup(ActivationGroupID id, ActivationGroupDesc desc) {
1605 1608 this.id = id;
1606 1609 this.desc = desc;
1607 1610 }
1608 1611
1609 1612 Object apply(Object state) {
1610 1613 // modify state directly; cant ask a nonexistent GroupEntry
1611 1614 // to register itself.
1612 1615 ((Activation) state).groupTable.put(id, ((Activation) state).new
1613 1616 GroupEntry(id, desc));
1614 1617 return state;
1615 1618 }
1616 1619 }
1617 1620
1618 1621 /**
1619 1622 * Log record for udpating an activation desc
1620 1623 */
1621 1624 private static class LogUpdateDesc extends LogRecord {
1622 1625 /** indicate compatibility with JDK 1.2 version of class */
1623 1626 private static final long serialVersionUID = 545511539051179885L;
1624 1627
1625 1628 private ActivationID id;
1626 1629 private ActivationDesc desc;
1627 1630
1628 1631 LogUpdateDesc(ActivationID id, ActivationDesc desc) {
1629 1632 this.id = id;
1630 1633 this.desc = desc;
1631 1634 }
1632 1635
1633 1636 Object apply(Object state) {
1634 1637 try {
1635 1638 ((Activation) state).getGroupEntry(id).
1636 1639 setActivationDesc(id, desc, false);
1637 1640 } catch (Exception ignore) {
1638 1641 System.err.println(
1639 1642 MessageFormat.format(
1640 1643 getTextResource("rmid.log.recover.warning"),
1641 1644 "LogUpdateDesc"));
1642 1645 ignore.printStackTrace();
1643 1646 }
1644 1647 return state;
1645 1648 }
1646 1649 }
1647 1650
1648 1651 /**
1649 1652 * Log record for unregistering a group.
1650 1653 */
1651 1654 private static class LogUpdateGroupDesc extends LogRecord {
1652 1655 /** indicate compatibility with JDK 1.2 version of class */
1653 1656 private static final long serialVersionUID = -1271300989218424337L;
1654 1657 private ActivationGroupID id;
1655 1658 private ActivationGroupDesc desc;
1656 1659
1657 1660 LogUpdateGroupDesc(ActivationGroupID id, ActivationGroupDesc desc) {
1658 1661 this.id = id;
1659 1662 this.desc = desc;
1660 1663 }
1661 1664
1662 1665 Object apply(Object state) {
1663 1666 try {
1664 1667 ((Activation) state).getGroupEntry(id).
1665 1668 setActivationGroupDesc(id, desc, false);
1666 1669 } catch (Exception ignore) {
1667 1670 System.err.println(
1668 1671 MessageFormat.format(
1669 1672 getTextResource("rmid.log.recover.warning"),
1670 1673 "LogUpdateGroupDesc"));
1671 1674 ignore.printStackTrace();
1672 1675 }
1673 1676 return state;
1674 1677 }
1675 1678 }
1676 1679
1677 1680 /**
1678 1681 * Log record for unregistering a group.
1679 1682 */
1680 1683 private static class LogUnregisterGroup extends LogRecord {
1681 1684 /** indicate compatibility with JDK 1.2 version of class */
1682 1685 private static final long serialVersionUID = -3356306586522147344L;
1683 1686 private ActivationGroupID id;
1684 1687
1685 1688 LogUnregisterGroup(ActivationGroupID id) {
1686 1689 this.id = id;
1687 1690 }
1688 1691
1689 1692 Object apply(Object state) {
1690 1693 GroupEntry entry = ((Activation) state).groupTable.remove(id);
1691 1694 try {
1692 1695 entry.unregisterGroup(false);
1693 1696 } catch (Exception ignore) {
1694 1697 System.err.println(
1695 1698 MessageFormat.format(
1696 1699 getTextResource("rmid.log.recover.warning"),
1697 1700 "LogUnregisterGroup"));
1698 1701 ignore.printStackTrace();
1699 1702 }
1700 1703 return state;
1701 1704 }
1702 1705 }
1703 1706
1704 1707 /**
1705 1708 * Log record for an active group incarnation
1706 1709 */
1707 1710 private static class LogGroupIncarnation extends LogRecord {
1708 1711 /** indicate compatibility with JDK 1.2 version of class */
1709 1712 private static final long serialVersionUID = 4146872747377631897L;
1710 1713 private ActivationGroupID id;
1711 1714 private long inc;
1712 1715
1713 1716 LogGroupIncarnation(ActivationGroupID id, long inc) {
1714 1717 this.id = id;
1715 1718 this.inc = inc;
1716 1719 }
1717 1720
1718 1721 Object apply(Object state) {
1719 1722 try {
1720 1723 GroupEntry entry = ((Activation) state).getGroupEntry(id);
1721 1724 entry.incarnation = inc;
1722 1725 } catch (Exception ignore) {
1723 1726 System.err.println(
1724 1727 MessageFormat.format(
1725 1728 getTextResource("rmid.log.recover.warning"),
1726 1729 "LogGroupIncarnation"));
1727 1730 ignore.printStackTrace();
1728 1731 }
1729 1732 return state;
1730 1733 }
1731 1734 }
1732 1735
1733 1736 /**
1734 1737 * Initialize command to exec a default group.
1735 1738 */
1736 1739 private void initCommand(String[] childArgs) {
1737 1740 command = new String[childArgs.length + 2];
1738 1741 AccessController.doPrivileged(new PrivilegedAction<Void>() {
1739 1742 public Void run() {
1740 1743 try {
1741 1744 command[0] = System.getProperty("java.home") +
1742 1745 File.separator + "bin" + File.separator + "java";
1743 1746 } catch (Exception e) {
1744 1747 System.err.println(
1745 1748 getTextResource("rmid.unfound.java.home.property"));
1746 1749 command[0] = "java";
1747 1750 }
1748 1751 return null;
1749 1752 }
1750 1753 });
1751 1754 System.arraycopy(childArgs, 0, command, 1, childArgs.length);
1752 1755 command[command.length-1] = "sun.rmi.server.ActivationGroupInit";
1753 1756 }
1754 1757
1755 1758 private static void bomb(String error) {
1756 1759 System.err.println("rmid: " + error); // $NON-NLS$
1757 1760 System.err.println(MessageFormat.format(getTextResource("rmid.usage"),
1758 1761 "rmid"));
1759 1762 System.exit(1);
1760 1763 }
1761 1764
1762 1765 /**
1763 1766 * The default policy for checking a command before it is executed
1764 1767 * makes sure the appropriate com.sun.rmi.rmid.ExecPermission and
1765 1768 * set of com.sun.rmi.rmid.ExecOptionPermissions have been granted.
1766 1769 */
1767 1770 public static class DefaultExecPolicy {
1768 1771
1769 1772 public void checkExecCommand(ActivationGroupDesc desc, String[] cmd)
1770 1773 throws SecurityException
1771 1774 {
1772 1775 PermissionCollection perms = getExecPermissions();
1773 1776
1774 1777 /*
1775 1778 * Check properties overrides.
1776 1779 */
1777 1780 Properties props = desc.getPropertyOverrides();
1778 1781 if (props != null) {
1779 1782 Enumeration<?> p = props.propertyNames();
1780 1783 while (p.hasMoreElements()) {
1781 1784 String name = (String) p.nextElement();
1782 1785 String value = props.getProperty(name);
1783 1786 String option = "-D" + name + "=" + value;
1784 1787 try {
1785 1788 checkPermission(perms,
1786 1789 new ExecOptionPermission(option));
1787 1790 } catch (AccessControlException e) {
1788 1791 if (value.equals("")) {
1789 1792 checkPermission(perms,
1790 1793 new ExecOptionPermission("-D" + name));
1791 1794 } else {
1792 1795 throw e;
1793 1796 }
1794 1797 }
1795 1798 }
1796 1799 }
1797 1800
1798 1801 /*
1799 1802 * Check group class name (allow nothing but the default),
1800 1803 * code location (must be null), and data (must be null).
1801 1804 */
1802 1805 String groupClassName = desc.getClassName();
1803 1806 if ((groupClassName != null &&
1804 1807 !groupClassName.equals(
1805 1808 ActivationGroupImpl.class.getName())) ||
1806 1809 (desc.getLocation() != null) ||
1807 1810 (desc.getData() != null))
1808 1811 {
1809 1812 throw new AccessControlException(
1810 1813 "access denied (custom group implementation not allowed)");
1811 1814 }
1812 1815
1813 1816 /*
1814 1817 * If group descriptor has a command environment, check
1815 1818 * command and options.
1816 1819 */
1817 1820 ActivationGroupDesc.CommandEnvironment cmdenv;
1818 1821 cmdenv = desc.getCommandEnvironment();
1819 1822 if (cmdenv != null) {
1820 1823 String path = cmdenv.getCommandPath();
1821 1824 if (path != null) {
1822 1825 checkPermission(perms, new ExecPermission(path));
1823 1826 }
1824 1827
1825 1828 String[] options = cmdenv.getCommandOptions();
1826 1829 if (options != null) {
1827 1830 for (String option : options) {
1828 1831 checkPermission(perms,
1829 1832 new ExecOptionPermission(option));
1830 1833 }
1831 1834 }
1832 1835 }
1833 1836 }
1834 1837
1835 1838 /**
1836 1839 * Prints warning message if installed Policy is the default Policy
1837 1840 * implementation and globally granted permissions do not include
1838 1841 * AllPermission or any ExecPermissions/ExecOptionPermissions.
1839 1842 */
1840 1843 static void checkConfiguration() {
1841 1844 Policy policy =
1842 1845 AccessController.doPrivileged(new PrivilegedAction<Policy>() {
1843 1846 public Policy run() {
1844 1847 return Policy.getPolicy();
1845 1848 }
1846 1849 });
1847 1850 if (!(policy instanceof PolicyFile)) {
1848 1851 return;
1849 1852 }
1850 1853 PermissionCollection perms = getExecPermissions();
1851 1854 for (Enumeration<Permission> e = perms.elements();
1852 1855 e.hasMoreElements();)
1853 1856 {
1854 1857 Permission p = e.nextElement();
1855 1858 if (p instanceof AllPermission ||
1856 1859 p instanceof ExecPermission ||
1857 1860 p instanceof ExecOptionPermission)
1858 1861 {
1859 1862 return;
1860 1863 }
1861 1864 }
1862 1865 System.err.println(getTextResource("rmid.exec.perms.inadequate"));
1863 1866 }
1864 1867
1865 1868 private static PermissionCollection getExecPermissions() {
1866 1869 /*
1867 1870 * The approach used here is taken from the similar method
1868 1871 * getLoaderAccessControlContext() in the class
1869 1872 * sun.rmi.server.LoaderHandler.
1870 1873 */
1871 1874
1872 1875 // obtain permissions granted to all code in current policy
1873 1876 PermissionCollection perms = AccessController.doPrivileged(
1874 1877 new PrivilegedAction<PermissionCollection>() {
1875 1878 public PermissionCollection run() {
1876 1879 CodeSource codesource =
1877 1880 new CodeSource(null, (Certificate[]) null);
1878 1881 Policy p = Policy.getPolicy();
1879 1882 if (p != null) {
1880 1883 return p.getPermissions(codesource);
1881 1884 } else {
1882 1885 return new Permissions();
1883 1886 }
1884 1887 }
1885 1888 });
1886 1889
1887 1890 return perms;
1888 1891 }
1889 1892
1890 1893 private static void checkPermission(PermissionCollection perms,
1891 1894 Permission p)
1892 1895 throws AccessControlException
1893 1896 {
1894 1897 if (!perms.implies(p)) {
1895 1898 throw new AccessControlException(
1896 1899 "access denied " + p.toString());
1897 1900 }
1898 1901 }
1899 1902 }
1900 1903
1901 1904 /**
1902 1905 * Main program to start the activation system. <br>
1903 1906 * The usage is as follows: rmid [-port num] [-log dir].
1904 1907 */
1905 1908 public static void main(String[] args) {
1906 1909 boolean stop = false;
1907 1910
1908 1911 // Create and install the security manager if one is not installed
1909 1912 // already.
1910 1913 if (System.getSecurityManager() == null) {
1911 1914 System.setSecurityManager(new SecurityManager());
1912 1915 }
1913 1916
1914 1917 try {
1915 1918 int port = ActivationSystem.SYSTEM_PORT;
1916 1919 RMIServerSocketFactory ssf = null;
1917 1920
1918 1921 /*
1919 1922 * If rmid has an inherited channel (meaning that it was
1920 1923 * launched from inetd), set the server socket factory to
1921 1924 * return the inherited server socket.
1922 1925 **/
1923 1926 Channel inheritedChannel = AccessController.doPrivileged(
1924 1927 new PrivilegedExceptionAction<Channel>() {
1925 1928 public Channel run() throws IOException {
1926 1929 return System.inheritedChannel();
1927 1930 }
1928 1931 });
1929 1932
1930 1933 if (inheritedChannel != null &&
1931 1934 inheritedChannel instanceof ServerSocketChannel)
1932 1935 {
1933 1936 /*
1934 1937 * Redirect System.err output to a file.
1935 1938 */
1936 1939 AccessController.doPrivileged(
1937 1940 new PrivilegedExceptionAction<Void>() {
1938 1941 public Void run() throws IOException {
1939 1942 File file =
1940 1943 File.createTempFile("rmid-err", null, null);
1941 1944 PrintStream errStream =
1942 1945 new PrintStream(new FileOutputStream(file));
1943 1946 System.setErr(errStream);
1944 1947 return null;
1945 1948 }
1946 1949 });
1947 1950
1948 1951 ServerSocket serverSocket =
1949 1952 ((ServerSocketChannel) inheritedChannel).socket();
↓ open down ↓ |
584 lines elided |
↑ open up ↑ |
1950 1953 port = serverSocket.getLocalPort();
1951 1954 ssf = new ActivationServerSocketFactory(serverSocket);
1952 1955
1953 1956 System.err.println(new Date());
1954 1957 System.err.println(getTextResource(
1955 1958 "rmid.inherited.channel.info") +
1956 1959 ": " + inheritedChannel);
1957 1960 }
1958 1961
1959 1962 String log = null;
1960 - List<String> childArgs = new ArrayList<String>();
1963 + List<String> childArgs = new ArrayList<>();
1961 1964
1962 1965 /*
1963 1966 * Parse arguments
1964 1967 */
1965 1968 for (int i = 0; i < args.length; i++) {
1966 1969 if (args[i].equals("-port")) {
1967 1970 if (ssf != null) {
1968 1971 bomb(getTextResource("rmid.syntax.port.badarg"));
1969 1972 }
1970 1973 if ((i + 1) < args.length) {
1971 1974 try {
1972 1975 port = Integer.parseInt(args[++i]);
1973 1976 } catch (NumberFormatException nfe) {
1974 1977 bomb(getTextResource("rmid.syntax.port.badnumber"));
1975 1978 }
1976 1979 } else {
1977 1980 bomb(getTextResource("rmid.syntax.port.missing"));
1978 1981 }
1979 1982
1980 1983 } else if (args[i].equals("-log")) {
1981 1984 if ((i + 1) < args.length) {
1982 1985 log = args[++i];
1983 1986 } else {
1984 1987 bomb(getTextResource("rmid.syntax.log.missing"));
1985 1988 }
1986 1989
1987 1990 } else if (args[i].equals("-stop")) {
1988 1991 stop = true;
1989 1992
1990 1993 } else if (args[i].startsWith("-C")) {
1991 1994 childArgs.add(args[i].substring(2));
1992 1995
1993 1996 } else {
1994 1997 bomb(MessageFormat.format(
1995 1998 getTextResource("rmid.syntax.illegal.option"),
1996 1999 args[i]));
1997 2000 }
1998 2001 }
1999 2002
2000 2003 if (log == null) {
2001 2004 if (ssf != null) {
2002 2005 bomb(getTextResource("rmid.syntax.log.required"));
2003 2006 } else {
2004 2007 log = "log";
2005 2008 }
2006 2009 }
2007 2010
2008 2011 debugExec = AccessController.doPrivileged(
2009 2012 new GetBooleanAction("sun.rmi.server.activation.debugExec"));
2010 2013
2011 2014 /**
2012 2015 * Determine class name for activation exec policy (if any).
2013 2016 */
2014 2017 String execPolicyClassName = AccessController.doPrivileged(
2015 2018 new GetPropertyAction("sun.rmi.activation.execPolicy", null));
2016 2019 if (execPolicyClassName == null) {
2017 2020 if (!stop) {
2018 2021 DefaultExecPolicy.checkConfiguration();
2019 2022 }
2020 2023 execPolicyClassName = "default";
2021 2024 }
2022 2025
2023 2026 /**
↓ open down ↓ |
53 lines elided |
↑ open up ↑ |
2024 2027 * Initialize method for activation exec policy.
2025 2028 */
2026 2029 if (!execPolicyClassName.equals("none")) {
2027 2030 if (execPolicyClassName.equals("") ||
2028 2031 execPolicyClassName.equals("default"))
2029 2032 {
2030 2033 execPolicyClassName = DefaultExecPolicy.class.getName();
2031 2034 }
2032 2035
2033 2036 try {
2034 - Class<?> execPolicyClass =
2035 - RMIClassLoader.loadClass(execPolicyClassName);
2037 + Class<?> execPolicyClass = getRMIClass(execPolicyClassName);
2038 +
2036 2039 execPolicy = execPolicyClass.newInstance();
2037 2040 execPolicyMethod =
2038 2041 execPolicyClass.getMethod("checkExecCommand",
2039 2042 ActivationGroupDesc.class,
2040 2043 String[].class);
2041 2044 } catch (Exception e) {
2042 2045 if (debugExec) {
2043 2046 System.err.println(
2044 2047 getTextResource("rmid.exec.policy.exception"));
2045 2048 e.printStackTrace();
2046 2049 }
2047 2050 bomb(getTextResource("rmid.exec.policy.invalid"));
2048 2051 }
2049 2052 }
2050 2053
2051 2054 if (stop == true) {
2052 2055 final int finalPort = port;
2053 2056 AccessController.doPrivileged(new PrivilegedAction<Void>() {
2054 2057 public Void run() {
2055 2058 System.setProperty("java.rmi.activation.port",
2056 2059 Integer.toString(finalPort));
2057 2060 return null;
2058 2061 }
2059 2062 });
2060 2063 ActivationSystem system = ActivationGroup.getSystem();
2061 2064 system.shutdown();
2062 2065 System.exit(0);
2063 2066 }
2064 2067
2065 2068 /*
2066 2069 * Fix for 4173960: Create and initialize activation using
2067 2070 * a static method, startActivation, which will build the
2068 2071 * Activation state in two ways: if when rmid is run, no
2069 2072 * log file is found, the ActLogHandler.recover(...)
2070 2073 * method will create a new Activation instance.
2071 2074 * Alternatively, if a logfile is available, a serialized
2072 2075 * instance of activation will be read from the log's
2073 2076 * snapshot file. Log updates will be applied to this
2074 2077 * Activation object until rmid's state has been fully
2075 2078 * recovered. In either case, only one instance of
2076 2079 * Activation is created.
2077 2080 */
2078 2081 startActivation(port, ssf, log,
2079 2082 childArgs.toArray(new String[childArgs.size()]));
2080 2083
2081 2084 // prevent activator from exiting
2082 2085 while (true) {
2083 2086 try {
2084 2087 Thread.sleep(Long.MAX_VALUE);
2085 2088 } catch (InterruptedException e) {
2086 2089 }
2087 2090 }
2088 2091 } catch (Exception e) {
2089 2092 System.err.println(
2090 2093 MessageFormat.format(
2091 2094 getTextResource("rmid.unexpected.exception"), e));
2092 2095 e.printStackTrace();
2093 2096 }
2094 2097 System.exit(1);
2095 2098 }
2096 2099
2097 2100 /**
2098 2101 * Retrieves text resources from the locale-specific properties file.
2099 2102 */
2100 2103 private static String getTextResource(String key) {
2101 2104 if (Activation.resources == null) {
2102 2105 try {
2103 2106 Activation.resources = ResourceBundle.getBundle(
2104 2107 "sun.rmi.server.resources.rmid");
2105 2108 } catch (MissingResourceException mre) {
2106 2109 }
2107 2110 if (Activation.resources == null) {
2108 2111 // throwing an Error is a bit extreme, methinks
2109 2112 return ("[missing resource file: " + key + "]");
2110 2113 }
2111 2114 }
2112 2115
2113 2116 String val = null;
2114 2117 try {
2115 2118 val = Activation.resources.getString (key);
↓ open down ↓ |
70 lines elided |
↑ open up ↑ |
2116 2119 } catch (MissingResourceException mre) {
2117 2120 }
2118 2121
2119 2122 if (val == null) {
2120 2123 return ("[missing resource: " + key + "]");
2121 2124 } else {
2122 2125 return val;
2123 2126 }
2124 2127 }
2125 2128
2129 + @SuppressWarnings("deprecation")
2130 + private static Class<?> getRMIClass(String execPolicyClassName) throws Exception {
2131 + return RMIClassLoader.loadClass(execPolicyClassName);
2132 + }
2126 2133 /*
2127 2134 * Dijkstra semaphore operations to limit the number of subprocesses
2128 2135 * rmid attempts to make at once.
2129 2136 */
2130 2137 /**
2131 2138 * Acquire the group semaphore and return a group name. Each
2132 2139 * Pstartgroup must be followed by a Vstartgroup. The calling thread
2133 2140 * will wait until there are fewer than <code>N</code> other threads
2134 2141 * holding the group semaphore. The calling thread will then acquire
2135 2142 * the semaphore and return.
2136 2143 */
2137 2144 private synchronized String Pstartgroup() throws ActivationException {
2138 2145 while (true) {
2139 2146 checkShutdown();
2140 2147 // Wait until positive, then decrement.
2141 2148 if (groupSemaphore > 0) {
2142 2149 groupSemaphore--;
2143 2150 return "Group-" + groupCounter++;
2144 2151 }
2145 2152
2146 2153 try {
2147 2154 wait();
2148 2155 } catch (InterruptedException e) {
2149 2156 }
2150 2157 }
2151 2158 }
2152 2159
2153 2160 /**
2154 2161 * Release the group semaphore. Every P operation must be
2155 2162 * followed by a V operation. This may cause another thread to
2156 2163 * wake up and return from its P operation.
2157 2164 */
2158 2165 private synchronized void Vstartgroup() {
2159 2166 // Increment and notify a waiter (not necessarily FIFO).
2160 2167 groupSemaphore++;
2161 2168 notifyAll();
2162 2169 }
2163 2170
2164 2171 /**
2165 2172 * A server socket factory to use when rmid is launched via 'inetd'
2166 2173 * with 'wait' status. This socket factory's 'createServerSocket'
2167 2174 * method returns the server socket specified during construction that
2168 2175 * is specialized to delay accepting requests until the
2169 2176 * 'initDone' flag is 'true'. The server socket supplied to
2170 2177 * the constructor should be the server socket obtained from the
2171 2178 * ServerSocketChannel returned from the 'System.inheritedChannel'
2172 2179 * method.
2173 2180 **/
2174 2181 private static class ActivationServerSocketFactory
2175 2182 implements RMIServerSocketFactory
2176 2183 {
2177 2184 private final ServerSocket serverSocket;
2178 2185
2179 2186 /**
2180 2187 * Constructs an 'ActivationServerSocketFactory' with the specified
2181 2188 * 'serverSocket'.
2182 2189 **/
2183 2190 ActivationServerSocketFactory(ServerSocket serverSocket) {
2184 2191 this.serverSocket = serverSocket;
2185 2192 }
2186 2193
2187 2194 /**
2188 2195 * Returns the server socket specified during construction wrapped
2189 2196 * in a 'DelayedAcceptServerSocket'.
2190 2197 **/
2191 2198 public ServerSocket createServerSocket(int port)
2192 2199 throws IOException
2193 2200 {
2194 2201 return new DelayedAcceptServerSocket(serverSocket);
2195 2202 }
2196 2203
2197 2204 }
2198 2205
2199 2206 /**
2200 2207 * A server socket that delegates all public methods to the underlying
2201 2208 * server socket specified at construction. The accept method is
2202 2209 * overridden to delay calling accept on the underlying server socket
2203 2210 * until the 'initDone' flag is 'true'.
2204 2211 **/
2205 2212 private static class DelayedAcceptServerSocket extends ServerSocket {
2206 2213
2207 2214 private final ServerSocket serverSocket;
2208 2215
2209 2216 DelayedAcceptServerSocket(ServerSocket serverSocket)
2210 2217 throws IOException
2211 2218 {
2212 2219 this.serverSocket = serverSocket;
2213 2220 }
2214 2221
2215 2222 public void bind(SocketAddress endpoint) throws IOException {
2216 2223 serverSocket.bind(endpoint);
2217 2224 }
2218 2225
2219 2226 public void bind(SocketAddress endpoint, int backlog)
2220 2227 throws IOException
2221 2228 {
2222 2229 serverSocket.bind(endpoint, backlog);
2223 2230 }
2224 2231
2225 2232 public InetAddress getInetAddress() {
2226 2233 return serverSocket.getInetAddress();
2227 2234 }
2228 2235
2229 2236 public int getLocalPort() {
2230 2237 return serverSocket.getLocalPort();
2231 2238 }
2232 2239
2233 2240 public SocketAddress getLocalSocketAddress() {
2234 2241 return serverSocket.getLocalSocketAddress();
2235 2242 }
2236 2243
2237 2244 /**
2238 2245 * Delays calling accept on the underlying server socket until the
2239 2246 * remote service is bound in the registry.
2240 2247 **/
2241 2248 public Socket accept() throws IOException {
2242 2249 synchronized (initLock) {
2243 2250 try {
2244 2251 while (!initDone) {
2245 2252 initLock.wait();
2246 2253 }
2247 2254 } catch (InterruptedException ignore) {
2248 2255 throw new AssertionError(ignore);
2249 2256 }
2250 2257 }
2251 2258 return serverSocket.accept();
2252 2259 }
2253 2260
2254 2261 public void close() throws IOException {
2255 2262 serverSocket.close();
2256 2263 }
2257 2264
2258 2265 public ServerSocketChannel getChannel() {
2259 2266 return serverSocket.getChannel();
2260 2267 }
2261 2268
2262 2269 public boolean isBound() {
2263 2270 return serverSocket.isBound();
2264 2271 }
2265 2272
2266 2273 public boolean isClosed() {
2267 2274 return serverSocket.isClosed();
2268 2275 }
2269 2276
2270 2277 public void setSoTimeout(int timeout)
2271 2278 throws SocketException
2272 2279 {
2273 2280 serverSocket.setSoTimeout(timeout);
2274 2281 }
2275 2282
2276 2283 public int getSoTimeout() throws IOException {
2277 2284 return serverSocket.getSoTimeout();
2278 2285 }
2279 2286
2280 2287 public void setReuseAddress(boolean on) throws SocketException {
2281 2288 serverSocket.setReuseAddress(on);
2282 2289 }
2283 2290
2284 2291 public boolean getReuseAddress() throws SocketException {
2285 2292 return serverSocket.getReuseAddress();
2286 2293 }
2287 2294
2288 2295 public String toString() {
2289 2296 return serverSocket.toString();
2290 2297 }
2291 2298
2292 2299 public void setReceiveBufferSize(int size)
2293 2300 throws SocketException
2294 2301 {
2295 2302 serverSocket.setReceiveBufferSize(size);
2296 2303 }
2297 2304
2298 2305 public int getReceiveBufferSize()
2299 2306 throws SocketException
2300 2307 {
2301 2308 return serverSocket.getReceiveBufferSize();
2302 2309 }
2303 2310 }
2304 2311 }
2305 2312
2306 2313 /**
2307 2314 * PipeWriter plugs together two pairs of input and output streams by
2308 2315 * providing readers for input streams and writing through to
2309 2316 * appropriate output streams. Both output streams are annotated on a
2310 2317 * per-line basis.
2311 2318 *
2312 2319 * @author Laird Dornin, much code borrowed from Peter Jones, Ken
2313 2320 * Arnold and Ann Wollrath.
2314 2321 */
2315 2322 class PipeWriter implements Runnable {
2316 2323
2317 2324 /** stream used for buffering lines */
2318 2325 private ByteArrayOutputStream bufOut;
2319 2326
2320 2327 /** count since last separator */
2321 2328 private int cLast;
2322 2329
2323 2330 /** current chunk of input being compared to lineSeparator.*/
2324 2331 private byte[] currSep;
2325 2332
2326 2333 private PrintWriter out;
2327 2334 private InputStream in;
2328 2335
2329 2336 private String pipeString;
2330 2337 private String execString;
2331 2338
2332 2339 private static String lineSeparator;
2333 2340 private static int lineSeparatorLength;
2334 2341
2335 2342 private static int numExecs = 0;
2336 2343
2337 2344 static {
2338 2345 lineSeparator = AccessController.doPrivileged(
2339 2346 new GetPropertyAction("line.separator"));
2340 2347 lineSeparatorLength = lineSeparator.length();
2341 2348 }
2342 2349
2343 2350 /**
2344 2351 * Create a new PipeWriter object. All methods of PipeWriter,
2345 2352 * except plugTogetherPair, are only accesible to PipeWriter
2346 2353 * itself. Synchronization is unnecessary on functions that will
2347 2354 * only be used internally in PipeWriter.
2348 2355 *
2349 2356 * @param in input stream from which pipe input flows
2350 2357 * @param out output stream to which log messages will be sent
2351 2358 * @param dest String which tags output stream as 'out' or 'err'
2352 2359 * @param nExecs number of execed processes, Activation groups.
2353 2360 */
2354 2361 private PipeWriter
2355 2362 (InputStream in, OutputStream out, String tag, int nExecs) {
2356 2363
2357 2364 this.in = in;
2358 2365 this.out = new PrintWriter(out);
2359 2366
2360 2367 bufOut = new ByteArrayOutputStream();
2361 2368 currSep = new byte[lineSeparatorLength];
2362 2369
2363 2370 /* set unique pipe/pair annotations */
2364 2371 execString = ":ExecGroup-" +
2365 2372 Integer.toString(nExecs) + ':' + tag + ':';
2366 2373 }
2367 2374
2368 2375 /**
2369 2376 * Create a thread to listen and read from input stream, in. buffer
2370 2377 * the data that is read until a marker which equals lineSeparator
2371 2378 * is read. Once such a string has been discovered; write out an
2372 2379 * annotation string followed by the buffered data and a line
2373 2380 * separator.
2374 2381 */
2375 2382 public void run() {
2376 2383 byte[] buf = new byte[256];
2377 2384 int count;
2378 2385
2379 2386 try {
2380 2387 /* read bytes till there are no more. */
2381 2388 while ((count = in.read(buf)) != -1) {
2382 2389 write(buf, 0, count);
2383 2390 }
2384 2391
2385 2392 /* flush internal buffer... may not have ended on a line
2386 2393 * separator, we also need a last annotation if
2387 2394 * something was left.
2388 2395 */
2389 2396 String lastInBuffer = bufOut.toString();
2390 2397 bufOut.reset();
2391 2398 if (lastInBuffer.length() > 0) {
2392 2399 out.println (createAnnotation() + lastInBuffer);
2393 2400 out.flush(); // add a line separator
2394 2401 // to make output nicer
2395 2402 }
2396 2403
2397 2404 } catch (IOException e) {
2398 2405 }
2399 2406 }
2400 2407
2401 2408 /**
2402 2409 * Write a subarray of bytes. Pass each through write byte method.
2403 2410 */
2404 2411 private void write(byte b[], int off, int len) throws IOException {
2405 2412
2406 2413 if (len < 0) {
2407 2414 throw new ArrayIndexOutOfBoundsException(len);
2408 2415 }
2409 2416 for (int i = 0; i < len; ++ i) {
2410 2417 write(b[off + i]);
2411 2418 }
2412 2419 }
2413 2420
2414 2421 /**
2415 2422 * Write a byte of data to the stream. If we have not matched a
2416 2423 * line separator string, then the byte is appended to the internal
2417 2424 * buffer. If we have matched a line separator, then the currently
2418 2425 * buffered line is sent to the output writer with a prepended
2419 2426 * annotation string.
2420 2427 */
2421 2428 private void write(byte b) throws IOException {
2422 2429 int i = 0;
2423 2430
2424 2431 /* shift current to the left */
2425 2432 for (i = 1 ; i < (currSep.length); i ++) {
2426 2433 currSep[i-1] = currSep[i];
2427 2434 }
2428 2435 currSep[i-1] = b;
2429 2436 bufOut.write(b);
2430 2437
2431 2438 /* enough characters for a separator? */
2432 2439 if ( (cLast >= (lineSeparatorLength - 1)) &&
2433 2440 (lineSeparator.equals(new String(currSep))) ) {
2434 2441
2435 2442 cLast = 0;
2436 2443
2437 2444 /* write prefix through to underlying byte stream */
2438 2445 out.print(createAnnotation() + bufOut.toString());
2439 2446 out.flush();
2440 2447 bufOut.reset();
2441 2448
2442 2449 if (out.checkError()) {
2443 2450 throw new IOException
2444 2451 ("PipeWriter: IO Exception when"+
2445 2452 " writing to output stream.");
2446 2453 }
2447 2454
2448 2455 } else {
2449 2456 cLast++;
2450 2457 }
2451 2458 }
2452 2459
2453 2460 /**
2454 2461 * Create an annotation string to be printed out after
2455 2462 * a new line and end of stream.
2456 2463 */
2457 2464 private String createAnnotation() {
2458 2465
2459 2466 /* construct prefix for log messages:
2460 2467 * date/time stamp...
2461 2468 */
2462 2469 return ((new Date()).toString() +
2463 2470 /* ... print pair # ... */
2464 2471 (execString));
2465 2472 }
2466 2473
2467 2474 /**
2468 2475 * Allow plugging together two pipes at a time, to associate
2469 2476 * output from an execed process. This is the only publicly
2470 2477 * accessible method of this object; this helps ensure that
2471 2478 * synchronization will not be an issue in the annotation
2472 2479 * process.
2473 2480 *
2474 2481 * @param in input stream from which pipe input comes
2475 2482 * @param out output stream to which log messages will be sent
2476 2483 * @param in1 input stream from which pipe input comes
2477 2484 * @param out1 output stream to which log messages will be sent
2478 2485 */
2479 2486 static void plugTogetherPair(InputStream in,
2480 2487 OutputStream out,
2481 2488 InputStream in1,
2482 2489 OutputStream out1) {
2483 2490 Thread inThread = null;
2484 2491 Thread outThread = null;
2485 2492
2486 2493 int nExecs = getNumExec();
2487 2494
2488 2495 /* start RMI threads to read output from child process */
2489 2496 inThread = AccessController.doPrivileged(
2490 2497 new NewThreadAction(new PipeWriter(in, out, "out", nExecs),
2491 2498 "out", true));
2492 2499 outThread = AccessController.doPrivileged(
2493 2500 new NewThreadAction(new PipeWriter(in1, out1, "err", nExecs),
2494 2501 "err", true));
2495 2502 inThread.start();
2496 2503 outThread.start();
2497 2504 }
2498 2505
2499 2506 private static synchronized int getNumExec() {
2500 2507 return numExecs++;
2501 2508 }
2502 2509 }
↓ open down ↓ |
367 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX