package org.jboss.management.j2ee;
import org.jboss.logging.Logger;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import java.net.URL;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class EJBModule
extends J2EEModule
implements EJBModuleMBean
{
private static final String[] eventTypes = {NotificationConstants.OBJECT_CREATED,
NotificationConstants.OBJECT_DELETED};
private static Logger log = Logger.getLogger(EJBModule.class);
private List mEJBs = new ArrayList();
private ObjectName moduleServiceName;
private String mJBossDD;
private String mJAWSDD;
private String mCMPDD;
private static final Map fakeJ2EEApps = new HashMap();
public static ObjectName create(MBeanServer mbeanServer,
String earName,
String jarName,
URL pURL,
ObjectName moduleServiceName)
{
String lDD = null;
String lJBossDD = null;
String lJAWSDD = null;
String lCMPDD = null;
ObjectName lParent = null;
ObjectName lCreated = null;
ObjectName jsr77Name = null;
ObjectName j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
try
{
Hashtable props = j2eeServerName.getKeyPropertyList();
String j2eeServer = props.get(J2EEManagedObject.TYPE) + "=" +
props.get("name");
if (earName == null)
{
lParent = j2eeServerName;
}
else
{
ObjectName lApplicationQuery = new ObjectName(J2EEDomain.getDomainName() + ":" +
J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEApplication + "," +
"name=" + earName + "," +
j2eeServer + "," +
"*");
Set parentApps = mbeanServer.queryNames(lApplicationQuery, null);
if (parentApps.isEmpty())
{
lCreated = J2EEApplication.create(mbeanServer,
earName,
null);
lParent = lCreated;
} else if (parentApps.size() == 1)
{
lParent = (ObjectName) parentApps.iterator().next();
} }
lDD = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.EJB);
lJBossDD = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.JBOSS);
lJAWSDD = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.JAWS);
lCMPDD = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.CMP);
}
catch (Exception e)
{
log.debug("Could not create JSR-77 EJBModule: " + jarName, e);
return null;
}
try
{
String[] jvms = (String[]) mbeanServer.getAttribute(j2eeServerName,
"javaVMs");
EJBModule ejbModule = new EJBModule(jarName, lParent,
jvms,
lDD,
moduleServiceName,
lJBossDD,
lJAWSDD,
lCMPDD);
jsr77Name = ejbModule.getObjectName();
mbeanServer.registerMBean(ejbModule, jsr77Name);
if (lCreated != null)
{
fakeJ2EEApps.put(jsr77Name, lCreated);
}
log.debug("Created JSR-77 EJBModule: " + jsr77Name);
}
catch (Exception e)
{
log.error("Could not create JSR-77 EJBModule: " + jarName, e);
}
return jsr77Name;
}
public static void destroy(MBeanServer mbeanServer, ObjectName jsr77Name)
{
try
{
log.debug("destroy(), remove EJB-Module: " + jsr77Name);
mbeanServer.unregisterMBean(jsr77Name);
ObjectName jsr77ParentName = (ObjectName) fakeJ2EEApps.get(jsr77Name);
if (jsr77ParentName != null)
{
log.debug("Remove fake JSR-77 parent Application: " + jsr77ParentName);
J2EEApplication.destroy(mbeanServer, jsr77ParentName);
}
}
catch (Exception e)
{
log.debug("Could not destroy JSR-77 EJBModule: " + jsr77Name, e);
}
}
public EJBModule(String jarName,
ObjectName jsr77ParentName,
String[] pJVMs,
String pDeploymentDescriptor,
ObjectName moduleServiceName,
String pJBossDD,
String pJAWSDD,
String pCMPDD)
throws
MalformedObjectNameException,
InvalidParentException
{
super(J2EETypeConstants.EJBModule, jarName, jsr77ParentName, pJVMs, pDeploymentDescriptor);
this.moduleServiceName = moduleServiceName;
mJBossDD = (pJBossDD == null ? "" : pJBossDD);
mJAWSDD = (pJAWSDD == null ? "" : pJAWSDD);
mCMPDD = (pCMPDD == null ? "" : pCMPDD);
}
public String[] getejbs()
{
return (String[]) mEJBs.toArray(new String[mEJBs.size()]);
}
public String getejb(int pIndex)
{
if (pIndex >= 0 && pIndex < mEJBs.size())
{
return (String) mEJBs.get(pIndex);
}
else
{
return null;
}
}
public String getjbossDeploymentDescriptor()
{
return mJBossDD;
}
public String getjawsDeploymentDescriptor()
{
return mJAWSDD;
}
public String getcmpDeploymentDescriptor()
{
return mCMPDD;
}
public void addChild(ObjectName pChild)
{
String lType = J2EEManagedObject.getType(pChild);
if (J2EETypeConstants.EntityBean.equals(lType) ||
J2EETypeConstants.StatelessSessionBean.equals(lType) ||
J2EETypeConstants.StatefulSessionBean.equals(lType) ||
J2EETypeConstants.MessageDrivenBean.equals(lType)
)
{
mEJBs.add(pChild.getCanonicalName());
}
}
public void removeChild(ObjectName pChild)
{
String lType = J2EEManagedObject.getType(pChild);
if (J2EETypeConstants.EntityBean.equals(lType) ||
J2EETypeConstants.StatelessSessionBean.equals(lType) ||
J2EETypeConstants.StatefulSessionBean.equals(lType) ||
J2EETypeConstants.MessageDrivenBean.equals(lType)
)
{
mEJBs.remove(pChild.getCanonicalName());
}
}
public void postCreation()
{
sendNotification(NotificationConstants.OBJECT_CREATED, "EJB Module created");
}
public void preDestruction()
{
sendNotification(NotificationConstants.OBJECT_DELETED, "EJB Module destroyed");
}
public String[] getEventTypes()
{
return eventTypes;
}
public String getEventType(int index)
{
String type = null;
if (index >= 0 && index < eventTypes.length)
{
type = eventTypes[index];
}
return type;
}
public String toString()
{
return "EJBModule[ " + super.toString() +
", EJBs: " + mEJBs +
", JBoss-DD: " + mJBossDD +
", JAWS-DD: " + mJAWSDD +
", CMP-2.0-DD: " + mCMPDD +
" ]";
}
protected Hashtable getParentKeys(ObjectName jsr77ParentName)
{
Hashtable parentKeys = new Hashtable();
Hashtable parentProps = jsr77ParentName.getKeyPropertyList();
String parentName = (String) parentProps.get("name");
String j2eeType = (String) parentProps.get(J2EEManagedObject.TYPE);
if (j2eeType.equals(J2EETypeConstants.J2EEApplication) == false)
{
parentKeys.put(J2EETypeConstants.J2EEServer, parentName);
parentKeys.put(J2EETypeConstants.J2EEApplication, "null");
}
else
{
parentKeys.put(J2EETypeConstants.J2EEApplication, parentName);
String j2eeServerName = (String) parentProps.get(J2EETypeConstants.J2EEServer);
parentKeys.put(J2EETypeConstants.J2EEServer, j2eeServerName);
}
return parentKeys;
}
}