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.Hashtable;
import java.util.List;
public class ServiceModule
extends J2EEModule
implements ServiceModuleMBean
{
private static final String[] eventTypes = {NotificationConstants.OBJECT_CREATED,
NotificationConstants.OBJECT_DELETED};
private static Logger log = Logger.getLogger(ServiceModule.class);
private List mbeans = new ArrayList();
public static ObjectName create(MBeanServer mbeanServer, String moduleName, URL url)
{
String lDD = null;
ObjectName jsr77Name = null;
ObjectName j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
lDD = J2EEDeployedObject.getDeploymentDescriptor(url, J2EEDeployedObject.APPLICATION);
try
{
String[] jvms = (String[]) mbeanServer.getAttribute(j2eeServerName, "javaVMs");
ServiceModule serviceModule = new ServiceModule(moduleName, j2eeServerName, jvms, lDD);
jsr77Name = serviceModule.getObjectName();
mbeanServer.registerMBean(serviceModule, jsr77Name);
log.debug("Created JSR-77 ServiceModule, name: " + moduleName);
}
catch (Exception e)
{
log.debug("Could not create JSR-77 ServiceModule: " + moduleName, e);
}
return jsr77Name;
}
public static void destroy(MBeanServer mbeanServer, String pModuleName)
{
try
{
log.debug("destroy(), remove Service Module: " + pModuleName);
if (pModuleName.indexOf(J2EEManagedObject.TYPE + "=" + J2EETypeConstants.ServiceModule) >= 0)
{
J2EEManagedObject.removeObject(mbeanServer, pModuleName);
}
else
{
J2EEManagedObject.removeObject(mbeanServer,
pModuleName,
J2EEDomain.getDomainName() + ":" +
J2EEManagedObject.TYPE + "=" + J2EETypeConstants.ServiceModule +
"," + "*");
}
}
catch (javax.management.InstanceNotFoundException infe)
{
}
catch (Throwable e)
{
log.error("Could not destroy JSR-77 ServiceModule: " + pModuleName, e);
}
}
public ServiceModule(String moduleName, ObjectName j2eeServerName,
String[] jvmNames, String pDeploymentDescriptor)
throws MalformedObjectNameException,
InvalidParentException
{
super(J2EETypeConstants.ServiceModule, moduleName, j2eeServerName, jvmNames, pDeploymentDescriptor);
}
public String[] getMBeans()
{
return (String[]) mbeans.toArray(new String[mbeans.size()]);
}
public String getMBean(int pIndex)
{
if (pIndex >= 0 && pIndex < mbeans.size())
{
return (String) mbeans.get(pIndex);
}
else
{
return null;
}
}
public void addChild(ObjectName pChild)
{
String lType = J2EEManagedObject.getType(pChild);
if (J2EETypeConstants.MBean.equals(lType))
{
mbeans.add(pChild.getCanonicalName());
}
}
public void removeChild(ObjectName pChild)
{
String lType = J2EEManagedObject.getType(pChild);
if (J2EETypeConstants.MBean.equals(lType))
{
mbeans.remove(pChild.getCanonicalName());
}
}
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 void postCreation()
{
sendNotification(NotificationConstants.OBJECT_CREATED, "SAR module created");
}
public void preDestruction()
{
sendNotification(NotificationConstants.OBJECT_DELETED, "SAR module destroyed");
}
public String toString()
{
return "ServiceModule[ " + super.toString() +
"MBeans: " + mbeans +
" ]";
}
protected Hashtable getParentKeys(ObjectName pParent)
{
Hashtable lReturn = new Hashtable();
Hashtable lProperties = pParent.getKeyPropertyList();
lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get("name"));
return lReturn;
}
}