KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > jmx > server > mx4j > DefaultHttpAdaptorServiceImpl


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.jmx.server.mx4j;
19
20
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import javax.management.Attribute JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.ObjectInstance JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29
30 import org.sape.carbon.core.component.Component;
31 import org.sape.carbon.core.component.ComponentConfiguration;
32 import org.sape.carbon.core.component.lifecycle.Configurable;
33 import org.sape.carbon.core.component.lifecycle.Destroyable;
34 import org.sape.carbon.core.component.lifecycle.Initializable;
35 import org.sape.carbon.core.component.lifecycle.Startable;
36 import org.sape.carbon.core.config.InvalidConfigurationException;
37 import org.sape.carbon.services.jmx.server.MBeanServerService;
38
39 import mx4j.adaptor.http.HttpAdaptor;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43
44
45 /**
46  * This component implementation configures and sets up the MX4J HTTP adaptor
47  * to create a web interface to JMX.
48  *
49  * Copyright 2002 Sapient
50  * @since carbon 1.0
51  * @author Greg Hinkle, June 2002
52  * @version $Revision: 1.9 $($Author: dvoet $ / $Date: 2003/05/05 21:21:31 $)
53  */

54 public class DefaultHttpAdaptorServiceImpl
55         implements Configurable, Initializable,
56                    Startable, Destroyable, MBeanServerService {
57
58     /**
59      * Provides a handle to Apache-commons logger
60      */

61     private Log log = LogFactory.getLog(this.getClass());
62
63     /** Holds the MBean Service being exposed. */
64     private MBeanServer JavaDoc server;
65
66     /** Holds the HttpAdapter for the service. */
67     private HttpAdaptor adaptor;
68
69     /** Holds the port of the adapter. */
70     private int port;
71
72     /** holds the hostname for the adapter. */
73     private String JavaDoc host;
74
75     /** Holds the port incrementals. */
76     private int retries;
77
78     /**
79      * Configure the component. This is preceded and followed by the suspend and
80      * resume operations if they are available on the component.
81      *
82      * @param configuration <code>HttpAdaptorConfiguration</code>
83      * for this component
84      */

85     public void configure(ComponentConfiguration configuration) {
86         try {
87             HttpAdaptorConfiguration config =
88                 (HttpAdaptorConfiguration) configuration;
89
90             this.server = config.getMBeanServerService().getMBeanServer();
91
92             this.port = config.getPort();
93             this.host = config.getHostname();
94             this.retries = config.getPortIncrementals();
95
96             ObjectName JavaDoc adaptorName = new ObjectName JavaDoc("Server:name=HttpAdaptor");
97             this.adaptor = new HttpAdaptor();
98             this.adaptor.setPort(config.getPort());
99             this.adaptor.setHost(config.getHostname());
100             this.server.registerMBean(adaptor, adaptorName);
101
102             ObjectName JavaDoc processorName =
103                 new ObjectName JavaDoc("Server:name=XSLTProcessor");
104
105             this.server.createMBean(
106                 "mx4j.adaptor.http.XSLTProcessor", processorName, null);
107
108             this.server.setAttribute(
109                 adaptorName, new Attribute JavaDoc("ProcessorName", processorName));
110
111             this.server.invoke(
112                 processorName,
113                 "addMimeType",
114                 new Object JavaDoc[] {".pdf", "application/pdf"},
115                 new String JavaDoc[] {"java.lang.String", "java.lang.String"});
116         } catch (Exception JavaDoc e) {
117             throw new InvalidConfigurationException(
118                 this.getClass(),
119                 configuration.getConfigurationName(), "",
120                 "Couldn't configure MBeanServer.",
121                 e);
122         }
123     }
124
125     /**
126      * Initialize the component. Called immediately after the Component's
127      * constructor. On return, the container may start the component.
128      *
129      * @param thisComponent reference to the component
130      */

131     public void initialize(Component thisComponent) {
132
133         //this.server = MBeanServerFactory.createMBeanServer();
134
//new MBeanServerImpl("HttpAdaptor");
135

136     }
137
138     /**
139      * Start the component. On return, the container will begin fowarding
140      * requests to the component. This may be an opportunity to dispatch
141      * worker threads.
142      *
143      * @throws LifecycleException
144      */

145     public void start() {
146
147         //adaptor.start();
148

149         int retriesLeft = this.retries;
150         int currentPort = this.port;
151         boolean success = false;
152         while ((retriesLeft > 0) && (success == false)) {
153             try {
154                 this.adaptor.setPort(currentPort);
155                 this.adaptor.start();
156                 success = true;
157             } catch (IOException JavaDoc ioe) {
158                 retriesLeft--;
159                 currentPort++;
160             }
161         }
162         if (success) {
163             this.port = currentPort;
164             if (log.isDebugEnabled()) {
165                 log.debug("Able to start JMX Web Console listener on port ["
166                          + this.port + "]");
167             }
168         } else {
169             if (log.isDebugEnabled()) {
170                 log.debug("Unable to start JMX Web Console listener on port ["
171                     + this.port + "] through ["
172                     + (this.port + this.retries) + "]");
173             }
174         }
175     }
176
177     /**
178      * Stop the component. Prior to entry, the container will cease forwarding
179      * requests for service to the component. This is an opportunity to cleanly
180      * complete or abort any outstanding work. For example, a set of worker
181      * threads could be killed off, or a queue set to drain.
182      *
183      * @throws LifecycleException
184      */

185     public void stop() {
186         adaptor.stop();
187     }
188
189
190
191     /**
192      * Retreives the MBean Server.
193      *
194      * @return the MBean Server
195      */

196     public MBeanServer JavaDoc getMBeanServer() {
197         return this.server;
198     }
199
200      /**
201       * Destroy the component. Called when the Component is already Stopped.
202       *
203       * @throws LifecycleException
204       */

205      public void destroy() {
206          this.adaptor.stop();
207          Set JavaDoc mbeanSet = this.server.queryMBeans(null, null);
208          Iterator JavaDoc mbeanIterator = mbeanSet.iterator();
209          while (mbeanIterator.hasNext()) {
210             ObjectInstance JavaDoc inst = (ObjectInstance JavaDoc) mbeanIterator.next();
211             try {
212                 ObjectName JavaDoc objName = inst.getObjectName();
213                 this.server.unregisterMBean(objName);
214                 if (log.isTraceEnabled()) {
215                     log.trace("Successfully deregistered object ["
216                         + inst.getObjectName() + "]");
217                 }
218             } catch (Exception JavaDoc e) {
219                 if (log.isTraceEnabled()) {
220                     log.trace("Could not deregistered object ["
221                         + inst.getObjectName() + "]");
222                 }
223             }
224          }
225
226      }
227
228 }
229
Popular Tags