package org.jboss.management.j2ee;
import java.security.InvalidParameterException;
import java.util.Hashtable;
import java.util.Set;
import javax.management.InstanceNotFoundException;
import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.Notification;
import javax.management.ObjectName;
import org.jboss.logging.Logger;
import org.jboss.management.j2ee.statistics.StatisticsProvider;
import org.jboss.mx.util.ObjectNameConverter;
import org.jboss.system.ServiceMBeanSupport;
public abstract class J2EEManagedObject
extends ServiceMBeanSupport
implements J2EEManagedObjectMBean
{
public static final String TYPE = "j2eeType";
public static final String NAME = "name";
private static Logger classLog = Logger.getLogger(J2EEManagedObject.class);
private ObjectName parentName = null;
private ObjectName name = null;
protected static String getType(String pName)
{
String lType = null;
if (pName != null)
{
ObjectName oname = newObjectName(pName);
lType = (String) oname.getKeyPropertyList().get(TYPE);
}
return lType == null ? "" : lType;
}
protected static String getType(ObjectName pName)
{
String lType = null;
if (pName != null)
{
lType = (String) pName.getKeyPropertyList().get(TYPE);
}
return lType == null ? "" : lType;
}
protected static ObjectName newObjectName(String pName)
{
try
{
return new ObjectName(pName);
}
catch (MalformedObjectNameException e)
{
throw new IllegalArgumentException("Invalid object name: " + pName);
}
}
protected static ObjectName removeObject(MBeanServer pServer, String pSearchCriteria)
throws JMException
{
ObjectName lSearch = ObjectNameConverter.convert(pSearchCriteria);
classLog.debug("removeObject(), search for: " + pSearchCriteria +
", search criteria: " + lSearch);
Set lNames = pServer.queryNames(lSearch, null);
if (!lNames.isEmpty())
{
ObjectName lName = (ObjectName) lNames.iterator().next();
pServer.unregisterMBean(lName);
return lName;
}
return null;
}
protected static ObjectName removeObject(MBeanServer pServer, String pName, String pSearchCriteria)
throws JMException
{
String lEncryptedName = ObjectNameConverter.convertCharacters(pName, true);
ObjectName lSearch = new ObjectName(pSearchCriteria + "," + NAME + "=" + lEncryptedName);
classLog.debug("removeObject(), name: " + pName +
", encrypted name: " + lEncryptedName +
", search criteria: " + lSearch);
Set lNames = pServer.queryNames(lSearch, null);
if (!lNames.isEmpty())
{
ObjectName lName = (ObjectName) lNames.iterator().next();
pServer.unregisterMBean(lName);
return lName;
}
return null;
}
public J2EEManagedObject(String domainName, String j2eeType, String resName)
throws MalformedObjectNameException
{
if (domainName == null)
{
throw new InvalidParameterException("Domain Name must be set");
}
Hashtable lProperties = new Hashtable();
lProperties.put(TYPE, j2eeType);
lProperties.put(NAME, resName);
name = ObjectNameConverter.convert(domainName, lProperties);
log.debug("ctor, name: " + name);
}
public J2EEManagedObject(String j2eeType, String resName, ObjectName jsr77ParentName)
throws MalformedObjectNameException,
InvalidParentException
{
Hashtable lProperties = getParentKeys(jsr77ParentName);
lProperties.put(TYPE, j2eeType);
lProperties.put(NAME, resName);
name = ObjectNameConverter.convert(J2EEDomain.getDomainName(), lProperties);
setparent(jsr77ParentName.getCanonicalName());
}
public ObjectName getObjectName()
{
return name;
}
public String getobjectName()
{
return name.getCanonicalName();
}
public String getparent()
{
return parentName.getCanonicalName();
}
public void setparent(String pParent)
throws InvalidParentException
{
if (pParent == null)
{
throw new InvalidParameterException("Parent must be set");
}
parentName = newObjectName(pParent);
}
public void addChild(ObjectName pChild)
{
}
public void removeChild(ObjectName pChild)
{
}
public boolean isstateManageable()
{
return this instanceof StateManageable;
}
public boolean isstatisticsProvider()
{
return this instanceof StatisticsProvider;
}
public boolean iseventProvider()
{
return this instanceof EventProvider;
}
public ObjectName getObjectName(MBeanServer pServer, ObjectName pName)
{
return getObjectName();
}
public final void postRegister(Boolean registrationDone)
{
try
{
log.debug("postRegister(), parent: " + parentName);
if (registrationDone.booleanValue())
{
postCreation();
if (parentName != null)
{
try
{
if (parentName.getKeyProperty("name").compareTo("null") != 0)
{
getServer().invoke(parentName,
"addChild",
new Object[]{name},
new String[]{ObjectName.class.getName()});
}
else
{
ObjectName j2eeServerName = J2EEDomain.getDomainServerName(server);
server.invoke(j2eeServerName,
"addChild",
new Object[]{name},
new String[]{ObjectName.class.getName()});
}
}
catch (JMException e)
{
log.debug("Failed to add child", e);
registrationDone = Boolean.FALSE;
}
}
}
}
catch (RuntimeException re)
{
log.debug("postRegister() caught this exception", re);
throw re;
}
super.postRegister(registrationDone);
}
public final void preDeregister()
throws Exception
{
log.debug("preDeregister(), parent: " + parentName);
if (parentName != null)
{
try
{
server.invoke(parentName,
"removeChild",
new Object[]{name},
new String[]{ObjectName.class.getName()});
}
catch (InstanceNotFoundException infe)
{
}
preDestruction();
}
}
public void sendNotification(String type, String info)
{
Notification msg = new Notification(type, this.getObjectName(),
this.getNextNotificationSequenceNumber(),
System.currentTimeMillis(),
info);
super.sendNotification(msg);
}
public String toString()
{
return "J2EEManagedObject [ name: " + name + ", parent: " + parentName + " ];";
}
protected void postCreation()
{
}
protected void preDestruction()
{
}
protected Hashtable getParentKeys(ObjectName pParent)
{
return new Hashtable();
}
}