1 /* |
|
2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. |
|
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 com.sun.jmx.interceptor; |
|
27 |
|
28 import com.sun.jmx.defaults.JmxProperties; |
|
29 import com.sun.jmx.mbeanserver.MBeanInstantiator; |
|
30 import com.sun.jmx.mbeanserver.Repository; |
|
31 import com.sun.jmx.mbeanserver.Util; |
|
32 import com.sun.jmx.namespace.NamespaceInterceptor; |
|
33 |
|
34 import java.util.Queue; |
|
35 import java.util.logging.Level; |
|
36 import java.util.logging.Logger; |
|
37 |
|
38 import javax.management.MBeanServer; |
|
39 import javax.management.MBeanServerDelegate; |
|
40 import javax.management.ObjectName; |
|
41 import javax.management.RuntimeOperationsException; |
|
42 import javax.management.namespace.JMXDomain; |
|
43 import javax.management.namespace.JMXNamespace; |
|
44 import static javax.management.namespace.JMXNamespaces.NAMESPACE_SEPARATOR; |
|
45 |
|
46 /** |
|
47 * A dispatcher that dispatches to NamespaceInterceptors. |
|
48 * <p><b> |
|
49 * This API is a Sun internal API and is subject to changes without notice. |
|
50 * </b></p> |
|
51 * @since 1.7 |
|
52 */ |
|
53 public class NamespaceDispatchInterceptor |
|
54 extends DispatchInterceptor<NamespaceInterceptor, JMXNamespace> { |
|
55 |
|
56 /** |
|
57 * A logger for this class. |
|
58 **/ |
|
59 private static final Logger LOG = JmxProperties.NAMESPACE_LOGGER; |
|
60 |
|
61 private static final int NAMESPACE_SEPARATOR_LENGTH = |
|
62 NAMESPACE_SEPARATOR.length(); |
|
63 private static final ObjectName X3 = ObjectName.valueOf("x:x=x"); |
|
64 |
|
65 private final DomainDispatchInterceptor nextInterceptor; |
|
66 private final String serverName; |
|
67 |
|
68 /** |
|
69 * Creates a NamespaceDispatchInterceptor with the specified |
|
70 * repository instance. |
|
71 * <p>Do not forget to call <code>initialize(outer,delegate)</code> |
|
72 * before using this object. |
|
73 * |
|
74 * @param outer A pointer to the MBeanServer object that must be |
|
75 * passed to the MBeans when invoking their |
|
76 * {@link javax.management.MBeanRegistration} interface. |
|
77 * @param delegate A pointer to the MBeanServerDelegate associated |
|
78 * with the new MBeanServer. The new MBeanServer must register |
|
79 * this MBean in its MBean repository. |
|
80 * @param instantiator The MBeanInstantiator that will be used to |
|
81 * instantiate MBeans and take care of class loading issues. |
|
82 * @param repository The repository to use for this MBeanServer |
|
83 */ |
|
84 public NamespaceDispatchInterceptor(MBeanServer outer, |
|
85 MBeanServerDelegate delegate, |
|
86 MBeanInstantiator instantiator, |
|
87 Repository repository) { |
|
88 nextInterceptor = new DomainDispatchInterceptor(outer,delegate, |
|
89 instantiator,repository,this); |
|
90 serverName = Util.getMBeanServerSecurityName(delegate); |
|
91 } |
|
92 |
|
93 /** |
|
94 * Get first name space in ObjectName path. Ignore leading namespace |
|
95 * separators. Includes the trailing //. |
|
96 * |
|
97 * Examples: |
|
98 * <pre> |
|
99 * For ObjectName: Returns: |
|
100 * foo//bar//baz:x=x -> "foo//" |
|
101 * foo//:type=JMXNamespace -> "foo//" |
|
102 * foo//:x=x -> "foo//" |
|
103 * foo////:x=x -> "foo//" |
|
104 * //foo//bar//baz:x=x -> "//" |
|
105 * ////foo//bar//baz:x=x -> "//" |
|
106 * //:x=x -> "//" |
|
107 * foo:x=x -> "" |
|
108 * (null) -> "" |
|
109 * :x=x -> "" |
|
110 * |
|
111 * </pre> |
|
112 **/ |
|
113 static String getFirstNamespaceWithSlash(ObjectName name) { |
|
114 if (name == null) return ""; |
|
115 final String domain = name.getDomain(); |
|
116 if (domain.equals("")) return ""; |
|
117 |
|
118 // go to next separator |
|
119 final int end = domain.indexOf(NAMESPACE_SEPARATOR); |
|
120 if (end == -1) return ""; // no namespace |
|
121 |
|
122 // This is the first element in the namespace path. |
|
123 final String namespace = |
|
124 domain.substring(0,end+NAMESPACE_SEPARATOR_LENGTH); |
|
125 |
|
126 return namespace; |
|
127 } |
|
128 |
|
129 /** |
|
130 * Called by the DefaultMBeanServerInterceptor, just before adding an |
|
131 * MBean to the repository. |
|
132 * |
|
133 * @param resource the MBean to be registered. |
|
134 * @param logicalName the name of the MBean to be registered. |
|
135 */ |
|
136 final void checkLocallyRegistrable(Object resource, |
|
137 ObjectName logicalName) { |
|
138 if (!(resource instanceof JMXNamespace) && |
|
139 logicalName.getDomain().contains(NAMESPACE_SEPARATOR)) |
|
140 throw new IllegalArgumentException(String.valueOf(logicalName)+ |
|
141 ": Invalid ObjectName for an instance of " + |
|
142 resource.getClass().getName()); |
|
143 } |
|
144 |
|
145 // Removes the trailing //. namespaceWithSlash should be either |
|
146 // "" or a namespace path ending with //. |
|
147 // |
|
148 private final String getKeyFor(String namespaceWithSlash) { |
|
149 final int end = namespaceWithSlash.length() - |
|
150 NAMESPACE_SEPARATOR_LENGTH; |
|
151 if (end <= 0) return ""; |
|
152 final String key = namespaceWithSlash.substring(0,end); |
|
153 return key; |
|
154 } |
|
155 |
|
156 @Override |
|
157 final MBeanServer getInterceptorOrNullFor(ObjectName name) { |
|
158 final String namespace = getFirstNamespaceWithSlash(name); |
|
159 |
|
160 // Leading separators should trigger instance not found exception. |
|
161 // returning null here has this effect. |
|
162 // |
|
163 if (namespace.equals(NAMESPACE_SEPARATOR)) { |
|
164 LOG.finer("ObjectName starts with: "+namespace); |
|
165 return null; |
|
166 } |
|
167 |
|
168 // namespace="" means that there was no namespace path in the |
|
169 // ObjectName. => delegate to the next interceptor (local MBS) |
|
170 // name.getDomain()=namespace means that we have an ObjectName of |
|
171 // the form blah//:x=x. This is either a JMXNamespace or a non |
|
172 // existent MBean. => delegate to the next interceptor (local MBS) |
|
173 if (namespace.equals("") || name.getDomain().equals(namespace)) { |
|
174 LOG.finer("dispatching to local name space"); |
|
175 return nextInterceptor; |
|
176 } |
|
177 |
|
178 // There was a namespace path in the ObjectName. Returns the |
|
179 // interceptor that handles it, or null if there is no such |
|
180 // interceptor. |
|
181 final String key = getKeyFor(namespace); |
|
182 final NamespaceInterceptor ns = getInterceptor(key); |
|
183 if (LOG.isLoggable(Level.FINER)) { |
|
184 if (ns != null) { |
|
185 LOG.finer("dispatching to name space: " + key); |
|
186 } else { |
|
187 LOG.finer("no handler for: " + key); |
|
188 } |
|
189 } |
|
190 return ns; |
|
191 } |
|
192 |
|
193 @Override |
|
194 final QueryInterceptor getInterceptorForQuery(ObjectName pattern) { |
|
195 final String namespace = getFirstNamespaceWithSlash(pattern); |
|
196 |
|
197 // Leading separators should trigger instance not found exception. |
|
198 // returning null here has this effect. |
|
199 // |
|
200 if (namespace.equals(NAMESPACE_SEPARATOR)) { |
|
201 LOG.finer("ObjectName starts with: "+namespace); |
|
202 return null; |
|
203 } |
|
204 |
|
205 // namespace="" means that there was no namespace path in the |
|
206 // ObjectName. => delegate to the next interceptor (local MBS) |
|
207 // name.getDomain()=namespace means that we have an ObjectName of |
|
208 // the form blah//:x=x. This is either a JMXNamespace or a non |
|
209 // existent MBean. => delegate to the next interceptor (local MBS) |
|
210 if (namespace.equals("") || pattern.getDomain().equals(namespace)) { |
|
211 LOG.finer("dispatching to local name space"); |
|
212 return new QueryInterceptor(nextInterceptor); |
|
213 } |
|
214 |
|
215 // This is a 'hack' to check whether the first namespace is a pattern. |
|
216 // We wan to throw RTOE wrapping IAE in that case |
|
217 if (X3.withDomain(namespace).isDomainPattern()) { |
|
218 throw new RuntimeOperationsException( |
|
219 new IllegalArgumentException("Pattern not allowed in namespace path")); |
|
220 } |
|
221 |
|
222 // There was a namespace path in the ObjectName. Returns the |
|
223 // interceptor that handles it, or null if there is no such |
|
224 // interceptor. |
|
225 // |
|
226 final String key = getKeyFor(namespace); |
|
227 final NamespaceInterceptor ns = getInterceptor(key); |
|
228 if (LOG.isLoggable(Level.FINER)) { |
|
229 if (ns != null) { |
|
230 LOG.finer("dispatching to name space: " + key); |
|
231 } else { |
|
232 LOG.finer("no handler for: " + key); |
|
233 } |
|
234 } |
|
235 if (ns == null) return null; |
|
236 return new QueryInterceptor(ns); |
|
237 } |
|
238 |
|
239 @Override |
|
240 final ObjectName getHandlerNameFor(String key) { |
|
241 return ObjectName.valueOf(key+NAMESPACE_SEPARATOR, |
|
242 "type", JMXNamespace.TYPE); |
|
243 } |
|
244 |
|
245 @Override |
|
246 final public String getHandlerKey(ObjectName name) { |
|
247 final String namespace = getFirstNamespaceWithSlash(name); |
|
248 // namespace is either "" or a namespace ending with // |
|
249 return getKeyFor(namespace); |
|
250 } |
|
251 |
|
252 @Override |
|
253 final NamespaceInterceptor createInterceptorFor(String key, |
|
254 ObjectName name, JMXNamespace handler, |
|
255 Queue<Runnable> postRegisterQueue) { |
|
256 final NamespaceInterceptor ns = |
|
257 new NamespaceInterceptor(serverName,handler,key); |
|
258 if (LOG.isLoggable(Level.FINER)) { |
|
259 LOG.finer("NamespaceInterceptor created: "+ns); |
|
260 } |
|
261 return ns; |
|
262 } |
|
263 |
|
264 @Override |
|
265 final DomainDispatchInterceptor getNextInterceptor() { |
|
266 return nextInterceptor; |
|
267 } |
|
268 |
|
269 /** |
|
270 * Returns the list of domains in which any MBean is currently |
|
271 * registered. |
|
272 */ |
|
273 @Override |
|
274 public String[] getDomains() { |
|
275 return nextInterceptor.getDomains(); |
|
276 } |
|
277 |
|
278 @Override |
|
279 public void addInterceptorFor(ObjectName name, JMXNamespace handler, |
|
280 Queue<Runnable> postRegisterQueue) { |
|
281 if (handler instanceof JMXDomain) |
|
282 nextInterceptor.addInterceptorFor(name, |
|
283 (JMXDomain)handler,postRegisterQueue); |
|
284 else super.addInterceptorFor(name,handler,postRegisterQueue); |
|
285 } |
|
286 |
|
287 @Override |
|
288 public void removeInterceptorFor(ObjectName name, JMXNamespace handler, |
|
289 Queue<Runnable> postDeregisterQueue) { |
|
290 if (handler instanceof JMXDomain) |
|
291 nextInterceptor.removeInterceptorFor(name,(JMXDomain)handler, |
|
292 postDeregisterQueue); |
|
293 else super.removeInterceptorFor(name,handler,postDeregisterQueue); |
|
294 } |
|
295 |
|
296 |
|
297 } |
|