001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.jmx.server.mx4j;
019:
020: import java.io.IOException;
021: import java.util.Iterator;
022: import java.util.Set;
023:
024: import javax.management.Attribute;
025: import javax.management.MBeanServer;
026: import javax.management.ObjectInstance;
027: import javax.management.ObjectName;
028:
029: import org.sape.carbon.core.component.Component;
030: import org.sape.carbon.core.component.ComponentConfiguration;
031: import org.sape.carbon.core.component.lifecycle.Configurable;
032: import org.sape.carbon.core.component.lifecycle.Destroyable;
033: import org.sape.carbon.core.component.lifecycle.Initializable;
034: import org.sape.carbon.core.component.lifecycle.Startable;
035: import org.sape.carbon.core.config.InvalidConfigurationException;
036: import org.sape.carbon.services.jmx.server.MBeanServerService;
037:
038: import mx4j.adaptor.http.HttpAdaptor;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: /**
043: * This component implementation configures and sets up the MX4J HTTP adaptor
044: * to create a web interface to JMX.
045: *
046: * Copyright 2002 Sapient
047: * @since carbon 1.0
048: * @author Greg Hinkle, June 2002
049: * @version $Revision: 1.9 $($Author: dvoet $ / $Date: 2003/05/05 21:21:31 $)
050: */
051: public class DefaultHttpAdaptorServiceImpl implements Configurable,
052: Initializable, Startable, Destroyable, MBeanServerService {
053:
054: /**
055: * Provides a handle to Apache-commons logger
056: */
057: private Log log = LogFactory.getLog(this .getClass());
058:
059: /** Holds the MBean Service being exposed. */
060: private MBeanServer server;
061:
062: /** Holds the HttpAdapter for the service. */
063: private HttpAdaptor adaptor;
064:
065: /** Holds the port of the adapter. */
066: private int port;
067:
068: /** holds the hostname for the adapter. */
069: private String host;
070:
071: /** Holds the port incrementals. */
072: private int retries;
073:
074: /**
075: * Configure the component. This is preceded and followed by the suspend and
076: * resume operations if they are available on the component.
077: *
078: * @param configuration <code>HttpAdaptorConfiguration</code>
079: * for this component
080: */
081: public void configure(ComponentConfiguration configuration) {
082: try {
083: HttpAdaptorConfiguration config = (HttpAdaptorConfiguration) configuration;
084:
085: this .server = config.getMBeanServerService()
086: .getMBeanServer();
087:
088: this .port = config.getPort();
089: this .host = config.getHostname();
090: this .retries = config.getPortIncrementals();
091:
092: ObjectName adaptorName = new ObjectName(
093: "Server:name=HttpAdaptor");
094: this .adaptor = new HttpAdaptor();
095: this .adaptor.setPort(config.getPort());
096: this .adaptor.setHost(config.getHostname());
097: this .server.registerMBean(adaptor, adaptorName);
098:
099: ObjectName processorName = new ObjectName(
100: "Server:name=XSLTProcessor");
101:
102: this .server.createMBean("mx4j.adaptor.http.XSLTProcessor",
103: processorName, null);
104:
105: this .server.setAttribute(adaptorName, new Attribute(
106: "ProcessorName", processorName));
107:
108: this .server.invoke(processorName, "addMimeType",
109: new Object[] { ".pdf", "application/pdf" },
110: new String[] { "java.lang.String",
111: "java.lang.String" });
112: } catch (Exception e) {
113: throw new InvalidConfigurationException(this .getClass(),
114: configuration.getConfigurationName(), "",
115: "Couldn't configure MBeanServer.", e);
116: }
117: }
118:
119: /**
120: * Initialize the component. Called immediately after the Component's
121: * constructor. On return, the container may start the component.
122: *
123: * @param thisComponent reference to the component
124: */
125: public void initialize(Component this Component) {
126:
127: //this.server = MBeanServerFactory.createMBeanServer();
128: //new MBeanServerImpl("HttpAdaptor");
129:
130: }
131:
132: /**
133: * Start the component. On return, the container will begin fowarding
134: * requests to the component. This may be an opportunity to dispatch
135: * worker threads.
136: *
137: * @throws LifecycleException
138: */
139: public void start() {
140:
141: //adaptor.start();
142:
143: int retriesLeft = this .retries;
144: int currentPort = this .port;
145: boolean success = false;
146: while ((retriesLeft > 0) && (success == false)) {
147: try {
148: this .adaptor.setPort(currentPort);
149: this .adaptor.start();
150: success = true;
151: } catch (IOException ioe) {
152: retriesLeft--;
153: currentPort++;
154: }
155: }
156: if (success) {
157: this .port = currentPort;
158: if (log.isDebugEnabled()) {
159: log
160: .debug("Able to start JMX Web Console listener on port ["
161: + this .port + "]");
162: }
163: } else {
164: if (log.isDebugEnabled()) {
165: log
166: .debug("Unable to start JMX Web Console listener on port ["
167: + this .port
168: + "] through ["
169: + (this .port + this .retries) + "]");
170: }
171: }
172: }
173:
174: /**
175: * Stop the component. Prior to entry, the container will cease forwarding
176: * requests for service to the component. This is an opportunity to cleanly
177: * complete or abort any outstanding work. For example, a set of worker
178: * threads could be killed off, or a queue set to drain.
179: *
180: * @throws LifecycleException
181: */
182: public void stop() {
183: adaptor.stop();
184: }
185:
186: /**
187: * Retreives the MBean Server.
188: *
189: * @return the MBean Server
190: */
191: public MBeanServer getMBeanServer() {
192: return this .server;
193: }
194:
195: /**
196: * Destroy the component. Called when the Component is already Stopped.
197: *
198: * @throws LifecycleException
199: */
200: public void destroy() {
201: this .adaptor.stop();
202: Set mbeanSet = this .server.queryMBeans(null, null);
203: Iterator mbeanIterator = mbeanSet.iterator();
204: while (mbeanIterator.hasNext()) {
205: ObjectInstance inst = (ObjectInstance) mbeanIterator.next();
206: try {
207: ObjectName objName = inst.getObjectName();
208: this .server.unregisterMBean(objName);
209: if (log.isTraceEnabled()) {
210: log.trace("Successfully deregistered object ["
211: + inst.getObjectName() + "]");
212: }
213: } catch (Exception e) {
214: if (log.isTraceEnabled()) {
215: log.trace("Could not deregistered object ["
216: + inst.getObjectName() + "]");
217: }
218: }
219: }
220:
221: }
222:
223: }
|