KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > remote > security > Client


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.examples.remote.security;
10
11 import java.util.Collections JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16 import javax.management.MBeanServerConnection JavaDoc;
17 import javax.management.ObjectName JavaDoc;
18 import javax.management.remote.JMXConnector JavaDoc;
19 import javax.management.remote.JMXConnectorFactory JavaDoc;
20 import javax.management.remote.JMXPrincipal JavaDoc;
21 import javax.management.remote.JMXServiceURL JavaDoc;
22 import javax.security.auth.Subject JavaDoc;
23
24 /**
25  * This example shows how to setup a JSR 160 connector client that connects to
26  * a secured JSR 160 connector server, and that uses the subject delegation features
27  * defined by JSR 160.
28  * Refer to the MX4J documentation on how to run this example and on how it
29  * works: this example is described in details.
30  *
31  * @version $Revision: 1.4 $
32  * @see Server
33  */

34 public class Client
35 {
36    public static void main(String JavaDoc[] args) throws Exception JavaDoc
37    {
38       // The address of the connector server
39
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("rmi", "localhost", 0, "/jndi/jmx");
40
41       // The credentials are passed via the environment Map
42
Map JavaDoc environment = new HashMap JavaDoc();
43       String JavaDoc[] credentials = new String JavaDoc[]{"guest", "guest"};
44       environment.put(JMXConnector.CREDENTIALS, credentials);
45
46       // Connect to the server
47
JMXConnector JavaDoc cntor = JMXConnectorFactory.connect(url, environment);
48
49       // Create a subject to delegate to
50
JMXPrincipal JavaDoc principal = new JMXPrincipal JavaDoc("anotherGuest");
51       Set JavaDoc principals = new HashSet JavaDoc();
52       principals.add(principal);
53       Subject JavaDoc delegate = new Subject JavaDoc(true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET);
54
55       // Get two MBeanServerConnection: one that uses the 'guest' principal directly,
56
// the second that uses the 'guest' user but delegates to another principal.
57
MBeanServerConnection JavaDoc connection = cntor.getMBeanServerConnection();
58       MBeanServerConnection JavaDoc delegateConnection = cntor.getMBeanServerConnection(delegate);
59
60       // The example policy file provided allows both MBeanServerConnections to call
61
// MBeanServerConnection.queryNames
62
Set JavaDoc 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       // The example policy file forbids to call MBeanServerConnection.getObjectInstance
73
try
74       {
75          connection.getObjectInstance(ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate"));
76          throw new Error JavaDoc();
77       }
78       catch (SecurityException JavaDoc x)
79       {
80          System.out.println("No permission to call getObjectInstance for the MBeanServerDelegate");
81       }
82    }
83 }
84
Popular Tags