1 28 29 package com.caucho.jmx; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.L10N; 33 34 import javax.management.*; 35 import java.util.ArrayList ; 36 import java.util.HashMap ; 37 import java.util.HashSet ; 38 import java.util.Hashtable ; 39 import java.util.Iterator ; 40 import java.util.Set ; 41 import java.util.logging.Logger ; 42 43 46 public class MBeanView { 47 private static final Logger log = Log.open(MBeanView.class); 48 private static final L10N L = new L10N(MBeanView.class); 49 50 51 private ClassLoader _classLoader; 52 53 private AbstractMBeanServer _mbeanServer; 54 55 private MBeanServerDelegateImpl _delegate; 56 57 private HashMap <ObjectName,MBeanWrapper> _mbeans = 58 new HashMap <ObjectName,MBeanWrapper>(8); 59 60 MBeanView(AbstractMBeanServer mbeanServer, 61 ClassLoader loader, 62 String agentId) 63 { 64 _mbeanServer = mbeanServer; 65 66 _classLoader = loader; 67 _delegate = new MBeanServerDelegateImpl(agentId); 68 } 69 70 73 public ClassLoader getClassLoader() 74 { 75 return _classLoader; 76 } 77 78 81 protected MBeanView getParentView() 82 { 83 if (_classLoader == null) 84 return null; 85 86 MBeanContext context = _mbeanServer.getContext(_classLoader.getParent()); 87 88 if (context.getView() == this) 89 return null; 90 else 91 return context.getView(); 92 93 99 } 100 101 104 protected MBeanView getParentGlobalView() 105 { 106 return null; 107 113 } 114 115 118 public int getMBeanCount() 119 { 120 MBeanView parentView = getParentView(); 121 122 if (parentView != null) 123 return _mbeans.size() + parentView.getMBeanCount(); 124 else 125 return _mbeans.size(); 126 } 127 128 131 public String []getDomains() 132 { 133 ArrayList <String > domains = new ArrayList <String >(); 134 135 getDomains(domains); 136 137 return domains.toArray(new String [domains.size()]); 138 } 139 140 143 protected void getDomains(ArrayList <String > domains) 144 { 145 synchronized (_mbeans) { 146 Iterator <ObjectName> names = _mbeans.keySet().iterator(); 147 while (names.hasNext()) { 148 ObjectName name = names.next(); 149 150 String domain = name.getDomain(); 151 152 if (! domains.contains(domain)) 153 domains.add(domain); 154 } 155 } 156 157 MBeanView parent = getParentView(); 158 159 if (parent != null) 160 parent.getDomains(domains); 161 } 162 163 166 public Set <ObjectName> queryNames(ObjectName queryName, QueryExp query) 167 throws BadStringOperationException, 168 BadBinaryOpValueExpException, 169 BadAttributeValueExpException, 170 InvalidApplicationException 171 { 172 HashSet <ObjectName> set = new HashSet <ObjectName>(); 174 175 queryNames(set, queryName, query); 176 177 return set; 178 } 179 180 183 protected void queryNames(Set <ObjectName> set, 184 ObjectName queryName, 185 QueryExp query) 186 throws BadStringOperationException, 187 BadBinaryOpValueExpException, 188 BadAttributeValueExpException, 189 InvalidApplicationException 190 { 191 synchronized (_mbeans) { 192 Iterator <ObjectName> iter = _mbeans.keySet().iterator(); 193 194 while (iter.hasNext()) { 195 ObjectName name = iter.next(); 196 197 if (isMatch(name, queryName, query)) { 198 set.add(name); 199 } 200 } 201 } 202 203 MBeanView parentView = getParentView(); 204 205 if (parentView != null) 206 parentView.queryNames(set, queryName, query); 207 } 208 209 212 public Set <ObjectInstance> queryMBeans(ObjectName name, QueryExp query) 213 throws BadStringOperationException, 214 BadBinaryOpValueExpException, 215 BadAttributeValueExpException, 216 InvalidApplicationException 217 { 218 HashSet <ObjectInstance> set = new HashSet <ObjectInstance>(); 219 220 queryMBeans(set, name, query); 221 222 return set; 223 } 224 225 228 protected void queryMBeans(Set <ObjectInstance> set, 229 ObjectName name, 230 QueryExp query) 231 throws BadStringOperationException, 232 BadBinaryOpValueExpException, 233 BadAttributeValueExpException, 234 InvalidApplicationException 235 { 236 synchronized (_mbeans) { 237 Iterator <ObjectName> iter = _mbeans.keySet().iterator(); 238 239 while (iter.hasNext()) { 240 ObjectName testName = iter.next(); 241 242 if (isMatch(testName, name, query)) { 243 MBeanWrapper mbean = _mbeans.get(testName); 244 245 if (mbean != null) 246 set.add(mbean.getObjectInstance()); 247 } 248 } 249 } 250 251 MBeanView parentView = getParentView(); 252 253 if (parentView != null) 254 parentView.queryMBeans(set, name, query); 255 } 256 257 263 private boolean isMatch(ObjectName name, 264 ObjectName queryName, 265 QueryExp query) 266 throws BadStringOperationException, 267 BadBinaryOpValueExpException, 268 BadAttributeValueExpException, 269 InvalidApplicationException 270 { 271 if (queryName == null) 272 return true; 273 274 if (! queryName.isDomainPattern() && 275 ! name.getDomain().equals(queryName.getDomain())) 276 return false; 277 278 if (queryName.isPropertyPattern()) { 279 282 Hashtable <String ,String > map = queryName.getKeyPropertyList(); 283 Iterator <String > iter = map.keySet().iterator(); 284 while (iter.hasNext()) { 285 String key = iter.next(); 286 String value = map.get(key); 287 288 if (! value.equals(name.getKeyProperty(key))) 289 return false; 290 } 291 } 292 else { 293 String testProps = name.getCanonicalKeyPropertyListString(); 294 String queryProps = queryName.getCanonicalKeyPropertyListString(); 295 296 if (! testProps.equals(queryProps)) 297 return false; 298 } 299 300 if (query != null && ! query.apply(name)) 301 return false; 302 303 return true; 304 } 305 306 309 boolean add(ObjectName name, MBeanWrapper mbean) 310 { 311 return add(name, mbean, false); 312 } 313 314 317 boolean add(ObjectName name, MBeanWrapper mbean, boolean overwrite) 318 { 319 synchronized (_mbeans) { 320 if (overwrite || _mbeans.get(name) == null) { 321 _mbeans.put(name, mbean); 322 323 return true; 324 } 325 else 326 return false; 327 } 328 } 329 330 333 MBeanWrapper remove(ObjectName name) 334 { 335 synchronized (_mbeans) { 336 return _mbeans.remove(name); 337 } 338 } 339 340 343 MBeanWrapper remove(ObjectName name, MBeanWrapper mbean) 344 { 345 synchronized (_mbeans) { 346 if (mbean != null && _mbeans.get(name) != mbean) 347 return null; 348 349 return _mbeans.remove(name); 350 } 351 } 352 353 356 public MBeanWrapper getMBean(ObjectName name) 357 { 358 synchronized (_mbeans) { 359 MBeanWrapper mbean = _mbeans.get(name); 360 if (mbean != null) 361 return mbean; 362 363 if (_classLoader == null) 364 return null; 365 366 MBeanView parentView = getParentView(); 367 368 if (parentView != null) 369 return parentView.getMBean(name); 370 else 371 return null; 372 } 373 } 374 375 378 void close() 379 { 380 _mbeans = null; 381 } 382 383 public String toString() 384 { 385 return "MBeanView[" + _classLoader + "]"; 386 } 387 388 } 389 390 | Popular Tags |