001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Contact: sequoia@continuent.org
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: * Initial developer(s): Marc Wick.
021: * Contributor(s): ______________________.
022: */package org.continuent.sequoia.controller.jmx;
023:
024: import java.net.InetAddress;
025: import java.net.UnknownHostException;
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: import javax.management.Attribute;
030: import javax.management.MBeanServer;
031: import javax.management.ObjectName;
032:
033: import org.continuent.sequoia.common.i18n.Translate;
034: import org.continuent.sequoia.common.jmx.JmxException;
035: import org.continuent.sequoia.common.log.Trace;
036: import org.continuent.sequoia.common.net.SSLConfiguration;
037:
038: /**
039: * This class defines a HttpAdaptor
040: *
041: * @author <a href="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
042: * @version 1.0
043: */
044: public class HttpAdaptor
045:
046: {
047: static Trace logger = Trace
048: .getLogger("org.continuent.sequoia.controller.jmx");
049:
050: private String hostName;
051: private int port;
052: //private JMXAuthenticator authenticator;
053: private SSLConfiguration sslConfig;
054:
055: private mx4j.tools.adaptor.http.HttpAdaptor adaptor;
056: private ObjectName objectName;
057: private ObjectName processorName;
058:
059: private static List httpAdaptors = new ArrayList();
060:
061: /**
062: * Creates a new <code>HttpAdaptor</code> object
063: *
064: * @param hostName the host name the adaptor binds to
065: * @param port the http port
066: * @param sslConfig the ssl configuration, if null ssl is disabled
067: * @throws JmxException problems to get name of localhost
068: */
069: public HttpAdaptor(String hostName, int port,
070: SSLConfiguration sslConfig) throws JmxException {
071: if (hostName != null) {
072: this .hostName = hostName;
073: } else {
074: try {
075: /** TODO: dssmith - determine applicability of getLocalHost() */
076: this .hostName = InetAddress.getLocalHost()
077: .getHostName();
078: } catch (UnknownHostException ex) {
079: throw new JmxException(ex);
080: }
081: }
082: this .port = port;
083: //this.authenticator = authenticator;
084: this .sslConfig = sslConfig;
085: addHttpAdaptor(this );
086: }
087:
088: /**
089: * Start the HTTP adaptor
090: *
091: * @throws JmxException the adaptor could not be started
092: */
093: public void start() throws JmxException {
094: try {
095:
096: MBeanServer server = MBeanServerManager.getInstance();
097: // register the HTTP adaptor MBean to the agent
098: logger.info(Translate.get("jmx.create.http.adaptor",
099: new Object[] { hostName, String.valueOf(port) }));
100: adaptor = new mx4j.tools.adaptor.http.HttpAdaptor();
101: objectName = new ObjectName("Server:name=HttpAdaptor");
102: server.registerMBean(adaptor, objectName);
103: adaptor.setHost(hostName);
104: adaptor.setPort(port);
105: // set XSLT processor
106: logger.debug(Translate.get("jmx.create.xslt.processor"));
107: processorName = new ObjectName("Server:name=XSLTProcessor");
108: server.createMBean(
109: mx4j.tools.adaptor.http.XSLTProcessor.class
110: .getName(), processorName, null);
111: server.setAttribute(objectName, new Attribute(
112: "ProcessorName", processorName));
113: if (this .sslConfig != null) {
114: // TODO: SSL support for HTTP adaptor
115: throw new JmxException("ssl for http not implemented");
116: }
117: adaptor.start();
118: } catch (Exception e) {
119: e.printStackTrace();
120: throw new JmxException(e);
121: }
122: }
123:
124: /**
125: * stop the http adaptor
126: *
127: * @throws JmxException problems stoping the adaptor
128: */
129: public void stop() throws JmxException {
130: try {
131: MBeanServer server = MBeanServerManager.getInstance();
132: adaptor.stop();
133: server.unregisterMBean(objectName);
134: server.unregisterMBean(processorName);
135: } catch (Exception e) {
136: throw new JmxException(e);
137: }
138: }
139:
140: /**
141: * Returns a list of HttpAdaptor .
142: *
143: * @return Returns list of HttpAdaptor.
144: */
145: public static List getHttpAdaptors() {
146: return httpAdaptors;
147: }
148:
149: /**
150: * Adds an HttpAdaptor to the list.
151: *
152: * @param httpAdaptor The HttpAdaptor to add.
153: */
154: private static synchronized void addHttpAdaptor(
155: HttpAdaptor httpAdaptor) {
156: httpAdaptors.add(httpAdaptor);
157: }
158: }
|