KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.MBeanServer JavaDoc;
20 import javax.management.MBeanServerFactory JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.springframework.beans.factory.DisposableBean;
26 import org.springframework.beans.factory.FactoryBean;
27 import org.springframework.beans.factory.InitializingBean;
28 import org.springframework.jmx.MBeanServerNotFoundException;
29
30 /**
31  * FactoryBean that obtains an {@link javax.management.MBeanServer} reference
32  * through the standard JMX 1.2 {@link javax.management.MBeanServerFactory}
33  * API (which is available on JDK 1.5 or as part of a JMX 1.2 provider).
34  * Exposes the <code>MBeanServer</code> for bean references.
35  *
36  * <p>By default, <code>MBeanServerFactoryBean</code> will always create
37  * a new <code>MBeanServer</code> even if one is already running. To have
38  * the <code>MBeanServerFactoryBean</code> attempt to locate a running
39  * <code>MBeanServer</code> first, set the value of the
40  * "locateExistingServerIfPossible" property to "true".
41  *
42  * @author Rob Harrop
43  * @author Juergen Hoeller
44  * @since 1.2
45  * @see #setLocateExistingServerIfPossible
46  * @see #locateMBeanServer
47  * @see javax.management.MBeanServer
48  * @see javax.management.MBeanServerFactory#findMBeanServer
49  * @see javax.management.MBeanServerFactory#createMBeanServer
50  * @see javax.management.MBeanServerFactory#newMBeanServer
51  * @see MBeanServerConnectionFactoryBean
52  * @see ConnectorServerFactoryBean
53  */

54 public class MBeanServerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
55
56     protected final Log logger = LogFactory.getLog(getClass());
57
58     private boolean locateExistingServerIfPossible = false;
59
60     private String JavaDoc agentId;
61
62     private String JavaDoc defaultDomain;
63
64     private boolean registerWithFactory = true;
65
66     private MBeanServer JavaDoc server;
67
68     private boolean newlyRegistered = false;
69
70
71     /**
72      * Set whether or not the <code>MBeanServerFactoryBean</code> should attempt
73      * to locate a running <code>MBeanServer</code> before creating one.
74      * <p>Default is <code>false</code>.
75      */

76     public void setLocateExistingServerIfPossible(boolean locateExistingServerIfPossible) {
77         this.locateExistingServerIfPossible = locateExistingServerIfPossible;
78     }
79
80     /**
81      * Set the agent id of the <code>MBeanServer</code> to locate.
82      * <p>Default is none. If specified, this will result in an
83      * automatic attempt being made to locate the attendant MBeanServer,
84      * and (importantly) if said MBeanServer cannot be located no
85      * attempt will be made to create a new MBeanServer (and an
86      * MBeanServerNotFoundException will be thrown at resolution time).
87      * @see javax.management.MBeanServerFactory#findMBeanServer(String)
88      */

89     public void setAgentId(String JavaDoc agentId) {
90         this.agentId = agentId;
91     }
92
93     /**
94      * Set the default domain to be used by the <code>MBeanServer</code>,
95      * to be passed to <code>MBeanServerFactory.createMBeanServer()</code>
96      * or <code>MBeanServerFactory.findMBeanServer()<code>.
97      * <p>Default is none.
98      * @see javax.management.MBeanServerFactory#createMBeanServer(String)
99      * @see javax.management.MBeanServerFactory#findMBeanServer(String)
100      */

101     public void setDefaultDomain(String JavaDoc defaultDomain) {
102         this.defaultDomain = defaultDomain;
103     }
104
105     /**
106      * Set whether to register the <code>MBeanServer</code> with the
107      * <code>MBeanServerFactory</code>, making it available through
108      * <code>MBeanServerFactory.findMBeanServer()<code>.
109      * @see javax.management.MBeanServerFactory#createMBeanServer
110      * @see javax.management.MBeanServerFactory#findMBeanServer
111      */

112     public void setRegisterWithFactory(boolean registerWithFactory) {
113         this.registerWithFactory = registerWithFactory;
114     }
115
116
117     /**
118      * Creates the <code>MBeanServer</code> instance.
119      */

120     public void afterPropertiesSet() throws MBeanServerNotFoundException {
121         // Try to locate existing MBeanServer, if desired.
122
if (this.locateExistingServerIfPossible || this.agentId != null) {
123             try {
124                 this.server = locateMBeanServer(this.agentId);
125             }
126             catch (MBeanServerNotFoundException ex) {
127                 // If agentId was specified, we were only supposed to locate that
128
// specific MBeanServer; so let's bail if we can't find it.
129
if (this.agentId != null) {
130                     throw ex;
131                 }
132                 logger.info("No existing MBeanServer found - creating new one");
133             }
134         }
135
136         // Create a new MBeanServer and register it, if desired.
137
if (this.server == null) {
138             this.server = createMBeanServer(this.defaultDomain, this.registerWithFactory);
139             this.newlyRegistered = this.registerWithFactory;
140         }
141     }
142
143     /**
144      * Attempt to locate an existing <code>MBeanServer</code>.
145      * Called if <code>locateExistingServerIfPossible</code> is set to <code>true</code>.
146      * <p>The default implementation attempts to find an <code>MBeanServer</code> using
147      * a standard lookup. Subclasses may override to add additional location logic.
148      * @param agentId the agent identifier of the MBeanServer to retrieve.
149      * If this parameter is <code>null</code>, all registered MBeanServers are
150      * considered.
151      * @return the <code>MBeanServer</code> if found
152      * @throws org.springframework.jmx.MBeanServerNotFoundException
153      * if no <code>MBeanServer</code> could be found
154      * @see #setLocateExistingServerIfPossible
155      * @see JmxUtils#locateMBeanServer(String)
156      * @see javax.management.MBeanServerFactory#findMBeanServer(String)
157      */

158     protected MBeanServer JavaDoc locateMBeanServer(String JavaDoc agentId) throws MBeanServerNotFoundException {
159         return JmxUtils.locateMBeanServer(agentId);
160     }
161
162     /**
163      * Create a new <code>MBeanServer</code> instance and register it with the
164      * <code>MBeanServerFactory</code>, if desired.
165      * @param defaultDomain the default domain, or <code>null</code> if none
166      * @param registerWithFactory whether to register the <code>MBeanServer</code>
167      * with the <code>MBeanServerFactory</code>
168      * @see javax.management.MBeanServerFactory#createMBeanServer
169      * @see javax.management.MBeanServerFactory#newMBeanServer
170      */

171     protected MBeanServer JavaDoc createMBeanServer(String JavaDoc defaultDomain, boolean registerWithFactory) {
172         if (registerWithFactory) {
173             return MBeanServerFactory.createMBeanServer(defaultDomain);
174         }
175         else {
176             return MBeanServerFactory.newMBeanServer(defaultDomain);
177         }
178     }
179
180
181     public Object JavaDoc getObject() {
182         return this.server;
183     }
184
185     public Class JavaDoc getObjectType() {
186         return (this.server != null ? this.server.getClass() : MBeanServer JavaDoc.class);
187     }
188
189     public boolean isSingleton() {
190         return true;
191     }
192
193
194     /**
195      * Unregisters the <code>MBeanServer</code> instance, if necessary.
196      */

197     public void destroy() {
198         if (this.newlyRegistered) {
199             MBeanServerFactory.releaseMBeanServer(this.server);
200         }
201     }
202
203 }
204
Popular Tags