1 24 package org.objectweb.jalisto.se.jmx.server.mx4j; 25 26 import mx4j.tools.adaptor.http.HttpAdaptor; 27 import org.objectweb.jalisto.se.jmx.exception.MBeanAdminException; 28 import org.objectweb.jalisto.se.jmx.MemoryAdmin; 29 import org.objectweb.jalisto.se.api.jmx.JalistoMBeanServer; 30 import org.objectweb.jalisto.se.api.JalistoProperties; 31 import org.objectweb.jalisto.se.exception.JalistoException; 32 import org.objectweb.jalisto.se.jmx.CacheAdmin; 33 import org.objectweb.jalisto.se.jmx.ClassDescriptionAdmin; 34 import org.objectweb.jalisto.se.JalistoFactory; 35 36 import javax.management.Attribute ; 37 import javax.management.MBeanServer ; 38 import javax.management.MBeanServerFactory ; 39 import javax.management.ObjectName ; 40 import javax.management.remote.JMXConnectorServer ; 41 import javax.management.remote.JMXConnectorServerFactory ; 42 import javax.management.remote.JMXServiceURL ; 43 44 45 public class JalistoMbeanServerMx4jImpl extends Thread implements JalistoMBeanServer { 46 47 public static void main(String [] args) { 48 String propertiesPath = "jalisto.properties"; 49 try { 50 propertiesPath = args[0]; 51 } catch (Exception e) { 52 System.out.println("Cannot find properties path argument, use default value = " + propertiesPath); 53 } 54 55 JalistoMbeanServerMx4jImpl jalistoServer = new JalistoMbeanServerMx4jImpl(); 56 jalistoServer.init(propertiesPath); 57 jalistoServer.run(); 58 } 59 60 public JalistoMbeanServerMx4jImpl() { 61 } 62 63 public void init(String propertiesPath) { 64 this.propertiesPath = propertiesPath; 65 this.mbeanServer = MBeanServerFactory.createMBeanServer(); 66 this.props = JalistoFactory.getInternalFactory().getProperties(propertiesPath); 67 68 enlistHttpAdaptor(props.getMBeanJmxPort()); 69 enlistJmxRmiConnector(); 70 } 71 72 public void run() { 73 registerMBean(mbeanServer, CacheAdmin.class.getName()); 74 registerMBean(mbeanServer, MemoryAdmin.class.getName()); 75 registerMBean(mbeanServer, ClassDescriptionAdmin.class.getName()); 76 77 System.out.println("Jalisto Mx4j MBean server is OK"); 78 } 79 80 public void addSession(String s) { 81 } 82 83 private void enlistHttpAdaptor(int port) { 84 try { 85 ObjectName name = new ObjectName (mbeanServer.getDefaultDomain()+":name=HttpAdaptor"); 86 ObjectName processorName = new ObjectName (mbeanServer.getDefaultDomain()+":name=XSLTProcessor"); 87 System.out.println("name = " + name); 88 System.out.println("processorName = " + processorName); 89 90 HttpAdaptor adaptor = new HttpAdaptor(); 91 mbeanServer.registerMBean(adaptor, name); 92 adaptor.setPort(port); 93 adaptor.setHost("localhost"); 94 System.out.println("uses localhost on port "+port+" for HttpAdaptor..."); 95 96 mbeanServer.createMBean("mx4j.tools.adaptor.http.XSLTProcessor", processorName, null); 97 mbeanServer.setAttribute(name, new Attribute ("ProcessorName", processorName)); 98 99 adaptor.start(); 100 } catch (JalistoException jalistoExc) { 101 throw jalistoExc; 102 } catch (Exception e) { 103 throw new MBeanAdminException("Could not create the Jalisto Mx4j HtmlAdaptor", e); 104 } 105 System.out.println("Jalisto Mx4j HtmlAdaptor started..."); 106 } 107 108 private void enlistJmxRmiConnector() { 109 try { 110 JMXServiceURL address = new JMXServiceURL ("service:jmx:rmi://localhost"); 111 JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(address, null, null); 112 113 ObjectName connectorServerName = ObjectName.getInstance("connectors:protocol=rmi"); 114 mbeanServer.registerMBean(connectorServer, connectorServerName); 115 116 connectorServer.start(); 117 118 } catch (Exception e) { 119 throw new MBeanAdminException("Could not create Jalisto jmx connector", e); 120 } 121 } 122 123 private void registerMBean(MBeanServer server, String mbeanClassName) { 124 try { 125 Object [] params = new Object [1]; 126 params[0] = propertiesPath; 127 String [] signature = new String [1]; 128 signature[0] = "java.lang.String"; 129 ObjectName mbeanObjectName = 130 new ObjectName (domain + ":type=" + mbeanClassName); 132 server.createMBean(mbeanClassName, 133 mbeanObjectName, 134 null, 135 params, 136 signature); 137 } catch (Exception e) { 138 throw new MBeanAdminException(e); 139 } 140 } 141 142 private String domain = "Jalisto"; 143 private String propertiesPath; 144 private JalistoProperties props; 145 private MBeanServer mbeanServer; 146 } 147 | Popular Tags |