001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jstatd;
027:
028: import java.rmi.*;
029: import java.rmi.server.*;
030: import java.rmi.registry.Registry;
031: import java.rmi.registry.LocateRegistry;
032: import java.net.MalformedURLException;
033: import sun.jvmstat.monitor.remote.*;
034:
035: /**
036: * Application providing remote access to the jvmstat instrumentation
037: * exported by local Java Virtual Machine processes. Remote access is
038: * provided through an RMI interface.
039: *
040: * @author Brian Doherty
041: * @version 1.9, 05/05/07
042: * @since 1.5
043: */
044: public class Jstatd {
045:
046: private static Registry registry;
047: private static int port = -1;
048: private static boolean startRegistry = true;
049:
050: private static void printUsage() {
051: System.err
052: .println("usage: jstatd [-nr] [-p port] [-n rminame]");
053: }
054:
055: static void bind(String name, RemoteHostImpl remoteHost)
056: throws RemoteException, MalformedURLException, Exception {
057:
058: try {
059: Naming.rebind(name, remoteHost);
060: } catch (java.rmi.ConnectException e) {
061: /*
062: * either the registry is not running or we cannot contact it.
063: * start an internal registry if requested.
064: */
065: if (startRegistry && registry == null) {
066: int localport = (port < 0) ? Registry.REGISTRY_PORT
067: : port;
068: registry = LocateRegistry.createRegistry(localport);
069: bind(name, remoteHost);
070: } else {
071: System.out.println("Could not contact registry\n"
072: + e.getMessage());
073: e.printStackTrace();
074: }
075: } catch (RemoteException e) {
076: System.err.println("Could not bind " + name
077: + " to RMI Registry");
078: e.printStackTrace();
079: }
080: }
081:
082: public static void main(String[] args) {
083: String rminame = null;
084: int argc = 0;
085:
086: for (; (argc < args.length) && (args[argc].startsWith("-")); argc++) {
087: String arg = args[argc];
088:
089: if (arg.compareTo("-nr") == 0) {
090: startRegistry = false;
091: } else if (arg.startsWith("-p")) {
092: if (arg.compareTo("-p") != 0) {
093: port = Integer.parseInt(arg.substring(2));
094: } else {
095: argc++;
096: if (argc >= args.length) {
097: printUsage();
098: System.exit(1);
099: }
100: port = Integer.parseInt(args[argc]);
101: }
102: } else if (arg.startsWith("-n")) {
103: if (arg.compareTo("-n") != 0) {
104: rminame = arg.substring(2);
105: } else {
106: argc++;
107: if (argc >= args.length) {
108: printUsage();
109: System.exit(1);
110: }
111: rminame = args[argc];
112: }
113: } else {
114: printUsage();
115: System.exit(1);
116: }
117: }
118:
119: if (argc < args.length) {
120: printUsage();
121: System.exit(1);
122: }
123:
124: if (System.getSecurityManager() == null) {
125: System.setSecurityManager(new RMISecurityManager());
126: }
127:
128: StringBuilder name = new StringBuilder();
129:
130: if (port >= 0) {
131: name.append("//:").append(port);
132: }
133:
134: if (rminame == null) {
135: rminame = "JStatRemoteHost";
136: }
137:
138: name.append("/").append(rminame);
139:
140: try {
141: // use 1.5.0 dynamically generated subs.
142: System.setProperty("java.rmi.server.ignoreSubClasses",
143: "true");
144: RemoteHostImpl remoteHost = new RemoteHostImpl();
145: RemoteHost stub = (RemoteHost) UnicastRemoteObject
146: .exportObject(remoteHost, 0);
147: bind(name.toString(), remoteHost);
148: } catch (MalformedURLException e) {
149: if (rminame != null) {
150: System.out.println("Bad RMI server name: " + rminame);
151: } else {
152: System.out.println("Bad RMI URL: " + name + " : "
153: + e.getMessage());
154: }
155: System.exit(1);
156: } catch (java.rmi.ConnectException e) {
157: // could not attach to or create a registry
158: System.out.println("Could not contact RMI registry\n"
159: + e.getMessage());
160: System.exit(1);
161: } catch (Exception e) {
162: System.out.println("Could not create remote object\n"
163: + e.getMessage());
164: e.printStackTrace();
165: System.exit(1);
166: }
167: }
168: }
|