package org.jboss.management.mejb;
import org.jboss.logging.Logger;
import org.jboss.management.j2ee.J2EEDomain;
import org.jboss.mx.util.MBeanServerLocator;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.management.*;
import javax.management.j2ee.ManagementHome;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.rmi.RemoteException;
import java.util.Set;
public class ManagementBean
implements SessionBean
{
private static Logger log = Logger.getLogger(ManagementBean.class);
private SessionContext mContext;
private MBeanServer mbeanServer;
private ObjectName mManagementService;
public Object getAttribute(ObjectName pName, String pAttribute)
throws
MBeanException,
AttributeNotFoundException,
InstanceNotFoundException,
ReflectionException,
RemoteException
{
try
{
if (mManagementService == null)
{
return mbeanServer.getAttribute(pName, pAttribute);
}
else
{
return mbeanServer.invoke(mManagementService,
"getAttribute",
new Object[]{
pName,
pAttribute
},
new String[]{
ObjectName.class.getName(),
String.class.getName()
});
}
}
catch (RuntimeOperationsException e)
{
if (e.getTargetException() instanceof IllegalArgumentException)
{
throw new AttributeNotFoundException("MBean attribute not found: " + pAttribute);
}
throw e;
}
}
public AttributeList getAttributes(ObjectName pName, String[] pAttributes)
throws
InstanceNotFoundException,
ReflectionException,
RemoteException
{
if (mManagementService == null)
{
return mbeanServer.getAttributes(pName, pAttributes);
}
else
{
try
{
return (AttributeList) mbeanServer.invoke(mManagementService,
"getAttributes",
new Object[]{
pName,
pAttributes
},
new String[]{
ObjectName.class.getName(),
String[].class.getName()
});
}
catch (MBeanException me)
{
log.error("getAttributes() got exception from cluster service", me);
return null;
}
}
}
public String getDefaultDomain()
throws RemoteException
{
if (mManagementService == null)
{
return J2EEDomain.getDomainName();
}
else
{
try
{
return (String) mbeanServer.getAttribute(mManagementService,
"DefaultDomain");
}
catch (JMException jme)
{
log.error("getDefaultDomain() got exception from cluster service", jme);
return null;
}
}
}
public Integer getMBeanCount()
throws RemoteException
{
if (mManagementService == null)
{
try
{
Set mbeans = this.queryNames(new ObjectName("*:*"), null);
return new Integer(mbeans.size());
}
catch (Exception e)
{
}
return new Integer(0);
}
else
{
try
{
return (Integer) mbeanServer.invoke(mManagementService,
"getMBeanCount",
new Object[]{},
new String[]{});
}
catch (JMException jme)
{
log.error("getMBeanCount() got exception from cluster service", jme);
return null;
}
}
}
public MBeanInfo getMBeanInfo(ObjectName pName)
throws
IntrospectionException,
InstanceNotFoundException,
ReflectionException,
RemoteException
{
if (mManagementService == null)
{
return mbeanServer.getMBeanInfo(pName);
}
else
{
try
{
return (MBeanInfo) mbeanServer.invoke(mManagementService,
"getMBeanInfo",
new Object[]{
pName
},
new String[]{
ObjectName.class.getName()
});
}
catch (MBeanException me)
{
log.error("getMBeanInfo() got exception from cluster service", me);
return null;
}
}
}
public javax.management.j2ee.ListenerRegistration getListenerRegistry()
throws RemoteException
{
return new ListenerRegistration((ManagementHome) mContext.getEJBObject().getEJBHome(),
new String[]{});
}
public Object invoke(ObjectName pName, String pOperationName, Object[] pParams, String[] pSignature)
throws
InstanceNotFoundException,
MBeanException,
ReflectionException,
RemoteException
{
if (pOperationName.equals("start"))
{
pOperationName = "mejbStart";
}
else if (pOperationName.equals("startRecursive"))
{
pOperationName = "mejbStartRecursive";
}
else if (pOperationName.equals("stop"))
{
pOperationName = "mejbStop";
}
if (mManagementService == null)
{
return mbeanServer.invoke(pName,
pOperationName,
pParams,
pSignature);
}
else
{
return mbeanServer.invoke(mManagementService,
"invoke",
new Object[]{
pName,
pOperationName,
pParams,
pSignature
},
new String[]{
ObjectName.class.getName(),
String.class.getName(),
Object[].class.getName(),
String[].class.getName()
});
}
}
public boolean isRegistered(ObjectName pName)
throws RemoteException
{
if (mManagementService == null)
{
return mbeanServer.isRegistered(pName);
}
else
{
try
{
Boolean lCheck = (Boolean) mbeanServer.invoke(mManagementService,
"isRegistered",
new Object[]{
pName
},
new String[]{
ObjectName.class.getName()
});
if (lCheck != null)
{
return lCheck.booleanValue();
}
}
catch (JMException jme)
{
log.error("isRegistered() got exception from cluster service", jme);
}
return false;
}
}
public Set queryNames(ObjectName pName, QueryExp pQuery)
throws RemoteException
{
if (mManagementService == null)
{
return mbeanServer.queryNames(pName, pQuery);
}
else
{
try
{
return (Set) mbeanServer.invoke(mManagementService,
"queryNames",
new Object[]{
pName,
pQuery
},
new String[]{
ObjectName.class.getName(),
QueryExp.class.getName()
});
}
catch (JMException jme)
{
log.error("queryNames() got exception from cluster service", jme);
return null;
}
}
}
public void setAttribute(ObjectName pName, Attribute pAttribute)
throws
AttributeNotFoundException,
InstanceNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException,
RemoteException
{
if (mManagementService == null)
{
mbeanServer.setAttribute(pName, pAttribute);
}
else
{
mbeanServer.invoke(mManagementService,
"setAttribute",
new Object[]{
pName,
pAttribute
},
new String[]{
ObjectName.class.getName(),
String.class.getName()
});
}
}
public AttributeList setAttributes(ObjectName pName, AttributeList pAttributes)
throws
InstanceNotFoundException,
ReflectionException,
RemoteException
{
if (mManagementService == null)
{
return setAttributes(pName, pAttributes);
}
else
{
try
{
return (AttributeList) mbeanServer.invoke(mManagementService,
"setAttributes",
new Object[]{
pName,
pAttributes
},
new String[]{
ObjectName.class.getName(),
AttributeList.class.getName()
});
}
catch (MBeanException me)
{
log.error("setAttributes() got exception from cluster service", me);
return null;
}
}
}
public ObjectInstance createMBean(String pClass,
ObjectName pName,
Object[] pParameters,
String[] pSignature)
throws
InstanceAlreadyExistsException,
MBeanException,
MBeanRegistrationException,
NotCompliantMBeanException,
ReflectionException,
RemoteException
{
if (mManagementService == null)
{
return mbeanServer.createMBean(pClass, pName, pParameters, pSignature);
}
else
{
try
{
return (ObjectInstance) mbeanServer.invoke(mManagementService,
"createMBean",
new Object[]{
pClass,
pName,
pParameters,
pSignature
},
new String[]{
String.class.getName(),
ObjectName.class.getName(),
Object[].class.getName(),
String[].class.getName()
});
}
catch (InstanceNotFoundException infe)
{
log.error("createMBean() got exception from cluster service", infe);
return null;
}
}
}
public void unregisterMBean(ObjectName pName)
throws
InstanceNotFoundException,
MBeanRegistrationException,
RemoteException
{
if (mManagementService == null)
{
mbeanServer.unregisterMBean(pName);
}
else
{
try
{
mbeanServer.invoke(mManagementService,
"unregisterMBean",
new Object[]{
pName
},
new String[]{
ObjectName.class.getName()
});
}
catch (MBeanException me)
{
log.error("unregisterMBean() got exception from cluster service", me);
}
catch (ReflectionException re)
{
log.error("unregisterMBean() got exception from cluster service", re);
}
}
}
public void addNotificationListener(ObjectName pBroadcaster,
ObjectName pListener,
NotificationFilter pFilter,
Object pHandback)
throws
InstanceNotFoundException,
RemoteException
{
if (mManagementService == null)
{
mbeanServer.addNotificationListener(pBroadcaster, pListener, pFilter, pHandback);
}
else
{
try
{
mbeanServer.invoke(mManagementService,
"addNotificationListener",
new Object[]{
pBroadcaster,
pListener,
pFilter,
pHandback
},
new String[]{
ObjectName.class.getName(),
ObjectName.class.getName(),
NotificationFilter.class.getName(),
Object.class.getName()
});
}
catch (MBeanException me)
{
log.error("addNotificationListener() got exception from cluster service", me);
}
catch (ReflectionException re)
{
log.error("addNotificationListener() got exception from cluster service", re);
}
}
}
public void removeNotificationListener(ObjectName pBroadcaster,
ObjectName pListener)
throws
InstanceNotFoundException,
ListenerNotFoundException,
RemoteException
{
if (mManagementService == null)
{
mbeanServer.removeNotificationListener(pBroadcaster, pListener);
}
else
{
try
{
mbeanServer.invoke(mManagementService,
"removeNotificationListener",
new Object[]{
pBroadcaster,
pListener
},
new String[]{
ObjectName.class.getName(),
ObjectName.class.getName()
});
}
catch (MBeanException me)
{
log.error("removeNotificationListener() got exception from cluster service", me);
}
catch (ReflectionException re)
{
log.error("removeNotificationListener() got exception from cluster service", re);
}
}
}
public void ejbCreate()
throws
CreateException
{
if (mbeanServer == null)
{
try
{
Context jndiCtx = new InitialContext();
String serverName = (String) jndiCtx.lookup("java:comp/env/Server-Name");
serverName = serverName.trim();
if (serverName == null || serverName.length() == 0 || serverName.equals("null"))
{
try
{
mbeanServer = MBeanServerLocator.locateJBoss();
}
catch (IllegalStateException e)
{
throw new CreateException("No local JMX MBeanServer available");
}
}
else
{
Object lServer = jndiCtx.lookup(serverName);
if (lServer != null)
{
if (lServer instanceof MBeanServer)
{
mbeanServer = (MBeanServer) lServer;
}
else
{
throw new CreateException("Server: " + lServer + " reference by Server-Name: " + serverName +
" is not of type MBeanServer");
}
}
else
{
throw new CreateException("Server-Name " + serverName
+ " does not reference an Object in JNDI");
}
}
}
catch (NamingException ne)
{
throw new EJBException(ne);
}
}
try
{
ObjectName haManagement = new ObjectName("jboss:service=HAManagement");
ObjectInstance oi = mbeanServer.getObjectInstance(haManagement);
mManagementService = oi.getObjectName();
}
catch (Exception e)
{
log.debug("ejbCreate() failed to locate jboss:service=HAManagement", e);
}
}
public String toString()
{
return "Management [ " + " ]";
}
public void setSessionContext(SessionContext aContext)
throws
EJBException
{
mContext = aContext;
}
public void ejbActivate()
throws
EJBException
{
}
public void ejbPassivate()
throws
EJBException
{
}
public void ejbRemove()
throws
EJBException
{
}
}