1 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 C3P0_REGISTRY_NAME = "com.mchange.v2.c3p0:type=C3P0Registry"; 34 35 final static MLogger logger = MLog.getLogger( ActiveManagementCoordinator.class ); 37 38 MBeanServer mbs; 39 40 public ActiveManagementCoordinator() throws Exception 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 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 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 name = getPdsObjectNameStr( pds ); 102 try 103 { 104 109 DynamicPooledDataSourceManagerMBean mbean = new DynamicPooledDataSourceManagerMBean( pds, name, mbs ); 111 } 112 catch (Exception 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 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 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 getPdsObjectNameStr(PooledDataSource pds) 149 { return "com.mchange.v2.c3p0:type=PooledDataSource[" + pds.getIdentityToken() + "]"; } 150 } 151 152 | Popular Tags |