package org.jboss.management.j2ee.cluster;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.InvalidAttributeValueException;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.NotCompliantMBeanException;
import javax.management.NotificationFilter;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.QueryExp;
import javax.management.ReflectionException;
import org.jboss.ha.framework.interfaces.HAPartition;
import org.jboss.management.j2ee.J2EEDomain;
import org.jboss.mx.util.ObjectNameFactory;
import org.jboss.system.ServiceMBeanSupport;
import org.jboss.system.server.ServerConfigUtil;
public class HAManagementService
extends ServiceMBeanSupport
implements HAManagementServiceMBean
{
private final static String SERVICE_NAME = "HAManagementService";
ObjectName OBJECT_NAME = ObjectNameFactory.create("jboss:service=HAManagement");
private ObjectName mHAManagementName;
private ObjectName mClusterPartitionName;
private String mBackgroundPartition = ServerConfigUtil.getDefaultPartitionName();
private HAPartition mPartition;
public HAManagementService()
{
}
public Object _getAttribute(ObjectName pName, String pAttribute)
{
Object lReturn = null;
try
{
lReturn = server.getAttribute(pName, pAttribute);
}
catch (Exception e)
{
}
return lReturn;
}
public Object getAttribute(ObjectName pName, String pAttribute)
throws
MBeanException,
AttributeNotFoundException,
InstanceNotFoundException,
ReflectionException,
RemoteException
{
Object lReturn = null;
try
{
lReturn = server.getAttribute(pName, pAttribute);
}
catch (InstanceNotFoundException infe)
{
Object[] lArguments = new Object[]{
pName,
pAttribute
};
List lValues = null;
try
{
lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
"_getAttribute",
lArguments,
new Class[]{ObjectName.class, String.class},
false
);
}
catch (Exception e)
{
throw new RemoteException("Could not get management attributes on the cluster", e);
}
Iterator i = lValues.iterator();
while (i.hasNext())
{
Object lValue = i.next();
if (lValue != null)
{
lReturn = lValue;
break;
}
}
if (lReturn == null)
{
throw infe;
}
}
return lReturn;
}
public AttributeList _getAttributes(ObjectName pName, String[] pAttributes)
{
AttributeList lReturn = null;
try
{
lReturn = server.getAttributes(pName, pAttributes);
}
catch (Exception e)
{
}
return lReturn;
}
public AttributeList getAttributes(ObjectName pName, String[] pAttributes)
throws
InstanceNotFoundException,
ReflectionException,
RemoteException
{
AttributeList lReturn = null;
try
{
lReturn = server.getAttributes(pName, pAttributes);
}
catch (InstanceNotFoundException infe)
{
Object[] lArguments = new Object[]{
pName,
pAttributes
};
List lValues = null;
try
{
lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
"_getAttributes",
lArguments,
new Class[]{ObjectName.class, String[].class},
false
);
}
catch (Exception e)
{
throw new RemoteException("Could not get management attributes on the cluster", e);
}
Iterator i = lValues.iterator();
while (i.hasNext())
{
Object lValue = i.next();
if (lValue != null)
{
lReturn = (AttributeList) lValue;
break;
}
}
if (lReturn == null)
{
throw infe;
}
}
return lReturn;
}
public String getDefaultDomain()
throws RemoteException
{
return J2EEDomain.getDomainName();
}
public Integer getMBeanCount()
throws RemoteException
{
try
{
return new Integer(queryNames(new ObjectName("*:*"),
null).size());
}
catch (Exception e)
{
}
return new Integer(0);
}
public MBeanInfo getMBeanInfo(ObjectName pName)
throws
IntrospectionException,
InstanceNotFoundException,
ReflectionException,
RemoteException
{
return server.getMBeanInfo(pName);
}
public javax.management.j2ee.ListenerRegistration getListenerRegistry()
throws RemoteException
{
return null;
}
public Object _invoke(ObjectName pName, String pOperationName, Object[] pParams, String[] pSignature)
{
Object lReturn = null;
try
{
log.info("_invoke(), name: " + pName + ", operation: " + pOperationName +
", params: " + pParams + ", signature: " + pSignature);
lReturn = server.invoke(pName,
pOperationName,
pParams,
pSignature);
}
catch (Exception e)
{
lReturn = e;
}
return lReturn;
}
public Object invoke(ObjectName pName, String pOperationName, Object[] pParams, String[] pSignature)
throws
InstanceNotFoundException,
MBeanException,
ReflectionException,
RemoteException
{
Object lReturn = null;
InstanceNotFoundException lException = null;
log.info("invoke(), name: " + pName + ", operation: " + pOperationName +
", params: " + pParams + ", signature: " + pSignature);
try
{
lReturn = server.invoke(pName, pOperationName, pParams, pSignature);
}
catch (InstanceNotFoundException infe)
{
lException = infe;
}
Object[] lArguments = new Object[]{
pName,
pOperationName,
pParams,
pSignature
};
List lValues = null;
try
{
log.info("call _invoke()");
lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
"_invoke",
lArguments,
new Class[]{ObjectName.class, String.class, Object[].class, String[].class},
true
);
}
catch (Exception e)
{
throw new RemoteException("Could not get management attributes on the cluster", e);
}
Iterator i = lValues.iterator();
while (i.hasNext())
{
Object lValue = i.next();
if (lValue instanceof Throwable)
{
log.debug("invoke a method on the cluster caused an exception: " + lValue);
if (lValue instanceof InstanceNotFoundException)
{
continue;
}
}
lException = null;
if (lValue != null)
{
lReturn = lValue;
break;
}
}
if (lException != null)
{
throw lException;
}
return lReturn;
}
public boolean isRegistered(ObjectName pName)
throws RemoteException
{
return server.isRegistered(pName);
}
public Set queryNames(ObjectName pName, QueryExp pQuery)
throws RemoteException
{
return server.queryNames(pName, pQuery);
}
public Object _setAttribute(ObjectName pName, Attribute pAttribute)
{
Object lReturn = null;
try
{
log.info("_setAttribute(), name: " + pName + ", attribute: " + pAttribute);
server.setAttribute(pName, pAttribute);
}
catch (Exception e)
{
lReturn = e;
}
return lReturn;
}
public void setAttribute(ObjectName pName, Attribute pAttribute)
throws
AttributeNotFoundException,
InstanceNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException,
RemoteException
{
InstanceNotFoundException lException = null;
try
{
server.setAttribute(pName, pAttribute);
}
catch (InstanceNotFoundException infe)
{
lException = infe; }
Object[] lArguments = new Object[]{
pName,
pAttribute
};
List lValues = null;
try
{
log.info("call _setAttribute()");
lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
"_setAttribute",
lArguments,
new Class[]{ObjectName.class, Attribute.class},
true
);
}
catch (Exception e)
{
throw new RemoteException("Could not set management attributes on the cluster", e);
}
Iterator i = lValues.iterator();
while (i.hasNext())
{
Object lValue = i.next();
if (lValue instanceof Throwable)
{
log.debug("invoke a method on the cluster caused an exception: " + lValue);
if (lValue instanceof InstanceNotFoundException)
{
if (lException == null)
{
lException = (InstanceNotFoundException) lValue; }
}
else
{
if (lValue instanceof AttributeNotFoundException)
{
throw (AttributeNotFoundException) lValue;
}
if (lValue instanceof InvalidAttributeValueException)
{
throw (InvalidAttributeValueException) lValue;
}
if (lValue instanceof MBeanException)
{
throw (MBeanException) lValue;
}
if (lValue instanceof ReflectionException)
{
throw (ReflectionException) lValue;
}
throw new RemoteException(lValue.toString());
}
}
}
if (lException != null)
{
throw lException; }
}
public Object _setAttributes(ObjectName pName, AttributeList pAttributes)
{
Object lReturn = null;
try
{
log.info("_setAttributes(), name: " + pName + ", attribute: " + pAttributes);
server.setAttributes(pName, pAttributes);
}
catch (Exception e)
{
lReturn = e;
}
return lReturn;
}
public AttributeList setAttributes(ObjectName pName, AttributeList pAttributes)
throws
InstanceNotFoundException,
ReflectionException,
RemoteException
{
Object lReturn = null;
InstanceNotFoundException lException = null;
try
{
lReturn = server.setAttributes(pName, pAttributes);
}
catch (InstanceNotFoundException infe)
{
lException = infe;
}
Object[] lArguments = new Object[]{
pName,
pAttributes
};
List lValues = null;
try
{
log.info("call _setAttributes()");
lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
"_setAttributes",
lArguments,
new Class[]{ObjectName.class, AttributeList.class},
true
);
}
catch (Exception e)
{
throw new RemoteException("Could not set management attributes on the cluster", e);
}
Iterator i = lValues.iterator();
while (i.hasNext())
{
Object lValue = i.next();
if (lValue instanceof Throwable)
{
log.debug("set Attributes on the cluster caused an exception: " + lValue);
if (lValue instanceof InstanceNotFoundException)
{
if (lException == null)
{
lException = (InstanceNotFoundException) lValue; }
}
else
{
if (lValue instanceof ReflectionException)
{
throw (ReflectionException) lValue;
}
throw new RemoteException(lValue.toString());
}
}
}
if (lException != null)
{
throw lException;
}
return (AttributeList) lReturn;
}
public Object _createMBean(String pClass,
ObjectName pName,
Object[] pParameters,
String[] pSignature)
{
Object lReturn = null;
try
{
log.info("_createMBean(), name: " + pName);
lReturn = server.createMBean(pClass, pName, pParameters, pSignature);
}
catch (Exception e)
{
lReturn = e;
}
return lReturn;
}
public ObjectInstance createMBean(String pClass,
ObjectName pName,
Object[] pParameters,
String[] pSignature)
throws
InstanceAlreadyExistsException,
MBeanException,
MBeanRegistrationException,
NotCompliantMBeanException,
ReflectionException,
RemoteException
{
List lValues = null;
Object[] lArguments = new Object[]{
pClass,
pName,
pParameters,
pSignature
};
try
{
log.info("call _createMBean()");
lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
"_createMBean",
lArguments,
new Class[]{String.class, ObjectName.class, Object[].class, String[].class},
false
);
}
catch (Exception e)
{
throw new RemoteException("Could not create a MBean on the cluster", e);
}
Iterator i = lValues.iterator();
ObjectInstance lInstance = null;
Throwable lException = null;
while (i.hasNext())
{
Object lValue = i.next();
if (lValue instanceof ObjectInstance)
{
if (lInstance == null)
{
lInstance = (ObjectInstance) lValue;
}
}
else if (lValue instanceof Throwable)
{
if (lException == null)
{
lException = (Throwable) lValue;
}
}
}
if (lException != null)
{
if (lInstance != null)
{
try
{
unregisterMBean(lInstance.getObjectName());
}
catch (Exception e)
{
}
}
if (lException instanceof InstanceAlreadyExistsException)
{
throw (InstanceAlreadyExistsException) lException;
}
if (lException instanceof MBeanException)
{
throw (MBeanException) lException;
}
if (lException instanceof MBeanRegistrationException)
{
throw (MBeanRegistrationException) lException;
}
if (lException instanceof NotCompliantMBeanException)
{
throw (NotCompliantMBeanException) lException;
}
if (lException instanceof ReflectionException)
{
throw (ReflectionException) lException;
}
throw new RemoteException(lException.toString());
}
return lInstance;
}
public Object _unregisterMBean(ObjectName pName)
{
Object lReturn = null;
try
{
log.info("_unregisterMBean(), name: " + pName);
server.unregisterMBean(pName);
}
catch (Exception e)
{
lReturn = e;
}
return lReturn;
}
public void unregisterMBean(ObjectName pName)
throws
InstanceNotFoundException,
MBeanRegistrationException,
RemoteException
{
List lValues = null;
Object[] lArguments = new Object[]{
pName
};
try
{
log.info("call _unregisterMBean()");
lValues = mPartition.callMethodOnCluster(SERVICE_NAME,
"_unregisterMBean",
lArguments,
new Class[]{ObjectName.class},
false
);
}
catch (Exception e)
{
throw new RemoteException("Could not unregister a MBean on the cluster", e);
}
Iterator i = lValues.iterator();
Throwable lException = null;
while (i.hasNext())
{
Object lValue = i.next();
if (lValue instanceof Throwable)
{
lException = (Throwable) lValue;
break;
}
}
if (lException != null)
{
if (lException instanceof InstanceNotFoundException)
{
throw (InstanceNotFoundException) lException;
}
if (lException instanceof MBeanRegistrationException)
{
throw (MBeanRegistrationException) lException;
}
throw new RemoteException(lException.toString());
}
}
public void addNotificationListener(ObjectName pBroadcaster,
ObjectName pListener,
NotificationFilter pFilter,
Object pHandback)
throws
InstanceNotFoundException,
RemoteException
{
server.addNotificationListener(pBroadcaster, pListener, pFilter, pHandback);
}
public void removeNotificationListener(ObjectName pBroadcaster,
ObjectName pListener)
throws
InstanceNotFoundException,
ListenerNotFoundException,
RemoteException
{
server.removeNotificationListener(pBroadcaster, pListener);
}
public ObjectName preRegister(MBeanServer pServer, ObjectName pName)
throws Exception
{
super.preRegister(pServer, pName);
log.info("HA Management Service MBean online");
mHAManagementName = new ObjectName(OBJECT_NAME + ",Partition=" + mBackgroundPartition);
return mHAManagementName;
}
public void preDeregister()
throws Exception
{
super.preDeregister();
}
public String getName()
{
return "HA Management Service";
}
protected void createService() throws Exception
{
}
protected void startService()
throws Exception
{
mClusterPartitionName = new ObjectName("jboss:service=" + mBackgroundPartition);
log.debug("registerRPCHandler");
mPartition = (HAPartition) server.getAttribute(mClusterPartitionName,
"HAPartition");
mPartition.registerRPCHandler(SERVICE_NAME, this);
}
protected void stopService()
{
}
private void logException(Throwable e)
{
Throwable t = org.jboss.mx.util.JMXExceptionDecoder.decode(e);
log.error("operation failed", t);
}
}