KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > jmx > MX4J_HtmlAdaptor


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2005 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27 package org.objectweb.speedo.jmx;
28
29 import mx4j.log.Log;
30 import mx4j.tools.adaptor.http.HttpAdaptor;
31 import mx4j.tools.adaptor.http.HttpAdaptorMBean;
32 import mx4j.tools.adaptor.http.XSLTProcessor;
33
34 import org.objectweb.fractal.api.control.BindingController;
35 import org.objectweb.fractal.api.control.LifeCycleController;
36 import org.objectweb.fractal.jmx.agent.AdminAttributes;
37 import org.objectweb.fractal.jmx.comm.CommunicatorAttributes;
38 import org.objectweb.util.monolog.api.BasicLevel;
39 import org.objectweb.util.monolog.api.Logger;
40
41 import javax.management.Attribute JavaDoc;
42 import javax.management.MBeanServer JavaDoc;
43 import javax.management.ObjectName JavaDoc;
44
45 /**
46  * This class is an adaptor for the HTML adaptor provided by MX4J.
47  *
48  * @author S.Chassande-Barrioz
49  */

50 public class MX4J_HtmlAdaptor
51     implements BindingController, LifeCycleController, CommunicatorAttributes {
52     
53     private static final String JavaDoc ADAPTOR = "MX4J_HtmlAdaptor";
54     private final static String JavaDoc BINDING_ADMIN_ATT = "adminAtt";
55     private AdminAttributes adminAtt;
56     private HttpAdaptorMBean adapter = null;
57     private int port;
58     private Logger logger;
59
60     private ObjectName JavaDoc getObjectName() throws Exception JavaDoc {
61         String JavaDoc id = '@' + Integer.toHexString(this.hashCode());
62         return new ObjectName JavaDoc(ADAPTOR + id
63                 + ":type=html,port=" + adapter.getPort());
64     }
65
66     // --------------------------------------------------------------------------
67
// Implementation of the BindingController interface
68
// --------------------------------------------------------------------------
69
public String JavaDoc[] listFc() {
70         return new String JavaDoc[] {BINDING_ADMIN_ATT};
71     }
72
73     public Object JavaDoc lookupFc(final String JavaDoc itfName) {
74         if (BINDING_ADMIN_ATT.equals(itfName)) {
75             return adminAtt;
76         } else {
77             return null;
78         }
79     }
80
81     public void bindFc(final String JavaDoc itfName, final Object JavaDoc itfValue) {
82         if ("logger".equals(itfName)) {
83             logger = (Logger) itfValue;
84         } else if (BINDING_ADMIN_ATT.equals(itfName)) {
85             adminAtt = (AdminAttributes) itfValue;
86         }
87     }
88
89     public void unbindFc(final String JavaDoc itfName) {
90         if (BINDING_ADMIN_ATT.equals(itfName)) {
91             adminAtt = null;
92         }
93     }
94     
95     // -------------------------------------------------------------------------
96
// implements Attribute controller
97
// -------------------------------------------------------------------------
98

99     public int getPort() {
100         return port;
101     }
102
103     public void setPort(final int port) {
104         this.port = port;
105     }
106
107     // -------------------------------------------------------------------------
108
// implementation of the LifeCycleController interface
109
// -------------------------------------------------------------------------
110
public String JavaDoc getFcState() {
111         return null;
112     }
113
114     public synchronized void startFc() {
115         try {
116             if (adapter != null
117                 && adapter.isActive()
118                 && adapter.getPort() == port) {
119                 return;
120             }
121             Log.redirectTo(new MX4JLoggerMonolog(logger));
122             MBeanServer JavaDoc server = adminAtt.getRawMBeanServer();
123             adapter = new HttpAdaptor();
124             server.registerMBean(adapter, getObjectName());
125
126             XSLTProcessor processor = new XSLTProcessor();
127             processor.setPathInJar("org/objectweb/speedo/jmx/xsl");
128             processor.setUseCache(false);
129             ObjectName JavaDoc processorName = new ObjectName JavaDoc("Server:name=XSLTProcessor");
130             server.registerMBean(processor, processorName);
131
132             server.setAttribute(getObjectName(),
133                     new Attribute JavaDoc("ProcessorName", processorName));
134
135             adapter.setPort(port);
136             adapter.start();
137             logger.log(BasicLevel.INFO, "MX4J HTTP Adaptor launched: "
138                     + "\n\tUrl: http://" + adapter.getHost()
139                     + ":" + adapter.getPort());
140         } catch (Exception JavaDoc e) {
141             logger.log(BasicLevel.ERROR,
142                     "Error during the instanciation of the MX4J Htttp adaptor:", e);
143         }
144     }
145
146     public void stopFc() {
147         if (adapter != null) {
148             adapter.stop();
149             try {
150                 adminAtt.getRawMBeanServer().unregisterMBean(getObjectName());
151             } catch (Exception JavaDoc ignore) {
152             }
153         }
154     }
155 }
156
Popular Tags