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