001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2005 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.jmx;
027:
028: import javax.management.Attribute;
029: import javax.management.MBeanServer;
030: import javax.management.MalformedObjectNameException;
031: import javax.management.ObjectName;
032:
033: import mx4j.log.Log;
034: import mx4j.tools.adaptor.http.HttpAdaptor;
035: import mx4j.tools.adaptor.http.HttpAdaptorMBean;
036: import mx4j.tools.adaptor.http.XSLTProcessor;
037:
038: import org.objectweb.fractal.api.control.BindingController;
039: import org.objectweb.fractal.api.control.LifeCycleController;
040: import org.objectweb.fractal.jmx.agent.AdminAttributes;
041: import org.objectweb.speedo.jmx.api.MX4J_HtmlAdaptorCA;
042: import org.objectweb.util.monolog.api.BasicLevel;
043: import org.objectweb.util.monolog.api.Logger;
044:
045: /**
046: * This class is an adaptor for the HTML adaptor provided by MX4J.
047: *
048: * @author S.Chassande-Barrioz
049: */
050: public class MX4J_HtmlAdaptor implements BindingController,
051: LifeCycleController, MX4J_HtmlAdaptorCA {
052:
053: private static final String ADAPTOR = "MX4J_HtmlAdaptor";
054: private final static String BINDING_ADMIN_ATT = "adminAtt";
055: private AdminAttributes adminAtt;
056: private HttpAdaptorMBean adapter = null;
057: private int port;
058: private String host;
059: private Logger logger;
060:
061: private ObjectName getObjectName()
062: throws MalformedObjectNameException {
063: String id = '@' + Integer.toHexString(this .hashCode());
064: return new ObjectName(ADAPTOR + id + ":type=html,port="
065: + adapter.getPort());
066: }
067:
068: // --------------------------------------------------------------------------
069: // Implementation of the BindingController interface
070: // --------------------------------------------------------------------------
071: public String[] listFc() {
072: return new String[] { BINDING_ADMIN_ATT };
073: }
074:
075: public Object lookupFc(final String itfName) {
076: if (BINDING_ADMIN_ATT.equals(itfName)) {
077: return adminAtt;
078: } else {
079: return null;
080: }
081: }
082:
083: public void bindFc(final String itfName, final Object itfValue) {
084: if ("logger".equals(itfName)) {
085: logger = (Logger) itfValue;
086: } else if (BINDING_ADMIN_ATT.equals(itfName)) {
087: adminAtt = (AdminAttributes) itfValue;
088: }
089: }
090:
091: public void unbindFc(final String itfName) {
092: if (BINDING_ADMIN_ATT.equals(itfName)) {
093: adminAtt = null;
094: }
095: }
096:
097: // -------------------------------------------------------------------------
098: // implements Attribute controller
099: // -------------------------------------------------------------------------
100:
101: public int getPort() {
102: return port;
103: }
104:
105: public void setPort(final int port) {
106: this .port = port;
107: }
108:
109: // -------------------------------------------------------------------------
110: // implements MX4J_HtmlAdaptatorCA
111: // -------------------------------------------------------------------------
112:
113: public String getHost() {
114: return host;
115: }
116:
117: public void setHost(String host) {
118: this .host = host;
119: }
120:
121: // -------------------------------------------------------------------------
122: // implementation of the LifeCycleController interface
123: // -------------------------------------------------------------------------
124: public String getFcState() {
125: return null;
126: }
127:
128: public synchronized void startFc() {
129: try {
130: if (adapter != null && adapter.isActive()
131: && adapter.getPort() == port) {
132: return;
133: }
134: Log.redirectTo(new MX4JLoggerMonolog(logger));
135: MBeanServer server = adminAtt.getRawMBeanServer();
136: adapter = new HttpAdaptor();
137: server.registerMBean(adapter, getObjectName());
138:
139: XSLTProcessor processor = new XSLTProcessor();
140: processor.setPathInJar("org/objectweb/speedo/jmx/xsl");
141: processor.setUseCache(false);
142: ObjectName processorName = new ObjectName(
143: "Server:name=XSLTProcessor");
144: server.registerMBean(processor, processorName);
145:
146: server.setAttribute(getObjectName(), new Attribute(
147: "ProcessorName", processorName));
148:
149: adapter.setPort(port);
150: adapter.setHost(host);
151: adapter.start();
152: logger.log(BasicLevel.INFO, "MX4J HTTP Adaptor launched: "
153: + "\n\tUrl: http://" + adapter.getHost() + ":"
154: + adapter.getPort());
155: } catch (Exception e) {
156: logger
157: .log(
158: BasicLevel.ERROR,
159: "Error during the instanciation of the MX4J Htttp adaptor:",
160: e);
161: }
162: }
163:
164: public void stopFc() {
165: if (adapter != null) {
166: logger.log(BasicLevel.INFO,
167: "Stopping MX4J HTTP console of Speedo");
168: adapter.stop();
169: }
170: }
171: }
|