author | emcmanus |
Tue, 09 Dec 2008 18:30:27 +0100 | |
changeset 1705 | 08644ea24ce6 |
parent 1247 | b4c26443dee5 |
child 4156 | acaa49a2768a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1247 | 2 |
* Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.management; |
|
27 |
||
28 |
||
29 |
/** |
|
833 | 30 |
* <p>Can be implemented by an MBean in order to |
2 | 31 |
* carry out operations before and after being registered or unregistered from |
833 | 32 |
* the MBean Server. An MBean can also implement this interface in order |
33 |
* to get a reference to the MBean Server and/or its name within that |
|
34 |
* MBean Server.</p> |
|
35 |
* |
|
36 |
* <h4 id="injection">Resource injection</h4> |
|
37 |
* |
|
38 |
* <p>As an alternative to implementing {@code MBeanRegistration}, if all that |
|
39 |
* is needed is the MBean Server or ObjectName then an MBean can use |
|
40 |
* <em>resource injection</em>.</p> |
|
41 |
* |
|
42 |
* <p>If a field in the MBean object has type {@link ObjectName} and has |
|
43 |
* the {@link javax.annotation.Resource @Resource} annotation, |
|
44 |
* then the {@code ObjectName} under which the MBean is registered is |
|
45 |
* assigned to that field during registration. Likewise, if a field has type |
|
46 |
* {@link MBeanServer} and the <code>@Resource</code> annotation, then it will |
|
47 |
* be set to the {@code MBeanServer} in which the MBean is registered.</p> |
|
48 |
* |
|
49 |
* <p>For example:</p> |
|
50 |
* |
|
51 |
* <pre> |
|
52 |
* public Configuration implements ConfigurationMBean { |
|
53 |
* @Resource |
|
54 |
* private volatile MBeanServer mbeanServer; |
|
55 |
* @Resource |
|
56 |
* private volatile ObjectName objectName; |
|
57 |
* ... |
|
58 |
* void unregisterSelf() throws Exception { |
|
59 |
* mbeanServer.unregisterMBean(objectName); |
|
60 |
* } |
|
61 |
* } |
|
62 |
* </pre> |
|
63 |
* |
|
64 |
* <p>Resource injection can also be used on fields of type |
|
65 |
* {@link SendNotification} to simplify notification sending. Such a field |
|
66 |
* will get a reference to an object of type {@code SendNotification} when |
|
67 |
* the MBean is registered, and it can use this reference to send notifications. |
|
68 |
* For example:</p> |
|
69 |
* |
|
70 |
* <pre> |
|
71 |
* public Configuration implements ConfigurationMBean { |
|
72 |
* @Resource |
|
73 |
* private volatile SendNotification sender; |
|
74 |
* ... |
|
75 |
* private void updated() { |
|
76 |
* Notification n = new Notification(...); |
|
77 |
* sender.sendNotification(n); |
|
78 |
* } |
|
79 |
* } |
|
80 |
* </pre> |
|
81 |
* |
|
1705
08644ea24ce6
6780803: Wrong parameter name in description of EventClient::addListeners()
emcmanus
parents:
1247
diff
changeset
|
82 |
* <p>(Listeners may be invoked in the same thread as the caller of |
08644ea24ce6
6780803: Wrong parameter name in description of EventClient::addListeners()
emcmanus
parents:
1247
diff
changeset
|
83 |
* {@code sender.sendNotification}.)</p> |
08644ea24ce6
6780803: Wrong parameter name in description of EventClient::addListeners()
emcmanus
parents:
1247
diff
changeset
|
84 |
* |
833 | 85 |
* <p>A field to be injected must not be static. It is recommended that |
86 |
* such fields be declared {@code volatile}.</p> |
|
87 |
* |
|
88 |
* <p>It is also possible to use the <code>@Resource</code> annotation on |
|
89 |
* methods. Such a method must have a {@code void} return type and a single |
|
90 |
* argument of the appropriate type, for example {@code ObjectName}.</p> |
|
91 |
* |
|
92 |
* <p>Any number of fields and methods may have the <code>@Resource</code> |
|
93 |
* annotation. All fields and methods with type {@code ObjectName} |
|
94 |
* (for example) will receive the same {@code ObjectName} value.</p> |
|
95 |
* |
|
96 |
* <p>Resource injection is available for all types of MBeans, not just |
|
97 |
* Standard MBeans.</p> |
|
98 |
* |
|
99 |
* <p>If an MBean implements the {@link DynamicWrapperMBean} interface then |
|
100 |
* resource injection happens on the object returned by that interface's |
|
101 |
* {@link DynamicWrapperMBean#getWrappedObject() getWrappedObject()} method |
|
102 |
* rather than on the MBean object itself. |
|
103 |
* |
|
104 |
* <p>Resource injection happens after the {@link #preRegister preRegister} |
|
105 |
* method is called (if any), and before the MBean is actually registered |
|
106 |
* in the MBean Server. If a <code>@Resource</code> method throws |
|
107 |
* an exception, the effect is the same as if {@code preRegister} had |
|
108 |
* thrown the exception. In particular it will prevent the MBean from being |
|
109 |
* registered.</p> |
|
110 |
* |
|
111 |
* <p>Resource injection can be used on a field or method where the type |
|
112 |
* is a parent of the injected type, if the injected type is explicitly |
|
113 |
* specified in the <code>@Resource</code> annotation. For example:</p> |
|
114 |
* |
|
115 |
* <pre> |
|
116 |
* @Resource(type = MBeanServer.class) |
|
117 |
* private volatile MBeanServerConnection mbsc; |
|
118 |
* </pre> |
|
119 |
* |
|
120 |
* <p>Formally, suppose <em>R</em> is the type in the <code>@Resource</code> |
|
121 |
* annotation and <em>T</em> is the type of the method parameter or field. |
|
122 |
* Then one of <em>R</em> and <em>T</em> must be a subtype of the other |
|
123 |
* (or they must be the same type). Injection happens if this subtype |
|
124 |
* is {@code MBeanServer}, {@code ObjectName}, or {@code SendNotification}. |
|
125 |
* Otherwise the <code>@Resource</code> annotation is ignored.</p> |
|
126 |
* |
|
127 |
* <p>Resource injection in MBeans is new in version 2.0 of the JMX API.</p> |
|
2 | 128 |
* |
129 |
* @since 1.5 |
|
130 |
*/ |
|
131 |
public interface MBeanRegistration { |
|
132 |
||
133 |
||
134 |
/** |
|
135 |
* Allows the MBean to perform any operations it needs before |
|
833 | 136 |
* being registered in the MBean Server. If the name of the MBean |
2 | 137 |
* is not specified, the MBean can provide a name for its |
138 |
* registration. If any exception is raised, the MBean will not be |
|
833 | 139 |
* registered in the MBean Server. |
2 | 140 |
* |
833 | 141 |
* @param server The MBean Server in which the MBean will be registered. |
2 | 142 |
* |
143 |
* @param name The object name of the MBean. This name is null if |
|
144 |
* the name parameter to one of the <code>createMBean</code> or |
|
145 |
* <code>registerMBean</code> methods in the {@link MBeanServer} |
|
146 |
* interface is null. In that case, this method must return a |
|
147 |
* non-null ObjectName for the new MBean. |
|
148 |
* |
|
149 |
* @return The name under which the MBean is to be registered. |
|
150 |
* This value must not be null. If the <code>name</code> |
|
151 |
* parameter is not null, it will usually but not necessarily be |
|
152 |
* the returned value. |
|
153 |
* |
|
154 |
* @exception java.lang.Exception This exception will be caught by |
|
833 | 155 |
* the MBean Server and re-thrown as an {@link |
2 | 156 |
* MBeanRegistrationException}. |
157 |
*/ |
|
158 |
public ObjectName preRegister(MBeanServer server, |
|
159 |
ObjectName name) throws java.lang.Exception; |
|
160 |
||
161 |
/** |
|
162 |
* Allows the MBean to perform any operations needed after having been |
|
163 |
* registered in the MBean server or after the registration has failed. |
|
1002
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
164 |
* <p>If the implementation of this method throws a {@link RuntimeException} |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
165 |
* or an {@link Error}, the MBean Server will rethrow those inside |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
166 |
* a {@link RuntimeMBeanException} or {@link RuntimeErrorException}, |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
167 |
* respectively. However, throwing an exception in {@code postRegister} |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
168 |
* will not change the state of the MBean: |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
169 |
* if the MBean was already registered ({@code registrationDone} is |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
170 |
* {@code true}), the MBean will remain registered. </p> |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
171 |
* <p>This might be confusing for the code calling {@code createMBean()} |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
172 |
* or {@code registerMBean()}, as such code might assume that MBean |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
173 |
* registration has failed when such an exception is raised. |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
174 |
* Therefore it is recommended that implementations of |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
175 |
* {@code postRegister} do not throw Runtime Exceptions or Errors if it |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
176 |
* can be avoided.</p> |
2 | 177 |
* @param registrationDone Indicates whether or not the MBean has |
178 |
* been successfully registered in the MBean server. The value |
|
179 |
* false means that the registration phase has failed. |
|
180 |
*/ |
|
181 |
public void postRegister(Boolean registrationDone); |
|
182 |
||
183 |
/** |
|
184 |
* Allows the MBean to perform any operations it needs before |
|
185 |
* being unregistered by the MBean server. |
|
186 |
* |
|
187 |
* @exception java.lang.Exception This exception will be caught by |
|
188 |
* the MBean server and re-thrown as an {@link |
|
189 |
* MBeanRegistrationException}. |
|
190 |
*/ |
|
191 |
public void preDeregister() throws java.lang.Exception ; |
|
192 |
||
193 |
/** |
|
194 |
* Allows the MBean to perform any operations needed after having been |
|
195 |
* unregistered in the MBean server. |
|
1002
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
196 |
* <p>If the implementation of this method throws a {@link RuntimeException} |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
197 |
* or an {@link Error}, the MBean Server will rethrow those inside |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
198 |
* a {@link RuntimeMBeanException} or {@link RuntimeErrorException}, |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
199 |
* respectively. However, throwing an excepption in {@code postDeregister} |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
200 |
* will not change the state of the MBean: |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
201 |
* the MBean was already successfully deregistered and will remain so. </p> |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
202 |
* <p>This might be confusing for the code calling |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
203 |
* {@code unregisterMBean()}, as it might assume that MBean deregistration |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
204 |
* has failed. Therefore it is recommended that implementations of |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
205 |
* {@code postDeregister} do not throw Runtime Exceptions or Errors if it |
1e6a1b77f22a
6730926: Document that create/registerMBean can throw RuntimeMBeanException from postRegister
dfuchs
parents:
833
diff
changeset
|
206 |
* can be avoided.</p> |
2 | 207 |
*/ |
208 |
public void postDeregister(); |
|
209 |
||
210 |
} |