jdk/test/java/rmi/testlibrary/ActivationLibrary.java
author mfang
Wed, 17 Aug 2011 14:18:26 -0700
changeset 10294 8fcdae2a7ec7
parent 5506 202f599c92aa
child 12692 72f0847dd477
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.rmi.Naming;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.rmi.NoSuchObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.rmi.NotBoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.rmi.activation.Activatable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.rmi.activation.ActivationID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.rmi.activation.ActivationSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.rmi.registry.LocateRegistry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Class of test utility/library methods related to Activatable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class ActivationLibrary {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /** time safeDestroy should wait before failing on shutdown rmid */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final int SAFE_WAIT_TIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        int slopFactor = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            slopFactor = Integer.valueOf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                TestLibrary.getExtraProperty("jcov.sleep.multiplier","1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        } catch (NumberFormatException ignore) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        SAFE_WAIT_TIME = 60000 * slopFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final String SYSTEM_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ActivationSystem.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static void mesg(Object mesg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.err.println("ACTIVATION_LIBRARY: " + mesg.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Deactivate an activated Activatable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static void deactivate(Remote remote,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                  ActivationID id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        for (int i = 0; i < 5; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                if (Activatable.inactive(id) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    mesg("inactive successful");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    Thread.sleep(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    // forcibly unexport the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    Activatable.unexportObject(remote, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                } catch (NoSuchObjectException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        mesg("unable to inactivate after several attempts");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        mesg("unexporting object forcibly instead");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Activatable.unexportObject(remote, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } catch (NoSuchObjectException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Simple method call to see if rmid is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * This method intentionally avoids performing a lookup on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * activation system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static boolean rmidRunning(int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int allowedNotReady = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int connectionRefusedExceptions = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (int i = 0; i < 15 ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                Thread.sleep(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                LocateRegistry.getRegistry(port).lookup(SYSTEM_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } catch (java.rmi.ConnectException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                // ignore connect exceptions until we decide rmid is not up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if ((connectionRefusedExceptions ++) >= allowedNotReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } catch (NotBoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                // print out other types of exceptions as an FYI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                // test should not fail as rmid is likely to be in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                // undetermined state at this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                mesg("caught an exception trying to" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                     " start rmid, last exception was: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                     e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Check to see if an arry of Strings contains a given string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        containsString(String[] strings, String contained)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (strings == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (contained == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        for (int i = 0 ; i < strings.length ; i ++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if ((strings[i] != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                (strings[i].indexOf(contained) >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /** cleanup after rmid */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static void rmidCleanup(RMID rmid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        rmidCleanup(rmid, TestLibrary.RMID_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public static void rmidCleanup(RMID rmid, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (rmid != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (!ActivationLibrary.safeDestroy(rmid, port, SAFE_WAIT_TIME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                TestLibrary.bomb("rmid not destroyed in: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                 SAFE_WAIT_TIME +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                 " milliseconds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        RMID.removeLog();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Invoke shutdown on rmid in a way that will not cause a test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * to hang.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return whether or not shutdown completed succesfully in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *         timeAllowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private static boolean safeDestroy(RMID rmid, int port, long timeAllowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        DestroyThread destroyThread = new DestroyThread(rmid, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        destroyThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            destroyThread.join(timeAllowed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return destroyThread.shutdownSucceeded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Thread class to handle the destruction of rmid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private static class DestroyThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        private final RMID rmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        private final int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        private boolean succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        DestroyThread(RMID rmid, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            this.rmid = rmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            this.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (ActivationLibrary.rmidRunning(port)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                rmid.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    // flag that the test was able to shutdown rmid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                mesg("finished destroying rmid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                mesg("tried to shutdown when rmid was not running");
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
        public synchronized boolean shutdownSucceeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return succeeded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}