Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/RMIHelper.java
41159 views
/*1* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot;2526import java.io.*;27import java.net.*;28import java.rmi.*;29import java.rmi.registry.*;30import java.util.regex.*;31import sun.jvm.hotspot.debugger.DebuggerException;3233public class RMIHelper {34private static final boolean startRegistry;35private static final Pattern CONNECT_PATTERN = Pattern.compile("^((?<serverid>.+?)@)?(?<host>.+?)(/(?<servername>.+))?$");36private static final String DEFAULT_RMI_OBJECT_NAME = "SARemoteDebugger";37private static int port;3839static {40String tmp = System.getProperty("sun.jvm.hotspot.rmi.startRegistry");41if (tmp != null && tmp.equals("false")) {42startRegistry = false;43} else {44// by default, we attempt to start rmiregistry45startRegistry = true;46}4748port = Registry.REGISTRY_PORT;49tmp = System.getProperty("sun.jvm.hotspot.rmi.port");50if (tmp != null) {51try {52port = Integer.parseInt(tmp);53} catch (NumberFormatException nfe) {54System.err.println("invalid port supplied, assuming default");55}56}57}5859public static void rebind(String serverID, String serverName, Remote object) throws DebuggerException {60String name = getName(serverID, serverName);61try {62Naming.rebind(name, object);63} catch (RemoteException re) {64if (startRegistry) {65// may be the user didn't start rmiregistry, try to start it66try {67LocateRegistry.createRegistry(port);68Naming.rebind(name, object);69} catch (Exception exp) {70throw new DebuggerException(exp);71}72} else {73throw new DebuggerException(re);74}75} catch (Exception exp) {76throw new DebuggerException(exp);77}78}7980public static void unbind(String serverID, String serverName) throws DebuggerException {81String name = getName(serverID, serverName);82try {83Naming.unbind(name);84} catch (Exception exp) {85throw new DebuggerException(exp);86}87}8889public static Remote lookup(String connectionString) throws DebuggerException {90// connectionString follows the pattern [serverid@]host[:port][/servername]91// we have to transform this as //host[:port]/<servername>['_'<serverid>]92Matcher matcher = CONNECT_PATTERN.matcher(connectionString);93matcher.find();9495String serverNamePrefix = System.getProperty("sun.jvm.hotspot.rmi.serverNamePrefix");96String rmiObjectName = matcher.group("servername");97if (serverNamePrefix != null) {98if (rmiObjectName == null) {99System.err.println("WARNING: sun.jvm.hotspot.rmi.serverNamePrefix is deprecated. Please specify it in --connect.");100rmiObjectName = serverNamePrefix;101} else {102throw new DebuggerException("Cannot set both sun.jvm.hotspot.rmi.serverNamePrefix and servername in --connect together");103}104}105if (rmiObjectName == null) {106rmiObjectName = DEFAULT_RMI_OBJECT_NAME;107}108StringBuilder nameBuf = new StringBuilder("//");109nameBuf.append(matcher.group("host"));110nameBuf.append('/');111nameBuf.append(rmiObjectName);112if (matcher.group("serverid") != null) {113nameBuf.append('_');114nameBuf.append(matcher.group("serverid"));115}116117try {118return Naming.lookup(nameBuf.toString());119} catch (Exception exp) {120throw new DebuggerException(exp);121}122}123124private static String getName(String serverID, String serverName) {125String name = serverName;126String serverNamePrefix = System.getProperty("sun.jvm.hotspot.rmi.serverNamePrefix");127if (serverNamePrefix != null) {128if (serverName == null) {129System.err.println("WARNING: sun.jvm.hotspot.rmi.serverNamePrefix is deprecated. Please specify it with --servername.");130name = serverNamePrefix;131} else {132throw new DebuggerException("Cannot set both sun.jvm.hotspot.rmi.serverNamePrefix and --servername together");133}134}135if (name == null) {136name = DEFAULT_RMI_OBJECT_NAME;137}138if (serverID != null) {139name += "_" + serverID;140}141if (port != Registry.REGISTRY_PORT) {142name = "//localhost:" + port + "/" + name;143}144return name;145}146}147148149