package org.jboss.management.j2ee;
import org.jboss.logging.Logger;
import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.util.Hashtable;
import java.util.Set;
public class JDBCDataSource
extends J2EEManagedObject
implements JDBCDataSourceMBean
{
private static Logger log = Logger.getLogger(JDBCDataSource.class);
private StateManagement mState;
private ObjectName mService;
private ObjectName mJdbcDriver;
public static ObjectName create(MBeanServer pServer, String pName, ObjectName pService)
{
ObjectName lServer = null;
try
{
lServer = (ObjectName) pServer.queryNames(new ObjectName(J2EEDomain.getDomainName() + ":" +
J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEServer + "," +
"*"),
null).iterator().next();
}
catch (Exception e)
{
log.error("Could not locate JSR-77 Server: " + pName, e);
return null;
}
ObjectName lJDBC = null;
try
{
Set lNames = pServer.queryNames(new ObjectName(J2EEDomain.getDomainName() + ":" +
J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JDBCResource + "," +
"*"),
null);
if (lNames.isEmpty())
{
lJDBC = JDBCResource.create(pServer, "JDBC");
}
else
{
lJDBC = (ObjectName) lNames.iterator().next();
}
}
catch (Exception e)
{
log.error("Could not create JSR-77 JDBC Manager", e);
return null;
}
try
{
pName = pName.replace(':', '~');
return pServer.createMBean("org.jboss.management.j2ee.JDBCDataSource",
null,
new Object[]{
pName,
lJDBC,
pService
},
new String[]{
String.class.getName(),
ObjectName.class.getName(),
ObjectName.class.getName()
}).getObjectName();
}
catch (Exception e)
{
log.error("Could not create JSR-77 JDBC DataSource: " + pName, e);
return null;
}
}
public static void destroy(MBeanServer pServer, String pName)
{
try
{
J2EEManagedObject.removeObject(pServer,
J2EEDomain.getDomainName() + ":" +
J2EEManagedObject.TYPE + "=" + J2EETypeConstants.JDBCDataSource + "," +
"name=" + pName + "," +
"*");
JDBCResource.destroy(pServer, "JDBC");
}
catch (Exception e)
{
log.error("Could not destroy JSR-77 JDBC DataSource: " + pName, e);
}
}
public JDBCDataSource(String pName, ObjectName pServer, ObjectName pService)
throws
MalformedObjectNameException,
InvalidParentException
{
super(J2EETypeConstants.JDBCDataSource, pName, pServer);
mService = pService;
mState = new StateManagement(this);
}
public String[] getEventTypes()
{
return StateManagement.stateTypes;
}
public String getEventType(int pIndex)
{
if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length)
{
return StateManagement.stateTypes[pIndex];
}
else
{
return null;
}
}
public long getStartTime()
{
return mState.getStartTime();
}
public int getState()
{
return mState.getState();
}
public void mejbStart()
{
try
{
getServer().invoke(mService,
"start",
new Object[]{},
new String[]{});
}
catch (Exception e)
{
getLog().error("start failed", e);
}
}
public void mejbStartRecursive()
{
mejbStart();
}
public void mejbStop()
{
try
{
getServer().invoke(mService,
"stop",
new Object[]{},
new String[]{});
}
catch (Exception e)
{
getLog().error("Stop of JDBCDataSource failed", e);
}
}
public void postCreation()
{
try
{
getServer().addNotificationListener(mService, mState, null, null);
}
catch (JMException jme)
{
getLog().debug("Could not add listener at target service", jme);
}
sendNotification(NotificationConstants.OBJECT_CREATED, "JDBC DataSource Resource deleted");
}
public void preDestruction()
{
sendNotification(NotificationConstants.OBJECT_DELETED, "JDBC DataSource Resource deleted");
try
{
getServer().removeNotificationListener(mService, mState);
}
catch (JMException jme)
{
}
}
public ObjectName getJdbcDriver()
{
return mJdbcDriver;
}
public String toString()
{
return "JDBCDatasource { " + super.toString() + " } [ " +
" ]";
}
protected Hashtable getParentKeys(ObjectName pParent)
{
Hashtable lReturn = new Hashtable();
Hashtable lProperties = pParent.getKeyPropertyList();
lReturn.put(J2EETypeConstants.JDBCResource, lProperties.get("name"));
lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get(J2EETypeConstants.J2EEServer));
return lReturn;
}
}