1 22 package org.objectweb.petals.tools.ant.util; 23 24 import java.io.IOException ; 25 import java.rmi.registry.LocateRegistry ; 26 import java.rmi.registry.Registry ; 27 import java.util.Hashtable ; 28 29 import javax.management.MBeanServer ; 30 import javax.management.MBeanServerFactory ; 31 import javax.management.ObjectName ; 32 import javax.management.remote.JMXConnectorServer ; 33 import javax.management.remote.JMXConnectorServerFactory ; 34 import javax.management.remote.JMXServiceURL ; 35 import javax.management.remote.rmi.RMIConnectorServer ; 36 37 42 public class ServerUtil { 43 44 private final static String PETALS_DOMAIN = "Petals"; 45 46 private final static String TYPE = "type"; 47 48 private final static String NAME = "name"; 49 50 private final static String SERVICE_TYPE = "service"; 51 52 private final static String ADMIN_NAME = "Admin"; 53 54 private final static String INSTALLATION_NAME = "Installation"; 55 56 private final static String DEPLOYMENT_NAME = "Deployment"; 57 58 61 public static boolean isStarted = false; 62 63 66 private static JMXConnectorServer cs; 67 68 74 public static int start(int port) throws Exception { 75 if (isStarted == false) { 76 Registry registry = LocateRegistry.getRegistry(port); 77 try { 78 registry.list(); 79 } 80 catch (Exception e){ 81 LocateRegistry.createRegistry(port); 82 } 83 MBeanServer beanServer = MBeanServerFactory.newMBeanServer(); 84 85 Hashtable <String , String > attributes = new Hashtable <String , String >(); 86 attributes.put(NAME, DEPLOYMENT_NAME); 87 attributes.put(TYPE, SERVICE_TYPE); 88 beanServer.registerMBean(new DeploymentServiceMoc(beanServer), new ObjectName (PETALS_DOMAIN, attributes)); 89 90 attributes.clear(); 91 attributes.put(NAME, INSTALLATION_NAME); 92 attributes.put(TYPE, SERVICE_TYPE); 93 beanServer.registerMBean(new InstallationServiceMoc(beanServer), new ObjectName (PETALS_DOMAIN, attributes)); 94 95 attributes.clear(); 96 attributes.put(NAME, ADMIN_NAME); 97 attributes.put(TYPE, SERVICE_TYPE); 98 beanServer.registerMBean(new AdminServiceMoc(beanServer), new ObjectName (PETALS_DOMAIN, attributes)); 99 100 String sJmxURL = "service:jmx:rmi:///jndi/rmi://127.0.0.1:" + port + "/management/rmi-jmx-connector"; 101 JMXServiceURL jmxUrl = new JMXServiceURL (sJmxURL); 102 RMIConnectorServer connectorServer = (RMIConnectorServer ) JMXConnectorServerFactory.newJMXConnectorServer(jmxUrl, null, beanServer); 103 cs = connectorServer; 104 cs.start(); 105 isStarted = true; 107 } 108 return port; 109 } 110 111 117 public static void stop() throws IOException { 118 if (isStarted) { 119 isStarted = false; 120 cs.stop(); 121 } 122 } 123 } 124 | Popular Tags |