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