1/* 2 * Copyright (c) 2004, 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 25package sun.jvm.hotspot; 26 27import java.io.*; 28import java.net.*; 29import java.rmi.*; 30import java.rmi.registry.*; 31import sun.jvm.hotspot.debugger.DebuggerException; 32 33public class RMIHelper { 34 private static final boolean startRegistry; 35 private static int port; 36 private static String serverNamePrefix; 37 38 static { 39 String tmp = System.getProperty("sun.jvm.hotspot.rmi.startRegistry"); 40 if (tmp != null && tmp.equals("false")) { 41 startRegistry = false; 42 } else { 43 // by default, we attempt to start rmiregistry 44 startRegistry = true; 45 } 46 47 port = Registry.REGISTRY_PORT; 48 tmp = System.getProperty("sun.jvm.hotspot.rmi.port"); 49 if (tmp != null) { 50 try { 51 port = Integer.parseInt(tmp); 52 } catch (NumberFormatException nfe) { 53 System.err.println("invalid port supplied, assuming default"); 54 } 55 } 56 57 tmp = System.getProperty("sun.jvm.hotspot.rmi.serverNamePrefix"); 58 serverNamePrefix = (tmp != null)? serverNamePrefix : "SARemoteDebugger"; 59 } 60 61 public static void rebind(String uniqueID, Remote object) throws DebuggerException { 62 String name = getName(uniqueID); 63 try { 64 Naming.rebind(name, object); 65 } catch (RemoteException re) { 66 if (startRegistry) { 67 // may be the user didn't start rmiregistry, try to start it 68 try { 69 LocateRegistry.createRegistry(port); 70 Naming.rebind(name, object); 71 } catch (Exception exp) { 72 throw new DebuggerException(exp); 73 } 74 } else { 75 throw new DebuggerException(re); 76 } 77 } catch (Exception exp) { 78 throw new DebuggerException(exp); 79 } 80 } 81 82 public static void unbind(String uniqueID) throws DebuggerException { 83 String name = getName(uniqueID); 84 try { 85 Naming.unbind(name); 86 } catch (Exception exp) { 87 throw new DebuggerException(exp); 88 } 89 } 90 91 public static Remote lookup(String debugServerID) throws DebuggerException { 92 // debugServerID follows the pattern [unique_id@]host[:port] 93 // we have to transform this as //host[:port]/<serverNamePrefix>['_'<unique_id>] 94 95 int index = debugServerID.indexOf('@'); 96 StringBuffer nameBuf = new StringBuffer("//"); 97 String uniqueID = null; 98 if (index != -1) { 99 nameBuf.append(debugServerID.substring(index + 1)); 100 uniqueID = debugServerID.substring(0, index); 101 } else { 102 nameBuf.append(debugServerID); 103 } 104 105 nameBuf.append('/'); 106 nameBuf.append(serverNamePrefix); 107 if (uniqueID != null) { 108 nameBuf.append('_'); 109 nameBuf.append(uniqueID); 110 } 111 112 try { 113 return Naming.lookup(nameBuf.toString()); 114 } catch (Exception exp) { 115 throw new DebuggerException(exp); 116 } 117 } 118 119 private static String getName(String uniqueID) { 120 String name = null; 121 if (uniqueID != null) { 122 name = serverNamePrefix + "_" + uniqueID; 123 } else { 124 name = serverNamePrefix; 125 } 126 if (port != Registry.REGISTRY_PORT) { 127 name = "//localhost:" + port + "/" + name; 128 } 129 return name; 130 } 131} 132