MBeanRegistration.java revision 467
0N/A/*
0N/A * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/Apackage javax.management;
0N/A
0N/A
0N/A/**
406N/A * <p>Can be implemented by an MBean in order to
0N/A * carry out operations before and after being registered or unregistered from
406N/A * the MBean Server. An MBean can also implement this interface in order
406N/A * to get a reference to the MBean Server and/or its name within that
406N/A * MBean Server.</p>
406N/A *
406N/A * <h4 id="injection">Resource injection</h4>
406N/A *
406N/A * <p>As an alternative to implementing {@code MBeanRegistration}, if all that
406N/A * is needed is the MBean Server or ObjectName then an MBean can use
406N/A * <em>resource injection</em>.</p>
406N/A *
406N/A * <p>If a field in the MBean object has type {@link ObjectName} and has
406N/A * the {@link javax.annotation.Resource &#64;Resource} annotation,
406N/A * then the {@code ObjectName} under which the MBean is registered is
406N/A * assigned to that field during registration. Likewise, if a field has type
406N/A * {@link MBeanServer} and the <code>&#64;Resource</code> annotation, then it will
406N/A * be set to the {@code MBeanServer} in which the MBean is registered.</p>
406N/A *
406N/A * <p>For example:</p>
406N/A *
406N/A * <pre>
406N/A * public Configuration implements ConfigurationMBean {
406N/A * &#64;Resource
406N/A * private volatile MBeanServer mbeanServer;
406N/A * &#64;Resource
406N/A * private volatile ObjectName objectName;
406N/A * ...
406N/A * void unregisterSelf() throws Exception {
406N/A * mbeanServer.unregisterMBean(objectName);
406N/A * }
406N/A * }
406N/A * </pre>
406N/A *
406N/A * <p>Resource injection can also be used on fields of type
406N/A * {@link SendNotification} to simplify notification sending. Such a field
406N/A * will get a reference to an object of type {@code SendNotification} when
406N/A * the MBean is registered, and it can use this reference to send notifications.
406N/A * For example:</p>
406N/A *
406N/A * <pre>
406N/A * public Configuration implements ConfigurationMBean {
406N/A * &#64;Resource
406N/A * private volatile SendNotification sender;
406N/A * ...
406N/A * private void updated() {
406N/A * Notification n = new Notification(...);
406N/A * sender.sendNotification(n);
406N/A * }
406N/A * }
406N/A * </pre>
406N/A *
406N/A * <p>A field to be injected must not be static. It is recommended that
406N/A * such fields be declared {@code volatile}.</p>
406N/A *
406N/A * <p>It is also possible to use the <code>&#64;Resource</code> annotation on
406N/A * methods. Such a method must have a {@code void} return type and a single
406N/A * argument of the appropriate type, for example {@code ObjectName}.</p>
406N/A *
406N/A * <p>Any number of fields and methods may have the <code>&#64;Resource</code>
406N/A * annotation. All fields and methods with type {@code ObjectName}
406N/A * (for example) will receive the same {@code ObjectName} value.</p>
406N/A *
406N/A * <p>Resource injection is available for all types of MBeans, not just
406N/A * Standard MBeans.</p>
406N/A *
406N/A * <p>If an MBean implements the {@link DynamicWrapperMBean} interface then
406N/A * resource injection happens on the object returned by that interface's
406N/A * {@link DynamicWrapperMBean#getWrappedObject() getWrappedObject()} method
406N/A * rather than on the MBean object itself.
406N/A *
406N/A * <p>Resource injection happens after the {@link #preRegister preRegister}
406N/A * method is called (if any), and before the MBean is actually registered
406N/A * in the MBean Server. If a <code>&#64;Resource</code> method throws
406N/A * an exception, the effect is the same as if {@code preRegister} had
406N/A * thrown the exception. In particular it will prevent the MBean from being
406N/A * registered.</p>
406N/A *
406N/A * <p>Resource injection can be used on a field or method where the type
406N/A * is a parent of the injected type, if the injected type is explicitly
406N/A * specified in the <code>&#64;Resource</code> annotation. For example:</p>
406N/A *
406N/A * <pre>
406N/A * &#64;Resource(type = MBeanServer.class)
406N/A * private volatile MBeanServerConnection mbsc;
406N/A * </pre>
406N/A *
406N/A * <p>Formally, suppose <em>R</em> is the type in the <code>&#64;Resource</code>
406N/A * annotation and <em>T</em> is the type of the method parameter or field.
406N/A * Then one of <em>R</em> and <em>T</em> must be a subtype of the other
406N/A * (or they must be the same type). Injection happens if this subtype
406N/A * is {@code MBeanServer}, {@code ObjectName}, or {@code SendNotification}.
406N/A * Otherwise the <code>&#64;Resource</code> annotation is ignored.</p>
406N/A *
406N/A * <p>Resource injection in MBeans is new in version 2.0 of the JMX API.</p>
0N/A *
0N/A * @since 1.5
0N/A */
0N/Apublic interface MBeanRegistration {
0N/A
0N/A
0N/A /**
0N/A * Allows the MBean to perform any operations it needs before
406N/A * being registered in the MBean Server. If the name of the MBean
0N/A * is not specified, the MBean can provide a name for its
0N/A * registration. If any exception is raised, the MBean will not be
406N/A * registered in the MBean Server.
0N/A *
406N/A * @param server The MBean Server in which the MBean will be registered.
0N/A *
0N/A * @param name The object name of the MBean. This name is null if
0N/A * the name parameter to one of the <code>createMBean</code> or
0N/A * <code>registerMBean</code> methods in the {@link MBeanServer}
0N/A * interface is null. In that case, this method must return a
0N/A * non-null ObjectName for the new MBean.
0N/A *
0N/A * @return The name under which the MBean is to be registered.
0N/A * This value must not be null. If the <code>name</code>
0N/A * parameter is not null, it will usually but not necessarily be
0N/A * the returned value.
0N/A *
0N/A * @exception java.lang.Exception This exception will be caught by
406N/A * the MBean Server and re-thrown as an {@link
0N/A * MBeanRegistrationException}.
0N/A */
0N/A public ObjectName preRegister(MBeanServer server,
0N/A ObjectName name) throws java.lang.Exception;
0N/A
0N/A /**
0N/A * Allows the MBean to perform any operations needed after having been
0N/A * registered in the MBean server or after the registration has failed.
467N/A * <p>If the implementation of this method throws a {@link RuntimeException}
467N/A * or an {@link Error}, the MBean Server will rethrow those inside
467N/A * a {@link RuntimeMBeanException} or {@link RuntimeErrorException},
467N/A * respectively. However, throwing an exception in {@code postRegister}
467N/A * will not change the state of the MBean:
467N/A * if the MBean was already registered ({@code registrationDone} is
467N/A * {@code true}), the MBean will remain registered. </p>
467N/A * <p>This might be confusing for the code calling {@code createMBean()}
467N/A * or {@code registerMBean()}, as such code might assume that MBean
467N/A * registration has failed when such an exception is raised.
467N/A * Therefore it is recommended that implementations of
467N/A * {@code postRegister} do not throw Runtime Exceptions or Errors if it
467N/A * can be avoided.</p>
0N/A * @param registrationDone Indicates whether or not the MBean has
0N/A * been successfully registered in the MBean server. The value
0N/A * false means that the registration phase has failed.
0N/A */
0N/A public void postRegister(Boolean registrationDone);
0N/A
0N/A /**
0N/A * Allows the MBean to perform any operations it needs before
0N/A * being unregistered by the MBean server.
0N/A *
0N/A * @exception java.lang.Exception This exception will be caught by
0N/A * the MBean server and re-thrown as an {@link
0N/A * MBeanRegistrationException}.
0N/A */
0N/A public void preDeregister() throws java.lang.Exception ;
0N/A
0N/A /**
0N/A * Allows the MBean to perform any operations needed after having been
0N/A * unregistered in the MBean server.
467N/A * <p>If the implementation of this method throws a {@link RuntimeException}
467N/A * or an {@link Error}, the MBean Server will rethrow those inside
467N/A * a {@link RuntimeMBeanException} or {@link RuntimeErrorException},
467N/A * respectively. However, throwing an excepption in {@code postDeregister}
467N/A * will not change the state of the MBean:
467N/A * the MBean was already successfully deregistered and will remain so. </p>
467N/A * <p>This might be confusing for the code calling
467N/A * {@code unregisterMBean()}, as it might assume that MBean deregistration
467N/A * has failed. Therefore it is recommended that implementations of
467N/A * {@code postDeregister} do not throw Runtime Exceptions or Errors if it
467N/A * can be avoided.</p>
0N/A */
0N/A public void postDeregister();
0N/A
0N/A }