Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/jstatd/Jstatd.java
38918 views
/*1* Copyright (c) 2004, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jstatd;2627import java.rmi.*;28import java.rmi.server.*;29import java.rmi.registry.Registry;30import java.rmi.registry.LocateRegistry;31import java.net.MalformedURLException;32import sun.jvmstat.monitor.remote.*;3334/**35* Application providing remote access to the jvmstat instrumentation36* exported by local Java Virtual Machine processes. Remote access is37* provided through an RMI interface.38*39* @author Brian Doherty40* @since 1.541*/42public class Jstatd {4344private static Registry registry;45private static int port = -1;46private static boolean startRegistry = true;4748private static void printUsage() {49System.err.println("usage: jstatd [-nr] [-p port] [-n rminame]");50}5152static void bind(String name, RemoteHostImpl remoteHost)53throws RemoteException, MalformedURLException, Exception {5455try {56Naming.rebind(name, remoteHost);57} catch (java.rmi.ConnectException e) {58/*59* either the registry is not running or we cannot contact it.60* start an internal registry if requested.61*/62if (startRegistry && registry == null) {63int localport = (port < 0) ? Registry.REGISTRY_PORT : port;64registry = LocateRegistry.createRegistry(localport);65bind(name, remoteHost);66}67else {68System.out.println("Could not contact registry\n"69+ e.getMessage());70e.printStackTrace();71}72} catch (RemoteException e) {73System.err.println("Could not bind " + name + " to RMI Registry");74e.printStackTrace();75}76}7778public static void main(String[] args) {79String rminame = null;80int argc = 0;8182for ( ; (argc < args.length) && (args[argc].startsWith("-")); argc++) {83String arg = args[argc];8485if (arg.compareTo("-nr") == 0) {86startRegistry = false;87} else if (arg.startsWith("-p")) {88if (arg.compareTo("-p") != 0) {89port = Integer.parseInt(arg.substring(2));90} else {91argc++;92if (argc >= args.length) {93printUsage();94System.exit(1);95}96port = Integer.parseInt(args[argc]);97}98} else if (arg.startsWith("-n")) {99if (arg.compareTo("-n") != 0) {100rminame = arg.substring(2);101} else {102argc++;103if (argc >= args.length) {104printUsage();105System.exit(1);106}107rminame = args[argc];108}109} else {110printUsage();111System.exit(1);112}113}114115if (argc < args.length) {116printUsage();117System.exit(1);118}119120if (System.getSecurityManager() == null) {121System.setSecurityManager(new RMISecurityManager());122}123124StringBuilder name = new StringBuilder();125126if (port >= 0) {127name.append("//:").append(port);128}129130if (rminame == null) {131rminame = "JStatRemoteHost";132}133134name.append("/").append(rminame);135136try {137// use 1.5.0 dynamically generated subs.138System.setProperty("java.rmi.server.ignoreSubClasses", "true");139RemoteHostImpl remoteHost = new RemoteHostImpl();140RemoteHost stub = (RemoteHost) UnicastRemoteObject.exportObject(141remoteHost, 0);142bind(name.toString(), remoteHost);143} catch (MalformedURLException e) {144if (rminame != null) {145System.out.println("Bad RMI server name: " + rminame);146} else {147System.out.println("Bad RMI URL: " + name + " : "148+ e.getMessage());149}150System.exit(1);151} catch (java.rmi.ConnectException e) {152// could not attach to or create a registry153System.out.println("Could not contact RMI registry\n"154+ e.getMessage());155System.exit(1);156} catch (Exception e) {157System.out.println("Could not create remote object\n"158+ e.getMessage());159e.printStackTrace();160System.exit(1);161}162}163}164165166