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.net.URL;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
public class J2EEApplication
extends J2EEDeployedObject
implements J2EEApplicationMBean
{
private static Logger log = Logger.getLogger(J2EEApplication.class);
private List moduleNames = new ArrayList();
public static ObjectName create(MBeanServer mbeanServer, String earName, URL url)
{
String lDD = null;
ObjectName jsr77Name = null;
ObjectName j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
lDD = J2EEDeployedObject.getDeploymentDescriptor(url, J2EEDeployedObject.APPLICATION);
try
{
J2EEApplication j2eeApp = new J2EEApplication(earName, j2eeServerName, lDD);
jsr77Name = j2eeApp.getObjectName();
if (mbeanServer.isRegistered(jsr77Name) == true)
{
String[] tmpModules = (String[]) mbeanServer.getAttribute(jsr77Name, "modules");
mbeanServer.unregisterMBean(jsr77Name);
mbeanServer.registerMBean(j2eeApp, jsr77Name);
if (tmpModules != null)
{
for (int m = 0; m < tmpModules.length; m++)
j2eeApp.addChild(newObjectName(tmpModules[m]));
}
}
else
{
mbeanServer.registerMBean(j2eeApp, jsr77Name);
}
log.debug("Created JSR-77 J2EEApplication: " + earName);
}
catch (Exception e)
{
log.debug("Could not create JSR-77 J2EEApplication: " + jsr77Name, e);
}
return jsr77Name;
}
public static void destroy(MBeanServer mbeanServer, ObjectName jsr77Name)
{
try
{
mbeanServer.unregisterMBean(jsr77Name);
log.debug("Destroyed JSR-77 J2EEApplication: " + jsr77Name);
}
catch (javax.management.InstanceNotFoundException infe)
{
}
catch (Exception e)
{
log.debug("Could not destroy JSR-77 J2EEApplication: " + jsr77Name, e);
}
}
public J2EEApplication(String name, ObjectName mbeanServer, String pDeploymentDescriptor)
throws MalformedObjectNameException, InvalidParentException
{
super(J2EETypeConstants.J2EEApplication, name, mbeanServer, pDeploymentDescriptor);
}
public String[] getmodules()
{
return (String[]) moduleNames.toArray(new String[moduleNames.size()]);
}
public String getmodule(int pIndex)
{
if (pIndex >= 0 && pIndex < moduleNames.size())
{
return (String) moduleNames.get(pIndex);
}
return null;
}
public void addChild(ObjectName pChild)
{
String lType = J2EEManagedObject.getType(pChild);
if (
J2EETypeConstants.EJBModule.equals(lType) ||
J2EETypeConstants.WebModule.equals(lType) ||
J2EETypeConstants.ResourceAdapterModule.equals(lType) ||
J2EETypeConstants.ServiceModule.equals(lType)
)
{
moduleNames.add(pChild.getCanonicalName());
try
{
server.invoke(newObjectName(getparent()),
"addChild",
new Object[]{pChild},
new String[]{ObjectName.class.getName()});
}
catch (JMException jme)
{
}
}
}
public void removeChild(ObjectName pChild)
{
String lType = J2EEManagedObject.getType(pChild);
if (
J2EETypeConstants.EJBModule.equals(lType) ||
J2EETypeConstants.WebModule.equals(lType) ||
J2EETypeConstants.ResourceAdapterModule.equals(lType) ||
J2EETypeConstants.ServiceModule.equals(lType)
)
{
moduleNames.remove(pChild.getCanonicalName());
try
{
server.invoke(newObjectName(getparent()),
"removeChild",
new Object[]{pChild},
new String[]{ObjectName.class.getName()});
}
catch (JMException jme)
{
}
}
}
public String toString()
{
return "J2EEApplication { " + super.toString() + " } [ " +
"modules: " + moduleNames +
" ]";
}
protected Hashtable getParentKeys(ObjectName pParent)
{
Hashtable lReturn = new Hashtable();
Hashtable lProperties = pParent.getKeyPropertyList();
lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get("name"));
return lReturn;
}
}