KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jmx > ConnectorServerFactoryBean


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.jmx;
18
19 import java.util.Map JavaDoc;
20
21 import javax.management.MBeanServer JavaDoc;
22 import javax.management.MalformedObjectNameException JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.springframework.beans.factory.DisposableBean;
27 import org.springframework.beans.factory.FactoryBean;
28 import org.springframework.beans.factory.InitializingBean;
29 import org.springframework.core.Constants;
30 import org.springframework.jmx.support.MBeanRegistrationSupport;
31
32 /**
33  * <code>FactoryBean</code> that creates a JSR-160 <code>JMXConnectorServer</code>,
34  * optionally registers it with the <code>MBeanServer</code> and then starts it.
35  *
36  * <p>The <code>JMXConnectorServer</code> can be started in a separate thread by setting the
37  * <code>threaded</code> property to <code>true</code>. You can configure this thread to be a
38  * daemon thread by setting the <code>daemon</code> property to <code>true</code>.
39  *
40  * This xbean-enabled factory is a wrapper on top of the existing Spring
41  * factory bean. It also logs the serviceUrl when starting.
42  *
43  * @author gnodet
44  * @org.apache.xbean.XBean element="jmxConnector"
45  */

46 public class ConnectorServerFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
47
48     private Log log = LogFactory.getLog(ConnectorServerFactoryBean.class);
49     private org.springframework.jmx.support.ConnectorServerFactoryBean csfb = new org.springframework.jmx.support.ConnectorServerFactoryBean();
50     private String JavaDoc serviceUrl = org.springframework.jmx.support.ConnectorServerFactoryBean.DEFAULT_SERVICE_URL;
51     private boolean daemon = false;
52     private boolean threaded = false;
53     private Map JavaDoc environment;
54     private String JavaDoc objectName;
55     private int registrationBehavior;
56     private MBeanServer JavaDoc server;
57     private static final Constants constants = new Constants(MBeanRegistrationSupport.class);
58     
59
60     /**
61      * Set whether any threads started for the <code>JMXConnectorServer</code> should be
62      * started as daemon threads.
63      * @param daemon
64      * @see org.springframework.jmx.support.ConnectorServerFactoryBean#setDaemon(boolean)
65      */

66     public void setDaemon(boolean daemon) {
67         this.daemon = daemon;
68     }
69
70     /**
71      * Set the environment properties used to construct the <code>JMXConnector</code>
72      * as a <code>Map</code> of String keys and arbitrary Object values.
73      * @param environment
74      * @see org.springframework.jmx.support.ConnectorServerFactoryBean#setEnvironmentMap(java.util.Map)
75      */

76     public void setEnvironment(Map JavaDoc environment) {
77         this.environment = environment;
78     }
79
80     /**
81      * Set the <code>ObjectName</code> used to register the <code>JMXConnectorServer</code>
82      * itself with the <code>MBeanServer</code>.
83      * @param objectName
84      * @throws MalformedObjectNameException if the <code>ObjectName</code> is malformed
85      * @see org.springframework.jmx.support.ConnectorServerFactoryBean#setObjectName(java.lang.String)
86      */

87     public void setObjectName(String JavaDoc objectName) throws MalformedObjectNameException JavaDoc {
88         this.objectName = objectName;
89     }
90
91     /**
92      * Specify what action should be taken when attempting to register an MBean
93      * under an {@link javax.management.ObjectName} that already exists.
94      * <p>Default is REGISTRATION_FAIL_ON_EXISTING.
95      * @see #setRegistrationBehaviorName(String)
96      * @see #REGISTRATION_FAIL_ON_EXISTING
97      * @see #REGISTRATION_IGNORE_EXISTING
98      * @see #REGISTRATION_REPLACE_EXISTING
99      * @param registrationBehavior
100      * @see org.springframework.jmx.support.MBeanRegistrationSupport#setRegistrationBehavior(int)
101      */

102     public void setRegistrationBehavior(int registrationBehavior) {
103         this.registrationBehavior = registrationBehavior;
104     }
105
106     /**
107      * Set the registration behavior by the name of the corresponding constant,
108      * e.g. "REGISTRATION_IGNORE_EXISTING".
109      * @see #setRegistrationBehavior
110      * @see #REGISTRATION_FAIL_ON_EXISTING
111      * @see #REGISTRATION_IGNORE_EXISTING
112      * @see #REGISTRATION_REPLACE_EXISTING
113      * @param registrationBehavior
114      * @see org.springframework.jmx.support.MBeanRegistrationSupport#setRegistrationBehaviorName(java.lang.String)
115      */

116     public void setRegistrationBehaviorName(String JavaDoc registrationBehavior) {
117         setRegistrationBehavior(constants.asNumber(registrationBehavior).intValue());
118     }
119
120     /**
121      * Specify the <code>MBeanServer</code> instance with which all beans should
122      * be registered. The <code>MBeanExporter</code> will attempt to locate an
123      * existing <code>MBeanServer</code> if none is supplied.
124      * @param server
125      * @see org.springframework.jmx.support.MBeanRegistrationSupport#setServer(javax.management.MBeanServer)
126      */

127     public void setServer(MBeanServer JavaDoc server) {
128         this.server = server;
129     }
130
131     /**
132      * Set the service URL for the <code>JMXConnectorServer</code>.
133      * @param serviceUrl
134      * @see org.springframework.jmx.support.ConnectorServerFactoryBean#setServiceUrl(java.lang.String)
135      */

136     public void setServiceUrl(String JavaDoc serviceUrl) {
137         this.serviceUrl = serviceUrl;
138     }
139
140     /**
141      * Set whether the <code>JMXConnectorServer</code> should be started in a separate thread.
142      * @param threaded
143      * @see org.springframework.jmx.support.ConnectorServerFactoryBean#setThreaded(boolean)
144      */

145     public void setThreaded(boolean threaded) {
146         csfb.setThreaded(threaded);
147     }
148
149     public Object JavaDoc getObject() throws Exception JavaDoc {
150         return csfb.getObject();
151     }
152
153     public Class JavaDoc getObjectType() {
154         return csfb.getObjectType();
155     }
156
157     public boolean isSingleton() {
158         return csfb.isSingleton();
159     }
160
161     public void afterPropertiesSet() throws Exception JavaDoc {
162         csfb = new org.springframework.jmx.support.ConnectorServerFactoryBean();
163         csfb.setDaemon(daemon);
164         csfb.setThreaded(threaded);
165         csfb.setRegistrationBehavior(registrationBehavior);
166         csfb.setEnvironmentMap(environment);
167         csfb.setObjectName(objectName);
168         csfb.setServiceUrl(serviceUrl);
169         csfb.setServer(server);
170         csfb.afterPropertiesSet();
171         log.info("JMX connector available at: " + serviceUrl);
172     }
173
174     public void destroy() throws Exception JavaDoc {
175         if (csfb != null) {
176             try {
177                 csfb.destroy();
178             } finally {
179                 csfb = null;
180             }
181         }
182     }
183
184 }
185
Popular Tags