1 23 24 package com.sun.enterprise.admin.server.core.jmx; 25 26 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 import java.util.HashSet ; 31 32 import java.util.logging.Logger ; 34 import java.util.logging.Level ; 35 import com.sun.logging.LogDomains; 36 37 import javax.management.ObjectName ; 39 40 import com.sun.enterprise.admin.common.ObjectNames; 42 import com.sun.enterprise.admin.common.ObjectNameHelper; 43 import com.sun.enterprise.admin.common.CombinedPatternMatcher; 44 import com.sun.enterprise.admin.util.IPatternMatcher; 45 import com.sun.enterprise.admin.server.core.jmx.storage.PersistenceChecker; 46 import com.sun.enterprise.admin.server.core.jmx.storage.MBeanManufacturer; 47 48 55 56 public class DomainRepository implements IRepository 57 { 58 private static final Logger sLogger = 59 LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 60 61 private Hashtable mDomainTable = null; 62 63 68 69 public DomainRepository() 70 { 71 mDomainTable = new Hashtable (); 72 } 73 74 82 83 public boolean contains(ObjectName objectName) 84 { 85 return ( find(objectName) != null ); 86 } 87 88 94 95 public int getCount(String domainName) 96 { 97 return ( ((Hashtable ) mDomainTable.get(domainName)).size()); 98 } 99 100 110 111 public Object find(ObjectName objectName) 112 { 113 Object match = null; 114 115 if (objectName != null && 116 ! objectName.isPattern() && 117 ! objectName.isPropertyPattern()) 118 { 119 Hashtable domain = findRepository(objectName); 120 if (domain != null) 121 { 122 match = domain.get(objectName); 123 } 124 } 125 return ( match ); 126 } 127 128 137 public Object findPersistent(ObjectName objectName) 138 { 139 Object match = null, cachedMatch = null; 140 if (objectName != null && 141 ! objectName.isPattern() && 142 ! objectName.isPropertyPattern()) 143 { 144 Hashtable domain = findRepository(objectName); 145 if (domain != null) 146 { 147 cachedMatch = domain.get(objectName); 148 } 149 } 150 boolean isMonitorMBean = ObjectNameHelper.isMonitorMBean(objectName); 152 if (!isMonitorMBean) { 154 match = findInPersistentStore(objectName, cachedMatch); 155 } else { 156 match = cachedMatch; 157 } 158 return ( match ); 160 } 161 162 172 173 public boolean add(ObjectName objectName, Object mbeanImpl) 174 { 175 Hashtable domain = findRepository(objectName); 176 String domainName = objectName.getDomain(); 177 boolean added = false; 178 179 boolean newDomainRequired = ( domain == null ); 180 181 if (newDomainRequired) 182 { 183 domain = addNewDomain(domainName); 184 domain.put(objectName, mbeanImpl); 185 added = true; 186 } 187 else 188 { 189 boolean noMatchFound = ( domain.get(objectName) == null ); 190 if (noMatchFound) 191 { 192 domain.put(objectName, mbeanImpl); 193 added = true; 194 } 195 } 196 return ( added ); 197 } 198 199 206 207 public boolean remove(ObjectName objectName) 208 { 209 Hashtable domain = findRepository(objectName); 210 boolean removed = false; 211 212 if (domain != null) 213 { 214 Object mappedObject = domain.remove(objectName); 215 if (mappedObject != null) 216 { 217 removed = true; 218 } 219 } 220 return removed; 221 } 222 223 231 232 private Hashtable findRepository(ObjectName objectName) 233 { 234 String domainName = objectName.getDomain(); 235 236 return ( (Hashtable ) mDomainTable.get(domainName) ); 237 } 238 239 private Hashtable addNewDomain(String domainName) 240 { 241 Hashtable newRepository = new Hashtable (); 242 mDomainTable.put(domainName, newRepository); 243 return ( newRepository ); 244 } 245 246 251 252 public int getTotalCount() 253 { 254 int count = 0; 255 Iterator domainNames = mDomainTable.keySet().iterator(); 256 while (domainNames.hasNext()) 257 { 258 String domainName = (String ) domainNames.next(); 259 Hashtable aTable = (Hashtable )mDomainTable.get(domainName); 260 count = count + aTable.size(); 261 } 262 return ( count ); 263 } 264 265 271 272 public Set getAllMBeans() 273 { 274 Set mbeans = new HashSet (); 275 Iterator domainNames = mDomainTable.keySet().iterator(); 276 while (domainNames.hasNext()) 277 { 278 String domainName = (String ) domainNames.next(); 279 Hashtable aTable = (Hashtable )mDomainTable.get(domainName); 280 mbeans.addAll(aTable.keySet()); 281 } 282 return ( mbeans ); 283 } 284 285 296 297 public Set query(ObjectName objectName) 298 { 299 Set mbeans = null; 300 Set allMBeans = null; 301 String domainNamePattern = null; 302 Hashtable propertyTable = null; 303 304 if (objectName != null) 305 { 306 mbeans = new HashSet (); 307 if(!objectName.isPattern()) 308 { 309 if(this.contains(objectName)) 310 { 311 mbeans.add(objectName); 312 } 313 } 314 else 315 { 316 allMBeans = this.getAllMBeans(); 317 domainNamePattern = objectName.getDomain(); 318 propertyTable = objectName.getKeyPropertyList(); 319 320 Iterator allMBeanIter = allMBeans.iterator(); 321 while (allMBeanIter.hasNext()) 322 { 323 ObjectName sample = (ObjectName ) allMBeanIter.next(); 324 String sampleDomainName = sample.getDomain(); 325 326 boolean domainNameMatches = matchDomain( 327 domainNamePattern, sampleDomainName); 328 if (!domainNameMatches) 329 { 330 continue; 331 } 332 333 Hashtable samplePropertyTable = sample.getKeyPropertyList(); 334 boolean sampleMatches = this.matchPropertiesWithPattern(propertyTable, samplePropertyTable); 335 if (sampleMatches) 336 { 337 mbeans.add(sample); 338 } 339 } 340 } 341 } 342 return ( mbeans ); 343 } 344 345 private boolean matchPropertiesWithPattern(Hashtable pattern, Hashtable sample) 346 { 347 boolean currentMatch = true; 348 Iterator keyIter = pattern.keySet().iterator(); 349 while (currentMatch && keyIter.hasNext()) 350 { 351 String key = (String ) keyIter.next(); 352 String patternVal = (String ) pattern.get(key); 353 String sampleVal = (String ) sample.get(key); 354 currentMatch = patternVal.equals(sampleVal); 355 } 356 357 return ( currentMatch ); 358 } 359 360 361 private boolean matchDomain(String domainNamePattern, String testDomainName) 362 { 363 IPatternMatcher matcher = new CombinedPatternMatcher(domainNamePattern, 364 testDomainName); 365 366 return ( matcher.matches() ); 367 } 368 388 389 private Object findInPersistentStore(ObjectName objectName, Object 390 cachedObject) 391 { 392 String type = ObjectNameHelper.getType (objectName); 395 if (type.equals(ObjectNames.kController) || 396 type.equals(ObjectNames.kGenericConfigurator)) 397 { 398 return cachedObject; 399 } 400 PersistenceChecker checker = new PersistenceChecker(); 401 Object storedObject = null; 402 try { 403 storedObject = checker.findElement(objectName); 404 } catch (Exception e) 405 { 406 } 407 408 Object match = null; 409 if (storedObject != null) 410 { 411 if (cachedObject != null) 412 { 413 419 match = cachedObject; 420 } 421 else 422 { 423 428 MBeanManufacturer producer = new MBeanManufacturer(objectName, storedObject); 429 match = producer.createMBeanInstance(); 430 this.add(objectName, match); 431 } 435 } 436 else { 438 match = null; if (cachedObject != null) 440 { 441 442 this.remove(objectName); 443 } 446 else 447 { 448 452 } 453 } 454 return ( match ); 456 } 457 } 458 | Popular Tags |