KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > jmx > server > mx4j > JalistoMbeanServerMx4jImpl


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

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 JavaDoc;
37 import javax.management.MBeanServer JavaDoc;
38 import javax.management.MBeanServerFactory JavaDoc;
39 import javax.management.ObjectName JavaDoc;
40 import javax.management.remote.JMXConnectorServer JavaDoc;
41 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
42 import javax.management.remote.JMXServiceURL JavaDoc;
43
44
45 public class JalistoMbeanServerMx4jImpl extends Thread JavaDoc implements JalistoMBeanServer {
46
47     public static void main(String JavaDoc[] args) {
48         String JavaDoc propertiesPath = "jalisto.properties";
49         try {
50             propertiesPath = args[0];
51         } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc s) {
81     }
82
83     private void enlistHttpAdaptor(int port) {
84         try {
85             ObjectName JavaDoc name = new ObjectName JavaDoc(mbeanServer.getDefaultDomain()+":name=HttpAdaptor");
86             ObjectName JavaDoc processorName = new ObjectName JavaDoc(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 JavaDoc("ProcessorName", processorName));
98
99             adaptor.start();
100         } catch (JalistoException jalistoExc) {
101             throw jalistoExc;
102         } catch (Exception JavaDoc 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 JavaDoc address = new JMXServiceURL JavaDoc("service:jmx:rmi://localhost");
111             JMXConnectorServer JavaDoc connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(address, null, null);
112
113             ObjectName JavaDoc connectorServerName = ObjectName.getInstance("connectors:protocol=rmi");
114             mbeanServer.registerMBean(connectorServer, connectorServerName);
115
116             connectorServer.start();
117
118         } catch (Exception JavaDoc e) {
119             throw new MBeanAdminException("Could not create Jalisto jmx connector", e);
120         }
121     }
122
123     private void registerMBean(MBeanServer server, String JavaDoc mbeanClassName) {
124         try {
125             Object JavaDoc[] params = new Object JavaDoc[1];
126             params[0] = propertiesPath;
127             String JavaDoc[] signature = new String JavaDoc[1];
128             signature[0] = "java.lang.String";
129             ObjectName JavaDoc mbeanObjectName =
130 // new ObjectName(server.getDefaultDomain() + ":type=" + mbeanClassName);
131
new ObjectName JavaDoc(domain + ":type=" + mbeanClassName);
132             server.createMBean(mbeanClassName,
133                                mbeanObjectName,
134                                null,
135                                params,
136                                signature);
137         } catch (Exception JavaDoc e) {
138             throw new MBeanAdminException(e);
139         }
140     }
141
142     private String JavaDoc domain = "Jalisto";
143     private String JavaDoc propertiesPath;
144     private JalistoProperties props;
145     private MBeanServer mbeanServer;
146 }
147
Popular Tags