1 22 package org.jboss.management.j2ee; 23 24 import org.jboss.logging.Logger; 25 import org.jboss.management.j2ee.statistics.JCAConnectionPoolStatsImpl; 26 import org.jboss.management.j2ee.statistics.JCAStatsImpl; 27 28 import javax.management.MBeanServer ; 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.ObjectName ; 31 import javax.management.j2ee.statistics.Stats ; 32 import java.util.ArrayList ; 33 import java.util.Hashtable ; 34 import java.util.List ; 35 import java.util.Set ; 36 37 45 public class JCAResource extends J2EEResource 46 implements JCAResourceMBean 47 { 48 private static Logger log = Logger.getLogger(JCAResource.class); 50 51 53 private List connectionFactories = new ArrayList (); 55 56 private ObjectName cmServiceName; 57 private ObjectName poolServiceName; 58 private JCAStatsImpl stats; 59 60 62 73 public static ObjectName create(MBeanServer mbeanServer, String resName, 74 ObjectName jsr77RAParentName, ObjectName cmServiceName, 75 ObjectName mcfServiceName, ObjectName poolServiceName) 76 { 77 ObjectName jsr77Name = null; 78 try 79 { 80 JCAResource jcaRes = new JCAResource(resName, jsr77RAParentName, 81 cmServiceName, poolServiceName); 82 jsr77Name = jcaRes.getObjectName(); 83 mbeanServer.registerMBean(jcaRes, jsr77Name); 84 log.debug("Created JSR-77 JCAResource: " + resName); 85 86 ObjectName jcaFactoryName = JCAConnectionFactory.create(mbeanServer, 88 resName, jsr77Name, cmServiceName, mcfServiceName); 89 } 90 catch (Exception e) 91 { 92 log.debug("Could not create JSR-77 JCAResource: " + resName, e); 93 } 94 return jsr77Name; 95 } 96 97 public static void destroy(MBeanServer mbeanServer, String resName) 98 { 99 try 100 { 101 ObjectName search = new ObjectName (J2EEDomain.getDomainName() + ":" + 103 J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JCAResource + "," + 104 "name=" + resName + "," + 105 "*"); 106 Set resNames = mbeanServer.queryNames(search, null); 107 if( resNames.isEmpty() == false ) 108 { 109 ObjectName jcaRes = (ObjectName ) resNames.iterator().next(); 110 String [] factories = (String []) mbeanServer.getAttribute(jcaRes, 112 "connectionFactories"); 113 for(int n = 0; n < factories.length; n ++) 114 { 115 ObjectName cf = new ObjectName (factories[n]); 117 mbeanServer.unregisterMBean(cf); 118 121 Hashtable props = cf.getKeyPropertyList(); 122 props.put("j2eeType", J2EETypeConstants.JCAManagedConnectionFactory); 123 props.remove(J2EETypeConstants.JCAResource); 124 ObjectName mcf = new ObjectName (cf.getDomain(), props); 125 mbeanServer.unregisterMBean(mcf); 126 } 127 mbeanServer.unregisterMBean(jcaRes); 128 } 129 } 130 catch (Exception e) 131 { 132 log.debug("Could not destroy JSR-77 JCAResource", e); 133 } 134 } 135 136 138 146 public JCAResource(String resName, ObjectName jsr77ParentName, 147 ObjectName cmServiceName, ObjectName poolServiceName) 148 throws MalformedObjectNameException , InvalidParentException 149 { 150 super(J2EETypeConstants.JCAResource, resName, jsr77ParentName); 151 this.cmServiceName = cmServiceName; 152 this.poolServiceName = poolServiceName; 153 } 154 155 157 159 162 public String [] getconnectionFactories() 163 { 164 return (String []) connectionFactories.toArray(new String [connectionFactories.size()]); 165 } 166 167 170 public String getconnectionFactory(int i) 171 { 172 if (i >= 0 && i < connectionFactories.size()) 173 { 174 return (String ) connectionFactories.get(i); 175 } 176 else 177 { 178 return null; 179 } 180 } 181 182 184 public void addChild(ObjectName pChild) 185 { 186 String lType = J2EEManagedObject.getType(pChild); 187 if (J2EETypeConstants.JCAConnectionFactory.equals(lType)) 188 { 189 connectionFactories.add(pChild.getCanonicalName()); 190 } 191 } 192 193 public void removeChild(ObjectName pChild) 194 { 195 String lType = J2EEManagedObject.getType(pChild); 196 if (J2EETypeConstants.JCAConnectionFactory.equals(lType)) 197 { 198 connectionFactories.remove(pChild.getCanonicalName()); 199 } 200 } 201 202 204 210 public Stats getstats() 211 { 212 try 213 { 214 ObjectName jsr77CFName = newObjectName(getconnectionFactory(0)); 215 Object [] params = {poolServiceName}; 216 String [] sig = {ObjectName .class.getName()}; 217 JCAConnectionPoolStatsImpl cfStats = (JCAConnectionPoolStatsImpl) 218 server.invoke(jsr77CFName, "getPoolStats", params, sig); 219 JCAConnectionPoolStatsImpl[] poolStats = {cfStats}; 220 stats = new JCAStatsImpl(null, poolStats); 221 } 222 catch (Exception e) 223 { 224 log.debug("Failed to create JCAStats", e); 225 } 226 return stats; 227 } 228 229 234 public void resetStats() 235 { 236 if (stats != null) 237 stats.reset(); 238 } 239 241 243 public String toString() 244 { 245 return "JCAResource { " + super.toString() + " } [ " + 246 "Connection Factories: " + connectionFactories + 247 " ]"; 248 } 249 250 253 protected Hashtable getParentKeys(ObjectName parentName) 254 { 255 Hashtable keys = new Hashtable (); 256 Hashtable nameProps = parentName.getKeyPropertyList(); 257 String adapterName = (String ) nameProps.get("name"); 258 String serverName = (String ) nameProps.get(J2EETypeConstants.J2EEServer); 259 keys.put(J2EETypeConstants.J2EEServer, serverName); 260 keys.put(J2EETypeConstants.ResourceAdapter, adapterName); 261 return keys; 262 } 263 } 264 | Popular Tags |