1 22 package org.jboss.management.j2ee; 23 24 import org.jboss.logging.Logger; 25 26 import javax.management.JMException ; 27 import javax.management.MBeanServer ; 28 import javax.management.MalformedObjectNameException ; 29 import javax.management.ObjectName ; 30 import java.util.Hashtable ; 31 import java.util.Set ; 32 33 40 public class JDBCDataSource extends J2EEManagedObject 41 implements JDBCDataSourceMBean 42 { 43 private static Logger log = Logger.getLogger(JDBCDataSource.class); 45 46 48 private StateManagement mState; 49 private ObjectName mService; 50 private ObjectName mJdbcDriver; 51 52 54 public static ObjectName create(MBeanServer pServer, String pName, ObjectName pService) 55 { 56 ObjectName lServer = null; 57 try 58 { 59 lServer = (ObjectName ) pServer.queryNames(new ObjectName (J2EEDomain.getDomainName() + ":" + 60 J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEServer + "," + 61 "*"), 62 null).iterator().next(); 63 } 64 catch (Exception e) 65 { 66 log.error("Could not locate JSR-77 Server: " + pName, e); 67 return null; 69 } 70 ObjectName lJDBC = null; 72 try 73 { 74 Set lNames = pServer.queryNames(new ObjectName (J2EEDomain.getDomainName() + ":" + 76 J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JDBCResource + "," + 77 "*"), 78 null); 79 if (lNames.isEmpty()) 80 { 81 lJDBC = JDBCResource.create(pServer, "JDBC"); 83 } 84 else 85 { 86 lJDBC = (ObjectName ) lNames.iterator().next(); 87 } 88 } 89 catch (Exception e) 90 { 91 log.error("Could not create JSR-77 JDBC Manager", e); 92 return null; 94 } 95 96 try 97 { 98 pName = pName.replace(':', '~'); 101 return pServer.createMBean("org.jboss.management.j2ee.JDBCDataSource", 103 null, 104 new Object []{ 105 pName, 106 lJDBC, 107 pService 108 }, 109 new String []{ 110 String .class.getName(), 111 ObjectName .class.getName(), 112 ObjectName .class.getName() 113 }).getObjectName(); 114 } 115 catch (Exception e) 116 { 117 log.error("Could not create JSR-77 JDBC DataSource: " + pName, e); 118 return null; 119 } 120 } 121 122 public static void destroy(MBeanServer pServer, String pName) 123 { 124 try 125 { 126 J2EEManagedObject.removeObject(pServer, 127 J2EEDomain.getDomainName() + ":" + 128 J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JDBCDataSource + "," + 129 "name=" + pName + "," + 130 "*"); 131 JDBCResource.destroy(pServer, "JDBC"); 133 } 134 catch (Exception e) 135 { 136 log.error("Could not destroy JSR-77 JDBC DataSource: " + pName, e); 137 } 138 } 139 140 142 147 public JDBCDataSource(String pName, ObjectName pServer, ObjectName pService) 148 throws 149 MalformedObjectNameException , 150 InvalidParentException 151 { 152 super(J2EETypeConstants.JDBCDataSource, pName, pServer); 153 mService = pService; 154 mState = new StateManagement(this); 155 } 156 157 159 161 public String [] getEventTypes() 162 { 163 return StateManagement.stateTypes; 164 } 165 166 public String getEventType(int pIndex) 167 { 168 if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length) 169 { 170 return StateManagement.stateTypes[pIndex]; 171 } 172 else 173 { 174 return null; 175 } 176 } 177 178 180 public long getStartTime() 181 { 182 return mState.getStartTime(); 183 } 184 185 public int getState() 186 { 187 return mState.getState(); 188 } 189 public String getStateString() 190 { 191 return mState.getStateString(); 192 } 193 194 public void mejbStart() 195 { 196 try 197 { 198 getServer().invoke(mService, 199 "start", 200 new Object []{}, 201 new String []{}); 202 } 203 catch (Exception e) 204 { 205 getLog().error("start failed", e); 206 } 207 } 208 209 public void mejbStartRecursive() 210 { 211 mejbStart(); 212 } 213 214 public void mejbStop() 215 { 216 try 217 { 218 getServer().invoke(mService, 219 "stop", 220 new Object []{}, 221 new String []{}); 222 } 223 catch (Exception e) 224 { 225 getLog().error("Stop of JDBCDataSource failed", e); 226 } 227 } 228 229 public void postCreation() 230 { 231 try 232 { 233 getServer().addNotificationListener(mService, mState, null, null); 234 } 235 catch (JMException jme) 236 { 237 getLog().debug("Could not add listener at target service", jme); 238 } 239 sendNotification(NotificationConstants.OBJECT_CREATED, "JDBC DataSource Resource deleted"); 240 } 241 242 public void preDestruction() 243 { 244 sendNotification(NotificationConstants.OBJECT_DELETED, "JDBC DataSource Resource deleted"); 245 try 247 { 248 getServer().removeNotificationListener(mService, mState); 249 } 250 catch (JMException jme) 251 { 252 } 254 } 255 256 258 public ObjectName getJdbcDriver() 259 { 260 return mJdbcDriver; 261 } 262 263 265 public String toString() 266 { 267 return "JDBCDatasource { " + super.toString() + " } [ " + 268 " ]"; 269 } 270 271 273 275 278 protected Hashtable getParentKeys(ObjectName pParent) 279 { 280 Hashtable lReturn = new Hashtable (); 281 Hashtable lProperties = pParent.getKeyPropertyList(); 282 lReturn.put(J2EETypeConstants.JDBCResource, lProperties.get("name")); 283 lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get(J2EETypeConstants.J2EEServer)); 286 287 return lReturn; 288 } 289 290 292 } 294 | Popular Tags |