/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// JMX RI
// JMX import
/**
* This is the default class for MBean manipulation on the agent side. It
* contains the methods necessary for the creation, registration, and
* deletion of MBeans as well as the access methods for registered MBeans.
* This is the core component of the JMX infrastructure.
* <P>
* Every MBean which is added to the MBean server becomes manageable: its attributes and operations
* become remotely accessible through the connectors/adaptors connected to that MBean server.
* A Java object cannot be registered in the MBean server unless it is a JMX compliant MBean.
* <P>
* When an MBean is registered or unregistered in the MBean server an
* {@link javax.management.MBeanServerNotification MBeanServerNotification}
* Notification is emitted. To register an object as listener to MBeanServerNotifications
* you should call the MBean server method {@link #addNotificationListener addNotificationListener} with <CODE>ObjectName</CODE>
* the <CODE>ObjectName</CODE> of the {@link javax.management.MBeanServerDelegate MBeanServerDelegate}.
* This <CODE>ObjectName</CODE> is:
* <BR>
* <CODE>JMImplementation:type=MBeanServerDelegate</CODE>.
*
* @since 1.5
*/
/** The MBeanInstantiator object used by the
* DefaultMBeanServerInterceptor */
/** The MBean server object that is associated to the
* DefaultMBeanServerInterceptor */
/** The MBean server delegate object that is associated to the
* DefaultMBeanServerInterceptor */
/** The Repository object used by the DefaultMBeanServerInterceptor */
/** Wrappers for client listeners. */
/* See the comment before addNotificationListener below. */
private final transient
/** The default domain of the object names */
/** The sequence number identifying the notifications sent */
// Now sequence number is handled by MBeanServerDelegate.
// private int sequenceNumber=0;
/**
* Creates a DefaultMBeanServerInterceptor with the specified
* repository instance.
* <p>Do not forget to call <code>initialize(outer,delegate)</code>
* before using this object.
* @param outer A pointer to the MBeanServer object that must be
* passed to the MBeans when invoking their
* {@link javax.management.MBeanRegistration} interface.
* @param delegate A pointer to the MBeanServerDelegate associated
* with the new MBeanServer. The new MBeanServer must register
* this MBean in its MBean repository.
* @param instantiator The MBeanInstantiator that will be used to
* instantiate MBeans and take care of class loading issues.
* @param repository The repository to use for this MBeanServer.
*/
IllegalArgumentException("outer MBeanServer cannot be null");
IllegalArgumentException("MBeanServerDelegate cannot be null");
if (instantiator == null) throw new
IllegalArgumentException("MBeanInstantiator cannot be null");
if (repository == null) throw new
IllegalArgumentException("Repository cannot be null");
this.instantiator = instantiator;
this.repository = repository;
}
}
}
try {
} catch (InstanceNotFoundException e) {
/* Can only happen if loaderName doesn't exist, but we just
passed null, so we shouldn't get this exception. */
new IllegalArgumentException("Unexpected exception: " + e), e);
}
}
}
boolean withDefaultLoaderRepository,
final RuntimeException wrapped =
new IllegalArgumentException("The class name cannot be null");
throw new RuntimeOperationsException(wrapped,
"Exception occurred during MBean creation");
}
final RuntimeException wrapped =
new IllegalArgumentException("Invalid name->" +
}
}
/* Load the appropriate class. */
if (withDefaultLoaderRepository) {
DefaultMBeanServerInterceptor.class.getName(),
"createMBean",
}
theClass =
} else if (loaderName == null) {
DefaultMBeanServerInterceptor.class.getName(),
}
} else {
DefaultMBeanServerInterceptor.class.getName(),
", ObjectName = " + name +
", Loader name = " + loaderName);
}
}
// Check that the MBean can be instantiated by the MBeanServer.
// Check the JMX MBean compliance of the class
}
// ------------------------------
// ------------------------------
}
throws NotCompliantMBeanException {
if (mbeanToRegister instanceof DynamicMBean) {
try {
} catch (Exception e) {
// Includes case where getMBeanInfo() returns null
new NotCompliantMBeanException("Bad getMBeanInfo()");
throw ncmbe;
}
throw new NotCompliantMBeanException(msg);
}
return name;
} else
}
new HashSet<ObjectName>();
final RuntimeException wrapped =
new IllegalArgumentException("Object name cannot be null");
throw new RuntimeOperationsException(wrapped,
"Exception occurred trying to unregister the MBean");
}
/* The semantics of preDeregister are tricky. If it throws an
exception, then the unregisterMBean fails. This allows an
MBean to refuse to be unregistered. If it returns
successfully, then the unregisterMBean can proceed. In
this case the preDeregister may have cleaned up some state,
and will not expect to be called a second time. So if two
threads try to unregister the same MBean at the same time
then one of them must wait for the other one to either (a)
call preDeregister and get an exception or (b) call
preDeregister successfully and unregister the MBean.
Suppose thread T1 is unregistering an MBean and thread T2
is trying to unregister the same MBean, so waiting for T1.
Then a deadlock is possible if the preDeregister for T1
ends up needing a lock held by T2. Given the semantics
just described, there does not seem to be any way to avoid
this. This will not happen to code where it is clear for
any given MBean what thread may unregister that MBean.
On the other hand we clearly do not want a thread that is
unregistering MBean A to have to wait for another thread
that is unregistering another MBean B (see bug 6318664). A
deadlock in this situation could reasonably be considered
gratuitous. So holding a global lock across the
preDeregister call would be bad.
So we have a set of ObjectNames that some thread is
currently unregistering. When a thread wants to unregister
a name, it must first check if the name is in the set, and
if so it must wait. When a thread successfully unregisters
a name it removes the name from the set and notifies any
waiting threads that the set has changed.
This implies that we must be very careful to ensure that
the name is removed from the set and waiters notified, no
matter what code path is taken. */
synchronized (beingUnregistered) {
try {
} catch (InterruptedException e) {
throw new MBeanRegistrationException(e, e.toString());
// pretend the exception came from preDeregister;
// in another execution sequence it could have
}
}
}
try {
} finally {
synchronized (beingUnregistered) {
}
}
}
// may throw InstanceNotFoundException
if (instance instanceof MBeanRegistration)
// Unregisters the MBean from the repository.
// Returns the resource context that was used.
// The returned context does nothing for regular MBeans.
// For ClassLoader MBeans and JMXNamespace (and JMXDomain)
// MBeans - the context makes it possible to unregister these
// objects from the appropriate framework artifacts, such as
// the CLR or the dispatcher, from within the repository lock.
// In case of success, we also need to call context.done() at the
// end of this method.
//
final ResourceContext context =
try {
if (instance instanceof MBeanRegistration)
} finally {
}
}
throws InstanceNotFoundException {
}
// Check if the caller has the right to invoke 'queryMBeans'
//
// Perform query without "query".
//
// Check if the caller has the right to invoke 'queryMBeans'
// on each specific classname/objectname in the list.
//
try {
} catch (SecurityException e) {
// OK: Do not add this ObjectInstance to the list
}
}
// Apply query to allowed MBeans only.
//
} else {
// Perform query.
//
}
}
// Query the MBeans on the repository
//
}
// Check if the caller has the right to invoke 'queryNames'
//
// Perform query without "query".
//
// Check if the caller has the right to invoke 'queryNames'
// on each specific classname/objectname in the list.
//
try {
} catch (SecurityException e) {
// OK: Do not add this ObjectInstance to the list
}
}
// Apply query to allowed MBeans only.
//
}
} else {
// Perform query.
//
}
return queryList;
}
// Query the MBeans on the repository
//
}
throw new RuntimeOperationsException(
new IllegalArgumentException("Object name cannot be null"),
"Object name cannot be null");
}
/* No Permission check */
// isRegistered is always unchecked as per JMX spec.
}
// Check if the caller has the right to invoke 'getDomains'
//
// Return domains
//
// Check if the caller has the right to invoke 'getDomains'
// on each specific domain in the list.
//
try {
} catch (SecurityException e) {
// OK: Do not add this domain to the list
}
}
// Make an array from result.
//
} else {
return repository.getDomains();
}
}
return (repository.getCount());
}
throws MBeanException, AttributeNotFoundException,
throw new RuntimeOperationsException(new
IllegalArgumentException("Object name cannot be null"),
"Exception occurred trying to invoke the getter on the MBean");
}
throw new RuntimeOperationsException(new
IllegalArgumentException("Attribute cannot be null"),
"Exception occurred trying to invoke the getter on the MBean");
}
DefaultMBeanServerInterceptor.class.getName(),
"getAttribute",
}
try {
} catch (AttributeNotFoundException e) {
throw e;
} catch (Throwable t) {
throw new AssertionError(); // not reached
}
}
throws InstanceNotFoundException, ReflectionException {
throw new RuntimeOperationsException(new
IllegalArgumentException("ObjectName name cannot be null"),
"Exception occurred trying to invoke the getter on the MBean");
}
if (attributes == null) {
throw new RuntimeOperationsException(new
IllegalArgumentException("Attributes cannot be null"),
"Exception occurred trying to invoke the getter on the MBean");
}
DefaultMBeanServerInterceptor.class.getName(),
}
final String[] allowedAttributes;
else {
// Check if the caller has the right to invoke 'getAttribute'
//
// Check if the caller has the right to invoke 'getAttribute'
// on each specific attribute
//
try {
} catch (SecurityException e) {
// OK: Do not add this attribute to the list
}
}
}
try {
} catch (Throwable t) {
rethrow(t);
throw new AssertionError();
}
}
throw new RuntimeOperationsException(new
IllegalArgumentException("ObjectName name cannot be null"),
"Exception occurred trying to invoke the setter on the MBean");
}
throw new RuntimeOperationsException(new
IllegalArgumentException("Attribute cannot be null"),
"Exception occurred trying to invoke the setter on the MBean");
}
DefaultMBeanServerInterceptor.class.getName(),
}
try {
} catch (AttributeNotFoundException e) {
throw e;
} catch (InvalidAttributeValueException e) {
throw e;
} catch (Throwable t) {
throw new AssertionError();
}
}
throws InstanceNotFoundException, ReflectionException {
throw new RuntimeOperationsException(new
IllegalArgumentException("ObjectName name cannot be null"),
"Exception occurred trying to invoke the setter on the MBean");
}
if (attributes == null) {
throw new RuntimeOperationsException(new
IllegalArgumentException("AttributeList cannot be null"),
"Exception occurred trying to invoke the setter on the MBean");
}
final AttributeList allowedAttributes;
else {
// Check if the caller has the right to invoke 'setAttribute'
//
// Check if the caller has the right to invoke 'setAttribute'
// on each specific attribute
//
try {
name, "setAttribute");
} catch (SecurityException e) {
// OK: Do not add this attribute to the list
}
}
}
try {
} catch (Throwable t) {
rethrow(t);
throw new AssertionError();
}
}
throws InstanceNotFoundException, MBeanException,
try {
} catch (Throwable t) {
throw new AssertionError();
}
}
/* Centralize some of the tedious exception wrapping demanded by the JMX
spec. */
throws ReflectionException {
try {
throw t;
} catch (ReflectionException e) {
throw e;
} catch (RuntimeOperationsException e) {
throw e;
} catch (RuntimeErrorException e) {
throw e;
} catch (RuntimeException e) {
throw new RuntimeMBeanException(e, e.toString());
} catch (Error e) {
throw new RuntimeErrorException(e, e.toString());
// should not happen
}
}
throws ReflectionException, MBeanException {
if (t instanceof MBeanException)
throw (MBeanException) t;
rethrow(t);
}
/**
* Register <code>object</code> in the repository, with the
* given <code>name</code>.
* This method is called by the various createMBean() flavours
* and by registerMBean() after all MBean compliance tests
* have been performed.
* <p>
* This method does not performed any kind of test compliance,
* and the caller should make sure that the given <code>object</code>
* is MBean compliant.
* <p>
* This methods performed all the basic steps needed for object
* registration:
* <ul>
* <li>If the <code>object</code> implements the MBeanRegistration
* interface, it invokes preRegister() on the object.</li>
* <li>Then the object is added to the repository with the given
* <code>name</code>.</li>
* <li>Finally, if the <code>object</code> implements the
* MBeanRegistration interface, it invokes postRegister()
* on the object.</li>
* </ul>
* @param object A reference to a MBean compliant object.
* @param name The ObjectName of the <code>object</code> MBean.
* @return the actual ObjectName with which the object was registered.
* @exception InstanceAlreadyExistsException if an object is already
* registered with that name.
* @exception MBeanRegistrationException if an exception occurs during
* registration.
**/
throws InstanceAlreadyExistsException,
final RuntimeException wrapped =
new IllegalArgumentException("Cannot add null object");
throw new RuntimeOperationsException(wrapped,
"Exception occurred trying to register the MBean");
}
}
throws InstanceAlreadyExistsException,
DefaultMBeanServerInterceptor.class.getName(),
}
// preRegister returned successfully, so from this point on we
// must call postRegister(false) if there is any problem.
boolean registered = false;
boolean registerFailed = false;
try {
if (mbean instanceof DynamicMBean2) {
try {
registerFailed = true; // until we succeed
} catch (Exception e) {
if (e instanceof RuntimeException)
throw (RuntimeException) e;
if (e instanceof InstanceAlreadyExistsException)
throw (InstanceAlreadyExistsException) e;
throw new RuntimeException(e);
}
}
}
if (logicalName == null) {
final RuntimeException wrapped =
new IllegalArgumentException("No object name specified");
throw new RuntimeOperationsException(wrapped,
"Exception occurred trying to register the MBean");
}
// Register the MBean with the repository.
// Returns the resource context that was used.
// The returned context does nothing for regular MBeans.
// For ClassLoader MBeans the context makes it possible to register these
// objects with the appropriate framework artifacts, such as
// the CLR, from within the repository lock.
// In case of success, we also need to call context.done() at the
// end of this method.
//
registerFailed = false;
registered = true;
} finally {
try {
} finally {
}
}
}
throws MBeanRegistrationException {
if (t instanceof RuntimeException) {
throw new RuntimeMBeanException((RuntimeException)t,
"RuntimeException thrown " + where);
} else if (t instanceof Error) {
throw new RuntimeErrorException((Error)t,
"Error thrown " + where);
} else if (t instanceof MBeanRegistrationException) {
throw (MBeanRegistrationException)t;
} else if (t instanceof Exception) {
throw new MBeanRegistrationException((Exception)t,
"Exception thrown " + where);
} else // neither Error nor Exception??
throw new RuntimeException(t);
}
try {
if (mbean instanceof MBeanRegistration)
} catch (Throwable t) {
throwMBeanRegistrationException(t, "in preRegister method");
}
else return name;
}
private static void postRegister(
boolean registrationDone, boolean registerFailed) {
try {
if (mbean instanceof MBeanRegistration)
} catch (RuntimeException e) {
"]: " + "Exception thrown by postRegister: " +
"rethrowing <"+e+">, but keeping the MBean registered");
throw new RuntimeMBeanException(e,
"RuntimeException thrown in postRegister method: "+
"rethrowing <"+e+">, but keeping the MBean registered");
"]: " + "Error thrown by postRegister: " +
throw new RuntimeErrorException(er,
"Error thrown in postRegister method: "+
}
}
throws MBeanRegistrationException {
try {
moi.preDeregister();
} catch (Throwable t) {
throwMBeanRegistrationException(t, "in preDeregister method");
}
}
try {
} catch (RuntimeException e) {
"]: " + "Exception thrown by postDeregister: " +
"rethrowing <"+e+">, although the MBean is succesfully " +
"unregistered");
throw new RuntimeMBeanException(e,
"RuntimeException thrown in postDeregister method: "+
"rethrowing <"+e+
">, although the MBean is sucessfully unregistered");
"]: " + "Error thrown by postDeregister: " +
"unregistered");
throw new RuntimeErrorException(er,
"Error thrown in postDeregister method: "+
"rethrowing <"+er+
">, although the MBean is sucessfully unregistered");
}
}
/**
* Gets a specific MBean controlled by the DefaultMBeanServerInterceptor.
* The name must have a non-default domain.
*/
throws InstanceNotFoundException {
throw new RuntimeOperationsException(new
IllegalArgumentException("Object name cannot be null"),
"Exception occurred trying to get an MBean");
}
DefaultMBeanServerInterceptor.class.getName(),
}
}
return obj;
}
if (mbean instanceof DynamicMBean2)
else
return mbean;
}
return name;
/* The ObjectName looks like ":a=b", and that's what its
toString() will return in this implementation. So
we can just stick the default domain in front of it
to get a non-default-domain name. We depend on the
fact that toString() works like that and that it
leaves wildcards in place (so we can detect an error
if one is supplied where it shouldn't be). */
}
return domain;
}
/*
* Notification handling.
*
* This is not trivial, because the MBeanServer translates the
* source of a received notification from a reference to an MBean
* into the ObjectName of that MBean. While that does make
* notification sending easier for MBean writers, it comes at a
* considerable cost. We need to replace the source of a
* notification, which is basically wrong if there are also
* listeners registered directly with the MBean (without going
* through the MBean server). We also need to wrap the listener
* supplied by the client of the MBeanServer with a listener that
* performs the substitution before forwarding. This is why we
* strongly discourage people from putting MBean references in the
* source of their notifications. Instead they should arrange to
* put the ObjectName there themselves.
*
* However, existing code relies on the substitution, so we are
* stuck with it.
*
* Here's how we handle it. When you add a listener, we make a
* ListenerWrapper around it. We look that up in the
* listenerWrappers map, and if there was already a wrapper for
* that listener with the given ObjectName, we reuse it. This map
* is a WeakHashMap, so a listener that is no longer registered
* with any MBean can be garbage collected.
*
* We cannot use simpler solutions such as always creating a new
* wrapper or always registering the same listener with the MBean
* and using the handback to find the client's original listener.
* The reason is that we need to support the removeListener
* variant that removes all (listener,filter,handback) triples on
* a broadcaster that have a given listener. And we do not have
* any way to inspect a broadcaster's internal list of triples.
* So the same client listener must always map to the same
* listener registered with the broadcaster.
*
* Another possible solution would be to map from ObjectName to
* list of listener wrappers (or IdentityHashMap of listener
* wrappers), making this list the first time a listener is added
* on a given MBean, and removing it when the MBean is removed.
* This is probably more costly in memory, but could be useful if
* some day we don't want to rely on weak references.
*/
throws InstanceNotFoundException {
// ------------------------------
// ------------------------------
DefaultMBeanServerInterceptor.class.getName(),
}
NotificationBroadcaster.class);
// ------------------
// Check listener
// ------------------
throw new RuntimeOperationsException(new
}
}
throws InstanceNotFoundException {
// ------------------------------
// ------------------------------
// ----------------
// Get listener object
// ----------------
if (!(resource instanceof NotificationListener)) {
throw new RuntimeOperationsException(new
"does not implement the NotificationListener interface") ;
}
// ----------------
// Add a listener on an MBean
// ----------------
DefaultMBeanServerInterceptor.class.getName(),
"addNotificationListener",
}
}
}
}
DefaultMBeanServerInterceptor.class.getName(),
"removeNotificationListener",
}
}
DefaultMBeanServerInterceptor.class.getName(),
"removeNotificationListener",
}
}
throws ListenerNotFoundException {
// ----------------
// Get listener object
// ----------------
try {
} catch (InstanceNotFoundException e) {
new ListenerNotFoundException(e.getMessage()), e);
}
if (!(resource instanceof NotificationListener)) {
final RuntimeException exc =
}
return (NotificationListener) resource;
}
boolean removeAll)
DefaultMBeanServerInterceptor.class.getName(),
}
/* We could simplify the code by assigning broadcaster after
assigning listenerWrapper, but that would change the error
behavior when both the broadcaster and the listener are
erroneous. */
if (listenerWrapper == null)
throw new ListenerNotFoundException("Unknown listener");
if (removeAll)
else {
handback);
}
}
private static <T extends NotificationBroadcaster>
if (instance instanceof DynamicMBean2)
final RuntimeException exc =
}
// ------------------------------
// ------------------------------
try {
} catch (RuntimeMBeanException e) {
throw e;
} catch (RuntimeErrorException e) {
throw e;
} catch (RuntimeException e) {
throw new RuntimeMBeanException(e,
"getMBeanInfo threw RuntimeException");
} catch (Error e) {
throw new RuntimeErrorException(e, "getMBeanInfo threw Error");
}
"has no MBeanInfo");
return mbi;
}
throws InstanceNotFoundException {
try {
final String resourceClassName =
(resource instanceof DynamicMBean) ?
return true;
return true;
} catch (Exception x) {
/* Could be SecurityException or ClassNotFoundException */
DefaultMBeanServerInterceptor.class.getName(),
"isInstanceOf", "Exception calling isInstanceOf", x);
}
return false;
}
}
/**
* <p>Return the {@link java.lang.ClassLoader} that was used for
* loading the class of the named MBean.
* @param mbeanName The ObjectName of the MBean.
* @return The ClassLoader used for that MBean.
* @exception InstanceNotFoundException if the named MBean is not found.
*/
throws InstanceNotFoundException {
}
/**
* <p>Return the named {@link java.lang.ClassLoader}.
* @param loaderName The ObjectName of the ClassLoader.
* @return The named ClassLoader.
* @exception InstanceNotFoundException if the named ClassLoader
* is not found.
*/
throws InstanceNotFoundException {
if (loaderName == null) {
}
/* Check if the given MBean is a ClassLoader */
if (!(resource instanceof ClassLoader))
" is not a classloader");
return (ClassLoader) resource;
}
/**
* Sends an MBeanServerNotifications with the specified type for the
* MBean with the specified ObjectName
*/
// ------------------------------
// ------------------------------
// ---------------------
// Create notification
// ---------------------
DefaultMBeanServerInterceptor.class.getName(),
}
}
/**
* Applies the specified queries to the set of NamedObjects.
*/
private Set<ObjectName>
// No query ...
}
} else {
// Access the filter
try {
boolean res;
try {
} catch (Exception e) {
res = false;
}
if (res) {
}
}
} finally {
/*
* query.setMBeanServer is probably
* QueryEval.setMBeanServer so put back the old
* value. Since that method uses a ThreadLocal
* variable, this code is only needed for the
* unusual case where the user creates a custom
* QueryExp that calls a nested query on another
* MBeanServer.
*/
}
}
return result;
}
/**
* Applies the specified queries to the set of NamedObjects.
*/
private Set<ObjectInstance>
// No query ...
}
} else {
// Access the filter
try {
boolean res;
try {
} catch (Exception e) {
res = false;
}
if (res) {
}
}
} finally {
/*
* query.setMBeanServer is probably
* QueryEval.setMBeanServer so put back the old
* value. Since that method uses a ThreadLocal
* variable, this code is only needed for the
* unusual case where the user creates a custom
* QueryExp that calls a nested query on another
* MBeanServer.
*/
}
}
return result;
}
try {
return getClassName(mbean);
} catch (Exception e) {
DefaultMBeanServerInterceptor.class.getName(),
"safeGetClassName",
"Exception getting MBean class name", e);
}
return null;
}
}
/**
* Applies the specified queries to the set of ObjectInstances.
*/
private Set<ObjectInstance>
// Null query.
//
return list;
} else {
// Access the filter.
//
boolean res = false;
try {
} catch (Exception e) {
res = false;
} finally {
/*
* query.setMBeanServer is probably
* QueryEval.setMBeanServer so put back the old
* value. Since that method uses a ThreadLocal
* variable, this code is only needed for the
* unusual case where the user creates a custom
* QueryExp that calls a nested query on another
* MBeanServer.
*/
}
if (res) {
}
}
return result;
}
}
/*
* Get the existing wrapper for this listener, name, and mbean, if
* there is one. Otherwise, if "create" is true, create and
* return one. Otherwise, return null.
*
* We use a WeakHashMap so that if the only reference to a user
* listener is in listenerWrappers, it can be garbage collected.
* This requires a certain amount of care, because only the key in
* a WeakHashMap is weak; the value is strong. We need to recover
* the existing wrapper object (not just an object that is equal
* to it), so we would like listenerWrappers to map any
* ListenerWrapper to the canonical ListenerWrapper for that
* (listener,name,mbean) set. But we do not want this canonical
* wrapper to be referenced strongly. Therefore we put it inside
* a WeakReference and that is the value in the WeakHashMap.
*/
boolean create) {
synchronized (listenerWrappers) {
return existing;
}
if (create) {
return wrapper;
} else
return null;
}
}
throw new UnsupportedOperationException("Not supported yet.");
}
throw new UnsupportedOperationException("Not supported yet.");
}
throw new UnsupportedOperationException("Not supported yet.");
}
throw new UnsupportedOperationException("Not supported yet.");
}
public ObjectInputStream deserialize(ObjectName name, byte[] data) throws InstanceNotFoundException,
throw new UnsupportedOperationException("Not supported yet.");
}
throw new UnsupportedOperationException("Not supported yet.");
}
throw new UnsupportedOperationException("Not supported yet.");
}
throw new UnsupportedOperationException("Not supported yet.");
}
this.listener = l;
}
if (notification != null) {
}
/*
* Listeners are not supposed to throw exceptions. If
* this one does, we could remove it from the MBean. It
* might indicate that a connector has stopped working,
* for instance, and there is no point in sending future
* notifications over that connection. However, this
* seems rather drastic, so instead we propagate the
* exception and let the broadcaster handle it.
*/
}
if (!(o instanceof ListenerWrapper))
return false;
ListenerWrapper w = (ListenerWrapper) o;
/*
* We compare all three, in case the same MBean object
* gets unregistered and then reregistered under a
* different name, or the same name gets assigned to two
* different MBean objects at different times. We do the
* comparisons in this order to avoid the slow
* ObjectName.equals when possible.
*/
}
public int hashCode() {
/*
* We do not include name.hashCode() in the hash because
* computing it is slow and usually we will not have two
* instances of ListenerWrapper with the same mbean but
* different ObjectNames. That can happen if the MBean is
* unregistered from one name and reregistered with
* another, and there is no garbage collection between; or
* if the same object is registered under two names (which
* is not recommended because MBeanRegistration will
* break). But even in these unusual cases the hash code
* does not have to be unique.
*/
}
}
// SECURITY CHECKS
//----------------
if (mbean instanceof DynamicMBean2)
else
}
actions);
}
}
actions);
}
}
throws SecurityException {
new PrivilegedAction<ProtectionDomain>() {
public ProtectionDomain run() {
return theClass.getProtectionDomain();
}
};
}
}
// ------------------------------------------------------------------
//
// Dealing with registration of special MBeans in the repository.
//
// ------------------------------------------------------------------
/**
* A RegistrationContext that makes it possible to perform additional
* post registration actions (or post unregistration actions) outside
* of the repository lock, once postRegister (or postDeregister) has
* been called.
* The method {@code done()} will be called in registerMBean or
* unregisterMBean, at the end.
*/
public void done();
/** An empty ResourceContext which does nothing **/
public void done() {}
public void registering() {}
public void unregistered() {}
};
}
/**
* Adds a MBean in the repository,
* sends MBeanServerNotification.REGISTRATION_NOTIFICATION,
* returns ResourceContext for special resources such as ClassLoaders
* or JMXNamespaces. For regular MBean this method returns
* ResourceContext.NONE.
* @return a ResourceContext for special resources such as ClassLoaders
* or JMXNamespaces.
*/
final DynamicMBean object,
final ObjectName logicalName)
throws InstanceAlreadyExistsException,
// Creates a registration context, if needed.
//
final ResourceContext context =
// May throw InstanceAlreadyExistsException
// ---------------------
// Send create event
// ---------------------
DefaultMBeanServerInterceptor.class.getName(),
"addObject", "Send create notification of object " +
}
return context;
}
/**
* Removes a MBean in the repository,
* sends MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
* returns ResourceContext for special resources such as ClassLoaders
* or JMXNamespaces, or null. For regular MBean this method returns
* ResourceContext.NONE.
*
* @return a ResourceContext for special resources such as ClassLoaders
* or JMXNamespaces.
*/
final DynamicMBean object,
final ObjectName logicalName)
throws InstanceNotFoundException {
// Creates a registration context, if needed.
//
final ResourceContext context =
// ---------------------
// Send deletion event
// ---------------------
DefaultMBeanServerInterceptor.class.getName(),
"unregisterMBean", "Send delete notification of object " +
}
return context;
}
/**
* Registers a ClassLoader with the CLR.
* This method is called by the ResourceContext from within the
* repository lock.
* @param loader The ClassLoader.
* @param logicalName The ClassLoader MBean ObjectName.
*/
final ObjectName logicalName) {
/**
* Called when the newly registered MBean is a ClassLoader
* If so, tell the ClassLoaderRepository (CLR) about it. We do
* this even if the loader is a PrivateClassLoader. In that
* case, the CLR remembers the loader for use when it is
* explicitly named (e.g. as the loader in createMBean) but
* does not add it to the list that is consulted by
* ClassLoaderRepository.loadClass.
*/
final RuntimeException wrapped =
"Dynamic addition of class loaders" +
" is not supported");
throw new RuntimeOperationsException(wrapped,
"Exception occurred trying to register" +
" the MBean as a class loader");
}
}
/**
* Unregisters a ClassLoader from the CLR.
* This method is called by the ResourceContext from within the
* repository lock.
* @param loader The ClassLoader.
* @param logicalName The ClassLoader MBean ObjectName.
*/
final ObjectName logicalName) {
/**
* Removes the MBean from the default loader repository.
*/
}
}
}
/**
* Creates a ResourceContext for a ClassLoader MBean.
* The resource context makes it possible to add the ClassLoader to
* (ResourceContext.registering) or resp. remove the ClassLoader from
* (ResourceContext.unregistered) the CLR
* when the associated MBean is added to or resp. removed from the
* repository.
*
* @param loader The ClassLoader MBean being registered or
* unregistered.
* @param logicalName The name of the ClassLoader MBean.
* @return a ResourceContext that takes in charge the addition or removal
* of the loader to or from the CLR.
*/
final ClassLoader loader,
final ObjectName logicalName) {
return new ResourceContext() {
public void registering() {
}
public void unregistered() {
}
public void done() {
}
};
}
/**
* Creates a ResourceContext for the given resource.
* If the resource does not need a ResourceContext, returns
* ResourceContext.NONE.
* At this time, only ClassLoaders need a ResourceContext.
*
* @param resource The resource being registered or unregistered.
* @param logicalName The name of the associated MBean.
* @return
*/
if (resource instanceof ClassLoader) {
}
return ResourceContext.NONE;
}
public ModifiableClassLoaderRepository run() {
}
});
}
}