1 8 9 package mx4j.examples.remote.interception; 10 11 import java.io.File ; 12 import java.io.FileOutputStream ; 13 import java.io.IOException ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 import java.util.Properties ; 17 import javax.management.MBeanServer ; 18 import javax.management.MBeanServerFactory ; 19 import javax.management.ObjectName ; 20 import javax.management.remote.JMXAuthenticator ; 21 import javax.management.remote.JMXConnectorServer ; 22 import javax.management.remote.JMXConnectorServerFactory ; 23 import javax.management.remote.JMXServiceURL ; 24 25 import mx4j.tools.naming.NamingService; 26 import mx4j.tools.remote.PasswordAuthenticator; 27 28 37 public class Server 38 { 39 private static final String PASSWORD_FILE = "users.properties"; 40 41 public static void main(String [] args) throws Exception 42 { 43 prepareUsersFile(); 44 45 JMXServiceURL url = new JMXServiceURL ("rmi", "localhost", 0, "/jndi/jmx"); 47 48 Map environment = new HashMap (); 51 JMXAuthenticator authenticator = new PasswordAuthenticator(new File (PASSWORD_FILE)); 52 environment.put(JMXConnectorServer.AUTHENTICATOR, authenticator); 53 54 JMXConnectorServer cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, environment, null); 56 ObjectName cntorServerName = ObjectName.getInstance(":service=" + JMXConnectorServer .class.getName() + ",protocol=" + url.getProtocol()); 57 MBeanServer server = MBeanServerFactory.createMBeanServer("remote.security.example"); 58 server.registerMBean(cntorServer, cntorServerName); 59 60 NamingService naming = new NamingService(); 62 ObjectName namingName = ObjectName.getInstance(":service=" + NamingService.class.getName()); 63 server.registerMBean(naming, namingName); 64 naming.start(); 65 66 SubjectTrackingMBeanServer interceptor = new SubjectTrackingMBeanServer(); 68 cntorServer.setMBeanServerForwarder(interceptor); 69 70 cntorServer.start(); 72 73 System.out.println("Server up and running"); 74 } 75 76 83 private static void prepareUsersFile() throws IOException 84 { 85 Properties properties = new Properties (); 86 87 String user = "admin"; 88 String 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 fos = new FileOutputStream (new File (PASSWORD_FILE)); 96 properties.store(fos, null); 97 } 98 } 99 | Popular Tags |