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.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class WebModule
extends J2EEModule
implements WebModuleMBean
{
private static final String[] eventTypes = {NotificationConstants.OBJECT_CREATED,
NotificationConstants.OBJECT_DELETED};
private static Logger log = Logger.getLogger(WebModule.class);
private List servletNames = new ArrayList();
private String jbossWebDD;
private static final Map fakeJ2EEApps = new HashMap();
public static ObjectName create(MBeanServer mbeanServer,
String earName,
String warName,
URL pURL,
ObjectName webContainerName)
{
String webXml = null;
String jbossWebXml = null;
ObjectName jsr77ParentName = null;
ObjectName lCreated = null;
ObjectName j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
ObjectName jsr77Name = null;
try
{
Hashtable props = j2eeServerName.getKeyPropertyList();
String j2eeServer = props.get(J2EEManagedObject.TYPE) + "=" +
props.get("name");
if (earName == null)
{
jsr77ParentName = j2eeServerName;
}
else
{
ObjectName lApplicationQuery = new ObjectName(J2EEDomain.getDomainName() + ":" +
J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEApplication + "," +
"name=" + earName + "," +
j2eeServer + "," +
"*");
Set lApplications = mbeanServer.queryNames(lApplicationQuery, null);
if (lApplications.isEmpty())
{
lCreated = J2EEApplication.create(mbeanServer,
earName,
null);
jsr77ParentName = lCreated;
} else if (lApplications.size() == 1)
{
jsr77ParentName = (ObjectName) lApplications.iterator().next();
} }
webXml = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.WEB);
jbossWebXml = J2EEDeployedObject.getDeploymentDescriptor(pURL,
J2EEDeployedObject.JBOSS_WEB);
}
catch (Exception e)
{
log.error("Could not create JSR-77 WebModule: " + warName, e);
return null;
}
try
{
String[] jvms = (String[]) mbeanServer.getAttribute(j2eeServerName,
"javaVMs");
WebModule webModule = new WebModule(warName, jsr77ParentName, jvms, webXml,
webContainerName, jbossWebXml);
jsr77Name = webModule.getObjectName();
mbeanServer.registerMBean(webModule, jsr77Name);
if (lCreated != null)
{
fakeJ2EEApps.put(jsr77Name, lCreated);
} log.debug("Created JSR-77 WebModule: " + jsr77Name);
}
catch (Exception e)
{
log.debug("Could not create JSR-77 WebModule: " + warName, e);
return null;
}
return jsr77Name;
}
public static void destroy(MBeanServer mbeanServer, ObjectName jsr77Name)
{
try
{
mbeanServer.unregisterMBean(jsr77Name);
log.debug("Remove JSR-77 WebModule: " + 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 WebModule: " + jsr77Name, e);
}
}
public WebModule(String warName, ObjectName j2eeAppName, String[] jvms,
String webDD, ObjectName webContainerName, String jbossWebDD)
throws MalformedObjectNameException,
InvalidParentException
{
super(J2EETypeConstants.WebModule, warName, j2eeAppName, jvms, webDD);
this.jbossWebDD = (jbossWebDD == null ? "" : jbossWebDD);
}
public String[] getservlets()
{
String[] servlets = new String[servletNames.size()];
servletNames.toArray(servlets);
return servlets;
}
public String getservlet(int pIndex)
{
if (pIndex >= 0 && pIndex < servletNames.size())
{
return (String) servletNames.get(pIndex);
}
else
{
return null;
}
}
public String getjbossWebDeploymentDescriptor()
{
return jbossWebDD;
}
public void addChild(ObjectName pChild)
{
String lType = J2EEManagedObject.getType(pChild);
if (J2EETypeConstants.Servlet.equals(lType)
)
{
servletNames.add(pChild.getCanonicalName());
}
}
public void removeChild(ObjectName pChild)
{
String lType = J2EEManagedObject.getType(pChild);
if (J2EETypeConstants.Servlet.equals(lType))
{
servletNames.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, "Web module created");
}
public void preDestruction()
{
sendNotification(NotificationConstants.OBJECT_DELETED, "Web module destroyed");
}
public String toString()
{
return "WebModule[ " + super.toString() +
", Servlets: " + servletNames +
", JBoss-Web-DD: " + jbossWebDD +
" ]";
}
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;
}
}