author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 526 | 61ba2d5ea9da |
child 834 | dc74d4ddc28e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 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 com.sun.jmx.mbeanserver; |
|
27 |
||
28 |
import com.sun.jmx.defaults.ServiceName; |
|
29 |
import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER; |
|
30 |
||
31 |
import java.util.ArrayList; |
|
32 |
import java.util.HashMap; |
|
33 |
import java.util.HashSet; |
|
34 |
import java.util.List; |
|
35 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
|
36 |
import java.util.logging.Level; |
|
37 |
import java.util.Map; |
|
38 |
import java.util.Set; |
|
39 |
import javax.management.DynamicMBean; |
|
40 |
import javax.management.InstanceAlreadyExistsException; |
|
41 |
import javax.management.InstanceNotFoundException; |
|
42 |
import javax.management.MalformedObjectNameException; |
|
43 |
import javax.management.ObjectName; |
|
44 |
import javax.management.QueryExp; |
|
45 |
import javax.management.RuntimeOperationsException; |
|
46 |
||
47 |
/** |
|
48 |
* The RepositorySupport implements the Repository interface. |
|
49 |
* This repository does not support persistency. |
|
50 |
* |
|
51 |
* @since 1.5 |
|
52 |
*/ |
|
53 |
public class Repository { |
|
54 |
||
55 |
// Private fields --------------------------------------------> |
|
56 |
||
57 |
/** |
|
58 |
* The structure for storing the objects is very basic. |
|
59 |
* A Hashtable is used for storing the different domains |
|
60 |
* For each domain, a hashtable contains the instances with |
|
61 |
* canonical key property list string as key and named object |
|
62 |
* aggregated from given object name and mbean instance as value. |
|
63 |
*/ |
|
64 |
private final Map<String,Map<String,NamedObject>> domainTb; |
|
65 |
||
66 |
/** |
|
67 |
* Number of elements contained in the Repository |
|
68 |
*/ |
|
69 |
private volatile int nbElements = 0; |
|
70 |
||
71 |
/** |
|
72 |
* Domain name of the server the repository is attached to. |
|
73 |
* It is quicker to store the information in the repository rather |
|
74 |
* than querying the framework each time the info is required. |
|
75 |
*/ |
|
76 |
private final String domain; |
|
77 |
||
78 |
/** |
|
79 |
* We use a global reentrant read write lock to protect the repository. |
|
80 |
* This seems safer and more efficient: we are using Maps of Maps, |
|
81 |
* Guaranteing consistency while using Concurent objects at each level |
|
82 |
* may be more difficult. |
|
83 |
**/ |
|
84 |
private final ReentrantReadWriteLock lock; |
|
85 |
||
86 |
// Private fields <============================================= |
|
87 |
||
88 |
// Private methods ---------------------------------------------> |
|
89 |
||
90 |
/* This class is used to match an ObjectName against a pattern. */ |
|
91 |
private final static class ObjectNamePattern { |
|
92 |
private final String[] keys; |
|
93 |
private final String[] values; |
|
94 |
private final String properties; |
|
95 |
private final boolean isPropertyListPattern; |
|
96 |
private final boolean isPropertyValuePattern; |
|
97 |
||
98 |
/** |
|
99 |
* The ObjectName pattern against which ObjectNames are matched. |
|
100 |
**/ |
|
101 |
public final ObjectName pattern; |
|
102 |
||
103 |
/** |
|
104 |
* Builds a new ObjectNamePattern object from an ObjectName pattern. |
|
105 |
* @param pattern The ObjectName pattern under examination. |
|
106 |
**/ |
|
107 |
public ObjectNamePattern(ObjectName pattern) { |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
287
diff
changeset
|
108 |
this(pattern.isPropertyListPattern(), |
2 | 109 |
pattern.isPropertyValuePattern(), |
110 |
pattern.getCanonicalKeyPropertyListString(), |
|
111 |
pattern.getKeyPropertyList(), |
|
112 |
pattern); |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Builds a new ObjectNamePattern object from an ObjectName pattern |
|
117 |
* constituents. |
|
118 |
* @param domain pattern.getDomain(). |
|
119 |
* @param propertyListPattern pattern.isPropertyListPattern(). |
|
120 |
* @param propertyValuePattern pattern.isPropertyValuePattern(). |
|
121 |
* @param canonicalProps pattern.getCanonicalKeyPropertyListString(). |
|
122 |
* @param keyPropertyList pattern.getKeyPropertyList(). |
|
123 |
* @param pattern The ObjectName pattern under examination. |
|
124 |
**/ |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
287
diff
changeset
|
125 |
ObjectNamePattern(boolean propertyListPattern, |
2 | 126 |
boolean propertyValuePattern, |
127 |
String canonicalProps, |
|
128 |
Map<String,String> keyPropertyList, |
|
129 |
ObjectName pattern) { |
|
130 |
this.isPropertyListPattern = propertyListPattern; |
|
131 |
this.isPropertyValuePattern = propertyValuePattern; |
|
132 |
this.properties = canonicalProps; |
|
133 |
final int len = keyPropertyList.size(); |
|
134 |
this.keys = new String[len]; |
|
135 |
this.values = new String[len]; |
|
136 |
int i = 0; |
|
137 |
for (Map.Entry<String,String> entry : keyPropertyList.entrySet()) { |
|
138 |
keys[i] = entry.getKey(); |
|
139 |
values[i] = entry.getValue(); |
|
140 |
i++; |
|
141 |
} |
|
142 |
this.pattern = pattern; |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Return true if the given ObjectName matches the ObjectName pattern |
|
147 |
* for which this object has been built. |
|
148 |
* WARNING: domain name is not considered here because it is supposed |
|
149 |
* not to be wildcard when called. PropertyList is also |
|
150 |
* supposed not to be zero-length. |
|
151 |
* @param name The ObjectName we want to match against the pattern. |
|
152 |
* @return true if <code>name</code> matches the pattern. |
|
153 |
**/ |
|
154 |
public boolean matchKeys(ObjectName name) { |
|
155 |
// If key property value pattern but not key property list |
|
156 |
// pattern, then the number of key properties must be equal |
|
157 |
// |
|
158 |
if (isPropertyValuePattern && |
|
159 |
!isPropertyListPattern && |
|
160 |
(name.getKeyPropertyList().size() != keys.length)) |
|
161 |
return false; |
|
162 |
||
163 |
// If key property value pattern or key property list pattern, |
|
164 |
// then every property inside pattern should exist in name |
|
165 |
// |
|
166 |
if (isPropertyValuePattern || isPropertyListPattern) { |
|
167 |
for (int i = keys.length - 1; i >= 0 ; i--) { |
|
168 |
// Find value in given object name for key at current |
|
169 |
// index in receiver |
|
170 |
// |
|
171 |
String v = name.getKeyProperty(keys[i]); |
|
172 |
// Did we find a value for this key ? |
|
173 |
// |
|
174 |
if (v == null) return false; |
|
175 |
// If this property is ok (same key, same value), go to next |
|
176 |
// |
|
177 |
if (isPropertyValuePattern && |
|
178 |
pattern.isPropertyValuePattern(keys[i])) { |
|
179 |
// wildmatch key property values |
|
180 |
final char[] val_pattern = values[i].toCharArray(); |
|
181 |
final char[] val_string = v.toCharArray(); |
|
182 |
if (wildmatch(val_string,val_pattern)) |
|
183 |
continue; |
|
184 |
else |
|
185 |
return false; |
|
186 |
} |
|
187 |
if (v.equals(values[i])) continue; |
|
188 |
return false; |
|
189 |
} |
|
190 |
return true; |
|
191 |
} |
|
192 |
||
193 |
// If no pattern, then canonical names must be equal |
|
194 |
// |
|
195 |
final String p1 = name.getCanonicalKeyPropertyListString(); |
|
196 |
final String p2 = properties; |
|
197 |
return (p1.equals(p2)); |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* Add all the matching objects from the given hashtable in the |
|
203 |
* result set for the given ObjectNamePattern |
|
204 |
* Do not check whether the domains match (only check for matching |
|
205 |
* key property lists - see <i>matchKeys()</i>) |
|
206 |
**/ |
|
207 |
private void addAllMatching(final Map<String,NamedObject> moiTb, |
|
208 |
final Set<NamedObject> result, |
|
209 |
final ObjectNamePattern pattern) { |
|
210 |
synchronized (moiTb) { |
|
211 |
for (NamedObject no : moiTb.values()) { |
|
212 |
final ObjectName on = no.getName(); |
|
213 |
// if all couples (property, value) are contained |
|
214 |
if (pattern.matchKeys(on)) result.add(no); |
|
215 |
} |
|
216 |
} |
|
217 |
} |
|
218 |
||
219 |
private void addNewDomMoi(final DynamicMBean object, final String dom, |
|
220 |
final ObjectName name) { |
|
221 |
final Map<String,NamedObject> moiTb = |
|
222 |
new HashMap<String,NamedObject>(); |
|
223 |
moiTb.put(name.getCanonicalKeyPropertyListString(), |
|
224 |
new NamedObject(name, object)); |
|
225 |
domainTb.put(dom, moiTb); |
|
226 |
nbElements++; |
|
227 |
} |
|
228 |
||
229 |
/** Match a string against a shell-style pattern. The only pattern |
|
230 |
characters recognised are <code>?</code>, standing for any one |
|
231 |
character, and <code>*</code>, standing for any string of |
|
232 |
characters, including the empty string. |
|
233 |
||
234 |
@param str the string to match, as a character array. |
|
235 |
@param pat the pattern to match the string against, as a |
|
236 |
character array. |
|
237 |
||
238 |
@return true if and only if the string matches the pattern. |
|
239 |
*/ |
|
240 |
/* The algorithm is a classical one. We advance pointers in |
|
241 |
parallel through str and pat. If we encounter a star in pat, |
|
242 |
we remember its position and continue advancing. If at any |
|
243 |
stage we get a mismatch between str and pat, we look to see if |
|
244 |
there is a remembered star. If not, we fail. If so, we |
|
245 |
retreat pat to just past that star and str to the position |
|
246 |
after the last one we tried, and we let the match advance |
|
247 |
again. |
|
248 |
||
249 |
Even though there is only one remembered star position, the |
|
250 |
algorithm works when there are several stars in the pattern. |
|
251 |
When we encounter the second star, we forget the first one. |
|
252 |
This is OK, because if we get to the second star in A*B*C |
|
253 |
(where A etc are arbitrary strings), we have already seen AXB. |
|
254 |
We're therefore setting up a match of *C against the remainder |
|
255 |
of the string, which will match if that remainder looks like |
|
256 |
YC, so the whole string looks like AXBYC. |
|
257 |
*/ |
|
258 |
public static boolean wildmatch(char[] str, char[] pat) { |
|
259 |
int stri; // index in str |
|
260 |
int pati; // index in pat |
|
261 |
int starstri; // index for backtrack if "*" attempt fails |
|
262 |
int starpati; // index for backtrack if "*" attempt fails, +1 |
|
263 |
final int strlen = str.length; |
|
264 |
final int patlen = pat.length; |
|
265 |
||
266 |
stri = pati = 0; |
|
267 |
starstri = starpati = -1; |
|
268 |
||
269 |
/* On each pass through this loop, we either advance pati, |
|
270 |
or we backtrack pati and advance starstri. Since starstri |
|
271 |
is only ever assigned from pati, the loop must terminate. */ |
|
272 |
while (true) { |
|
273 |
if (pati < patlen) { |
|
274 |
final char patc = pat[pati]; |
|
275 |
switch (patc) { |
|
276 |
case '?': |
|
277 |
if (stri == strlen) |
|
278 |
break; |
|
279 |
stri++; |
|
280 |
pati++; |
|
281 |
continue; |
|
282 |
case '*': |
|
283 |
pati++; |
|
284 |
starpati = pati; |
|
285 |
starstri = stri; |
|
286 |
continue; |
|
287 |
default: |
|
288 |
if (stri < strlen && str[stri] == patc) { |
|
289 |
stri++; |
|
290 |
pati++; |
|
291 |
continue; |
|
292 |
} |
|
293 |
break; |
|
294 |
} |
|
295 |
} else if (stri == strlen) |
|
296 |
return true; |
|
297 |
||
298 |
// Mismatched, can we backtrack to a "*"? |
|
299 |
if (starpati < 0 || starstri == strlen) |
|
300 |
return false; |
|
301 |
||
302 |
// Retry the match one position later in str |
|
303 |
pati = starpati; |
|
304 |
starstri++; |
|
305 |
stri = starstri; |
|
306 |
} |
|
307 |
} |
|
308 |
||
309 |
/** |
|
310 |
* Retrieves the named object contained in repository |
|
311 |
* from the given objectname. |
|
312 |
*/ |
|
313 |
private NamedObject retrieveNamedObject(ObjectName name) { |
|
314 |
||
315 |
// No patterns inside reposit |
|
316 |
if (name.isPattern()) return null; |
|
317 |
||
318 |
// Extract the domain name. |
|
319 |
String dom= name.getDomain().intern(); |
|
320 |
||
321 |
// Default domain case |
|
322 |
if (dom.length() == 0) { |
|
323 |
dom = domain; |
|
324 |
} |
|
325 |
||
326 |
Map<String,NamedObject> moiTb = domainTb.get(dom); |
|
327 |
if (moiTb == null) { |
|
328 |
return null; // No domain containing registered object names |
|
329 |
} |
|
330 |
||
331 |
return moiTb.get(name.getCanonicalKeyPropertyListString()); |
|
332 |
} |
|
333 |
||
334 |
// Private methods <============================================= |
|
335 |
||
336 |
// Protected methods ---------------------------------------------> |
|
337 |
||
338 |
// Protected methods <============================================= |
|
339 |
||
340 |
// Public methods ---------------------------------------------> |
|
341 |
||
342 |
/** |
|
343 |
* Construct a new repository with the given default domain. |
|
344 |
*/ |
|
345 |
public Repository(String domain) { |
|
346 |
this(domain,true); |
|
347 |
} |
|
348 |
||
349 |
/** |
|
350 |
* Construct a new repository with the given default domain. |
|
351 |
*/ |
|
352 |
public Repository(String domain, boolean fairLock) { |
|
353 |
lock = new ReentrantReadWriteLock(fairLock); |
|
354 |
||
355 |
domainTb = new HashMap<String,Map<String,NamedObject>>(5); |
|
356 |
||
357 |
if (domain != null && domain.length() != 0) |
|
358 |
this.domain = domain; |
|
359 |
else |
|
360 |
this.domain = ServiceName.DOMAIN; |
|
361 |
||
362 |
// Creates an new hastable for the default domain |
|
363 |
domainTb.put(this.domain.intern(), new HashMap<String,NamedObject>()); |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Returns the list of domains in which any MBean is currently |
|
368 |
* registered. |
|
369 |
* |
|
370 |
*/ |
|
371 |
public String[] getDomains() { |
|
372 |
||
373 |
lock.readLock().lock(); |
|
374 |
final List<String> result; |
|
375 |
try { |
|
376 |
// Temporary list |
|
377 |
result = new ArrayList<String>(domainTb.size()); |
|
378 |
for (Map.Entry<String,Map<String,NamedObject>> entry : |
|
379 |
domainTb.entrySet()) { |
|
380 |
// Skip domains that are in the table but have no |
|
381 |
// MBean registered in them |
|
382 |
// in particular the default domain may be like this |
|
383 |
Map<String,NamedObject> t = entry.getValue(); |
|
384 |
if (t != null && t.size() != 0) |
|
385 |
result.add(entry.getKey()); |
|
386 |
} |
|
387 |
} finally { |
|
388 |
lock.readLock().unlock(); |
|
389 |
} |
|
390 |
||
391 |
// Make an array from result. |
|
392 |
return result.toArray(new String[result.size()]); |
|
393 |
} |
|
394 |
||
395 |
/** |
|
396 |
* Stores an MBean associated with its object name in the repository. |
|
397 |
* |
|
398 |
* @param object MBean to be stored in the repository. |
|
399 |
* @param name MBean object name. |
|
400 |
*/ |
|
401 |
public void addMBean(final DynamicMBean object, ObjectName name) |
|
402 |
throws InstanceAlreadyExistsException { |
|
403 |
||
404 |
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) { |
|
405 |
MBEANSERVER_LOGGER.logp(Level.FINER, Repository.class.getName(), |
|
406 |
"addMBean", "name = " + name); |
|
407 |
} |
|
408 |
||
409 |
// Extract the domain name. |
|
410 |
String dom = name.getDomain().intern(); |
|
411 |
boolean to_default_domain = false; |
|
412 |
||
413 |
// Set domain to default if domain is empty and not already set |
|
287 | 414 |
if (dom.length() == 0) |
415 |
name = Util.newObjectName(domain + name.toString()); |
|
2 | 416 |
|
417 |
// Do we have default domain ? |
|
418 |
if (dom == domain) { |
|
419 |
to_default_domain = true; |
|
420 |
dom = domain; |
|
421 |
} else { |
|
422 |
to_default_domain = false; |
|
423 |
} |
|
424 |
||
425 |
// Validate name for an object |
|
426 |
if (name.isPattern()) { |
|
427 |
throw new RuntimeOperationsException( |
|
428 |
new IllegalArgumentException("Repository: cannot add mbean for " + |
|
429 |
"pattern name " + name.toString())); |
|
430 |
} |
|
431 |
||
432 |
lock.writeLock().lock(); |
|
433 |
try { |
|
434 |
// Domain cannot be JMImplementation if entry does not exists |
|
435 |
if ( !to_default_domain && |
|
436 |
dom.equals("JMImplementation") && |
|
437 |
domainTb.containsKey("JMImplementation")) { |
|
438 |
throw new RuntimeOperationsException( |
|
439 |
new IllegalArgumentException( |
|
440 |
"Repository: domain name cannot be JMImplementation")); |
|
441 |
} |
|
442 |
||
443 |
// If domain not already exists, add it to the hash table |
|
444 |
final Map<String,NamedObject> moiTb = domainTb.get(dom); |
|
445 |
if (moiTb == null) { |
|
446 |
addNewDomMoi(object, dom, name); |
|
447 |
return; |
|
448 |
} |
|
449 |
||
450 |
// Add instance if not already present |
|
451 |
String cstr = name.getCanonicalKeyPropertyListString(); |
|
452 |
NamedObject elmt= moiTb.get(cstr); |
|
453 |
if (elmt != null) { |
|
454 |
throw new InstanceAlreadyExistsException(name.toString()); |
|
455 |
} else { |
|
456 |
nbElements++; |
|
457 |
moiTb.put(cstr, new NamedObject(name, object)); |
|
458 |
} |
|
459 |
||
460 |
} finally { |
|
461 |
lock.writeLock().unlock(); |
|
462 |
} |
|
463 |
} |
|
464 |
||
465 |
/** |
|
466 |
* Checks whether an MBean of the name specified is already stored in |
|
467 |
* the repository. |
|
468 |
* |
|
469 |
* @param name name of the MBean to find. |
|
470 |
* |
|
471 |
* @return true if the MBean is stored in the repository, |
|
472 |
* false otherwise. |
|
473 |
*/ |
|
474 |
public boolean contains(ObjectName name) { |
|
475 |
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) { |
|
476 |
MBEANSERVER_LOGGER.logp(Level.FINER, Repository.class.getName(), |
|
477 |
"contains", " name = " + name); |
|
478 |
} |
|
479 |
lock.readLock().lock(); |
|
480 |
try { |
|
481 |
return (retrieveNamedObject(name) != null); |
|
482 |
} finally { |
|
483 |
lock.readLock().unlock(); |
|
484 |
} |
|
485 |
} |
|
486 |
||
487 |
/** |
|
488 |
* Retrieves the MBean of the name specified from the repository. The |
|
489 |
* object name must match exactly. |
|
490 |
* |
|
491 |
* @param name name of the MBean to retrieve. |
|
492 |
* |
|
493 |
* @return The retrieved MBean if it is contained in the repository, |
|
494 |
* null otherwise. |
|
495 |
*/ |
|
496 |
public DynamicMBean retrieve(ObjectName name) { |
|
497 |
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) { |
|
498 |
MBEANSERVER_LOGGER.logp(Level.FINER, Repository.class.getName(), |
|
499 |
"retrieve", "name = " + name); |
|
500 |
} |
|
501 |
||
502 |
// Calls internal retrieve method to get the named object |
|
503 |
lock.readLock().lock(); |
|
504 |
try { |
|
505 |
NamedObject no = retrieveNamedObject(name); |
|
506 |
if (no == null) return null; |
|
507 |
else return no.getObject(); |
|
508 |
} finally { |
|
509 |
lock.readLock().unlock(); |
|
510 |
} |
|
511 |
} |
|
512 |
||
513 |
/** |
|
514 |
* Selects and retrieves the list of MBeans whose names match the specified |
|
515 |
* object name pattern and which match the specified query expression |
|
516 |
* (optionally). |
|
517 |
* |
|
518 |
* @param pattern The name of the MBean(s) to retrieve - may be a specific |
|
519 |
* object or a name pattern allowing multiple MBeans to be selected. |
|
520 |
* @param query query expression to apply when selecting objects - this |
|
521 |
* parameter will be ignored when the Repository Service does not |
|
522 |
* support filtering. |
|
523 |
* |
|
524 |
* @return The list of MBeans selected. There may be zero, one or many |
|
525 |
* MBeans returned in the set. |
|
526 |
*/ |
|
527 |
public Set<NamedObject> query(ObjectName pattern, QueryExp query) { |
|
528 |
||
529 |
final Set<NamedObject> result = new HashSet<NamedObject>(); |
|
530 |
||
531 |
// The following filter cases are considered: |
|
532 |
// null, "", "*:*" : names in all domains |
|
533 |
// ":*", ":[key=value],*" : names in defaultDomain |
|
534 |
// "domain:*", "domain:[key=value],*" : names in the specified domain |
|
535 |
||
536 |
// Surely one of the most frequent case ... query on the whole world |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
287
diff
changeset
|
537 |
ObjectName name; |
2 | 538 |
if (pattern == null || |
539 |
pattern.getCanonicalName().length() == 0 || |
|
540 |
pattern.equals(ObjectName.WILDCARD)) |
|
541 |
name = ObjectName.WILDCARD; |
|
542 |
else name = pattern; |
|
543 |
||
544 |
lock.readLock().lock(); |
|
545 |
try { |
|
546 |
||
547 |
// If pattern is not a pattern, retrieve this mbean ! |
|
548 |
if (!name.isPattern()) { |
|
549 |
final NamedObject no; |
|
550 |
no = retrieveNamedObject(name); |
|
551 |
if (no != null) result.add(no); |
|
552 |
return result; |
|
553 |
} |
|
554 |
||
555 |
// All names in all domains |
|
556 |
if (name == ObjectName.WILDCARD) { |
|
557 |
for (Map<String,NamedObject> moiTb : domainTb.values()) { |
|
558 |
result.addAll(moiTb.values()); |
|
559 |
} |
|
560 |
return result; |
|
561 |
} |
|
562 |
||
563 |
final String canonical_key_property_list_string = |
|
564 |
name.getCanonicalKeyPropertyListString(); |
|
565 |
final boolean allNames = |
|
566 |
(canonical_key_property_list_string.length()==0); |
|
567 |
final ObjectNamePattern namePattern = |
|
568 |
(allNames?null:new ObjectNamePattern(name)); |
|
569 |
||
570 |
// All names in default domain |
|
571 |
if (name.getDomain().length() == 0) { |
|
572 |
final Map<String,NamedObject> moiTb = domainTb.get(domain); |
|
573 |
if (allNames) |
|
574 |
result.addAll(moiTb.values()); |
|
575 |
else |
|
576 |
addAllMatching(moiTb, result, namePattern); |
|
577 |
return result; |
|
578 |
} |
|
579 |
||
580 |
// Pattern matching in the domain name (*, ?) |
|
581 |
char[] dom2Match = name.getDomain().toCharArray(); |
|
582 |
for (String domain : domainTb.keySet()) { |
|
583 |
char[] theDom = domain.toCharArray(); |
|
584 |
if (wildmatch(theDom, dom2Match)) { |
|
585 |
final Map<String,NamedObject> moiTb = domainTb.get(domain); |
|
586 |
if (allNames) |
|
587 |
result.addAll(moiTb.values()); |
|
588 |
else |
|
589 |
addAllMatching(moiTb, result, namePattern); |
|
590 |
} |
|
591 |
} |
|
592 |
return result; |
|
593 |
} finally { |
|
594 |
lock.readLock().unlock(); |
|
595 |
} |
|
596 |
} |
|
597 |
||
598 |
/** |
|
599 |
* Removes an MBean from the repository. |
|
600 |
* |
|
601 |
* @param name name of the MBean to remove. |
|
602 |
* |
|
603 |
* @exception InstanceNotFoundException The MBean does not exist in |
|
604 |
* the repository. |
|
605 |
*/ |
|
606 |
public void remove(final ObjectName name) |
|
607 |
throws InstanceNotFoundException { |
|
608 |
||
609 |
// Debugging stuff |
|
610 |
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) { |
|
611 |
MBEANSERVER_LOGGER.logp(Level.FINER, Repository.class.getName(), |
|
612 |
"remove", "name = " + name); |
|
613 |
} |
|
614 |
||
615 |
// Extract domain name. |
|
616 |
String dom= name.getDomain().intern(); |
|
617 |
||
618 |
// Default domain case |
|
619 |
if (dom.length() == 0) dom = domain; |
|
620 |
||
621 |
lock.writeLock().lock(); |
|
622 |
try { |
|
623 |
// Find the domain subtable |
|
624 |
final Map<String,NamedObject> moiTb = domainTb.get(dom); |
|
625 |
if (moiTb == null) { |
|
626 |
throw new InstanceNotFoundException(name.toString()); |
|
627 |
} |
|
628 |
||
629 |
// Remove the corresponding element |
|
630 |
if (moiTb.remove(name.getCanonicalKeyPropertyListString())==null) { |
|
631 |
throw new InstanceNotFoundException(name.toString()); |
|
632 |
} |
|
633 |
||
634 |
// We removed it ! |
|
635 |
nbElements--; |
|
636 |
||
637 |
// No more object for this domain, we remove this domain hashtable |
|
638 |
if (moiTb.isEmpty()) { |
|
639 |
domainTb.remove(dom); |
|
640 |
||
641 |
// set a new default domain table (always present) |
|
642 |
// need to reinstantiate a hashtable because of possible |
|
643 |
// big buckets array size inside table, never cleared, |
|
644 |
// thus the new ! |
|
645 |
if (dom == domain) |
|
646 |
domainTb.put(domain, new HashMap<String,NamedObject>()); |
|
647 |
} |
|
648 |
} finally { |
|
649 |
lock.writeLock().unlock(); |
|
650 |
} |
|
651 |
} |
|
652 |
||
653 |
/** |
|
654 |
* Gets the number of MBeans stored in the repository. |
|
655 |
* |
|
656 |
* @return Number of MBeans. |
|
657 |
*/ |
|
658 |
public Integer getCount() { |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
287
diff
changeset
|
659 |
return nbElements; |
2 | 660 |
} |
661 |
||
662 |
/** |
|
663 |
* Gets the name of the domain currently used by default in the |
|
664 |
* repository. |
|
665 |
* |
|
666 |
* @return A string giving the name of the default domain name. |
|
667 |
*/ |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
287
diff
changeset
|
668 |
public String getDefaultDomain() { |
2 | 669 |
return domain; |
670 |
} |
|
671 |
||
672 |
// Public methods <============================================= |
|
673 |
} |