1 40 41 package com.sun.jmx.examples.scandir; 42 43 import java.net.InetAddress ; 44 import java.net.UnknownHostException ; 45 import java.util.HashMap ; 46 import java.util.Map ; 47 import javax.management.MBeanServerConnection ; 48 import javax.management.remote.JMXConnector ; 49 import javax.management.remote.JMXConnectorFactory ; 50 import javax.management.remote.JMXServiceURL ; 51 import javax.rmi.ssl.SslRMIClientSocketFactory ; 52 53 67 public class ScanDirClient { 68 69 private ScanDirClient() { } 71 72 75 public static final String USAGE = ScanDirClient.class.getSimpleName() + 76 " <server-host> <rmi-port-number>"; 77 78 90 public static void main(String [] args) { 91 try { 92 if (args==null || args.length!=2) { 95 System.err.println("Bad number of arguments: usage is: \n\t" + 96 USAGE); 97 System.exit(1); 98 } 99 try { 100 InetAddress.getByName(args[0]); 101 } catch (UnknownHostException x) { 102 System.err.println("No such host: " + args[0]+ 103 "\n usage is: \n\t" + USAGE); 104 System.exit(2); 105 } catch (Exception x) { 106 System.err.println("Bad address: " + args[0]+ 107 "\n usage is: \n\t" + USAGE); 108 System.exit(2); 109 } 110 try { 111 if (Integer.parseInt(args[1]) <= 0) { 112 System.err.println("Bad port value: " + args[1]+ 113 "\n usage is: \n\t" + USAGE); 114 System.exit(2); 115 } 116 } catch (Exception x) { 117 System.err.println("Bad argument: " + args[1]+ 118 "\n usage is: \n\t" + USAGE); 119 System.exit(2); 120 } 121 122 System.out.println("\nInitialize the environment map"); 127 final Map <String ,Object > env = new HashMap <String ,Object >(); 128 129 final String [] credentials = new String [] { "guest" , "guestpasswd" }; 133 env.put("jmx.remote.credentials", credentials); 134 135 env.put("com.sun.jndi.rmi.factory.socket", 140 new SslRMIClientSocketFactory ()); 141 142 System.out.println("\nCreate the RMI connector client and " + 148 "connect it to the RMI connector server"); 149 final JMXServiceURL url = new JMXServiceURL ( 150 "service:jmx:rmi:///jndi/rmi://"+args[0]+":"+args[1] + 151 "/jmxrmi"); 152 153 System.out.println("Connecting to: "+url); 154 final JMXConnector jmxc = JMXConnectorFactory.connect(url, env); 155 156 System.out.println("\nGet the MBeanServerConnection"); 159 final MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); 160 161 final ScanManagerMXBean proxy = 164 ScanManager.newSingletonProxy(mbsc); 165 166 System.out.println( 169 "\nGet ScanDirConfigMXBean from ScanManagerMXBean"); 170 final ScanDirConfigMXBean configMBean = 171 proxy.getConfigurationMBean(); 172 173 System.out.println( 176 "\nGet 'Configuration' attribute on ScanDirConfigMXBean"); 177 System.out.println("\nConfiguration:\n" + 178 configMBean.getConfiguration()); 179 180 System.out.println("\nInvoke 'close' on ScanManagerMXBean"); 186 try { 187 proxy.close(); 188 } catch (SecurityException e) { 189 System.out.println("\nGot expected security exception: " + e); 190 } 191 192 System.out.println("\nClose the connection to the server"); 195 jmxc.close(); 196 System.out.println("\nBye! Bye!"); 197 } catch (Exception e) { 198 System.out.println("\nGot unexpected exception: " + e); 199 e.printStackTrace(); 200 System.exit(3); 201 } 202 } 203 } 204 | Popular Tags |