KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > stream > ConnectionPoolMBeanProxyFactory


1 // $Id: MultithreadedServerMBeanProxyFactory.java 1134 2007-04-05 17:44:43Z grro $
2
/*
3  * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
20  * The latest copy of this software may be found on http://www.xsocket.org/
21  */

22 package org.xsocket.stream;
23
24 import java.lang.management.ManagementFactory JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import javax.management.JMException JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.xsocket.ILifeCycle;
33 import org.xsocket.IntrospectionBasedDynamicBean;
34
35
36
37 /**
38  * A Mbean proxy factory, which creates and registers an appropriated mbean
39  * for a given connction pool instance.
40  *
41  * <br><br><b>This class is for test purpose only, and will be modified or discarded in future versions</b>
42  *
43  * @author grro@xsocket.org
44  */

45 final class ConnectionPoolMBeanProxyFactory {
46
47     
48     /**
49      * creates and registers a mbean for the given connection pool on the platform MBeanServer
50      *
51      * @param pool the pool to register
52      * @throws JMException if an jmx exception occurs
53      */

54     public static void createAndRegister(BlockingConnectionPool pool) throws JMException JavaDoc {
55         createAndRegister(pool, "org.xsocket.stream");
56     }
57
58     /**
59      * creates and registers a mbean for the given connection pool on the platform MBeanServer
60      *
61      * @param pool the pool to register
62      * @throws JMException if an jmx exception occurs
63      */

64     public static void createAndRegister(NonBlockingConnectionPool pool) throws JMException JavaDoc {
65         createAndRegister(pool, "org.xsocket.stream");
66     }
67     
68     /**
69      * creates and registers a mbean for the given connection pool on the platform MBeanServer
70      * under the given domain name
71      *
72      * @param pool the pool to register
73      * @param domain the domain name to use
74      * @throws JMException if an jmx exception occurs
75      */

76     public static void createAndRegister(BlockingConnectionPool pool, String JavaDoc domain) throws JMException JavaDoc {
77         createAndRegister(ManagementFactory.getPlatformMBeanServer(), pool, domain);
78     }
79     
80
81     /**
82      * creates and registers a mbean for the given connection pool on the platform MBeanServer
83      * under the given domain name
84      *
85      * @param pool the pool to register
86      * @param domain the domain name to use
87      * @throws JMException if an jmx exception occurs
88      */

89     public static void createAndRegister(NonBlockingConnectionPool pool, String JavaDoc domain) throws JMException JavaDoc {
90         createAndRegister(ManagementFactory.getPlatformMBeanServer(), pool, domain);
91     }
92     
93     
94     /**
95      * creates and registers a mbean for the given pool on the given MBeanServer
96      * under the given domain name
97      *
98      * @param mbeanServer the mbean server to use
99      * @param pool the pool to register
100      * @param domain the domain name to use
101      * @throws JMException if an jmx exception occurs
102      */

103     public static void createAndRegister(MBeanServer JavaDoc mbeanServer, BlockingConnectionPool pool, String JavaDoc domain) throws JMException JavaDoc {
104         ObjectName JavaDoc objectName = new ObjectName JavaDoc(domain + ":type=BlockingConnectionPool,name=" + pool.hashCode());
105         ManagementFactory.getPlatformMBeanServer().registerMBean(new IntrospectionBasedDynamicBean(pool), objectName);
106         
107         new BlockingConnectionPoolListener(pool, domain);
108     }
109     
110     
111     /**
112      * creates and registers a mbean for the given pool on the given MBeanServer
113      * under the given domain name
114      *
115      * @param mbeanServer the mbean server to use
116      * @param pool the pool to register
117      * @param domain the domain name to use
118      * @throws JMException if an jmx exception occurs
119      */

120     public static void createAndRegister(MBeanServer JavaDoc mbeanServer, NonBlockingConnectionPool pool, String JavaDoc domain) throws JMException JavaDoc {
121         ObjectName JavaDoc objectName = new ObjectName JavaDoc(domain + ":type=NonBlockingConnectionPool,name=" + pool.hashCode());
122         ManagementFactory.getPlatformMBeanServer().registerMBean(new IntrospectionBasedDynamicBean(pool), objectName);
123         
124         new NonBlockingConnectionPoolListener(pool, domain);
125     }
126
127     private static void unregister(BlockingConnectionPool pool, String JavaDoc domain) throws JMException JavaDoc {
128         ObjectName JavaDoc objectName = new ObjectName JavaDoc(domain + ":type=BlockingConnectionPool,name=" + pool.hashCode());
129         ManagementFactory.getPlatformMBeanServer().unregisterMBean(objectName);
130     }
131     
132
133     private static void unregister(NonBlockingConnectionPool pool, String JavaDoc domain) throws JMException JavaDoc {
134         ObjectName JavaDoc objectName = new ObjectName JavaDoc(domain + ":type=NonBlockingConnectionPool,name=" + pool.hashCode());
135         ManagementFactory.getPlatformMBeanServer().unregisterMBean(objectName);
136     }
137
138     
139     private static final class BlockingConnectionPoolListener implements ILifeCycle {
140         
141         private static final Logger JavaDoc LOG = Logger.getLogger(BlockingConnectionPoolListener.class.getName());
142         
143         private BlockingConnectionPool pool = null;
144         private String JavaDoc domain = null;
145         
146         BlockingConnectionPoolListener(BlockingConnectionPool pool, String JavaDoc domain) {
147             this.pool = pool;
148             this.domain = domain;
149             
150             pool.addListener(this);
151         }
152         
153         public void onInit() {
154         }
155         
156         public void onDestroy() {
157             try {
158                 unregister(pool, domain);
159             } catch (Exception JavaDoc ex) {
160                 if (LOG.isLoggable(Level.FINE)) {
161                     LOG.fine("error occured by deregistering the pool (domain=" + domain + "). reason: " + ex.toString());
162                 }
163             }
164         }
165     }
166     
167     private static final class NonBlockingConnectionPoolListener implements ILifeCycle {
168         
169         private static final Logger JavaDoc LOG = Logger.getLogger(BlockingConnectionPoolListener.class.getName());
170         
171         private NonBlockingConnectionPool pool = null;
172         private String JavaDoc domain = null;
173         
174         NonBlockingConnectionPoolListener(NonBlockingConnectionPool pool, String JavaDoc domain) {
175             this.pool = pool;
176             this.domain = domain;
177             
178             pool.addListener(this);
179         }
180         
181         public void onInit() {
182         }
183         
184         public void onDestroy() {
185             try {
186                 unregister(pool, domain);
187             } catch (Exception JavaDoc ex) {
188                 if (LOG.isLoggable(Level.FINE)) {
189                     LOG.fine("error occured by deregistering the pool (domain=" + domain + "). reason: " + ex.toString());
190                 }
191             }
192         }
193     }
194
195 }
196
Popular Tags