KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > remote > security > 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.security;
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 runs under
30  * security manager and that forbids access to non-authorized users.
31  * Refer to the MX4J documentation on how to run this example and on how to tune
32  * the policy files: this example is described in details.
33  *
34  * @version $Revision: 1.5 $
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       // Start the connector server
67
cntorServer.start();
68
69       System.out.println("Server up and running");
70    }
71
72    /**
73     * Writes a user/password file in the filesystem, with two hardcoded users:
74     * 'admin' and 'guest'.
75     * Normally this file is provided externally, not created by a program.
76     * Purpose of this method is to show how to obfuscate passwords using
77     * {@link PasswordAuthenticator}.
78     */

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