1 21 22 package org.jacorb.notification.jmx; 23 24 import java.util.Map ; 25 26 import javax.management.MBeanServer ; 27 import javax.management.MBeanServerFactory ; 28 import javax.management.ObjectName ; 29 import javax.management.StandardMBean ; 30 import javax.management.remote.JMXConnectorServer ; 31 import javax.management.remote.JMXConnectorServerFactory ; 32 import javax.management.remote.JMXServiceURL ; 33 34 import org.tanukisoftware.wrapper.WrapperListener; 35 import org.tanukisoftware.wrapper.WrapperManager; 36 import org.tanukisoftware.wrapper.jmx.WrapperManagerMBean; 37 import org.tanukisoftware.wrapper.jmx.WrapperManagerTestingMBean; 38 39 43 public class JMXMain implements WrapperListener 44 { 45 private ObjectName notificationServiceName; 46 47 private MBeanServer server; 48 49 private JMXConnectorServer cntorServer; 50 51 private Thread jmxConnectorRunner; 52 53 private JMXMain() throws Exception { 54 notificationServiceName = ObjectName.getInstance("NotificationService:mbean=EventChannelFactory"); 55 } 56 57 58 public Integer start(String [] args) 59 { 60 init(args); 61 62 return null; 63 } 64 65 public int stop(int code) 66 { 67 try 68 { 69 server.invoke(notificationServiceName, "stop", null, null); 70 cntorServer.stop(); 71 } catch (Exception e) 72 { 73 return 1; 74 } 75 return 0; 76 } 77 78 public void controlEvent(int event) 79 { 80 if (WrapperManager.isControlledByNativeWrapper()) 81 { 82 } 84 else 85 { 86 89 if ((event == WrapperManager.WRAPPER_CTRL_C_EVENT) 90 || (event == WrapperManager.WRAPPER_CTRL_CLOSE_EVENT) 91 || (event == WrapperManager.WRAPPER_CTRL_SHUTDOWN_EVENT)) 92 { 93 org.tanukisoftware.wrapper.WrapperManager.stop(0); 94 } 95 } 96 } 97 98 private void init(String [] args) 99 { 100 try 101 { 102 server = MBeanServerFactory.createMBeanServer(); 103 104 JMXServiceURL address = getServiceURL(); 105 106 Map environment = null; 108 109 cntorServer = 111 JMXConnectorServerFactory.newJMXConnectorServer(address, environment, 112 server); 113 114 registerNotificationService(); 115 116 registerWrapperManager(); 117 118 120 jmxConnectorRunner = new Thread () 122 { 123 public void run() 124 { 125 try 126 { 127 cntorServer.start(); 128 } catch (Exception e) 129 { 130 e.printStackTrace(); 131 } 132 } 133 }; 134 135 jmxConnectorRunner.setName("JMX Connector Runner"); 136 137 jmxConnectorRunner.start(); 138 } catch (Exception e) 139 { 140 e.printStackTrace(); 141 142 throw new RuntimeException (); 143 } 144 } 145 146 private JMXServiceURL getServiceURL() throws Exception 147 { 148 ObjectName namingName = ObjectName.getInstance("naming:type=rmiregistry"); 149 server.createMBean("mx4j.tools.naming.NamingService", namingName, null); 150 System.err.println("Starting NamingService"); 151 152 server.invoke(namingName, "start", null, null); 153 int namingPort = ((Integer ) server.getAttribute(namingName, "Port")).intValue(); 154 155 String jndiPath = "/jmxconnector"; 156 157 JMXServiceURL address = new JMXServiceURL ( 158 "service:jmx:rmi://localhost/jndi/rmi://localhost:" + namingPort + jndiPath); 159 return address; 160 } 161 162 private void registerNotificationService() throws Exception 163 { 164 System.err.println("Registering NotificationService MBean"); 165 166 StandardMBean bean = new StandardMBean (new EventChannelFactoryControl(), 167 EventChannelFactoryMBean.class); 168 169 server.registerMBean(bean, notificationServiceName); 170 } 171 172 private void registerWrapperManager() throws Exception 173 { 174 if (!WrapperManager.isControlledByNativeWrapper()) { 175 return; 176 } 177 178 System.err.println("Registering WrapperMBean"); 179 180 ObjectName wrapperManagerName = ObjectName 181 .getInstance("JavaServiceWrapper:service=WrapperManager"); 182 StandardMBean wrapperManagerBean = new StandardMBean ( 183 new org.tanukisoftware.wrapper.jmx.WrapperManager(), WrapperManagerMBean.class); 184 server.registerMBean(wrapperManagerBean, wrapperManagerName); 185 } 186 187 private void registerWrapperManagerTesting() throws Exception 188 { 189 ObjectName wrapperManagerTestingName = ObjectName 190 .getInstance("JavaServiceWrapper:service=WrapperManagerTesting"); 191 StandardMBean wrapperManagerTestingBean = new StandardMBean ( 192 new org.tanukisoftware.wrapper.jmx.WrapperManagerTesting(), 193 WrapperManagerTestingMBean.class); 194 server.registerMBean(wrapperManagerTestingBean, wrapperManagerTestingName); 195 } 196 197 public static void main(String [] args) throws Exception 198 { 199 WrapperManager.start(new JMXMain(), args); 200 } 201 } | Popular Tags |