0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.rmi.server;
0N/A
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.File;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
4105N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.PrintWriter;
0N/Aimport java.io.Serializable;
0N/Aimport java.lang.Process;
0N/Aimport java.lang.reflect.InvocationTargetException;
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.net.InetAddress;
0N/Aimport java.net.ServerSocket;
0N/Aimport java.net.Socket;
0N/Aimport java.net.SocketAddress;
0N/Aimport java.net.SocketException;
5089N/Aimport java.nio.file.Files;
0N/Aimport java.nio.channels.Channel;
0N/Aimport java.nio.channels.ServerSocketChannel;
0N/Aimport java.rmi.AccessException;
0N/Aimport java.rmi.AlreadyBoundException;
0N/Aimport java.rmi.ConnectException;
0N/Aimport java.rmi.ConnectIOException;
0N/Aimport java.rmi.MarshalledObject;
0N/Aimport java.rmi.NoSuchObjectException;
0N/Aimport java.rmi.NotBoundException;
0N/Aimport java.rmi.Remote;
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.activation.ActivationDesc;
0N/Aimport java.rmi.activation.ActivationException;
0N/Aimport java.rmi.activation.ActivationGroupDesc;
0N/Aimport java.rmi.activation.ActivationGroup;
0N/Aimport java.rmi.activation.ActivationGroupID;
0N/Aimport java.rmi.activation.ActivationID;
0N/Aimport java.rmi.activation.ActivationInstantiator;
0N/Aimport java.rmi.activation.ActivationMonitor;
0N/Aimport java.rmi.activation.ActivationSystem;
0N/Aimport java.rmi.activation.Activator;
0N/Aimport java.rmi.activation.UnknownGroupException;
0N/Aimport java.rmi.activation.UnknownObjectException;
0N/Aimport java.rmi.registry.Registry;
0N/Aimport java.rmi.server.ObjID;
0N/Aimport java.rmi.server.RMIClassLoader;
0N/Aimport java.rmi.server.RMIClientSocketFactory;
0N/Aimport java.rmi.server.RMIServerSocketFactory;
0N/Aimport java.rmi.server.RemoteObject;
0N/Aimport java.rmi.server.RemoteServer;
0N/Aimport java.rmi.server.UnicastRemoteObject;
0N/Aimport java.security.AccessControlException;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.AllPermission;
0N/Aimport java.security.CodeSource;
0N/Aimport java.security.Permission;
0N/Aimport java.security.PermissionCollection;
0N/Aimport java.security.Permissions;
0N/Aimport java.security.Policy;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.security.PrivilegedExceptionAction;
0N/Aimport java.security.cert.Certificate;
0N/Aimport java.text.MessageFormat;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.Date;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.util.MissingResourceException;
0N/Aimport java.util.Properties;
0N/Aimport java.util.ResourceBundle;
0N/Aimport java.util.Set;
4105N/Aimport java.util.concurrent.ConcurrentHashMap;
0N/Aimport sun.rmi.log.LogHandler;
0N/Aimport sun.rmi.log.ReliableLog;
0N/Aimport sun.rmi.registry.RegistryImpl;
0N/Aimport sun.rmi.runtime.NewThreadAction;
0N/Aimport sun.rmi.server.UnicastServerRef;
0N/Aimport sun.rmi.transport.LiveRef;
0N/Aimport sun.security.action.GetBooleanAction;
0N/Aimport sun.security.action.GetIntegerAction;
0N/Aimport sun.security.action.GetPropertyAction;
0N/Aimport sun.security.provider.PolicyFile;
0N/Aimport com.sun.rmi.rmid.ExecPermission;
0N/Aimport com.sun.rmi.rmid.ExecOptionPermission;
0N/A
0N/A/**
0N/A * The Activator facilitates remote object activation. A "faulting"
0N/A * remote reference calls the activator's <code>activate</code> method
0N/A * to obtain a "live" reference to a activatable remote object. Upon
0N/A * receiving a request for activation, the activator looks up the
0N/A * activation descriptor for the activation identifier, id, determines
0N/A * the group in which the object should be activated and invokes the
0N/A * activate method on the object's activation group (described by the
0N/A * remote interface <code>ActivationInstantiator</code>). The
0N/A * activator initiates the execution of activation groups as
0N/A * necessary. For example, if an activation group for a specific group
0N/A * identifier is not already executing, the activator will spawn a
0N/A * child process for the activation group. <p>
0N/A *
0N/A * The activator is responsible for monitoring and detecting when
0N/A * activation groups fail so that it can remove stale remote references
0N/A * from its internal tables. <p>
0N/A *
0N/A * @author Ann Wollrath
0N/A * @since 1.2
0N/A */
0N/Apublic class Activation implements Serializable {
0N/A
0N/A /** indicate compatibility with JDK 1.2 version of class */
0N/A private static final long serialVersionUID = 2921265612698155191L;
0N/A private static final byte MAJOR_VERSION = 1;
0N/A private static final byte MINOR_VERSION = 0;
0N/A
0N/A /** exec policy object */
0N/A private static Object execPolicy;
0N/A private static Method execPolicyMethod;
0N/A private static boolean debugExec;
0N/A
0N/A /** maps activation id to its respective group id */
0N/A private Map<ActivationID,ActivationGroupID> idTable =
4105N/A new ConcurrentHashMap<>();
0N/A /** maps group id to its GroupEntry groups */
0N/A private Map<ActivationGroupID,GroupEntry> groupTable =
4105N/A new ConcurrentHashMap<>();
0N/A
0N/A private byte majorVersion = MAJOR_VERSION;
0N/A private byte minorVersion = MINOR_VERSION;
0N/A
0N/A /** number of simultaneous group exec's */
0N/A private transient int groupSemaphore;
0N/A /** counter for numbering groups */
0N/A private transient int groupCounter;
0N/A /** reliable log to hold descriptor table */
0N/A private transient ReliableLog log;
0N/A /** number of updates since last snapshot */
0N/A private transient int numUpdates;
0N/A
0N/A /** the java command */
0N/A // accessed by GroupEntry
0N/A private transient String[] command;
0N/A /** timeout on wait for child process to be created or destroyed */
0N/A private static final long groupTimeout =
0N/A getInt("sun.rmi.activation.groupTimeout", 60000);
0N/A /** take snapshot after this many updates */
0N/A private static final int snapshotInterval =
0N/A getInt("sun.rmi.activation.snapshotInterval", 200);
0N/A /** timeout on wait for child process to be created */
0N/A private static final long execTimeout =
0N/A getInt("sun.rmi.activation.execTimeout", 30000);
0N/A
0N/A private static final Object initLock = new Object();
0N/A private static boolean initDone = false;
0N/A
0N/A // this should be a *private* method since it is privileged
0N/A private static int getInt(String name, int def) {
0N/A return AccessController.doPrivileged(new GetIntegerAction(name, def));
0N/A }
0N/A
0N/A private transient Activator activator;
0N/A private transient Activator activatorStub;
0N/A private transient ActivationSystem system;
0N/A private transient ActivationSystem systemStub;
0N/A private transient ActivationMonitor monitor;
0N/A private transient Registry registry;
0N/A private transient volatile boolean shuttingDown = false;
0N/A private transient volatile Object startupLock;
0N/A private transient Thread shutdownHook;
0N/A
0N/A private static ResourceBundle resources = null;
0N/A
0N/A /**
0N/A * Create an uninitialized instance of Activation that can be
0N/A * populated with log data. This is only called when the initial
0N/A * snapshot is taken during the first incarnation of rmid.
0N/A */
0N/A private Activation() {}
0N/A
0N/A /**
0N/A * Recover activation state from the reliable log and initialize
0N/A * activation services.
0N/A */
0N/A private static void startActivation(int port,
0N/A RMIServerSocketFactory ssf,
0N/A String logName,
0N/A String[] childArgs)
0N/A throws Exception
0N/A {
0N/A ReliableLog log = new ReliableLog(logName, new ActLogHandler());
0N/A Activation state = (Activation) log.recover();
0N/A state.init(port, ssf, log, childArgs);
0N/A }
0N/A
0N/A /**
0N/A * Initialize the Activation instantiation; start activation
0N/A * services.
0N/A */
0N/A private void init(int port,
0N/A RMIServerSocketFactory ssf,
0N/A ReliableLog log,
0N/A String[] childArgs)
0N/A throws Exception
0N/A {
0N/A // initialize
0N/A this.log = log;
0N/A numUpdates = 0;
0N/A shutdownHook = new ShutdownHook();
0N/A groupSemaphore = getInt("sun.rmi.activation.groupThrottle", 3);
0N/A groupCounter = 0;
0N/A Runtime.getRuntime().addShutdownHook(shutdownHook);
4105N/A
4105N/A // Use array size of 0, since the value from calling size()
4105N/A // may be out of date by the time toArray() is called.
0N/A ActivationGroupID[] gids =
4105N/A groupTable.keySet().toArray(new ActivationGroupID[0]);
0N/A
0N/A synchronized (startupLock = new Object()) {
0N/A // all the remote methods briefly synchronize on startupLock
0N/A // (via checkShutdown) to make sure they don't happen in the
0N/A // middle of this block. This block must not cause any such
0N/A // incoming remote calls to happen, or deadlock would result!
0N/A activator = new ActivatorImpl(port, ssf);
0N/A activatorStub = (Activator) RemoteObject.toStub(activator);
0N/A system = new ActivationSystemImpl(port, ssf);
0N/A systemStub = (ActivationSystem) RemoteObject.toStub(system);
0N/A monitor = new ActivationMonitorImpl(port, ssf);
0N/A initCommand(childArgs);
0N/A registry = new SystemRegistryImpl(port, null, ssf, systemStub);
0N/A
0N/A if (ssf != null) {
0N/A synchronized (initLock) {
0N/A initDone = true;
0N/A initLock.notifyAll();
0N/A }
0N/A }
0N/A }
0N/A startupLock = null;
0N/A
0N/A // restart services
0N/A for (int i = gids.length; --i >= 0; ) {
0N/A try {
0N/A getGroupEntry(gids[i]).restartServices();
0N/A } catch (UnknownGroupException e) {
0N/A System.err.println(
0N/A getTextResource("rmid.restart.group.warning"));
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A
4105N/A /**
4105N/A * Previous versions used HashMap instead of ConcurrentHashMap.
4105N/A * Replace any HashMaps found during deserialization with
4105N/A * ConcurrentHashMaps.
4105N/A */
4105N/A private void readObject(ObjectInputStream ois)
4105N/A throws IOException, ClassNotFoundException
4105N/A {
4105N/A ois.defaultReadObject();
4105N/A if (! (groupTable instanceof ConcurrentHashMap)) {
4105N/A groupTable = new ConcurrentHashMap<>(groupTable);
4105N/A }
4105N/A if (! (idTable instanceof ConcurrentHashMap)) {
4105N/A idTable = new ConcurrentHashMap<>(idTable);
4105N/A }
4105N/A }
4105N/A
0N/A private static class SystemRegistryImpl extends RegistryImpl {
0N/A
0N/A private static final String NAME = ActivationSystem.class.getName();
5559N/A private static final long serialVersionUID = 4877330021609408794L;
0N/A private final ActivationSystem systemStub;
0N/A
0N/A SystemRegistryImpl(int port,
0N/A RMIClientSocketFactory csf,
0N/A RMIServerSocketFactory ssf,
0N/A ActivationSystem systemStub)
0N/A throws RemoteException
0N/A {
0N/A super(port, csf, ssf);
0N/A this.systemStub = systemStub;
0N/A }
0N/A
0N/A /**
0N/A * Returns the activation system stub if the specified name
0N/A * matches the activation system's class name, otherwise
0N/A * returns the result of invoking super.lookup with the specified
0N/A * name.
0N/A */
0N/A public Remote lookup(String name)
0N/A throws RemoteException, NotBoundException
0N/A {
0N/A if (name.equals(NAME)) {
0N/A return systemStub;
0N/A } else {
0N/A return super.lookup(name);
0N/A }
0N/A }
0N/A
0N/A public String[] list() throws RemoteException {
0N/A String[] list1 = super.list();
0N/A int length = list1.length;
0N/A String[] list2 = new String[length + 1];
0N/A if (length > 0) {
0N/A System.arraycopy(list1, 0, list2, 0, length);
0N/A }
0N/A list2[length] = NAME;
0N/A return list2;
0N/A }
0N/A
0N/A public void bind(String name, Remote obj)
0N/A throws RemoteException, AlreadyBoundException, AccessException
0N/A {
0N/A if (name.equals(NAME)) {
0N/A throw new AccessException(
0N/A "binding ActivationSystem is disallowed");
0N/A } else {
0N/A super.bind(name, obj);
0N/A }
0N/A }
0N/A
0N/A public void unbind(String name)
0N/A throws RemoteException, NotBoundException, AccessException
0N/A {
0N/A if (name.equals(NAME)) {
0N/A throw new AccessException(
0N/A "unbinding ActivationSystem is disallowed");
0N/A } else {
0N/A super.unbind(name);
0N/A }
0N/A }
0N/A
0N/A
0N/A public void rebind(String name, Remote obj)
0N/A throws RemoteException, AccessException
0N/A {
0N/A if (name.equals(NAME)) {
0N/A throw new AccessException(
0N/A "binding ActivationSystem is disallowed");
0N/A } else {
0N/A super.rebind(name, obj);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A class ActivatorImpl extends RemoteServer implements Activator {
0N/A // Because ActivatorImpl has a fixed ObjID, it can be
0N/A // called by clients holding stale remote references. Each of
0N/A // its remote methods, then, must check startupLock (calling
0N/A // checkShutdown() is easiest).
0N/A
0N/A private static final long serialVersionUID = -3654244726254566136L;
0N/A
0N/A /**
0N/A * Construct a new Activator on a specified port.
0N/A */
0N/A ActivatorImpl(int port, RMIServerSocketFactory ssf)
0N/A throws RemoteException
0N/A {
0N/A /* Server ref must be created and assigned before remote object
0N/A * 'this' can be exported.
0N/A */
0N/A LiveRef lref =
0N/A new LiveRef(new ObjID(ObjID.ACTIVATOR_ID), port, null, ssf);
0N/A UnicastServerRef uref = new UnicastServerRef(lref);
0N/A ref = uref;
0N/A uref.exportObject(this, null, false);
0N/A }
0N/A
0N/A public MarshalledObject<? extends Remote> activate(ActivationID id,
0N/A boolean force)
0N/A throws ActivationException, UnknownObjectException, RemoteException
0N/A {
0N/A checkShutdown();
0N/A return getGroupEntry(id).activate(id, force);
0N/A }
0N/A }
0N/A
0N/A class ActivationMonitorImpl extends UnicastRemoteObject
0N/A implements ActivationMonitor
0N/A {
0N/A private static final long serialVersionUID = -6214940464757948867L;
0N/A
0N/A ActivationMonitorImpl(int port, RMIServerSocketFactory ssf)
0N/A throws RemoteException
0N/A {
0N/A super(port, null, ssf);
0N/A }
0N/A
0N/A public void inactiveObject(ActivationID id)
0N/A throws UnknownObjectException, RemoteException
0N/A {
0N/A try {
0N/A checkShutdown();
0N/A } catch (ActivationException e) {
0N/A return;
0N/A }
0N/A RegistryImpl.checkAccess("Activator.inactiveObject");
0N/A getGroupEntry(id).inactiveObject(id);
0N/A }
0N/A
0N/A public void activeObject(ActivationID id,
0N/A MarshalledObject<? extends Remote> mobj)
0N/A throws UnknownObjectException, RemoteException
0N/A {
0N/A try {
0N/A checkShutdown();
0N/A } catch (ActivationException e) {
0N/A return;
0N/A }
0N/A RegistryImpl.checkAccess("ActivationSystem.activeObject");
0N/A getGroupEntry(id).activeObject(id, mobj);
0N/A }
0N/A
0N/A public void inactiveGroup(ActivationGroupID id,
0N/A long incarnation)
0N/A throws UnknownGroupException, RemoteException
0N/A {
0N/A try {
0N/A checkShutdown();
0N/A } catch (ActivationException e) {
0N/A return;
0N/A }
0N/A RegistryImpl.checkAccess("ActivationMonitor.inactiveGroup");
0N/A getGroupEntry(id).inactiveGroup(incarnation, false);
0N/A }
0N/A }
0N/A
0N/A
0N/A class ActivationSystemImpl
0N/A extends RemoteServer
0N/A implements ActivationSystem
0N/A {
0N/A private static final long serialVersionUID = 9100152600327688967L;
0N/A
0N/A // Because ActivationSystemImpl has a fixed ObjID, it can be
0N/A // called by clients holding stale remote references. Each of
0N/A // its remote methods, then, must check startupLock (calling
0N/A // checkShutdown() is easiest).
0N/A ActivationSystemImpl(int port, RMIServerSocketFactory ssf)
0N/A throws RemoteException
0N/A {
0N/A /* Server ref must be created and assigned before remote object
0N/A * 'this' can be exported.
0N/A */
0N/A LiveRef lref = new LiveRef(new ObjID(4), port, null, ssf);
0N/A UnicastServerRef uref = new UnicastServerRef(lref);
0N/A ref = uref;
0N/A uref.exportObject(this, null);
0N/A }
0N/A
0N/A public ActivationID registerObject(ActivationDesc desc)
0N/A throws ActivationException, UnknownGroupException, RemoteException
0N/A {
0N/A checkShutdown();
0N/A RegistryImpl.checkAccess("ActivationSystem.registerObject");
0N/A
0N/A ActivationGroupID groupID = desc.getGroupID();
0N/A ActivationID id = new ActivationID(activatorStub);
0N/A getGroupEntry(groupID).registerObject(id, desc, true);
0N/A return id;
0N/A }
0N/A
0N/A public void unregisterObject(ActivationID id)
0N/A throws ActivationException, UnknownObjectException, RemoteException
0N/A {
0N/A checkShutdown();
0N/A RegistryImpl.checkAccess("ActivationSystem.unregisterObject");
0N/A getGroupEntry(id).unregisterObject(id, true);
0N/A }
0N/A
0N/A public ActivationGroupID registerGroup(ActivationGroupDesc desc)
0N/A throws ActivationException, RemoteException
0N/A {
0N/A checkShutdown();
0N/A RegistryImpl.checkAccess("ActivationSystem.registerGroup");
0N/A checkArgs(desc, null);
0N/A
0N/A ActivationGroupID id = new ActivationGroupID(systemStub);
0N/A GroupEntry entry = new GroupEntry(id, desc);
0N/A // table insertion must take place before log update
4105N/A groupTable.put(id, entry);
0N/A addLogRecord(new LogRegisterGroup(id, desc));
0N/A return id;
0N/A }
0N/A
0N/A public ActivationMonitor activeGroup(ActivationGroupID id,
0N/A ActivationInstantiator group,
0N/A long incarnation)
0N/A throws ActivationException, UnknownGroupException, RemoteException
0N/A {
0N/A checkShutdown();
0N/A RegistryImpl.checkAccess("ActivationSystem.activeGroup");
0N/A
0N/A getGroupEntry(id).activeGroup(group, incarnation);
0N/A return monitor;
0N/A }
0N/A
0N/A public void unregisterGroup(ActivationGroupID id)
0N/A throws ActivationException, UnknownGroupException, RemoteException
0N/A {
0N/A checkShutdown();
0N/A RegistryImpl.checkAccess("ActivationSystem.unregisterGroup");
0N/A
0N/A // remove entry before unregister so state is updated before
0N/A // logged
4105N/A removeGroupEntry(id).unregisterGroup(true);
0N/A }
0N/A
0N/A public ActivationDesc setActivationDesc(ActivationID id,
0N/A ActivationDesc desc)
0N/A throws ActivationException, UnknownObjectException, RemoteException
0N/A {
0N/A checkShutdown();
0N/A RegistryImpl.checkAccess("ActivationSystem.setActivationDesc");
0N/A
0N/A if (!getGroupID(id).equals(desc.getGroupID())) {
0N/A throw new ActivationException(
0N/A "ActivationDesc contains wrong group");
0N/A }
0N/A return getGroupEntry(id).setActivationDesc(id, desc, true);
0N/A }
0N/A
0N/A public ActivationGroupDesc setActivationGroupDesc(ActivationGroupID id,
0N/A ActivationGroupDesc desc)
0N/A throws ActivationException, UnknownGroupException, RemoteException
0N/A {
0N/A checkShutdown();
0N/A RegistryImpl.checkAccess(
0N/A "ActivationSystem.setActivationGroupDesc");
0N/A
0N/A checkArgs(desc, null);
0N/A return getGroupEntry(id).setActivationGroupDesc(id, desc, true);
0N/A }
0N/A
0N/A public ActivationDesc getActivationDesc(ActivationID id)
0N/A throws ActivationException, UnknownObjectException, RemoteException
0N/A {
0N/A checkShutdown();
0N/A RegistryImpl.checkAccess("ActivationSystem.getActivationDesc");
0N/A
0N/A return getGroupEntry(id).getActivationDesc(id);
0N/A }
0N/A
0N/A public ActivationGroupDesc getActivationGroupDesc(ActivationGroupID id)
0N/A throws ActivationException, UnknownGroupException, RemoteException
0N/A {
0N/A checkShutdown();
0N/A RegistryImpl.checkAccess
0N/A ("ActivationSystem.getActivationGroupDesc");
0N/A
0N/A return getGroupEntry(id).desc;
0N/A }
0N/A
0N/A /**
0N/A * Shutdown the activation system. Destroys all groups spawned by
0N/A * the activation daemon and exits the activation daemon.
0N/A */
0N/A public void shutdown() throws AccessException {
0N/A RegistryImpl.checkAccess("ActivationSystem.shutdown");
0N/A
0N/A Object lock = startupLock;
0N/A if (lock != null) {
0N/A synchronized (lock) {
0N/A // nothing
0N/A }
0N/A }
0N/A
0N/A synchronized (Activation.this) {
0N/A if (!shuttingDown) {
0N/A shuttingDown = true;
0N/A (new Shutdown()).start();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void checkShutdown() throws ActivationException {
0N/A // if the startup critical section is running, wait until it
0N/A // completes/fails before continuing with the remote call.
0N/A Object lock = startupLock;
0N/A if (lock != null) {
0N/A synchronized (lock) {
0N/A // nothing
0N/A }
0N/A }
0N/A
0N/A if (shuttingDown == true) {
0N/A throw new ActivationException(
0N/A "activation system shutting down");
0N/A }
0N/A }
0N/A
0N/A private static void unexport(Remote obj) {
0N/A for (;;) {
0N/A try {
0N/A if (UnicastRemoteObject.unexportObject(obj, false) == true) {
0N/A break;
0N/A } else {
0N/A Thread.sleep(100);
0N/A }
0N/A } catch (Exception e) {
0N/A continue;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Thread to shutdown rmid.
0N/A */
0N/A private class Shutdown extends Thread {
0N/A Shutdown() {
0N/A super("rmid Shutdown");
0N/A }
0N/A
0N/A public void run() {
0N/A try {
0N/A /*
0N/A * Unexport activation system services
0N/A */
0N/A unexport(activator);
0N/A unexport(system);
0N/A
0N/A // destroy all child processes (groups)
4105N/A for (GroupEntry groupEntry : groupTable.values()) {
0N/A groupEntry.shutdown();
0N/A }
0N/A
0N/A Runtime.getRuntime().removeShutdownHook(shutdownHook);
0N/A
0N/A /*
0N/A * Unexport monitor safely since all processes are destroyed.
0N/A */
0N/A unexport(monitor);
0N/A
0N/A /*
0N/A * Close log file, fix for 4243264: rmid shutdown thread
0N/A * interferes with remote calls in progress. Make sure
0N/A * the log file is only closed when it is impossible for
0N/A * its closure to interfere with any pending remote calls.
0N/A * We close the log when all objects in the rmid VM are
0N/A * unexported.
0N/A */
0N/A try {
0N/A synchronized (log) {
0N/A log.close();
0N/A }
0N/A } catch (IOException e) {
0N/A }
0N/A
0N/A } finally {
0N/A /*
0N/A * Now exit... A System.exit should only be done if
0N/A * the RMI activation system daemon was started up
0N/A * by the main method below (in which should always
0N/A * be the case since the Activation contructor is private).
0N/A */
0N/A System.err.println(getTextResource("rmid.daemon.shutdown"));
0N/A System.exit(0);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /** Thread to destroy children in the event of abnormal termination. */
0N/A private class ShutdownHook extends Thread {
0N/A ShutdownHook() {
0N/A super("rmid ShutdownHook");
0N/A }
0N/A
0N/A public void run() {
0N/A synchronized (Activation.this) {
0N/A shuttingDown = true;
0N/A }
0N/A
0N/A // destroy all child processes (groups) quickly
4105N/A for (GroupEntry groupEntry : groupTable.values()) {
4105N/A groupEntry.shutdownFast();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the groupID for a given id of an object in the group.
0N/A * Throws UnknownObjectException if the object is not registered.
0N/A */
0N/A private ActivationGroupID getGroupID(ActivationID id)
0N/A throws UnknownObjectException
0N/A {
4105N/A ActivationGroupID groupID = idTable.get(id);
4105N/A if (groupID != null) {
4105N/A return groupID;
4105N/A }
4105N/A throw new UnknownObjectException("unknown object: " + id);
4105N/A }
4105N/A
4105N/A /**
4105N/A * Returns the group entry for the group id, optionally removing it.
4105N/A * Throws UnknownGroupException if the group is not registered.
4105N/A */
4105N/A private GroupEntry getGroupEntry(ActivationGroupID id, boolean rm)
4105N/A throws UnknownGroupException
4105N/A {
4105N/A if (id.getClass() == ActivationGroupID.class) {
4105N/A GroupEntry entry;
4105N/A if (rm) {
4105N/A entry = groupTable.remove(id);
4105N/A } else {
4105N/A entry = groupTable.get(id);
4105N/A }
4105N/A if (entry != null && !entry.removed) {
4105N/A return entry;
0N/A }
0N/A }
4105N/A throw new UnknownGroupException("group unknown");
0N/A }
0N/A
0N/A /**
0N/A * Returns the group entry for the group id. Throws
0N/A * UnknownGroupException if the group is not registered.
0N/A */
0N/A private GroupEntry getGroupEntry(ActivationGroupID id)
0N/A throws UnknownGroupException
0N/A {
4105N/A return getGroupEntry(id, false);
4105N/A }
4105N/A
4105N/A /**
4105N/A * Removes and returns the group entry for the group id. Throws
4105N/A * UnknownGroupException if the group is not registered.
4105N/A */
4105N/A private GroupEntry removeGroupEntry(ActivationGroupID id)
4105N/A throws UnknownGroupException
4105N/A {
4105N/A return getGroupEntry(id, true);
0N/A }
0N/A
0N/A /**
0N/A * Returns the group entry for the object's id. Throws
0N/A * UnknownObjectException if the object is not registered or the
0N/A * object's group is not registered.
0N/A */
0N/A private GroupEntry getGroupEntry(ActivationID id)
0N/A throws UnknownObjectException
0N/A {
0N/A ActivationGroupID gid = getGroupID(id);
4105N/A GroupEntry entry = groupTable.get(gid);
4105N/A if (entry != null && !entry.removed) {
4105N/A return entry;
0N/A }
0N/A throw new UnknownObjectException("object's group removed");
0N/A }
0N/A
0N/A /**
0N/A * Container for group information: group's descriptor, group's
0N/A * instantiator, flag to indicate pending group creation, and
0N/A * table of the group's actived objects.
0N/A *
0N/A * WARNING: GroupEntry objects should not be written into log file
0N/A * updates. GroupEntrys are inner classes of Activation and they
0N/A * can not be serialized independent of this class. If the
0N/A * complete Activation system is written out as a log update, the
0N/A * point of having updates is nullified.
0N/A */
0N/A private class GroupEntry implements Serializable {
0N/A
0N/A /** indicate compatibility with JDK 1.2 version of class */
0N/A private static final long serialVersionUID = 7222464070032993304L;
0N/A private static final int MAX_TRIES = 2;
0N/A private static final int NORMAL = 0;
0N/A private static final int CREATING = 1;
0N/A private static final int TERMINATE = 2;
0N/A private static final int TERMINATING = 3;
0N/A
0N/A ActivationGroupDesc desc = null;
0N/A ActivationGroupID groupID = null;
0N/A long incarnation = 0;
5559N/A Map<ActivationID,ObjectEntry> objects = new HashMap<>();
5559N/A Set<ActivationID> restartSet = new HashSet<>();
0N/A
0N/A transient ActivationInstantiator group = null;
0N/A transient int status = NORMAL;
0N/A transient long waitTime = 0;
0N/A transient String groupName = null;
0N/A transient Process child = null;
0N/A transient boolean removed = false;
0N/A transient Watchdog watchdog = null;
0N/A
0N/A GroupEntry(ActivationGroupID groupID, ActivationGroupDesc desc) {
0N/A this.groupID = groupID;
0N/A this.desc = desc;
0N/A }
0N/A
0N/A void restartServices() {
0N/A Iterator<ActivationID> iter = null;
0N/A
0N/A synchronized (this) {
0N/A if (restartSet.isEmpty()) {
0N/A return;
0N/A }
0N/A
0N/A /*
0N/A * Clone the restartSet so the set does not have to be locked
0N/A * during iteration. Locking the restartSet could cause
0N/A * deadlock if an object we are restarting caused another
0N/A * object in this group to be activated.
0N/A */
0N/A iter = (new HashSet<ActivationID>(restartSet)).iterator();
0N/A }
0N/A
0N/A while (iter.hasNext()) {
0N/A ActivationID id = iter.next();
0N/A try {
0N/A activate(id, true);
0N/A } catch (Exception e) {
0N/A if (shuttingDown) {
0N/A return;
0N/A }
0N/A System.err.println(
0N/A getTextResource("rmid.restart.service.warning"));
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A
0N/A synchronized void activeGroup(ActivationInstantiator inst,
0N/A long instIncarnation)
0N/A throws ActivationException, UnknownGroupException
0N/A {
0N/A if (incarnation != instIncarnation) {
0N/A throw new ActivationException("invalid incarnation");
0N/A }
0N/A
0N/A if (group != null) {
0N/A if (group.equals(inst)) {
0N/A return;
0N/A } else {
0N/A throw new ActivationException("group already active");
0N/A }
0N/A }
0N/A
0N/A if (child != null && status != CREATING) {
0N/A throw new ActivationException("group not being created");
0N/A }
0N/A
0N/A group = inst;
0N/A status = NORMAL;
0N/A notifyAll();
0N/A }
0N/A
0N/A private void checkRemoved() throws UnknownGroupException {
0N/A if (removed) {
0N/A throw new UnknownGroupException("group removed");
0N/A }
0N/A }
0N/A
0N/A private ObjectEntry getObjectEntry(ActivationID id)
0N/A throws UnknownObjectException
0N/A {
0N/A if (removed) {
0N/A throw new UnknownObjectException("object's group removed");
0N/A }
0N/A ObjectEntry objEntry = objects.get(id);
0N/A if (objEntry == null) {
0N/A throw new UnknownObjectException("object unknown");
0N/A }
0N/A return objEntry;
0N/A }
0N/A
0N/A synchronized void registerObject(ActivationID id,
0N/A ActivationDesc desc,
0N/A boolean addRecord)
0N/A throws UnknownGroupException, ActivationException
0N/A {
0N/A checkRemoved();
0N/A objects.put(id, new ObjectEntry(desc));
0N/A if (desc.getRestartMode() == true) {
0N/A restartSet.add(id);
0N/A }
0N/A
0N/A // table insertion must take place before log update
4105N/A idTable.put(id, groupID);
0N/A
0N/A if (addRecord) {
0N/A addLogRecord(new LogRegisterObject(id, desc));
0N/A }
0N/A }
0N/A
0N/A synchronized void unregisterObject(ActivationID id, boolean addRecord)
0N/A throws UnknownGroupException, ActivationException
0N/A {
0N/A ObjectEntry objEntry = getObjectEntry(id);
0N/A objEntry.removed = true;
0N/A objects.remove(id);
0N/A if (objEntry.desc.getRestartMode() == true) {
0N/A restartSet.remove(id);
0N/A }
0N/A
4105N/A // table removal must take place before log update
4105N/A idTable.remove(id);
0N/A if (addRecord) {
0N/A addLogRecord(new LogUnregisterObject(id));
0N/A }
0N/A }
0N/A
0N/A synchronized void unregisterGroup(boolean addRecord)
0N/A throws UnknownGroupException, ActivationException
0N/A {
0N/A checkRemoved();
0N/A removed = true;
0N/A for (Map.Entry<ActivationID,ObjectEntry> entry :
0N/A objects.entrySet())
0N/A {
0N/A ActivationID id = entry.getKey();
4105N/A idTable.remove(id);
0N/A ObjectEntry objEntry = entry.getValue();
0N/A objEntry.removed = true;
0N/A }
0N/A objects.clear();
0N/A restartSet.clear();
0N/A reset();
0N/A childGone();
0N/A
0N/A // removal should be recorded before log update
0N/A if (addRecord) {
0N/A addLogRecord(new LogUnregisterGroup(groupID));
0N/A }
0N/A }
0N/A
0N/A synchronized ActivationDesc setActivationDesc(ActivationID id,
0N/A ActivationDesc desc,
0N/A boolean addRecord)
0N/A throws UnknownObjectException, UnknownGroupException,
0N/A ActivationException
0N/A {
0N/A ObjectEntry objEntry = getObjectEntry(id);
0N/A ActivationDesc oldDesc = objEntry.desc;
0N/A objEntry.desc = desc;
0N/A if (desc.getRestartMode() == true) {
0N/A restartSet.add(id);
0N/A } else {
0N/A restartSet.remove(id);
0N/A }
0N/A // restart information should be recorded before log update
0N/A if (addRecord) {
0N/A addLogRecord(new LogUpdateDesc(id, desc));
0N/A }
0N/A
0N/A return oldDesc;
0N/A }
0N/A
0N/A synchronized ActivationDesc getActivationDesc(ActivationID id)
0N/A throws UnknownObjectException, UnknownGroupException
0N/A {
0N/A return getObjectEntry(id).desc;
0N/A }
0N/A
0N/A synchronized ActivationGroupDesc setActivationGroupDesc(
0N/A ActivationGroupID id,
0N/A ActivationGroupDesc desc,
0N/A boolean addRecord)
0N/A throws UnknownGroupException, ActivationException
0N/A {
0N/A checkRemoved();
0N/A ActivationGroupDesc oldDesc = this.desc;
0N/A this.desc = desc;
0N/A // state update should occur before log update
0N/A if (addRecord) {
0N/A addLogRecord(new LogUpdateGroupDesc(id, desc));
0N/A }
0N/A return oldDesc;
0N/A }
0N/A
0N/A synchronized void inactiveGroup(long incarnation, boolean failure)
0N/A throws UnknownGroupException
0N/A {
0N/A checkRemoved();
0N/A if (this.incarnation != incarnation) {
0N/A throw new UnknownGroupException("invalid incarnation");
0N/A }
0N/A
0N/A reset();
0N/A if (failure) {
0N/A terminate();
0N/A } else if (child != null && status == NORMAL) {
0N/A status = TERMINATE;
0N/A watchdog.noRestart();
0N/A }
0N/A }
0N/A
0N/A synchronized void activeObject(ActivationID id,
0N/A MarshalledObject<? extends Remote> mobj)
0N/A throws UnknownObjectException
0N/A {
0N/A getObjectEntry(id).stub = mobj;
0N/A }
0N/A
0N/A synchronized void inactiveObject(ActivationID id)
0N/A throws UnknownObjectException
0N/A {
0N/A getObjectEntry(id).reset();
0N/A }
0N/A
0N/A private synchronized void reset() {
0N/A group = null;
0N/A for (ObjectEntry objectEntry : objects.values()) {
0N/A objectEntry.reset();
0N/A }
0N/A }
0N/A
0N/A private void childGone() {
0N/A if (child != null) {
0N/A child = null;
0N/A watchdog.dispose();
0N/A watchdog = null;
0N/A status = NORMAL;
0N/A notifyAll();
0N/A }
0N/A }
0N/A
0N/A private void terminate() {
0N/A if (child != null && status != TERMINATING) {
0N/A child.destroy();
0N/A status = TERMINATING;
0N/A waitTime = System.currentTimeMillis() + groupTimeout;
0N/A notifyAll();
0N/A }
0N/A }
0N/A
5559N/A /*
5559N/A * Fallthrough from TERMINATE to TERMINATING
5559N/A * is intentional
5559N/A */
5559N/A @SuppressWarnings("fallthrough")
0N/A private void await() {
0N/A while (true) {
0N/A switch (status) {
0N/A case NORMAL:
0N/A return;
0N/A case TERMINATE:
0N/A terminate();
0N/A case TERMINATING:
0N/A try {
0N/A child.exitValue();
0N/A } catch (IllegalThreadStateException e) {
0N/A long now = System.currentTimeMillis();
0N/A if (waitTime > now) {
0N/A try {
0N/A wait(waitTime - now);
0N/A } catch (InterruptedException ee) {
0N/A }
0N/A continue;
0N/A }
0N/A // REMIND: print message that group did not terminate?
0N/A }
0N/A childGone();
0N/A return;
0N/A case CREATING:
0N/A try {
0N/A wait();
0N/A } catch (InterruptedException e) {
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // no synchronization to avoid delay wrt getInstantiator
0N/A void shutdownFast() {
0N/A Process p = child;
0N/A if (p != null) {
0N/A p.destroy();
0N/A }
0N/A }
0N/A
0N/A synchronized void shutdown() {
0N/A reset();
0N/A terminate();
0N/A await();
0N/A }
0N/A
0N/A MarshalledObject<? extends Remote> activate(ActivationID id,
0N/A boolean force)
0N/A throws ActivationException
0N/A {
0N/A Exception detail = null;
0N/A
0N/A /*
0N/A * Attempt to activate object and reattempt (several times)
0N/A * if activation fails due to communication problems.
0N/A */
0N/A for (int tries = MAX_TRIES; tries > 0; tries--) {
0N/A ActivationInstantiator inst;
0N/A long currentIncarnation;
0N/A
0N/A // look up object to activate
0N/A ObjectEntry objEntry;
0N/A synchronized (this) {
0N/A objEntry = getObjectEntry(id);
0N/A // if not forcing activation, return cached stub
0N/A if (!force && objEntry.stub != null) {
0N/A return objEntry.stub;
0N/A }
0N/A inst = getInstantiator(groupID);
0N/A currentIncarnation = incarnation;
0N/A }
0N/A
0N/A boolean groupInactive = false;
0N/A boolean failure = false;
0N/A // activate object
0N/A try {
0N/A return objEntry.activate(id, force, inst);
0N/A } catch (NoSuchObjectException e) {
0N/A groupInactive = true;
0N/A detail = e;
0N/A } catch (ConnectException e) {
0N/A groupInactive = true;
0N/A failure = true;
0N/A detail = e;
0N/A } catch (ConnectIOException e) {
0N/A groupInactive = true;
0N/A failure = true;
0N/A detail = e;
0N/A } catch (InactiveGroupException e) {
0N/A groupInactive = true;
0N/A detail = e;
0N/A } catch (RemoteException e) {
0N/A // REMIND: wait some here before continuing?
0N/A if (detail == null) {
0N/A detail = e;
0N/A }
0N/A }
0N/A
0N/A if (groupInactive) {
0N/A // group has failed or is inactive; mark inactive
0N/A try {
0N/A System.err.println(
0N/A MessageFormat.format(
0N/A getTextResource("rmid.group.inactive"),
0N/A detail.toString()));
0N/A detail.printStackTrace();
0N/A getGroupEntry(groupID).
0N/A inactiveGroup(currentIncarnation, failure);
0N/A } catch (UnknownGroupException e) {
0N/A // not a problem
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * signal that group activation failed, nested exception
0N/A * specifies what exception occurred when the group did not
0N/A * activate
0N/A */
0N/A throw new ActivationException("object activation failed after " +
0N/A MAX_TRIES + " tries", detail);
0N/A }
0N/A
0N/A /**
0N/A * Returns the instantiator for the group specified by id and
0N/A * entry. If the group is currently inactive, exec some
0N/A * bootstrap code to create the group.
0N/A */
0N/A private ActivationInstantiator getInstantiator(ActivationGroupID id)
0N/A throws ActivationException
0N/A {
0N/A assert Thread.holdsLock(this);
0N/A
0N/A await();
0N/A if (group != null) {
0N/A return group;
0N/A }
0N/A checkRemoved();
0N/A boolean acquired = false;
0N/A
0N/A try {
0N/A groupName = Pstartgroup();
0N/A acquired = true;
0N/A String[] argv = activationArgs(desc);
0N/A checkArgs(desc, argv);
0N/A
0N/A if (debugExec) {
0N/A StringBuffer sb = new StringBuffer(argv[0]);
0N/A int j;
0N/A for (j = 1; j < argv.length; j++) {
0N/A sb.append(' ');
0N/A sb.append(argv[j]);
0N/A }
0N/A System.err.println(
0N/A MessageFormat.format(
0N/A getTextResource("rmid.exec.command"),
0N/A sb.toString()));
0N/A }
0N/A
0N/A try {
0N/A child = Runtime.getRuntime().exec(argv);
0N/A status = CREATING;
0N/A ++incarnation;
0N/A watchdog = new Watchdog();
0N/A watchdog.start();
0N/A addLogRecord(new LogGroupIncarnation(id, incarnation));
0N/A
0N/A // handle child I/O streams before writing to child
0N/A PipeWriter.plugTogetherPair
0N/A (child.getInputStream(), System.out,
0N/A child.getErrorStream(), System.err);
5559N/A try (MarshalOutputStream out =
5559N/A new MarshalOutputStream(child.getOutputStream())) {
5559N/A out.writeObject(id);
5559N/A out.writeObject(desc);
5559N/A out.writeLong(incarnation);
5559N/A out.flush();
5559N/A }
0N/A
0N/A
0N/A } catch (IOException e) {
0N/A terminate();
0N/A throw new ActivationException(
0N/A "unable to create activation group", e);
0N/A }
0N/A
0N/A try {
0N/A long now = System.currentTimeMillis();
0N/A long stop = now + execTimeout;
0N/A do {
0N/A wait(stop - now);
0N/A if (group != null) {
0N/A return group;
0N/A }
0N/A now = System.currentTimeMillis();
0N/A } while (status == CREATING && now < stop);
0N/A } catch (InterruptedException e) {
0N/A }
0N/A
0N/A terminate();
0N/A throw new ActivationException(
0N/A (removed ?
0N/A "activation group unregistered" :
0N/A "timeout creating child process"));
0N/A } finally {
0N/A if (acquired) {
0N/A Vstartgroup();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Waits for process termination and then restarts services.
0N/A */
0N/A private class Watchdog extends Thread {
0N/A private final Process groupProcess = child;
0N/A private final long groupIncarnation = incarnation;
0N/A private boolean canInterrupt = true;
0N/A private boolean shouldQuit = false;
0N/A private boolean shouldRestart = true;
0N/A
0N/A Watchdog() {
0N/A super("WatchDog-" + groupName + "-" + incarnation);
0N/A setDaemon(true);
0N/A }
0N/A
0N/A public void run() {
0N/A
0N/A if (shouldQuit) {
0N/A return;
0N/A }
0N/A
0N/A /*
0N/A * Wait for the group to crash or exit.
0N/A */
0N/A try {
0N/A groupProcess.waitFor();
0N/A } catch (InterruptedException exit) {
0N/A return;
0N/A }
0N/A
0N/A boolean restart = false;
0N/A synchronized (GroupEntry.this) {
0N/A if (shouldQuit) {
0N/A return;
0N/A }
0N/A canInterrupt = false;
0N/A interrupted(); // clear interrupt bit
0N/A /*
0N/A * Since the group crashed, we should
0N/A * reset the entry before activating objects
0N/A */
0N/A if (groupIncarnation == incarnation) {
0N/A restart = shouldRestart && !shuttingDown;
0N/A reset();
0N/A childGone();
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Activate those objects that require restarting
0N/A * after a crash.
0N/A */
0N/A if (restart) {
0N/A restartServices();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Marks this thread as one that is no longer needed.
0N/A * If the thread is in a state in which it can be interrupted,
0N/A * then the thread is interrupted.
0N/A */
0N/A void dispose() {
0N/A shouldQuit = true;
0N/A if (canInterrupt) {
0N/A interrupt();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Marks this thread as no longer needing to restart objects.
0N/A */
0N/A void noRestart() {
0N/A shouldRestart = false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A private String[] activationArgs(ActivationGroupDesc desc) {
0N/A ActivationGroupDesc.CommandEnvironment cmdenv;
0N/A cmdenv = desc.getCommandEnvironment();
0N/A
0N/A // argv is the literal command to exec
5559N/A List<String> argv = new ArrayList<>();
0N/A
0N/A // Command name/path
0N/A argv.add((cmdenv != null && cmdenv.getCommandPath() != null)
0N/A ? cmdenv.getCommandPath()
0N/A : command[0]);
0N/A
0N/A // Group-specific command options
0N/A if (cmdenv != null && cmdenv.getCommandOptions() != null) {
0N/A argv.addAll(Arrays.asList(cmdenv.getCommandOptions()));
0N/A }
0N/A
0N/A // Properties become -D parameters
0N/A Properties props = desc.getPropertyOverrides();
0N/A if (props != null) {
0N/A for (Enumeration<?> p = props.propertyNames();
0N/A p.hasMoreElements();)
0N/A {
0N/A String name = (String) p.nextElement();
0N/A /* Note on quoting: it would be wrong
0N/A * here, since argv will be passed to
0N/A * Runtime.exec, which should not parse
0N/A * arguments or split on whitespace.
0N/A */
0N/A argv.add("-D" + name + "=" + props.getProperty(name));
0N/A }
0N/A }
0N/A
0N/A /* Finally, rmid-global command options (e.g. -C options)
0N/A * and the classname
0N/A */
0N/A for (int i = 1; i < command.length; i++) {
0N/A argv.add(command[i]);
0N/A }
0N/A
0N/A String[] realArgv = new String[argv.size()];
0N/A System.arraycopy(argv.toArray(), 0, realArgv, 0, realArgv.length);
0N/A
0N/A return realArgv;
0N/A }
0N/A
0N/A private void checkArgs(ActivationGroupDesc desc, String[] cmd)
0N/A throws SecurityException, ActivationException
0N/A {
0N/A /*
0N/A * Check exec command using execPolicy object
0N/A */
0N/A if (execPolicyMethod != null) {
0N/A if (cmd == null) {
0N/A cmd = activationArgs(desc);
0N/A }
0N/A try {
0N/A execPolicyMethod.invoke(execPolicy, desc, cmd);
0N/A } catch (InvocationTargetException e) {
0N/A Throwable targetException = e.getTargetException();
0N/A if (targetException instanceof SecurityException) {
0N/A throw (SecurityException) targetException;
0N/A } else {
0N/A throw new ActivationException(
0N/A execPolicyMethod.getName() + ": unexpected exception",
0N/A e);
0N/A }
0N/A } catch (Exception e) {
0N/A throw new ActivationException(
0N/A execPolicyMethod.getName() + ": unexpected exception", e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static class ObjectEntry implements Serializable {
0N/A
0N/A private static final long serialVersionUID = -5500114225321357856L;
0N/A
0N/A /** descriptor for object */
0N/A ActivationDesc desc;
0N/A /** the stub (if active) */
0N/A volatile transient MarshalledObject<? extends Remote> stub = null;
0N/A volatile transient boolean removed = false;
0N/A
0N/A ObjectEntry(ActivationDesc desc) {
0N/A this.desc = desc;
0N/A }
0N/A
0N/A synchronized MarshalledObject<? extends Remote>
0N/A activate(ActivationID id,
0N/A boolean force,
0N/A ActivationInstantiator inst)
0N/A throws RemoteException, ActivationException
0N/A {
0N/A MarshalledObject<? extends Remote> nstub = stub;
0N/A if (removed) {
0N/A throw new UnknownObjectException("object removed");
0N/A } else if (!force && nstub != null) {
0N/A return nstub;
0N/A }
0N/A
0N/A nstub = inst.newInstance(id, desc);
0N/A stub = nstub;
0N/A /*
0N/A * stub could be set to null by a group reset, so return
0N/A * the newstub here to prevent returning null.
0N/A */
0N/A return nstub;
0N/A }
0N/A
0N/A void reset() {
0N/A stub = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Add a record to the activation log. If the number of updates
0N/A * passes a predetermined threshold, record a snapshot.
0N/A */
0N/A private void addLogRecord(LogRecord rec) throws ActivationException {
0N/A synchronized (log) {
0N/A checkShutdown();
0N/A try {
0N/A log.update(rec, true);
0N/A } catch (Exception e) {
0N/A numUpdates = snapshotInterval;
0N/A System.err.println(getTextResource("rmid.log.update.warning"));
0N/A e.printStackTrace();
0N/A }
0N/A if (++numUpdates < snapshotInterval) {
0N/A return;
0N/A }
0N/A try {
0N/A log.snapshot(this);
0N/A numUpdates = 0;
0N/A } catch (Exception e) {
0N/A System.err.println(
0N/A getTextResource("rmid.log.snapshot.warning"));
0N/A e.printStackTrace();
0N/A try {
0N/A // shutdown activation system because snapshot failed
0N/A system.shutdown();
0N/A } catch (RemoteException ignore) {
0N/A // can't happen
0N/A }
0N/A // warn the client of the original update problem
0N/A throw new ActivationException("log snapshot failed", e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Handler for the log that knows how to take the initial snapshot
0N/A * and apply an update (a LogRecord) to the current state.
0N/A */
0N/A private static class ActLogHandler extends LogHandler {
0N/A
0N/A ActLogHandler() {
0N/A }
0N/A
0N/A public Object initialSnapshot()
0N/A {
0N/A /**
0N/A * Return an empty Activation object. Log will update
0N/A * this object with recovered state.
0N/A */
0N/A return new Activation();
0N/A }
0N/A
0N/A public Object applyUpdate(Object update, Object state)
0N/A throws Exception
0N/A {
0N/A return ((LogRecord) update).apply(state);
0N/A }
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Abstract class for all log records. The subclass contains
0N/A * specific update information and implements the apply method
0N/A * that applys the update information contained in the record
0N/A * to the current state.
0N/A */
0N/A private static abstract class LogRecord implements Serializable {
0N/A /** indicate compatibility with JDK 1.2 version of class */
0N/A private static final long serialVersionUID = 8395140512322687529L;
0N/A abstract Object apply(Object state) throws Exception;
0N/A }
0N/A
0N/A /**
0N/A * Log record for registering an object.
0N/A */
0N/A private static class LogRegisterObject extends LogRecord {
0N/A /** indicate compatibility with JDK 1.2 version of class */
0N/A private static final long serialVersionUID = -6280336276146085143L;
0N/A private ActivationID id;
0N/A private ActivationDesc desc;
0N/A
0N/A LogRegisterObject(ActivationID id, ActivationDesc desc) {
0N/A this.id = id;
0N/A this.desc = desc;
0N/A }
0N/A
0N/A Object apply(Object state) {
0N/A try {
0N/A ((Activation) state).getGroupEntry(desc.getGroupID()).
0N/A registerObject(id, desc, false);
0N/A } catch (Exception ignore) {
0N/A System.err.println(
0N/A MessageFormat.format(
0N/A getTextResource("rmid.log.recover.warning"),
0N/A "LogRegisterObject"));
0N/A ignore.printStackTrace();
0N/A }
0N/A return state;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Log record for unregistering an object.
0N/A */
0N/A private static class LogUnregisterObject extends LogRecord {
0N/A /** indicate compatibility with JDK 1.2 version of class */
0N/A private static final long serialVersionUID = 6269824097396935501L;
0N/A private ActivationID id;
0N/A
0N/A LogUnregisterObject(ActivationID id) {
0N/A this.id = id;
0N/A }
0N/A
0N/A Object apply(Object state) {
0N/A try {
0N/A ((Activation) state).getGroupEntry(id).
0N/A unregisterObject(id, false);
0N/A } catch (Exception ignore) {
0N/A System.err.println(
0N/A MessageFormat.format(
0N/A getTextResource("rmid.log.recover.warning"),
0N/A "LogUnregisterObject"));
0N/A ignore.printStackTrace();
0N/A }
0N/A return state;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Log record for registering a group.
0N/A */
0N/A private static class LogRegisterGroup extends LogRecord {
0N/A /** indicate compatibility with JDK 1.2 version of class */
0N/A private static final long serialVersionUID = -1966827458515403625L;
0N/A private ActivationGroupID id;
0N/A private ActivationGroupDesc desc;
0N/A
0N/A LogRegisterGroup(ActivationGroupID id, ActivationGroupDesc desc) {
0N/A this.id = id;
0N/A this.desc = desc;
0N/A }
0N/A
0N/A Object apply(Object state) {
0N/A // modify state directly; cant ask a nonexistent GroupEntry
0N/A // to register itself.
0N/A ((Activation) state).groupTable.put(id, ((Activation) state).new
0N/A GroupEntry(id, desc));
0N/A return state;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Log record for udpating an activation desc
0N/A */
0N/A private static class LogUpdateDesc extends LogRecord {
0N/A /** indicate compatibility with JDK 1.2 version of class */
0N/A private static final long serialVersionUID = 545511539051179885L;
0N/A
0N/A private ActivationID id;
0N/A private ActivationDesc desc;
0N/A
0N/A LogUpdateDesc(ActivationID id, ActivationDesc desc) {
0N/A this.id = id;
0N/A this.desc = desc;
0N/A }
0N/A
0N/A Object apply(Object state) {
0N/A try {
0N/A ((Activation) state).getGroupEntry(id).
0N/A setActivationDesc(id, desc, false);
0N/A } catch (Exception ignore) {
0N/A System.err.println(
0N/A MessageFormat.format(
0N/A getTextResource("rmid.log.recover.warning"),
0N/A "LogUpdateDesc"));
0N/A ignore.printStackTrace();
0N/A }
0N/A return state;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Log record for unregistering a group.
0N/A */
0N/A private static class LogUpdateGroupDesc extends LogRecord {
0N/A /** indicate compatibility with JDK 1.2 version of class */
0N/A private static final long serialVersionUID = -1271300989218424337L;
0N/A private ActivationGroupID id;
0N/A private ActivationGroupDesc desc;
0N/A
0N/A LogUpdateGroupDesc(ActivationGroupID id, ActivationGroupDesc desc) {
0N/A this.id = id;
0N/A this.desc = desc;
0N/A }
0N/A
0N/A Object apply(Object state) {
0N/A try {
0N/A ((Activation) state).getGroupEntry(id).
0N/A setActivationGroupDesc(id, desc, false);
0N/A } catch (Exception ignore) {
0N/A System.err.println(
0N/A MessageFormat.format(
0N/A getTextResource("rmid.log.recover.warning"),
0N/A "LogUpdateGroupDesc"));
0N/A ignore.printStackTrace();
0N/A }
0N/A return state;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Log record for unregistering a group.
0N/A */
0N/A private static class LogUnregisterGroup extends LogRecord {
0N/A /** indicate compatibility with JDK 1.2 version of class */
0N/A private static final long serialVersionUID = -3356306586522147344L;
0N/A private ActivationGroupID id;
0N/A
0N/A LogUnregisterGroup(ActivationGroupID id) {
0N/A this.id = id;
0N/A }
0N/A
0N/A Object apply(Object state) {
0N/A GroupEntry entry = ((Activation) state).groupTable.remove(id);
0N/A try {
0N/A entry.unregisterGroup(false);
0N/A } catch (Exception ignore) {
0N/A System.err.println(
0N/A MessageFormat.format(
0N/A getTextResource("rmid.log.recover.warning"),
0N/A "LogUnregisterGroup"));
0N/A ignore.printStackTrace();
0N/A }
0N/A return state;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Log record for an active group incarnation
0N/A */
0N/A private static class LogGroupIncarnation extends LogRecord {
0N/A /** indicate compatibility with JDK 1.2 version of class */
0N/A private static final long serialVersionUID = 4146872747377631897L;
0N/A private ActivationGroupID id;
0N/A private long inc;
0N/A
0N/A LogGroupIncarnation(ActivationGroupID id, long inc) {
0N/A this.id = id;
0N/A this.inc = inc;
0N/A }
0N/A
0N/A Object apply(Object state) {
0N/A try {
0N/A GroupEntry entry = ((Activation) state).getGroupEntry(id);
0N/A entry.incarnation = inc;
0N/A } catch (Exception ignore) {
0N/A System.err.println(
0N/A MessageFormat.format(
0N/A getTextResource("rmid.log.recover.warning"),
0N/A "LogGroupIncarnation"));
0N/A ignore.printStackTrace();
0N/A }
0N/A return state;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Initialize command to exec a default group.
0N/A */
0N/A private void initCommand(String[] childArgs) {
0N/A command = new String[childArgs.length + 2];
0N/A AccessController.doPrivileged(new PrivilegedAction<Void>() {
0N/A public Void run() {
0N/A try {
0N/A command[0] = System.getProperty("java.home") +
0N/A File.separator + "bin" + File.separator + "java";
0N/A } catch (Exception e) {
0N/A System.err.println(
0N/A getTextResource("rmid.unfound.java.home.property"));
0N/A command[0] = "java";
0N/A }
0N/A return null;
0N/A }
0N/A });
0N/A System.arraycopy(childArgs, 0, command, 1, childArgs.length);
0N/A command[command.length-1] = "sun.rmi.server.ActivationGroupInit";
0N/A }
0N/A
0N/A private static void bomb(String error) {
0N/A System.err.println("rmid: " + error); // $NON-NLS$
0N/A System.err.println(MessageFormat.format(getTextResource("rmid.usage"),
0N/A "rmid"));
0N/A System.exit(1);
0N/A }
0N/A
0N/A /**
0N/A * The default policy for checking a command before it is executed
0N/A * makes sure the appropriate com.sun.rmi.rmid.ExecPermission and
0N/A * set of com.sun.rmi.rmid.ExecOptionPermissions have been granted.
0N/A */
0N/A public static class DefaultExecPolicy {
0N/A
0N/A public void checkExecCommand(ActivationGroupDesc desc, String[] cmd)
0N/A throws SecurityException
0N/A {
0N/A PermissionCollection perms = getExecPermissions();
0N/A
0N/A /*
0N/A * Check properties overrides.
0N/A */
0N/A Properties props = desc.getPropertyOverrides();
0N/A if (props != null) {
0N/A Enumeration<?> p = props.propertyNames();
0N/A while (p.hasMoreElements()) {
0N/A String name = (String) p.nextElement();
0N/A String value = props.getProperty(name);
0N/A String option = "-D" + name + "=" + value;
0N/A try {
0N/A checkPermission(perms,
0N/A new ExecOptionPermission(option));
0N/A } catch (AccessControlException e) {
0N/A if (value.equals("")) {
0N/A checkPermission(perms,
0N/A new ExecOptionPermission("-D" + name));
0N/A } else {
0N/A throw e;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Check group class name (allow nothing but the default),
0N/A * code location (must be null), and data (must be null).
0N/A */
0N/A String groupClassName = desc.getClassName();
0N/A if ((groupClassName != null &&
0N/A !groupClassName.equals(
0N/A ActivationGroupImpl.class.getName())) ||
0N/A (desc.getLocation() != null) ||
0N/A (desc.getData() != null))
0N/A {
0N/A throw new AccessControlException(
0N/A "access denied (custom group implementation not allowed)");
0N/A }
0N/A
0N/A /*
0N/A * If group descriptor has a command environment, check
0N/A * command and options.
0N/A */
0N/A ActivationGroupDesc.CommandEnvironment cmdenv;
0N/A cmdenv = desc.getCommandEnvironment();
0N/A if (cmdenv != null) {
0N/A String path = cmdenv.getCommandPath();
0N/A if (path != null) {
0N/A checkPermission(perms, new ExecPermission(path));
0N/A }
0N/A
0N/A String[] options = cmdenv.getCommandOptions();
0N/A if (options != null) {
0N/A for (String option : options) {
0N/A checkPermission(perms,
0N/A new ExecOptionPermission(option));
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Prints warning message if installed Policy is the default Policy
0N/A * implementation and globally granted permissions do not include
0N/A * AllPermission or any ExecPermissions/ExecOptionPermissions.
0N/A */
0N/A static void checkConfiguration() {
0N/A Policy policy =
0N/A AccessController.doPrivileged(new PrivilegedAction<Policy>() {
0N/A public Policy run() {
0N/A return Policy.getPolicy();
0N/A }
0N/A });
0N/A if (!(policy instanceof PolicyFile)) {
0N/A return;
0N/A }
0N/A PermissionCollection perms = getExecPermissions();
0N/A for (Enumeration<Permission> e = perms.elements();
0N/A e.hasMoreElements();)
0N/A {
0N/A Permission p = e.nextElement();
0N/A if (p instanceof AllPermission ||
0N/A p instanceof ExecPermission ||
0N/A p instanceof ExecOptionPermission)
0N/A {
0N/A return;
0N/A }
0N/A }
0N/A System.err.println(getTextResource("rmid.exec.perms.inadequate"));
0N/A }
0N/A
0N/A private static PermissionCollection getExecPermissions() {
0N/A /*
0N/A * The approach used here is taken from the similar method
0N/A * getLoaderAccessControlContext() in the class
0N/A * sun.rmi.server.LoaderHandler.
0N/A */
0N/A
0N/A // obtain permissions granted to all code in current policy
0N/A PermissionCollection perms = AccessController.doPrivileged(
0N/A new PrivilegedAction<PermissionCollection>() {
0N/A public PermissionCollection run() {
0N/A CodeSource codesource =
0N/A new CodeSource(null, (Certificate[]) null);
0N/A Policy p = Policy.getPolicy();
0N/A if (p != null) {
0N/A return p.getPermissions(codesource);
0N/A } else {
0N/A return new Permissions();
0N/A }
0N/A }
0N/A });
0N/A
0N/A return perms;
0N/A }
0N/A
0N/A private static void checkPermission(PermissionCollection perms,
0N/A Permission p)
0N/A throws AccessControlException
0N/A {
0N/A if (!perms.implies(p)) {
0N/A throw new AccessControlException(
0N/A "access denied " + p.toString());
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Main program to start the activation system. <br>
0N/A * The usage is as follows: rmid [-port num] [-log dir].
0N/A */
0N/A public static void main(String[] args) {
0N/A boolean stop = false;
0N/A
0N/A // Create and install the security manager if one is not installed
0N/A // already.
0N/A if (System.getSecurityManager() == null) {
0N/A System.setSecurityManager(new SecurityManager());
0N/A }
0N/A
0N/A try {
0N/A int port = ActivationSystem.SYSTEM_PORT;
0N/A RMIServerSocketFactory ssf = null;
0N/A
0N/A /*
0N/A * If rmid has an inherited channel (meaning that it was
0N/A * launched from inetd), set the server socket factory to
0N/A * return the inherited server socket.
0N/A **/
0N/A Channel inheritedChannel = AccessController.doPrivileged(
0N/A new PrivilegedExceptionAction<Channel>() {
0N/A public Channel run() throws IOException {
0N/A return System.inheritedChannel();
0N/A }
0N/A });
0N/A
0N/A if (inheritedChannel != null &&
0N/A inheritedChannel instanceof ServerSocketChannel)
0N/A {
0N/A /*
0N/A * Redirect System.err output to a file.
0N/A */
0N/A AccessController.doPrivileged(
0N/A new PrivilegedExceptionAction<Void>() {
0N/A public Void run() throws IOException {
0N/A File file =
5089N/A Files.createTempFile("rmid-err", null).toFile();
0N/A PrintStream errStream =
0N/A new PrintStream(new FileOutputStream(file));
0N/A System.setErr(errStream);
0N/A return null;
0N/A }
0N/A });
0N/A
0N/A ServerSocket serverSocket =
0N/A ((ServerSocketChannel) inheritedChannel).socket();
0N/A port = serverSocket.getLocalPort();
0N/A ssf = new ActivationServerSocketFactory(serverSocket);
0N/A
0N/A System.err.println(new Date());
0N/A System.err.println(getTextResource(
0N/A "rmid.inherited.channel.info") +
0N/A ": " + inheritedChannel);
0N/A }
0N/A
0N/A String log = null;
5559N/A List<String> childArgs = new ArrayList<>();
0N/A
0N/A /*
0N/A * Parse arguments
0N/A */
0N/A for (int i = 0; i < args.length; i++) {
0N/A if (args[i].equals("-port")) {
0N/A if (ssf != null) {
0N/A bomb(getTextResource("rmid.syntax.port.badarg"));
0N/A }
0N/A if ((i + 1) < args.length) {
0N/A try {
0N/A port = Integer.parseInt(args[++i]);
0N/A } catch (NumberFormatException nfe) {
0N/A bomb(getTextResource("rmid.syntax.port.badnumber"));
0N/A }
0N/A } else {
0N/A bomb(getTextResource("rmid.syntax.port.missing"));
0N/A }
0N/A
0N/A } else if (args[i].equals("-log")) {
0N/A if ((i + 1) < args.length) {
0N/A log = args[++i];
0N/A } else {
0N/A bomb(getTextResource("rmid.syntax.log.missing"));
0N/A }
0N/A
0N/A } else if (args[i].equals("-stop")) {
0N/A stop = true;
0N/A
0N/A } else if (args[i].startsWith("-C")) {
0N/A childArgs.add(args[i].substring(2));
0N/A
0N/A } else {
0N/A bomb(MessageFormat.format(
0N/A getTextResource("rmid.syntax.illegal.option"),
0N/A args[i]));
0N/A }
0N/A }
0N/A
0N/A if (log == null) {
0N/A if (ssf != null) {
0N/A bomb(getTextResource("rmid.syntax.log.required"));
0N/A } else {
0N/A log = "log";
0N/A }
0N/A }
0N/A
0N/A debugExec = AccessController.doPrivileged(
0N/A new GetBooleanAction("sun.rmi.server.activation.debugExec"));
0N/A
0N/A /**
0N/A * Determine class name for activation exec policy (if any).
0N/A */
0N/A String execPolicyClassName = AccessController.doPrivileged(
0N/A new GetPropertyAction("sun.rmi.activation.execPolicy", null));
0N/A if (execPolicyClassName == null) {
0N/A if (!stop) {
0N/A DefaultExecPolicy.checkConfiguration();
0N/A }
0N/A execPolicyClassName = "default";
0N/A }
0N/A
0N/A /**
0N/A * Initialize method for activation exec policy.
0N/A */
0N/A if (!execPolicyClassName.equals("none")) {
0N/A if (execPolicyClassName.equals("") ||
0N/A execPolicyClassName.equals("default"))
0N/A {
0N/A execPolicyClassName = DefaultExecPolicy.class.getName();
0N/A }
0N/A
0N/A try {
5559N/A Class<?> execPolicyClass = getRMIClass(execPolicyClassName);
0N/A execPolicy = execPolicyClass.newInstance();
0N/A execPolicyMethod =
0N/A execPolicyClass.getMethod("checkExecCommand",
0N/A ActivationGroupDesc.class,
0N/A String[].class);
0N/A } catch (Exception e) {
0N/A if (debugExec) {
0N/A System.err.println(
0N/A getTextResource("rmid.exec.policy.exception"));
0N/A e.printStackTrace();
0N/A }
0N/A bomb(getTextResource("rmid.exec.policy.invalid"));
0N/A }
0N/A }
0N/A
0N/A if (stop == true) {
0N/A final int finalPort = port;
0N/A AccessController.doPrivileged(new PrivilegedAction<Void>() {
0N/A public Void run() {
0N/A System.setProperty("java.rmi.activation.port",
0N/A Integer.toString(finalPort));
0N/A return null;
0N/A }
0N/A });
0N/A ActivationSystem system = ActivationGroup.getSystem();
0N/A system.shutdown();
0N/A System.exit(0);
0N/A }
0N/A
0N/A /*
0N/A * Fix for 4173960: Create and initialize activation using
0N/A * a static method, startActivation, which will build the
0N/A * Activation state in two ways: if when rmid is run, no
0N/A * log file is found, the ActLogHandler.recover(...)
0N/A * method will create a new Activation instance.
0N/A * Alternatively, if a logfile is available, a serialized
0N/A * instance of activation will be read from the log's
0N/A * snapshot file. Log updates will be applied to this
0N/A * Activation object until rmid's state has been fully
0N/A * recovered. In either case, only one instance of
0N/A * Activation is created.
0N/A */
0N/A startActivation(port, ssf, log,
0N/A childArgs.toArray(new String[childArgs.size()]));
0N/A
0N/A // prevent activator from exiting
0N/A while (true) {
0N/A try {
0N/A Thread.sleep(Long.MAX_VALUE);
0N/A } catch (InterruptedException e) {
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A System.err.println(
0N/A MessageFormat.format(
0N/A getTextResource("rmid.unexpected.exception"), e));
0N/A e.printStackTrace();
0N/A }
0N/A System.exit(1);
0N/A }
0N/A
0N/A /**
0N/A * Retrieves text resources from the locale-specific properties file.
0N/A */
0N/A private static String getTextResource(String key) {
0N/A if (Activation.resources == null) {
0N/A try {
0N/A Activation.resources = ResourceBundle.getBundle(
0N/A "sun.rmi.server.resources.rmid");
0N/A } catch (MissingResourceException mre) {
0N/A }
0N/A if (Activation.resources == null) {
0N/A // throwing an Error is a bit extreme, methinks
0N/A return ("[missing resource file: " + key + "]");
0N/A }
0N/A }
0N/A
0N/A String val = null;
0N/A try {
0N/A val = Activation.resources.getString (key);
0N/A } catch (MissingResourceException mre) {
0N/A }
0N/A
0N/A if (val == null) {
0N/A return ("[missing resource: " + key + "]");
0N/A } else {
0N/A return val;
0N/A }
0N/A }
0N/A
5559N/A @SuppressWarnings("deprecation")
5559N/A private static Class<?> getRMIClass(String execPolicyClassName) throws Exception {
5559N/A return RMIClassLoader.loadClass(execPolicyClassName);
5559N/A }
0N/A /*
0N/A * Dijkstra semaphore operations to limit the number of subprocesses
0N/A * rmid attempts to make at once.
0N/A */
0N/A /**
0N/A * Acquire the group semaphore and return a group name. Each
0N/A * Pstartgroup must be followed by a Vstartgroup. The calling thread
0N/A * will wait until there are fewer than <code>N</code> other threads
0N/A * holding the group semaphore. The calling thread will then acquire
0N/A * the semaphore and return.
0N/A */
0N/A private synchronized String Pstartgroup() throws ActivationException {
0N/A while (true) {
0N/A checkShutdown();
0N/A // Wait until positive, then decrement.
0N/A if (groupSemaphore > 0) {
0N/A groupSemaphore--;
0N/A return "Group-" + groupCounter++;
0N/A }
0N/A
0N/A try {
0N/A wait();
0N/A } catch (InterruptedException e) {
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Release the group semaphore. Every P operation must be
0N/A * followed by a V operation. This may cause another thread to
0N/A * wake up and return from its P operation.
0N/A */
0N/A private synchronized void Vstartgroup() {
0N/A // Increment and notify a waiter (not necessarily FIFO).
0N/A groupSemaphore++;
0N/A notifyAll();
0N/A }
0N/A
0N/A /**
0N/A * A server socket factory to use when rmid is launched via 'inetd'
0N/A * with 'wait' status. This socket factory's 'createServerSocket'
0N/A * method returns the server socket specified during construction that
0N/A * is specialized to delay accepting requests until the
0N/A * 'initDone' flag is 'true'. The server socket supplied to
0N/A * the constructor should be the server socket obtained from the
0N/A * ServerSocketChannel returned from the 'System.inheritedChannel'
0N/A * method.
0N/A **/
0N/A private static class ActivationServerSocketFactory
0N/A implements RMIServerSocketFactory
0N/A {
0N/A private final ServerSocket serverSocket;
0N/A
0N/A /**
0N/A * Constructs an 'ActivationServerSocketFactory' with the specified
0N/A * 'serverSocket'.
0N/A **/
0N/A ActivationServerSocketFactory(ServerSocket serverSocket) {
0N/A this.serverSocket = serverSocket;
0N/A }
0N/A
0N/A /**
0N/A * Returns the server socket specified during construction wrapped
0N/A * in a 'DelayedAcceptServerSocket'.
0N/A **/
0N/A public ServerSocket createServerSocket(int port)
0N/A throws IOException
0N/A {
0N/A return new DelayedAcceptServerSocket(serverSocket);
0N/A }
0N/A
0N/A }
0N/A
0N/A /**
0N/A * A server socket that delegates all public methods to the underlying
0N/A * server socket specified at construction. The accept method is
0N/A * overridden to delay calling accept on the underlying server socket
0N/A * until the 'initDone' flag is 'true'.
0N/A **/
0N/A private static class DelayedAcceptServerSocket extends ServerSocket {
0N/A
0N/A private final ServerSocket serverSocket;
0N/A
0N/A DelayedAcceptServerSocket(ServerSocket serverSocket)
0N/A throws IOException
0N/A {
0N/A this.serverSocket = serverSocket;
0N/A }
0N/A
0N/A public void bind(SocketAddress endpoint) throws IOException {
0N/A serverSocket.bind(endpoint);
0N/A }
0N/A
0N/A public void bind(SocketAddress endpoint, int backlog)
0N/A throws IOException
0N/A {
0N/A serverSocket.bind(endpoint, backlog);
0N/A }
0N/A
0N/A public InetAddress getInetAddress() {
6319N/A return AccessController.doPrivileged(
6319N/A new PrivilegedAction<InetAddress>() {
6319N/A @Override
6319N/A public InetAddress run() {
6319N/A return serverSocket.getInetAddress();
6319N/A }
6319N/A });
0N/A }
0N/A
0N/A public int getLocalPort() {
0N/A return serverSocket.getLocalPort();
0N/A }
0N/A
0N/A public SocketAddress getLocalSocketAddress() {
6319N/A return AccessController.doPrivileged(
6319N/A new PrivilegedAction<SocketAddress>() {
6319N/A @Override
6319N/A public SocketAddress run() {
6319N/A return serverSocket.getLocalSocketAddress();
6319N/A }
6319N/A });
0N/A }
0N/A
0N/A /**
0N/A * Delays calling accept on the underlying server socket until the
0N/A * remote service is bound in the registry.
0N/A **/
0N/A public Socket accept() throws IOException {
0N/A synchronized (initLock) {
0N/A try {
0N/A while (!initDone) {
0N/A initLock.wait();
0N/A }
0N/A } catch (InterruptedException ignore) {
0N/A throw new AssertionError(ignore);
0N/A }
0N/A }
0N/A return serverSocket.accept();
0N/A }
0N/A
0N/A public void close() throws IOException {
0N/A serverSocket.close();
0N/A }
0N/A
0N/A public ServerSocketChannel getChannel() {
0N/A return serverSocket.getChannel();
0N/A }
0N/A
0N/A public boolean isBound() {
0N/A return serverSocket.isBound();
0N/A }
0N/A
0N/A public boolean isClosed() {
0N/A return serverSocket.isClosed();
0N/A }
0N/A
0N/A public void setSoTimeout(int timeout)
0N/A throws SocketException
0N/A {
0N/A serverSocket.setSoTimeout(timeout);
0N/A }
0N/A
0N/A public int getSoTimeout() throws IOException {
0N/A return serverSocket.getSoTimeout();
0N/A }
0N/A
0N/A public void setReuseAddress(boolean on) throws SocketException {
0N/A serverSocket.setReuseAddress(on);
0N/A }
0N/A
0N/A public boolean getReuseAddress() throws SocketException {
0N/A return serverSocket.getReuseAddress();
0N/A }
0N/A
0N/A public String toString() {
0N/A return serverSocket.toString();
0N/A }
0N/A
0N/A public void setReceiveBufferSize(int size)
0N/A throws SocketException
0N/A {
0N/A serverSocket.setReceiveBufferSize(size);
0N/A }
0N/A
0N/A public int getReceiveBufferSize()
0N/A throws SocketException
0N/A {
0N/A return serverSocket.getReceiveBufferSize();
0N/A }
0N/A }
0N/A}
0N/A
0N/A/**
0N/A * PipeWriter plugs together two pairs of input and output streams by
0N/A * providing readers for input streams and writing through to
0N/A * appropriate output streams. Both output streams are annotated on a
0N/A * per-line basis.
0N/A *
0N/A * @author Laird Dornin, much code borrowed from Peter Jones, Ken
0N/A * Arnold and Ann Wollrath.
0N/A */
0N/Aclass PipeWriter implements Runnable {
0N/A
0N/A /** stream used for buffering lines */
0N/A private ByteArrayOutputStream bufOut;
0N/A
0N/A /** count since last separator */
0N/A private int cLast;
0N/A
0N/A /** current chunk of input being compared to lineSeparator.*/
0N/A private byte[] currSep;
0N/A
0N/A private PrintWriter out;
0N/A private InputStream in;
0N/A
0N/A private String pipeString;
0N/A private String execString;
0N/A
0N/A private static String lineSeparator;
0N/A private static int lineSeparatorLength;
0N/A
0N/A private static int numExecs = 0;
0N/A
0N/A static {
0N/A lineSeparator = AccessController.doPrivileged(
0N/A new GetPropertyAction("line.separator"));
0N/A lineSeparatorLength = lineSeparator.length();
0N/A }
0N/A
0N/A /**
0N/A * Create a new PipeWriter object. All methods of PipeWriter,
0N/A * except plugTogetherPair, are only accesible to PipeWriter
0N/A * itself. Synchronization is unnecessary on functions that will
0N/A * only be used internally in PipeWriter.
0N/A *
0N/A * @param in input stream from which pipe input flows
0N/A * @param out output stream to which log messages will be sent
0N/A * @param dest String which tags output stream as 'out' or 'err'
0N/A * @param nExecs number of execed processes, Activation groups.
0N/A */
0N/A private PipeWriter
0N/A (InputStream in, OutputStream out, String tag, int nExecs) {
0N/A
0N/A this.in = in;
0N/A this.out = new PrintWriter(out);
0N/A
0N/A bufOut = new ByteArrayOutputStream();
0N/A currSep = new byte[lineSeparatorLength];
0N/A
0N/A /* set unique pipe/pair annotations */
0N/A execString = ":ExecGroup-" +
0N/A Integer.toString(nExecs) + ':' + tag + ':';
0N/A }
0N/A
0N/A /**
0N/A * Create a thread to listen and read from input stream, in. buffer
0N/A * the data that is read until a marker which equals lineSeparator
0N/A * is read. Once such a string has been discovered; write out an
0N/A * annotation string followed by the buffered data and a line
0N/A * separator.
0N/A */
0N/A public void run() {
0N/A byte[] buf = new byte[256];
0N/A int count;
0N/A
0N/A try {
0N/A /* read bytes till there are no more. */
0N/A while ((count = in.read(buf)) != -1) {
0N/A write(buf, 0, count);
0N/A }
0N/A
0N/A /* flush internal buffer... may not have ended on a line
0N/A * separator, we also need a last annotation if
0N/A * something was left.
0N/A */
0N/A String lastInBuffer = bufOut.toString();
0N/A bufOut.reset();
0N/A if (lastInBuffer.length() > 0) {
0N/A out.println (createAnnotation() + lastInBuffer);
0N/A out.flush(); // add a line separator
0N/A // to make output nicer
0N/A }
0N/A
0N/A } catch (IOException e) {
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Write a subarray of bytes. Pass each through write byte method.
0N/A */
0N/A private void write(byte b[], int off, int len) throws IOException {
0N/A
0N/A if (len < 0) {
0N/A throw new ArrayIndexOutOfBoundsException(len);
0N/A }
0N/A for (int i = 0; i < len; ++ i) {
0N/A write(b[off + i]);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Write a byte of data to the stream. If we have not matched a
0N/A * line separator string, then the byte is appended to the internal
0N/A * buffer. If we have matched a line separator, then the currently
0N/A * buffered line is sent to the output writer with a prepended
0N/A * annotation string.
0N/A */
0N/A private void write(byte b) throws IOException {
0N/A int i = 0;
0N/A
0N/A /* shift current to the left */
0N/A for (i = 1 ; i < (currSep.length); i ++) {
0N/A currSep[i-1] = currSep[i];
0N/A }
0N/A currSep[i-1] = b;
0N/A bufOut.write(b);
0N/A
0N/A /* enough characters for a separator? */
0N/A if ( (cLast >= (lineSeparatorLength - 1)) &&
0N/A (lineSeparator.equals(new String(currSep))) ) {
0N/A
0N/A cLast = 0;
0N/A
0N/A /* write prefix through to underlying byte stream */
0N/A out.print(createAnnotation() + bufOut.toString());
0N/A out.flush();
0N/A bufOut.reset();
0N/A
0N/A if (out.checkError()) {
0N/A throw new IOException
0N/A ("PipeWriter: IO Exception when"+
0N/A " writing to output stream.");
0N/A }
0N/A
0N/A } else {
0N/A cLast++;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Create an annotation string to be printed out after
0N/A * a new line and end of stream.
0N/A */
0N/A private String createAnnotation() {
0N/A
0N/A /* construct prefix for log messages:
0N/A * date/time stamp...
0N/A */
0N/A return ((new Date()).toString() +
0N/A /* ... print pair # ... */
0N/A (execString));
0N/A }
0N/A
0N/A /**
0N/A * Allow plugging together two pipes at a time, to associate
0N/A * output from an execed process. This is the only publicly
0N/A * accessible method of this object; this helps ensure that
0N/A * synchronization will not be an issue in the annotation
0N/A * process.
0N/A *
0N/A * @param in input stream from which pipe input comes
0N/A * @param out output stream to which log messages will be sent
0N/A * @param in1 input stream from which pipe input comes
0N/A * @param out1 output stream to which log messages will be sent
0N/A */
0N/A static void plugTogetherPair(InputStream in,
0N/A OutputStream out,
0N/A InputStream in1,
0N/A OutputStream out1) {
0N/A Thread inThread = null;
0N/A Thread outThread = null;
0N/A
0N/A int nExecs = getNumExec();
0N/A
0N/A /* start RMI threads to read output from child process */
0N/A inThread = AccessController.doPrivileged(
0N/A new NewThreadAction(new PipeWriter(in, out, "out", nExecs),
0N/A "out", true));
0N/A outThread = AccessController.doPrivileged(
0N/A new NewThreadAction(new PipeWriter(in1, out1, "err", nExecs),
0N/A "err", true));
0N/A inThread.start();
0N/A outThread.start();
0N/A }
0N/A
0N/A private static synchronized int getNumExec() {
0N/A return numExecs++;
0N/A }
0N/A}