src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/RMIHelper.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 35217 hotspot/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/RMIHelper.java@ce4b5303a813
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import java.rmi.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import java.rmi.registry.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
import sun.jvm.hotspot.debugger.DebuggerException;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
public class RMIHelper {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
    private static final boolean startRegistry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
    private static int port;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    private static String serverNamePrefix;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    static {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
        String tmp = System.getProperty("sun.jvm.hotspot.rmi.startRegistry");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
        if (tmp != null && tmp.equals("false")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
            startRegistry = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
            // by default, we attempt to start rmiregistry
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
            startRegistry = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
        port = Registry.REGISTRY_PORT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
        tmp = System.getProperty("sun.jvm.hotspot.rmi.port");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
        if (tmp != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
            try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
                port = Integer.parseInt(tmp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
            } catch (NumberFormatException nfe) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
                System.err.println("invalid port supplied, assuming default");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
        tmp = System.getProperty("sun.jvm.hotspot.rmi.serverNamePrefix");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
        serverNamePrefix = (tmp != null)? serverNamePrefix : "SARemoteDebugger";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    public static void rebind(String uniqueID, Remote object) throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
        String name = getName(uniqueID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
            Naming.rebind(name, object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
        } catch (RemoteException re) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
            if (startRegistry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
                // may be the user didn't start rmiregistry, try to start it
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
                try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
                    LocateRegistry.createRegistry(port);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
                    Naming.rebind(name, object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
                } catch (Exception exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
                    throw new DebuggerException(exp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
                throw new DebuggerException(re);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
        } catch (Exception exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
            throw new DebuggerException(exp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    public static void unbind(String uniqueID) throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
        String name = getName(uniqueID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
            Naming.unbind(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
        } catch (Exception exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
            throw new DebuggerException(exp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    public static Remote lookup(String debugServerID) throws DebuggerException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
        // debugServerID follows the pattern [unique_id@]host[:port]
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
        // we have to transform this as //host[:port]/<serverNamePrefix>['_'<unique_id>]
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
        int index = debugServerID.indexOf('@');
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
        StringBuffer nameBuf = new StringBuffer("//");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
        String uniqueID = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
        if (index != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
            nameBuf.append(debugServerID.substring(index + 1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
            uniqueID = debugServerID.substring(0, index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
            nameBuf.append(debugServerID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
        nameBuf.append('/');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
        nameBuf.append(serverNamePrefix);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
        if (uniqueID != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
            nameBuf.append('_');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
            nameBuf.append(uniqueID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
            return Naming.lookup(nameBuf.toString());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
        } catch (Exception exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
            throw new DebuggerException(exp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    private static String getName(String uniqueID) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
        String name = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
        if (uniqueID != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
           name = serverNamePrefix + "_" + uniqueID;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
           name = serverNamePrefix;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
        if (port != Registry.REGISTRY_PORT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
           name = "//localhost:" + port + "/" + name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
        return name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
}