1 8 9 package mx4j.examples.remote.security; 10 11 import java.util.Collections ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.Map ; 15 import java.util.Set ; 16 import javax.management.MBeanServerConnection ; 17 import javax.management.ObjectName ; 18 import javax.management.remote.JMXConnector ; 19 import javax.management.remote.JMXConnectorFactory ; 20 import javax.management.remote.JMXPrincipal ; 21 import javax.management.remote.JMXServiceURL ; 22 import javax.security.auth.Subject ; 23 24 34 public class Client 35 { 36 public static void main(String [] args) throws Exception 37 { 38 JMXServiceURL url = new JMXServiceURL ("rmi", "localhost", 0, "/jndi/jmx"); 40 41 Map environment = new HashMap (); 43 String [] credentials = new String []{"guest", "guest"}; 44 environment.put(JMXConnector.CREDENTIALS, credentials); 45 46 JMXConnector cntor = JMXConnectorFactory.connect(url, environment); 48 49 JMXPrincipal principal = new JMXPrincipal ("anotherGuest"); 51 Set principals = new HashSet (); 52 principals.add(principal); 53 Subject delegate = new Subject (true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET); 54 55 MBeanServerConnection connection = cntor.getMBeanServerConnection(); 58 MBeanServerConnection delegateConnection = cntor.getMBeanServerConnection(delegate); 59 60 Set mbeans = connection.queryNames(null, null); 63 System.out.println("MBeans retrieved by a connection without delegate subject:"); 64 System.out.println(mbeans); 65 System.out.println(); 66 67 mbeans = delegateConnection.queryNames(null, null); 68 System.out.println("MBeans retrieved by a connection with a delegate subject:"); 69 System.out.println(mbeans); 70 System.out.println(); 71 72 try 74 { 75 connection.getObjectInstance(ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate")); 76 throw new Error (); 77 } 78 catch (SecurityException x) 79 { 80 System.out.println("No permission to call getObjectInstance for the MBeanServerDelegate"); 81 } 82 } 83 } 84 | Popular Tags |