KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > support > ConnectorServerFactoryBean


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jmx.support;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import javax.management.JMException JavaDoc;
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.MalformedObjectNameException JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.management.remote.JMXConnectorServer JavaDoc;
28 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
29 import javax.management.remote.JMXServiceURL JavaDoc;
30
31 import org.springframework.beans.factory.DisposableBean;
32 import org.springframework.beans.factory.FactoryBean;
33 import org.springframework.beans.factory.InitializingBean;
34 import org.springframework.jmx.JmxException;
35
36 /**
37  * <code>FactoryBean</code> that creates a JSR-160 <code>JMXConnectorServer</code>,
38  * optionally registers it with the <code>MBeanServer</code> and then starts it.
39  *
40  * <p>The <code>JMXConnectorServer</code> can be started in a separate thread by setting the
41  * <code>threaded</code> property to <code>true</code>. You can configure this thread to be a
42  * daemon thread by setting the <code>daemon</code> property to <code>true</code>.
43  *
44  * <p>The <code>JMXConnectorServer</code> is correctly shutdown when an instance of this
45  * class is destroyed on shutdown of the containing <code>ApplicationContext</code>.
46  *
47  * @author Rob Harrop
48  * @author Juergen Hoeller
49  * @since 1.2
50  * @see FactoryBean
51  * @see JMXConnectorServer
52  * @see MBeanServer
53  */

54 public class ConnectorServerFactoryBean extends MBeanRegistrationSupport
55         implements FactoryBean, InitializingBean, DisposableBean {
56
57     /** The default service URL */
58     public static final String JavaDoc DEFAULT_SERVICE_URL = "service:jmx:jmxmp://localhost:9875";
59
60
61     private String JavaDoc serviceUrl = DEFAULT_SERVICE_URL;
62
63     private Map JavaDoc environment;
64
65     private ObjectName JavaDoc objectName;
66
67     private boolean threaded = false;
68
69     private boolean daemon = false;
70
71     private JMXConnectorServer JavaDoc connectorServer;
72
73
74     /**
75      * Set the service URL for the <code>JMXConnectorServer</code>.
76      */

77     public void setServiceUrl(String JavaDoc serviceUrl) {
78         this.serviceUrl = serviceUrl;
79     }
80
81     /**
82      * Set the environment properties used to construct the <code>JMXConnectorServer</code>
83      * as <code>java.util.Properties</code> (String key/value pairs).
84      */

85     public void setEnvironment(Properties JavaDoc environment) {
86         this.environment = environment;
87     }
88
89     /**
90      * Set the environment properties used to construct the <code>JMXConnector</code>
91      * as a <code>Map</code> of String keys and arbitrary Object values.
92      */

93     public void setEnvironmentMap(Map JavaDoc environment) {
94         this.environment = environment;
95     }
96
97     /**
98      * Set the <code>ObjectName</code> used to register the <code>JMXConnectorServer</code>
99      * itself with the <code>MBeanServer</code>, as <code>ObjectName</code> instance
100      * or as <code>String</code>.
101      * @throws MalformedObjectNameException if the <code>ObjectName</code> is malformed
102      */

103     public void setObjectName(Object JavaDoc objectName) throws MalformedObjectNameException JavaDoc {
104         this.objectName = ObjectNameManager.getInstance(objectName);
105     }
106
107     /**
108      * Set whether the <code>JMXConnectorServer</code> should be started in a separate thread.
109      */

110     public void setThreaded(boolean threaded) {
111         this.threaded = threaded;
112     }
113
114     /**
115      * Set whether any threads started for the <code>JMXConnectorServer</code> should be
116      * started as daemon threads.
117      */

118     public void setDaemon(boolean daemon) {
119         this.daemon = daemon;
120     }
121
122
123     /**
124      * Start the connector server. If the <code>threaded</code> flag is set to <code>true</code>,
125      * the <code>JMXConnectorServer</code> will be started in a separate thread.
126      * If the <code>daemon</code> flag is set to <code>true</code>, that thread will be
127      * started as a daemon thread.
128      * @throws JMException if a problem occured when registering the connector server
129      * with the <code>MBeanServer</code>
130      * @throws IOException if there is a problem starting the connector server
131      */

132     public void afterPropertiesSet() throws JMException JavaDoc, IOException JavaDoc {
133         if (this.server == null) {
134             this.server = JmxUtils.locateMBeanServer();
135         }
136
137         // Create the JMX service URL.
138
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc(this.serviceUrl);
139
140         // Create the connector server now.
141
this.connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, this.environment, this.server);
142
143         // Do we want to register the connector with the MBean server?
144
if (this.objectName != null) {
145             doRegister(this.connectorServer, this.objectName);
146         }
147
148         try {
149             if (this.threaded) {
150                 // Start the connector server asynchronously (in a separate thread).
151
Thread JavaDoc connectorThread = new Thread JavaDoc() {
152                     public void run() {
153                         try {
154                             connectorServer.start();
155                         }
156                         catch (IOException JavaDoc ex) {
157                             throw new JmxException("Could not start JMX connector server after delay", ex);
158                         }
159                     }
160                 };
161
162                 connectorThread.setName("JMX Connector Thread [" + this.serviceUrl + "]");
163                 connectorThread.setDaemon(this.daemon);
164                 connectorThread.start();
165             }
166             else {
167                 // Start the connector server in the same thread.
168
this.connectorServer.start();
169             }
170
171             if (logger.isInfoEnabled()) {
172                 logger.info("JMX connector server started: " + this.connectorServer);
173             }
174         }
175
176         catch (IOException JavaDoc ex) {
177             // Unregister the connector server if startup failed.
178
unregisterBeans();
179             throw ex;
180         }
181     }
182
183
184     public Object JavaDoc getObject() {
185         return this.connectorServer;
186     }
187
188     public Class JavaDoc getObjectType() {
189         return (this.connectorServer != null ? this.connectorServer.getClass() : JMXConnectorServer JavaDoc.class);
190     }
191
192     public boolean isSingleton() {
193         return true;
194     }
195
196
197     /**
198      * Stop the <code>JMXConnectorServer</code> managed by an instance of this class.
199      * Automatically called on <code>ApplicationContext</code> shutdown.
200      * @throws IOException if there is an error stopping the connector server
201      */

202     public void destroy() throws IOException JavaDoc {
203         if (logger.isInfoEnabled()) {
204             logger.info("Stopping JMX connector server: " + this.connectorServer);
205         }
206         try {
207             this.connectorServer.stop();
208         }
209         finally {
210             unregisterBeans();
211         }
212     }
213
214 }
215
Popular Tags