KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > remote > interception > Server


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.interception;
10
11 import java.io.File JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Properties JavaDoc;
17 import javax.management.MBeanServer JavaDoc;
18 import javax.management.MBeanServerFactory JavaDoc;
19 import javax.management.ObjectName JavaDoc;
20 import javax.management.remote.JMXAuthenticator JavaDoc;
21 import javax.management.remote.JMXConnectorServer JavaDoc;
22 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
23 import javax.management.remote.JMXServiceURL JavaDoc;
24
25 import mx4j.tools.naming.NamingService;
26 import mx4j.tools.remote.PasswordAuthenticator;
27
28 /**
29  * This example shows how to setup a JSR 160 connector server that intercepts calls to its target MBeanServer.
30  * It will be shown how to intercept and print on the console the Subject of the current call.
31  * It is very similar to the {@link mx4j.examples.remote.security.Server security example}, because it needs
32  * an authenticated Subject to be present in order to log the Subject of the current invocation.
33  *
34  * @version $Revision: 1.1 $
35  * @see Client
36  */

37 public class Server
38 {
39    private static final String JavaDoc PASSWORD_FILE = "users.properties";
40
41    public static void main(String JavaDoc[] args) throws Exception JavaDoc
42    {
43       prepareUsersFile();
44
45       // The address of the connector server
46
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("rmi", "localhost", 0, "/jndi/jmx");
47
48       // Specify the authenticator in the environment Map, using the
49
// standard property JMXConnector.AUTHENTICATOR
50
Map JavaDoc environment = new HashMap JavaDoc();
51       JMXAuthenticator JavaDoc authenticator = new PasswordAuthenticator(new File JavaDoc(PASSWORD_FILE));
52       environment.put(JMXConnectorServer.AUTHENTICATOR, authenticator);
53
54       // Create and register the connector server
55
JMXConnectorServer JavaDoc cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, environment, null);
56       ObjectName JavaDoc cntorServerName = ObjectName.getInstance(":service=" + JMXConnectorServer JavaDoc.class.getName() + ",protocol=" + url.getProtocol());
57       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer("remote.security.example");
58       server.registerMBean(cntorServer, cntorServerName);
59
60       // Setup the rmiregistry to bind in JNDI the RMIConnectorServer stub.
61
NamingService naming = new NamingService();
62       ObjectName JavaDoc namingName = ObjectName.getInstance(":service=" + NamingService.class.getName());
63       server.registerMBean(naming, namingName);
64       naming.start();
65
66       // Setup the interception
67
SubjectTrackingMBeanServer interceptor = new SubjectTrackingMBeanServer();
68       cntorServer.setMBeanServerForwarder(interceptor);
69
70       // Start the connector server
71
cntorServer.start();
72
73       System.out.println("Server up and running");
74    }
75
76    /**
77     * Writes a user/password file in the filesystem, with two hardcoded users:
78     * 'admin' and 'guest'.
79     * Normally this file is provided externally, not created by a program.
80     * Purpose of this method is to show how to obfuscate passwords using
81     * {@link PasswordAuthenticator}.
82     */

83    private static void prepareUsersFile() throws IOException JavaDoc
84    {
85       Properties JavaDoc properties = new Properties JavaDoc();
86
87       String JavaDoc user = "admin";
88       String JavaDoc password = PasswordAuthenticator.obfuscatePassword("admin");
89       properties.setProperty(user, password);
90
91       user = "guest";
92       password = PasswordAuthenticator.obfuscatePassword("guest");
93       properties.setProperty(user, password);
94
95       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(new File JavaDoc(PASSWORD_FILE));
96       properties.store(fos, null);
97    }
98 }
99
Popular Tags