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