KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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.net.MalformedURLException JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.management.MBeanServerConnection JavaDoc;
25 import javax.management.remote.JMXConnector JavaDoc;
26 import javax.management.remote.JMXConnectorFactory JavaDoc;
27 import javax.management.remote.JMXServiceURL JavaDoc;
28
29 import org.springframework.aop.TargetSource;
30 import org.springframework.aop.framework.ProxyFactory;
31 import org.springframework.aop.target.AbstractLazyCreationTargetSource;
32 import org.springframework.beans.factory.BeanClassLoaderAware;
33 import org.springframework.beans.factory.DisposableBean;
34 import org.springframework.beans.factory.FactoryBean;
35 import org.springframework.beans.factory.InitializingBean;
36 import org.springframework.util.ClassUtils;
37
38 /**
39  * <code>FactoryBean</code> implementation that creates an <code>MBeanServerConnection</code>
40  * to a remote <code>MBeanServer</code> exposed via a <code>JMXServerConnector</code>.
41  * Exposes the <code>MBeanServer</code> for bean references.
42  *
43  * @author Rob Harrop
44  * @author Juergen Hoeller
45  * @since 1.2
46  * @see MBeanServerFactoryBean
47  * @see ConnectorServerFactoryBean
48  */

49 public class MBeanServerConnectionFactoryBean
50         implements FactoryBean, BeanClassLoaderAware, InitializingBean, DisposableBean {
51
52     private JMXServiceURL JavaDoc serviceUrl;
53
54     private Map JavaDoc environment;
55
56     private boolean connectOnStartup = true;
57
58     private ClassLoader JavaDoc beanClassLoader = ClassUtils.getDefaultClassLoader();
59
60     private JMXConnector JavaDoc connector;
61
62     private MBeanServerConnection JavaDoc connection;
63
64     private JMXConnectorLazyInitTargetSource connectorTargetSource;
65
66
67     /**
68      * Set the service URL of the remote <code>MBeanServer</code>.
69      */

70     public void setServiceUrl(String JavaDoc url) throws MalformedURLException JavaDoc {
71         this.serviceUrl = new JMXServiceURL JavaDoc(url);
72     }
73
74     /**
75      * Set the environment properties used to construct the <code>JMXConnector</code>
76      * as <code>java.util.Properties</code> (String key/value pairs).
77      */

78     public void setEnvironment(Properties JavaDoc environment) {
79         this.environment = environment;
80     }
81
82     /**
83      * Set the environment properties used to construct the <code>JMXConnector</code>
84      * as a <code>Map</code> of String keys and arbitrary Object values.
85      */

86     public void setEnvironmentMap(Map JavaDoc environment) {
87         this.environment = environment;
88     }
89
90     /**
91      * Set whether to connect to the server on startup. Default is "true".
92      * <p>Can be turned off to allow for late start of the JMX server.
93      * In this case, the JMX connector will be fetched on first access.
94      */

95     public void setConnectOnStartup(boolean connectOnStartup) {
96         this.connectOnStartup = connectOnStartup;
97     }
98
99     public void setBeanClassLoader(ClassLoader JavaDoc classLoader) {
100         this.beanClassLoader = classLoader;
101     }
102
103
104     /**
105      * Creates a <code>JMXConnector</code> for the given settings
106      * and exposes the associated <code>MBeanServerConnection</code>.
107      */

108     public void afterPropertiesSet() throws IOException JavaDoc {
109         if (this.serviceUrl == null) {
110             throw new IllegalArgumentException JavaDoc("serviceUrl is required");
111         }
112
113         if (this.connectOnStartup) {
114             connect();
115         }
116         else {
117             createLazyConnection();
118         }
119     }
120
121     /**
122      * Connects to the remote <code>MBeanServer</code> using the configured service URL and
123      * environment properties.
124      */

125     private void connect() throws IOException JavaDoc {
126         this.connector = JMXConnectorFactory.connect(this.serviceUrl, this.environment);
127         this.connection = this.connector.getMBeanServerConnection();
128     }
129
130     /**
131      * Creates lazy proxies for the <code>JMXConnector</code> and <code>MBeanServerConnection</code>
132      */

133     private void createLazyConnection() {
134         this.connectorTargetSource = new JMXConnectorLazyInitTargetSource();
135         TargetSource connectionTargetSource = new MBeanServerConnectionLazyInitTargetSource();
136
137         this.connector = (JMXConnector JavaDoc)
138                 new ProxyFactory(JMXConnector JavaDoc.class, this.connectorTargetSource).getProxy(this.beanClassLoader);
139         this.connection = (MBeanServerConnection JavaDoc)
140                 new ProxyFactory(MBeanServerConnection JavaDoc.class, connectionTargetSource).getProxy(this.beanClassLoader);
141     }
142
143
144     public Object JavaDoc getObject() {
145         return this.connection;
146     }
147
148     public Class JavaDoc getObjectType() {
149         return (this.connection != null ? this.connection.getClass() : MBeanServerConnection JavaDoc.class);
150     }
151
152     public boolean isSingleton() {
153         return true;
154     }
155
156
157     /**
158      * Closes the underlying <code>JMXConnector</code>.
159      */

160     public void destroy() throws IOException JavaDoc {
161         if (this.connectorTargetSource == null || this.connectorTargetSource.isInitialized()) {
162             this.connector.close();
163         }
164     }
165
166
167     /**
168      * Lazily creates a <code>JMXConnector</code> using the configured service URL
169      * and environment properties.
170      * @see MBeanServerConnectionFactoryBean#setServiceUrl(String)
171      * @see MBeanServerConnectionFactoryBean#setEnvironment(java.util.Properties)
172      */

173     private class JMXConnectorLazyInitTargetSource extends AbstractLazyCreationTargetSource {
174
175         protected Object JavaDoc createObject() throws Exception JavaDoc {
176             return JMXConnectorFactory.connect(serviceUrl, environment);
177         }
178
179         public Class JavaDoc getTargetClass() {
180             return JMXConnector JavaDoc.class;
181         }
182     }
183
184
185     /**
186      * Lazily creates an <code>MBeanServerConnection</code>.
187      */

188     private class MBeanServerConnectionLazyInitTargetSource extends AbstractLazyCreationTargetSource {
189
190         protected Object JavaDoc createObject() throws Exception JavaDoc {
191             return connector.getMBeanServerConnection();
192         }
193
194         public Class JavaDoc getTargetClass() {
195             return MBeanServerConnection JavaDoc.class;
196         }
197     }
198
199 }
200
Popular Tags