1 22 package org.jboss.management.j2ee; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Set ; 28 29 import javax.management.JMException ; 30 import javax.management.MBeanServer ; 31 import javax.management.MalformedObjectNameException ; 32 import javax.management.ObjectName ; 33 34 import org.jboss.logging.Logger; 35 import org.jboss.system.ServiceMBean; 36 37 49 public class JDBCResource extends J2EEResource 50 implements JDBCResourceMBean 51 { 52 private static Logger log = Logger.getLogger(JDBCResource.class); 54 55 57 private StateManagement mState; 58 private ObjectName mService; 59 60 private List mDataSources = new ArrayList (); 62 63 65 public static ObjectName create(MBeanServer mbeanServer, String resName) 66 { 67 ObjectName lServer = null; 68 try 69 { 70 lServer = (ObjectName ) mbeanServer.queryNames(new ObjectName (J2EEDomain.getDomainName() + ":" + 71 J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEServer + "," + 72 "*"), 73 null).iterator().next(); 74 } 75 catch (Exception e) 76 { 77 log.error("Could not find parent J2EEServer", e); 78 return null; 79 } 80 try 81 { 82 JDBCResource jdbcRes = new JDBCResource(resName, lServer); 83 ObjectName jsr77Name = jdbcRes.getObjectName(); 84 mbeanServer.registerMBean(jdbcRes, jsr77Name); 85 log.debug("Created JSR-77 JDBC Manager: " + resName); 86 87 return jsr77Name; 88 } 89 catch (Exception e) 90 { 91 log.error("Could not create JSR-77 JDBC Manager", e); 92 return null; 93 } 94 } 95 96 public static void destroy(MBeanServer mbeanServer, String resName) 97 { 98 try 99 { 100 ObjectName lSearch = new ObjectName (J2EEDomain.getDomainName() + ":" + 102 J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JDBCResource + "," + 103 "name=" + resName + "," + 104 "*"); 105 Set lNames = mbeanServer.queryNames(lSearch, 106 null); 107 if (!lNames.isEmpty()) 108 { 109 ObjectName lJDBCResource = (ObjectName ) lNames.iterator().next(); 110 String [] lDataSources = (String []) mbeanServer.getAttribute(lJDBCResource, 112 "jdbcDataSources"); 113 if (lDataSources.length == 0) 114 { 115 mbeanServer.unregisterMBean(lJDBCResource); 117 } 118 } 119 } 120 catch (Exception e) 121 { 122 log.error("Could not destroy JSR-77 JDBC Manager", e); 123 } 124 } 125 126 128 131 public JDBCResource(String resName, ObjectName pServer) 132 throws 133 MalformedObjectNameException , 134 InvalidParentException 135 { 136 super(J2EETypeConstants.JDBCResource, resName, pServer); 137 mState = new StateManagement(this); 138 } 139 140 142 144 public String [] getEventTypes() 145 { 146 return StateManagement.stateTypes; 147 } 148 149 public String getEventType(int pIndex) 150 { 151 if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length) 152 { 153 return StateManagement.stateTypes[pIndex]; 154 } 155 else 156 { 157 return null; 158 } 159 } 160 161 163 public long getStartTime() 164 { 165 return mState.getStartTime(); 166 } 167 168 public int getState() 169 { 170 return mState.getState(); 171 } 172 public String getStateString() 173 { 174 return mState.getStateString(); 175 } 176 177 public void mejbStart() 178 { 179 mState.setState(ServiceMBean.STARTING + 2); 181 mState.setState(ServiceMBean.STARTED + 2); 182 } 183 184 public void mejbStartRecursive() 185 { 186 mState.setState(ServiceMBean.STOPPING + 2); 187 Iterator i = mDataSources.iterator(); 188 String lDataSource = null; 189 while (i.hasNext()) 190 { 191 lDataSource = (String ) i.next(); 192 try 193 { 194 getServer().invoke(newObjectName(lDataSource), 195 "mejbStart", 196 new Object []{}, 197 new String []{}); 198 } 199 catch (JMException jme) 200 { 201 getLog().error("Could not start JSR-77 JDBC-DataSource: " + lDataSource, jme); 202 } 203 } 204 mState.setState(ServiceMBean.STOPPED + 2); 205 } 206 207 public void mejbStop() 208 { 209 mState.setState(3); 211 Iterator i = mDataSources.iterator(); 212 while (i.hasNext()) 213 { 214 String lDataSource = (String ) i.next(); 215 try 216 { 217 getServer().invoke(newObjectName(lDataSource), 218 "mejbStop", 219 new Object []{}, 220 new String []{}); 221 } 222 catch (JMException jme) 223 { 224 getLog().error("Could not stop JSR-77 JDBC-DataSource: " + lDataSource, jme); 225 } 226 } 227 mState.setState(2); 229 } 230 231 236 public void postCreation() 237 { 238 sendNotification(NotificationConstants.OBJECT_DELETED, "JDBC Resource Resource deleted"); 239 } 240 241 246 public void preDestruction() 247 { 248 sendNotification(NotificationConstants.OBJECT_CREATED, "JDBC Resource Resource created"); 249 } 250 251 253 256 public String [] getjdbcDataSources() 257 { 258 return (String []) mDataSources.toArray(new String [mDataSources.size()]); 259 } 260 261 264 public String getjdbcDataSource(int pIndex) 265 { 266 if (pIndex >= 0 && pIndex < mDataSources.size()) 267 { 268 return (String ) mDataSources.get(pIndex); 269 } 270 else 271 { 272 return null; 273 } 274 } 275 276 278 public void addChild(ObjectName pChild) 279 { 280 String lType = J2EEManagedObject.getType(pChild); 281 if (J2EETypeConstants.JDBCDataSource.equals(lType)) 282 { 283 mDataSources.add(pChild.getCanonicalName()); 284 } 285 } 286 287 public void removeChild(ObjectName pChild) 288 { 289 String lType = J2EEManagedObject.getType(pChild); 290 if (J2EETypeConstants.JDBCDataSource.equals(lType)) 291 { 292 mDataSources.remove(pChild.getCanonicalName()); 293 } 294 } 295 296 298 public String toString() 299 { 300 return "JDBCResource { " + super.toString() + " } [ " + 301 "Datasources: " + mDataSources + 302 " ]"; 303 } 304 305 307 309 311 } 313 | Popular Tags |