| Method from org.jboss.mx.server.registry.BasicMBeanRegistry Detail: |
protected synchronized void add(MBeanEntry entry) throws InstanceAlreadyExistsException {
// Determine the MBean's name and properties
ObjectName name = entry.getObjectName();
String domain = name.getDomain();
String props = name.getCanonicalKeyPropertyListString();
// Create a properties - > entry map if we don't have one
Map mbeanMap = getMBeanMap(domain, true);
// Make sure we aren't already registered
if (mbeanMap.get(props) != null)
throw new InstanceAlreadyExistsException(name + " already registered.");
// Ok, we are registered
mbeanMap.put(props, entry);
}
|
public boolean contains(ObjectName name) {
// null safety check
if (name == null)
return false;
// Determine the domain and retrieve its entries
String domain = name.getDomain();
if (domain.length() == 0)
domain = defaultDomain;
String props = name.getCanonicalKeyPropertyListString();
Map mbeanMap = getMBeanMap(domain, false);
// Return the result
return (null != mbeanMap && mbeanMap.containsKey(props));
}
|
public List findEntries(ObjectName pattern) {
ArrayList retval = new ArrayList();
// There are a couple of shortcuts we can employ to make this a
// bit faster - they're commented.
// First, if pattern == null or pattern.getCanonicalName() == "*:*" we want the
// set of all MBeans.
if (pattern == null || pattern.getCanonicalName().equals("*:*"))
{
for (Iterator domainIter = domainMap.values().iterator(); domainIter.hasNext();)
retval.addAll(((Map)domainIter.next()).values());
}
// Next, if !pattern.isPattern() then we are doing a simple get (maybe defaultDomain).
else if (!pattern.isPattern())
{
// simple get
try
{
retval.add(get(pattern));
}
catch (InstanceNotFoundException e)
{
// we don't care
}
}
// Now we have to do a brute force, oh well.
else
{
String patternDomain = pattern.getDomain();
if (patternDomain.length() == 0)
patternDomain = defaultDomain;
PropertyPattern propertyPattern = new PropertyPattern(pattern);
// Here we go, step through every domain and see if our pattern matches before optionally checking
// each ObjectName's properties for a match.
for (Iterator domainIter = domainMap.entrySet().iterator(); domainIter.hasNext();)
{
Map.Entry mapEntry = (Map.Entry) domainIter.next();
Map value = (Map) mapEntry.getValue();
if (value != null && value.isEmpty() == false)
{
if (ObjectNamePatternHelper.patternMatch((String) mapEntry.getKey(), patternDomain))
{
for (Iterator mbeanIter = value.values().iterator(); mbeanIter.hasNext();)
{
MBeanEntry entry = (MBeanEntry) mbeanIter.next();
if (propertyPattern.patternMatch(entry.getObjectName()))
retval.add(entry);
}
}
}
}
}
return retval;
}
|
public MBeanEntry get(ObjectName name) throws InstanceNotFoundException {
if (name == null)
throw new RuntimeOperationsException(new IllegalArgumentException("null object name"));
// Determine the domain and retrieve its entries
String domain = name.getDomain();
if (domain.length() == 0)
domain = defaultDomain;
String props = name.getCanonicalKeyPropertyListString();
Map mbeanMap = getMBeanMap(domain, false);
// Retrieve the mbean entry
Object o = null;
if (null == mbeanMap || null == (o = mbeanMap.get(props)))
throw new InstanceNotFoundException(name + " is not registered.");
// We are done
return (MBeanEntry) o;
}
|
public String getDefaultDomain() {
return defaultDomain;
}
|
public String[] getDomains() {
ArrayList domains = new ArrayList(domainMap.size());
for (Iterator iterator = domainMap.entrySet().iterator(); iterator.hasNext();)
{
Map.Entry entry = (Map.Entry) iterator.next();
String domainName = (String) entry.getKey();
Map mbeans = (Map) entry.getValue();
if (mbeans != null && mbeans.isEmpty() == false)
domains.add(domainName);
}
return (String[]) domains.toArray(new String[domains.size()]);
}
|
public ObjectInstance getObjectInstance(ObjectName name) throws InstanceNotFoundException {
if (!contains(name))
throw new InstanceNotFoundException(name + " not registered.");
return new ServerObjectInstance(qualifyName(name),
get(name).getResourceClassName(), delegate.getMBeanServerId());
}
|
public int getSize() {
int retval = 0;
for (Iterator iterator = domainMap.values().iterator(); iterator.hasNext();)
{
retval += ((Map)iterator.next()).size();
}
return retval;
}
|
public Object getValue(ObjectName name,
String key) throws InstanceNotFoundException {
return get(name).getValue(key);
}
|
protected void handlePreDeregister(MBeanRegistration registrationInterface) throws Exception {
registrationInterface.preDeregister ();
}
subclasses can override to provide any custom preDeregister logic
and must call preDregister on the MBeanRegistration instance |
protected ObjectName handlePreRegistration(MBeanRegistration registrationInterface,
ObjectName regName) throws Exception {
ObjectName mbean = registrationInterface.preRegister (server, regName);
if (regName == null)
{
return mbean;
}
else
{
return regName;
}
}
subclasses can override to provide their own pre-registration pre- and post- logic for
preRegister and must call preRegister on the MBeanRegistration instance |
protected ObjectName invokePreRegister(MBeanInvoker invoker,
ObjectName regName,
String magicToken) throws MBeanRegistrationException, NotCompliantMBeanException {
// if we were given a non-null object name for registration, qualify it
// and expand default domain
if (regName != null)
regName = qualifyName(regName);
// store the name returned by preRegister() here
ObjectName mbeanName = null;
try
{
// invoke preregister on the invoker, it will delegate to the resource
// if needed
mbeanName = invoker.preRegister(server, regName);
}
// if during pre registration, the mbean turns out to be not compliant
catch (NotCompliantMBeanException ncex)
{
throw ncex;
}
// catch all exceptions cause by preRegister, these will abort registration
catch (Exception e)
{
if (e instanceof MBeanRegistrationException)
{
throw (MBeanRegistrationException)e;
}
throw new MBeanRegistrationException(e,
"preRegister() failed: " +
"[ObjectName='" + regName +
"', Class=" + invoker.getResource().getClass().getName() +
" (" + invoker.getResource() + ")]"
);
}
catch (Throwable t)
{
log.warn("preRegister() failed for " + regName + ": ", t);
if (t instanceof Error)
throw new RuntimeErrorException((Error)t);
else
throw new RuntimeException(t.toString());
}
// if registered with null name, use the default name returned by
// the preregister implementation
if (regName == null)
regName = mbeanName;
return validateAndQualifyName(regName, magicToken);
}
|
protected Vector mbInfosToStore() {
if(fMbInfosToStore == null)
{
fMbInfosToStore = new Vector(10);
}
return fMbInfosToStore;
}
ObjectName objects bound to MBean Info objects that are waiting to be stored in the
persistence store. |
protected void persistIfRequired(MBeanInfo info,
ObjectName name) throws MalformedObjectNameException, InstanceNotFoundException, ReflectionException, MBeanException {
if(!(info instanceof ModelMBeanInfo))
{
return;
}
ModelMBeanInfo mmbInfo = (ModelMBeanInfo) info;
Descriptor descriptor;
try
{
descriptor = mmbInfo.getMBeanDescriptor();
}
catch(MBeanException cause)
{
log.error("Error trying to get descriptors.", cause);
return;
}
if (descriptor == null)
return;
String persistInfo = (String) descriptor.getFieldValue(ModelMBeanConstants.PERSIST_INFO);
if (persistInfo == null)
return; // use default -- no persistence
log.debug("persistInfo: " + persistInfo);
Boolean shouldPersist = new Boolean(persistInfo);
if(!shouldPersist.booleanValue())
{
return;
}
mbInfosToStore().add(name);
// see if MBeanDb is available
if(contains(mbeanInfoService))
{
// flush queue to the MBeanDb
log.debug("flushing queue");
server.invoke(
mbeanInfoService,
"add",
new Object[] { mbInfosToStore().clone() },
new String[] { mbInfosToStore().getClass().getName() });
log.debug("clearing queue");
mbInfosToStore().clear();
}
else
{
log.debug("service is not registered. items remain in queue");
}
}
Adds the given MBean Info object to the persistence queue if it explicity denotes
(via metadata) that it should be stored. |
protected ObjectName qualifyName(ObjectName name) {
if (name == null)
throw new RuntimeOperationsException(
new IllegalArgumentException("Null object name"));
try
{
if (name.getDomain().length() == 0)
return new ObjectName(defaultDomain + ":" +
name.getCanonicalKeyPropertyListString());
else
return name;
}
catch (MalformedObjectNameException e)
{
throw new RuntimeOperationsException(
new IllegalArgumentException(e.toString()));
}
}
|
protected void registerClassLoader(ClassLoader cl) {
if( (cl instanceof RealClassLoader) == false )
{
// Only register non-UCLs as UCLs already have a repository
loaderRepository.addClassLoader(cl);
}
}
Subclasses can override if they wish to control the classloader
registration to loader repository. |
public ObjectInstance registerMBean(Object object,
ObjectName name,
Map valueMap) throws MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException {
ObjectName regName = name;
boolean registrationDone = true;
boolean invokedPreRegister = false;
String magicToken = null;
MBeanInvoker invoker = null;
if (object == null)
throw new RuntimeOperationsException(
new IllegalArgumentException("Attempting to register null object"));
// get mbean type, dynamic or standard
MBeanCapability mbcap = MBeanCapability.of(object.getClass());
try
{
if (valueMap != null)
magicToken = (String) valueMap.get(JMI_DOMAIN);
// TODO: allow custom factory for diff invoker types
int mbeanType = mbcap.getMBeanType();
if (mbeanType == MBeanCapability.STANDARD_MBEAN)
{
invoker = new XMBean(object, XMBeanConstants.STANDARD_MBEAN);
}
else if (object instanceof MBeanInvoker)
{
invoker = (MBeanInvoker)object;
}
else if (mbeanType == MBeanCapability.DYNAMIC_MBEAN)
{
if( object instanceof RequiredModelMBean )
invoker = new RequiredModelMBeanInvoker((DynamicMBean)object);
else
invoker = new RawDynamicInvoker((DynamicMBean)object);
}
// Perform the pregistration
MBeanEntry entry = new MBeanEntry(regName, invoker, object, valueMap);
AbstractMBeanInvoker.setMBeanEntry(entry);
regName = invokePreRegister(invoker, regName, magicToken);
invokedPreRegister = true;
try
{
MBeanInfo info = invoker.getMBeanInfo();
verifyMBeanInfo(info, name);
entry.setResourceClassName(info.getClassName());
// Register the mbean
// Update the registered name to the final value
entry.setObjectName(regName);
add(entry);
try
{
// Add the classloader to the repository
if (object instanceof ClassLoader)
registerClassLoader((ClassLoader)object);
try
{
if (delegate != null)
sendRegistrationNotification (regName);
else if (serverConfig.getMBeanServerDelegateName().equals(name))
delegate = (MBeanServerDelegate) object;
ServerObjectInstance serverObjInst = new ServerObjectInstance
(regName, entry.getResourceClassName(), delegate.getMBeanServerId());
persistIfRequired(invoker.getMBeanInfo(), regName);
return serverObjInst;
}
catch (Throwable t)
{
// Problem, remove a classloader from the repository
if (object instanceof ClassLoader)
loaderRepository.removeClassLoader((ClassLoader)object);
throw t;
}
}
catch (Throwable t)
{
// Problem, remove the mbean from the registry
remove(regName);
throw t;
}
}
// Throw for null MBeanInfo
catch (NotCompliantMBeanException e)
{
throw e;
}
// Thrown by the registry
catch (InstanceAlreadyExistsException e)
{
throw e;
}
catch (Throwable t)
{
// Something is broken
log.error("Unexpected Exception:", t);
throw t;
}
}
catch (NotCompliantMBeanException e)
{
registrationDone = false;
throw e;
}
catch (InstanceAlreadyExistsException e)
{
// It was already registered
registrationDone = false;
throw e;
}
catch (MBeanRegistrationException e)
{
// The MBean cancelled the registration
registrationDone = false;
log.warn(e.toString());
throw e;
}
catch (RuntimeOperationsException e)
{
// There was a problem with one the arguments
registrationDone = false;
throw e;
}
catch (Exception ex)
{
// any other exception is mapped to NotCompliantMBeanException
registrationDone = false;
NotCompliantMBeanException ncex = new NotCompliantMBeanException("Cannot register MBean: " + name);
ncex.initCause(ex);
throw ncex;
}
catch (Throwable t)
{
// Some other error
log.error("Cannot register MBean", t);
registrationDone = false;
return null;
}
finally
{
// Tell the MBean the result of the registration
if (invoker != null)
{
try
{
invoker.postRegister(new Boolean(registrationDone));
}
catch(Exception e)
{
// Only throw this if preRegister succeeded
if( invokedPreRegister == true )
{
if( e instanceof RuntimeException )
throw new RuntimeMBeanException((RuntimeException) e);
else
throw new MBeanRegistrationException(e);
}
}
}
AbstractMBeanInvoker.setMBeanEntry(null);
}
}
|
public void releaseRegistry() {
server = null;
delegate = null;
// clear each value element from the domainMap
for (Iterator iterator = domainMap.keySet().iterator(); iterator.hasNext();)
{
Map nextMap = (Map) domainMap.get(iterator.next());
if ( nextMap.size() > 0 )
{
nextMap.clear();
}
}
domainMap.clear();
domainMap = null;
}
Cleans up the registry before the MBean server is released. |
protected synchronized void remove(ObjectName name) throws InstanceNotFoundException {
// Determine the MBean's name and properties
String domain = name.getDomain();
String props = name.getCanonicalKeyPropertyListString();
Map mbeanMap = getMBeanMap(domain, false);
// Remove the entry, raise an exception when it didn't exist
if (null == mbeanMap || null == mbeanMap.remove(props))
throw new InstanceNotFoundException(name + " not registered.");
}
Removes an MBean entry
WARNING: The object name should be fully qualified. |
protected void sendRegistrationNotification(ObjectName regName) {
long sequence = registrationNotificationSequence.increment ();
delegate.sendNotification (
new MBeanServerNotification (
MBeanServerNotification.REGISTRATION_NOTIFICATION,
delegate, sequence, regName));
}
send a MBeanServerNotification.REGISTRATION_NOTIFICATION notification
to regName |
protected void sendUnRegistrationNotification(ObjectName name) {
long sequence = unregistrationNotificationSequence.increment ();
delegate.sendNotification (
new MBeanServerNotification (
MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
delegate,
sequence,
name
)
);
}
send MBeanServerNotification.UNREGISTRATION_NOTIFICATION notification to
name |
public void unregisterMBean(ObjectName name) throws InstanceNotFoundException, MBeanRegistrationException {
name = qualifyName(name);
if (name.getDomain().equals(JMI_DOMAIN))
throw new RuntimeOperationsException(new IllegalArgumentException(
"Not allowed to unregister: " + name.toString()));
MBeanEntry entry = get(name);
Object resource = entry.getResourceInstance();
try
{
// allow subclasses to perform their own pre- and post- pre-deregister logic
handlePreDeregister (entry.getInvoker());
}
catch (Exception e)
{
// don't double wrap MBeanRegistrationException
if (e instanceof MBeanRegistrationException)
throw (MBeanRegistrationException)e;
throw new MBeanRegistrationException(e, "preDeregister");
}
// Remove any classloader
if (resource instanceof ClassLoader)
loaderRepository.removeClassLoader((ClassLoader)resource);
// It is no longer registered
remove(name);
sendUnRegistrationNotification (name);
entry.getInvoker().postDeregister();
}
|
protected ObjectName validateAndQualifyName(ObjectName name,
String magicToken) {
// Check for qualification
ObjectName result = qualifyName(name);
// Make sure the name is not a pattern
if (result.isPattern())
throw new RuntimeOperationsException(
new IllegalArgumentException("Object name is a pattern:" + name));
// Check for reserved domain
if (magicToken != JMI_DOMAIN &&
result.getDomain().equals(JMI_DOMAIN))
throw new RuntimeOperationsException(new IllegalArgumentException(
"Domain " + JMI_DOMAIN + " is reserved"));
// I can't think of anymore tests, we're done
return result;
}
Validates and qualifies an MBean
Validates the name is not a pattern.
Adds the default domain if no domain is specified.
Checks the name is not in the reserved domain JMImplementation when
the magicToken is not JMI_DOMAIN |