KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > management > ActiveManagementCoordinator


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.c3p0.management;
25
26 import java.lang.management.*;
27 import javax.management.*;
28 import com.mchange.v2.log.*;
29 import com.mchange.v2.c3p0.*;
30
31 public class ActiveManagementCoordinator implements ManagementCoordinator
32 {
33     private final static String JavaDoc C3P0_REGISTRY_NAME = "com.mchange.v2.c3p0:type=C3P0Registry";
34     
35     //MT: thread-safe
36
final static MLogger logger = MLog.getLogger( ActiveManagementCoordinator.class );
37
38     MBeanServer mbs;
39
40     public ActiveManagementCoordinator() throws Exception JavaDoc
41     {
42         this.mbs = ManagementFactory.getPlatformMBeanServer();
43     }
44
45     public void attemptManageC3P0Registry()
46     {
47         try
48         {
49             ObjectName name = new ObjectName(C3P0_REGISTRY_NAME );
50             C3P0RegistryManager mbean = new C3P0RegistryManager();
51
52             if (mbs.isRegistered(name))
53             {
54                 if (logger.isLoggable(MLevel.WARNING))
55                 {
56                     logger.warning("A C3P0Registry mbean is already registered. " +
57                                     "This probably means that an application using c3p0 was undeployed, " +
58                                     "but not all PooledDataSources were closed prior to undeployment. " +
59                                     "This may lead to resource leaks over time. Please take care to close " +
60                                     "all PooledDataSources.");
61                 }
62                 mbs.unregisterMBean(name);
63             }
64             mbs.registerMBean(mbean, name);
65         }
66         catch (Exception JavaDoc e)
67         {
68             if ( logger.isLoggable( MLevel.WARNING ) )
69                 logger.log( MLevel.WARNING,
70                         "Failed to set up C3P0RegistryManager mBean. " +
71                         "[c3p0 will still function normally, but management via JMX may not be possible.]",
72                         e);
73         }
74     }
75
76     public void attemptUnmanageC3P0Registry()
77     {
78         try
79         {
80             ObjectName name = new ObjectName(C3P0_REGISTRY_NAME );
81             if (mbs.isRegistered(name))
82             {
83                 mbs.unregisterMBean(name);
84                 if (logger.isLoggable(MLevel.FINER))
85                     logger.log(MLevel.FINER, "C3P0Registry mbean unregistered.");
86             }
87             else if (logger.isLoggable(MLevel.FINE))
88                 logger.fine("The C3P0Registry mbean was not found in the registry, so could not be unregistered.");
89         }
90         catch (Exception JavaDoc e)
91         {
92             if ( logger.isLoggable( MLevel.WARNING ) )
93                 logger.log( MLevel.WARNING,
94                         "An Exception occurred while trying to unregister the C3P0RegistryManager mBean." +
95                         e);
96         }
97     }
98     
99     public void attemptManagePooledDataSource(PooledDataSource pds)
100     {
101         String JavaDoc name = getPdsObjectNameStr( pds );
102         try
103         {
104             //PooledDataSourceManager mbean = new PooledDataSourceManager( pds );
105
//mbs.registerMBean(mbean, ObjectName.getInstance(name));
106
//if (logger.isLoggable(MLevel.FINER))
107
// logger.log(MLevel.FINER, "MBean: " + name + " registered.");
108

109             // DynamicPooledDataSourceManagerMBean registers itself on construction (and logs its own registration)
110
DynamicPooledDataSourceManagerMBean mbean = new DynamicPooledDataSourceManagerMBean( pds, name, mbs );
111         }
112         catch (Exception JavaDoc e)
113         {
114             if ( logger.isLoggable( MLevel.WARNING ) )
115                 logger.log( MLevel.WARNING,
116                         "Failed to set up a PooledDataSourceManager mBean. [" + name + "] " +
117                         "[c3p0 will still functioning normally, but management via JMX may not be possible.]",
118                         e);
119         }
120     }
121    
122     
123     public void attemptUnmanagePooledDataSource(PooledDataSource pds)
124     {
125         String JavaDoc nameStr = getPdsObjectNameStr( pds );
126         try
127         {
128             ObjectName name = new ObjectName( nameStr );
129             if (mbs.isRegistered(name))
130             {
131                 mbs.unregisterMBean(name);
132                 if (logger.isLoggable(MLevel.FINER))
133                     logger.log(MLevel.FINER, "MBean: " + nameStr + " unregistered.");
134             }
135             else
136                 if (logger.isLoggable(MLevel.FINE))
137                     logger.fine("The mbean " + nameStr + " was not found in the registry, so could not be unregistered.");
138         }
139         catch (Exception JavaDoc e)
140         {
141             if ( logger.isLoggable( MLevel.WARNING ) )
142                 logger.log( MLevel.WARNING,
143                         "An Exception occurred while unregistering mBean. [" + nameStr + "] " +
144                         e);
145         }
146     }
147     
148     private String JavaDoc getPdsObjectNameStr(PooledDataSource pds)
149     { return "com.mchange.v2.c3p0:type=PooledDataSource[" + pds.getIdentityToken() + "]"; }
150 }
151
152
Popular Tags