001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package mx4j.examples.remote.interception;
010:
011: import java.io.File;
012: import java.io.FileOutputStream;
013: import java.io.IOException;
014: import java.util.HashMap;
015: import java.util.Map;
016: import java.util.Properties;
017: import javax.management.MBeanServer;
018: import javax.management.MBeanServerFactory;
019: import javax.management.ObjectName;
020: import javax.management.remote.JMXAuthenticator;
021: import javax.management.remote.JMXConnectorServer;
022: import javax.management.remote.JMXConnectorServerFactory;
023: import javax.management.remote.JMXServiceURL;
024:
025: import mx4j.tools.naming.NamingService;
026: import mx4j.tools.remote.PasswordAuthenticator;
027:
028: /**
029: * This example shows how to setup a JSR 160 connector server that intercepts calls to its target MBeanServer.
030: * It will be shown how to intercept and print on the console the Subject of the current call.
031: * It is very similar to the {@link mx4j.examples.remote.security.Server security example}, because it needs
032: * an authenticated Subject to be present in order to log the Subject of the current invocation.
033: *
034: * @version $Revision: 1.1 $
035: * @see Client
036: */
037: public class Server {
038: private static final String PASSWORD_FILE = "users.properties";
039:
040: public static void main(String[] args) throws Exception {
041: prepareUsersFile();
042:
043: // The address of the connector server
044: JMXServiceURL url = new JMXServiceURL("rmi", "localhost", 0,
045: "/jndi/jmx");
046:
047: // Specify the authenticator in the environment Map, using the
048: // standard property JMXConnector.AUTHENTICATOR
049: Map environment = new HashMap();
050: JMXAuthenticator authenticator = new PasswordAuthenticator(
051: new File(PASSWORD_FILE));
052: environment
053: .put(JMXConnectorServer.AUTHENTICATOR, authenticator);
054:
055: // Create and register the connector server
056: JMXConnectorServer cntorServer = JMXConnectorServerFactory
057: .newJMXConnectorServer(url, environment, null);
058: ObjectName cntorServerName = ObjectName.getInstance(":service="
059: + JMXConnectorServer.class.getName() + ",protocol="
060: + url.getProtocol());
061: MBeanServer server = MBeanServerFactory
062: .createMBeanServer("remote.security.example");
063: server.registerMBean(cntorServer, cntorServerName);
064:
065: // Setup the rmiregistry to bind in JNDI the RMIConnectorServer stub.
066: NamingService naming = new NamingService();
067: ObjectName namingName = ObjectName.getInstance(":service="
068: + NamingService.class.getName());
069: server.registerMBean(naming, namingName);
070: naming.start();
071:
072: // Setup the interception
073: SubjectTrackingMBeanServer interceptor = new SubjectTrackingMBeanServer();
074: cntorServer.setMBeanServerForwarder(interceptor);
075:
076: // Start the connector server
077: cntorServer.start();
078:
079: System.out.println("Server up and running");
080: }
081:
082: /**
083: * Writes a user/password file in the filesystem, with two hardcoded users:
084: * 'admin' and 'guest'.
085: * Normally this file is provided externally, not created by a program.
086: * Purpose of this method is to show how to obfuscate passwords using
087: * {@link PasswordAuthenticator}.
088: */
089: private static void prepareUsersFile() throws IOException {
090: Properties properties = new Properties();
091:
092: String user = "admin";
093: String password = PasswordAuthenticator
094: .obfuscatePassword("admin");
095: properties.setProperty(user, password);
096:
097: user = "guest";
098: password = PasswordAuthenticator.obfuscatePassword("guest");
099: properties.setProperty(user, password);
100:
101: FileOutputStream fos = new FileOutputStream(new File(
102: PASSWORD_FILE));
103: properties.store(fos, null);
104: }
105: }
|