KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > jmx > DomainRepository


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.server.core.jmx;
25
26 //JDK imports
27
import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.HashSet JavaDoc;
31
32 //Logging imports
33
import java.util.logging.Logger JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import com.sun.logging.LogDomains;
36
37 //JMX imports
38
import javax.management.ObjectName JavaDoc;
39
40 //Admin imports
41
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 /**
49     An implementation of IRepository based on Hashtable. Encapsulates
50     all the domain. One Hashtable is allocated per domain.
51  
52     @author Kedar Mhaswade
53     @version 1.0
54 */

55
56 public class DomainRepository implements IRepository
57 {
58     private static final Logger JavaDoc sLogger =
59             LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
60
61     private Hashtable JavaDoc mDomainTable = null;
62
63     /**
64         Creates new Repository, with the default domain name from
65         the ServiceName class. This will be the central repository of
66         all MBeans for a particular domain.
67     */

68     
69     public DomainRepository()
70     {
71         mDomainTable = new Hashtable JavaDoc();
72     }
73     
74     /**
75         Tests whether an Object with given ObjectName is present in this
76         Repository.
77
78         @param objectName - ObjectName of the Object to be searched for. ObjectName
79                 can't be a Pattern or PropertyPattern.
80         @return true if the Object could be found, false otherwise.
81     */

82     
83     public boolean contains(ObjectName JavaDoc objectName)
84     {
85         return ( find(objectName) != null );
86     }
87     
88     /**
89         Returns the number of Objects stored in this repository for
90         given domain, the domain's name may not be null.
91
92         @param domainName - the name of domain.
93     */

94     
95     public int getCount(String JavaDoc domainName)
96     {
97     return ( ((Hashtable JavaDoc) mDomainTable.get(domainName)).size());
98     }
99     
100     /**
101         Returns the Object with given ObjectName. Returns null if there
102         is no Object with given ObjectName. Passed ObjectName may not be null.
103         @param objectName - ObjectName of the Object to be searched for.
104         Note that ObjectName may not be a pattern for query or pattern
105         on key properties.
106
107         @return Object searched, null if there is none. Also returns null,
108         if the ObjectName is pattern.
109     */

110     
111     public Object JavaDoc find(ObjectName JavaDoc objectName)
112     {
113         Object JavaDoc match = null;
114         
115         if (objectName != null &&
116             ! objectName.isPattern() &&
117             ! objectName.isPropertyPattern())
118         {
119             Hashtable JavaDoc domain = findRepository(objectName);
120             if (domain != null)
121             {
122                 match = domain.get(objectName);
123             }
124         }
125         return ( match );
126     }
127     
128     /**
129         Makes the check for the existence of corresponding element in the
130         persistent store. This method will also register or unregister
131         the MBeans as required by adding/removing them, depending on its existence
132         in the persistent store.
133         @return MBean that is registered, null otherwise. In case of the MBean
134         that exists in both MBeanRepository and persistent store simply the
135         object in memory is returned.
136     */

137     public Object JavaDoc findPersistent(ObjectName JavaDoc objectName)
138     {
139         Object JavaDoc match = null, cachedMatch = null;
140         if (objectName != null &&
141             ! objectName.isPattern() &&
142             ! objectName.isPropertyPattern())
143         {
144             Hashtable JavaDoc domain = findRepository(objectName);
145             if (domain != null)
146             {
147                 cachedMatch = domain.get(objectName);
148             }
149         }
150         //sLogger.info("*In find: INFO: Now testing new lazyinit stuff....b4");
151
boolean isMonitorMBean = ObjectNameHelper.isMonitorMBean(objectName);
152         if (!isMonitorMBean) //don't check for monitorMBeans
153
{
154             match = findInPersistentStore(objectName, cachedMatch);
155         } else {
156             match = cachedMatch;
157         }
158         //sLogger.info("*In find:INFO: match from store = " + match);
159
return ( match );
160     }
161     
162     /**
163         Adds an Object in the repository. Neither Object nor the ObjectName
164         should be null. Given Object will not be added if an Object with the
165         same ObjectName already exists in the repository.
166
167         @param objectName - ObjectName of the Object to be stored.
168         @param mbeanImpl - Object that is to be stored.
169
170         @return true if the addition was successful, false otherwise.
171     */

172     
173     public boolean add(ObjectName JavaDoc objectName, Object JavaDoc mbeanImpl)
174     {
175         Hashtable JavaDoc domain = findRepository(objectName);
176         String JavaDoc 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     /**
200         Removes the Object with given ObjectName from repository. The passed
201         ObjectName must not be null.
202
203         @param objectName - ObjectName of the Object to be removed.
204         @return true if the Object could be removed, false otherwise.
205     */

206     
207     public boolean remove(ObjectName JavaDoc objectName)
208     {
209         Hashtable JavaDoc domain = findRepository(objectName);
210         boolean removed = false;
211
212         if (domain != null)
213         {
214                 Object JavaDoc mappedObject = domain.remove(objectName);
215                 if (mappedObject != null)
216                 {
217                 removed = true;
218                 }
219         }
220         return removed;
221     }
222   
223     /**
224         Finds the repository for the given objectName. Given objectName
225         may not be null. This is helper method.
226
227         @return the Hashtable for MBean with this objectName, null if
228         there is no such Hashtable. Note that all the MBeans in hashtable for
229         domain are having same domainName.
230     */

231     
232     private Hashtable JavaDoc findRepository(ObjectName JavaDoc objectName)
233     {
234         String JavaDoc domainName = objectName.getDomain();
235
236         return ( (Hashtable JavaDoc) mDomainTable.get(domainName) );
237     }
238     
239     private Hashtable JavaDoc addNewDomain(String JavaDoc domainName)
240     {
241         Hashtable JavaDoc newRepository = new Hashtable JavaDoc();
242         mDomainTable.put(domainName, newRepository);
243         return ( newRepository );
244     }
245     
246     /**
247         Returns the total number of MBeans stored in entire repository.
248
249         @return int representing all MBeans in this repository.
250     */

251     
252     public int getTotalCount()
253     {
254         int count = 0;
255         Iterator JavaDoc domainNames = mDomainTable.keySet().iterator();
256         while (domainNames.hasNext())
257         {
258                 String JavaDoc domainName = (String JavaDoc) domainNames.next();
259                 Hashtable JavaDoc aTable = (Hashtable JavaDoc)mDomainTable.get(domainName);
260                 count = count + aTable.size();
261         }
262         return ( count );
263     }
264     
265     /**
266         Method to get ALL MBeans in all domains in this repository.
267         The returned Set contains the ObjectNames of all MBeans.
268
269         @return Set containing MBean ObjectNames, null if none exists.
270     */

271     
272     public Set JavaDoc getAllMBeans()
273     {
274         Set JavaDoc mbeans = new HashSet JavaDoc();
275         Iterator JavaDoc domainNames = mDomainTable.keySet().iterator();
276         while (domainNames.hasNext())
277         {
278                 String JavaDoc domainName = (String JavaDoc) domainNames.next();
279                 Hashtable JavaDoc aTable = (Hashtable JavaDoc)mDomainTable.get(domainName);
280                 mbeans.addAll(aTable.keySet());
281         }
282         return ( mbeans );
283     }
284     
285     /**
286         Returns a Set of Objects whose ObjectNames match zero or more Objects in the
287         repository as per the pattern suggested by the argument. If the argument
288         is NOT a pattern an exact match will be performed.
289
290         @param objectName the ObjectName that may represent a pattern. The
291         objectName may not be null.
292
293         @return Set of all Objects that match the pattern in argument, null if
294         there is no such object.
295     */

296
297     public Set JavaDoc query(ObjectName JavaDoc objectName)
298     {
299         Set JavaDoc mbeans = null;
300         Set JavaDoc allMBeans = null;
301         String JavaDoc domainNamePattern = null;
302         Hashtable JavaDoc propertyTable = null;
303
304         if (objectName != null)
305         {
306             mbeans = new HashSet JavaDoc();
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 JavaDoc allMBeanIter = allMBeans.iterator();
321                 while (allMBeanIter.hasNext())
322                 {
323                     ObjectName JavaDoc sample = (ObjectName JavaDoc) allMBeanIter.next();
324                     String JavaDoc sampleDomainName = sample.getDomain();
325
326                     boolean domainNameMatches = matchDomain(
327                         domainNamePattern, sampleDomainName);
328                     if (!domainNameMatches)
329                     {
330                         continue;
331                     }
332
333                     Hashtable JavaDoc 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 JavaDoc pattern, Hashtable JavaDoc sample)
346     {
347         boolean currentMatch = true;
348         Iterator JavaDoc keyIter = pattern.keySet().iterator();
349         while (currentMatch && keyIter.hasNext())
350         {
351             String JavaDoc key = (String JavaDoc) keyIter.next();
352             String JavaDoc patternVal = (String JavaDoc) pattern.get(key);
353             String JavaDoc sampleVal = (String JavaDoc) sample.get(key);
354             currentMatch = patternVal.equals(sampleVal);
355         }
356
357         return ( currentMatch );
358     }
359
360     
361     private boolean matchDomain(String JavaDoc domainNamePattern, String JavaDoc testDomainName)
362     {
363         IPatternMatcher matcher = new CombinedPatternMatcher(domainNamePattern,
364             testDomainName);
365         
366         return ( matcher.matches() );
367     }
368     /**
369         This is where the check into persistent
370         storage registry comes into picture. So first
371         check whether a Bean corresponding exactly to this
372         objectName exists in the Config API. For now, this check
373         is controlled by a boolean flag mDoLazyInit, which is
374         set to true.
375         All the MBeans that have corresponding config beans
376         will be checked here. Only ServerController and GenericConfigurator
377         won't be.
378         This method operates on the given ObjectName and finds whether
379         corresponding Bean exists in storage. It modifies the internal
380         cached registry based on its findings. Thus note that this method
381         indeed modifies the state of MBeanServer though it is only a finder.
382         @param objectName ObjectName of the MBean to be found, may not be null.
383         @param cachedObject Object reference to the corresponding MBean in
384         internal registry. This is accepted for the sake of optimization. If this
385         is null, it means that the MBean is not in cache.
386         @return the object found, null, if there is no such Object in persistent store.
387     */

388
389     private Object JavaDoc findInPersistentStore(ObjectName JavaDoc objectName, Object JavaDoc
390             cachedObject)
391     {
392         //for Generic Configurator and ServerController, just return the passed
393
// as these will NEVER be there in persistent store.
394
String JavaDoc 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 JavaDoc storedObject = null;
402         try {
403             storedObject = checker.findElement(objectName);
404         } catch (Exception JavaDoc e)
405         {
406         }
407         
408         Object JavaDoc match = null;
409         if (storedObject != null)
410         {
411             if (cachedObject != null)
412             {
413                 //sLogger.info("*In findInPersistentStore: stored-cached nonnull");
414
/*
415                     MBean is registered, cachedObject is in sync with storedObject,
416                     no need to do anything. Most of the times, this is the case.
417                     Hence simply return the cached object.
418                 */

419                 match = cachedObject;
420             }
421             else
422             {
423                 /*
424                     We will have to construct the proper Object
425                     and register it as MBean. Will be invoked: for the
426                     first time any MBean is invoked.
427                 */

428                 MBeanManufacturer producer = new MBeanManufacturer(objectName, storedObject);
429                 match = producer.createMBeanInstance();
430                 this.add(objectName, match);
431                 //sLogger.info("*In findInPersistentStore: " +
432
//"stored non null-cached null - constructing a new MBean " +
433
//match.getClass().getName() );
434
}
435         }
436         else //Not found in the persistent store
437
{
438             match = null; // if not found in persistent store, just return null.
439
if (cachedObject != null)
440             {
441                 /* This means that the cached copy is stale and out of sync, remove it */
442                 this.remove(objectName);
443                 //sLogger.info("*In findInPersistentStore: stored is null, "
444
// + "cached is non-null, removed it");
445
}
446             else
447             {
448                 //sLogger.info("*In findInPersistentStore: stored is null, "
449
// + "cached is null, no prob");
450
/* do nothing, as persistent store does not
451                  have it and the cached store also does not have it */

452             }
453         }
454         //Now return the stored object, be it null or non-null.
455
return ( match );
456     }
457 }
458
Popular Tags