KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > stats > PWCFileCacheStatsImpl


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 package com.sun.enterprise.web.stats;
24
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.text.MessageFormat JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.MBeanServerFactory JavaDoc;
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
34 import javax.management.j2ee.statistics.Statistic JavaDoc;
35 import com.sun.logging.LogDomains;
36 import com.sun.enterprise.admin.monitor.stats.PWCFileCacheStats;
37 import com.sun.enterprise.admin.monitor.stats.MutableCountStatistic;
38 import com.sun.enterprise.admin.monitor.stats.MutableCountStatisticImpl;
39 import com.sun.enterprise.admin.monitor.stats.GenericStatsImpl;
40 import com.sun.enterprise.admin.monitor.stats.CountStatisticImpl;
41
42
43 /**
44  * <code>FileCache</code> monitoring support.
45  *
46  * @author Jeanfrancois Arcand
47  */

48 public class PWCFileCacheStatsImpl implements PWCFileCacheStats {
49     private final static Logger JavaDoc logger
50         = LogDomains.getLogger(LogDomains.WEB_LOGGER);
51     
52     private GenericStatsImpl baseStatsImpl;
53     private ObjectName JavaDoc fileCacheName;
54     private MBeanServer JavaDoc server;
55     
56     private MutableCountStatistic flagEnabled;
57     private MutableCountStatistic secondsMaxAge;
58     private MutableCountStatistic countEntries;
59     private MutableCountStatistic maxEntries;
60     private MutableCountStatistic countOpenEntries;
61     private MutableCountStatistic maxOpenEntries;
62     private MutableCountStatistic sizeHeapCache;
63     private MutableCountStatistic maxHeapCacheSize;
64     private MutableCountStatistic sizeMmapCache;
65     private MutableCountStatistic maxMmapCacheSize;
66     private MutableCountStatistic countHits;
67     private MutableCountStatistic countMisses;
68     private MutableCountStatistic countInfoHits;
69     private MutableCountStatistic countInfoMisses;
70     private MutableCountStatistic countContentHits;
71     private MutableCountStatistic countContentMisses;
72     
73     
74     public PWCFileCacheStatsImpl(String JavaDoc domain) {
75         
76         baseStatsImpl = new GenericStatsImpl(
77             com.sun.enterprise.admin.monitor.stats.PWCFileCacheStats.class,
78             this);
79         
80         // get an instance of the MBeanServer
81
ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
82         if(!servers.isEmpty())
83             server = (MBeanServer JavaDoc)servers.get(0);
84         else
85             server = MBeanServerFactory.createMBeanServer();
86         
87         String JavaDoc objNameStr = domain + ":type=PWCFileCache,*";
88         try {
89             fileCacheName = new ObjectName JavaDoc(objNameStr);
90         } catch (Throwable JavaDoc t) {
91             String JavaDoc msg = logger.getResourceBundle().getString(
92                                     "webcontainer.objectNameCreationError");
93             msg = MessageFormat.format(msg, new Object JavaDoc[] { objNameStr });
94             logger.log(Level.SEVERE, msg, t);
95         }
96
97         // initialize all the MutableStatistic Classes
98
initializeStatistics();
99     }
100     
101      
102     /**
103      * Returns flag indicating whether file cache has been enabled
104      * @return 1 if file cache has been enabled, 0 otherwise
105      */

106     public CountStatistic JavaDoc getFlagEnabled() {
107         flagEnabled.setCount(
108             StatsUtil.getMaxStatistic(server, fileCacheName,"flagEnabled"));
109         return (CountStatistic JavaDoc)flagEnabled.unmodifiableView();
110     }
111     
112     
113     /**
114      * Return the maximum age of a valid cache entry
115      * @return cache entry maximum age
116      */

117     public CountStatistic JavaDoc getSecondsMaxAge() {
118         secondsMaxAge.setCount(
119           StatsUtil.getMaxStatistic(server, fileCacheName,"secondsMaxAge"));
120         return (CountStatistic JavaDoc)secondsMaxAge.unmodifiableView();
121     }
122     
123     
124     /**
125      * Return the number of current cache entries.
126      */

127     public CountStatistic JavaDoc getCountEntries() {
128         countEntries.setCount(getAggregateLong("countEntries"));
129         return (CountStatistic JavaDoc)countEntries.unmodifiableView();
130     }
131     
132     
133     /**
134      * Return the maximum number of cache entries
135      */

136     public CountStatistic JavaDoc getMaxEntries() {
137         maxEntries.setCount(
138                 StatsUtil.getMaxStatistic(server, fileCacheName,"maxEntries"));
139         return (CountStatistic JavaDoc)maxEntries.unmodifiableView();
140     }
141     
142     
143     /**
144      * Return the number of current open cache entries
145      * @return open cache entries
146      */

147     public CountStatistic JavaDoc getCountOpenEntries() {
148         countOpenEntries.setCount(getAggregateLong("countOpenEntries"));
149         return (CountStatistic JavaDoc)countOpenEntries.unmodifiableView();
150     }
151     
152     
153     /**
154      * The Maximum number of open cache entries
155      */

156     public CountStatistic JavaDoc getMaxOpenEntries() {
157         maxOpenEntries.setCount(
158             StatsUtil.getMaxStatistic(server, fileCacheName,"maxOpenEntries"));
159         return (CountStatistic JavaDoc)maxOpenEntries.unmodifiableView();
160     }
161     
162     
163     /**
164      * The Heap space used for cache
165      * @return heap size
166      */

167     public CountStatistic JavaDoc getSizeHeapCache() {
168         sizeHeapCache.setCount(
169             StatsUtil.getMaxLongStatistic(server,
170                 fileCacheName,"sizeHeapCache"));
171         return (CountStatistic JavaDoc)sizeHeapCache.unmodifiableView();
172     }
173     
174     
175     /**
176      * Return he Maximum heap space used for cache
177      */

178     public CountStatistic JavaDoc getMaxHeapCacheSize() {
179         maxHeapCacheSize.setCount(
180             StatsUtil.getMaxLongStatistic(server,
181                 fileCacheName,"maxHeapCacheSize"));
182         return (CountStatistic JavaDoc)maxHeapCacheSize.unmodifiableView();
183     }
184     
185     
186     /**
187      * Return he size of Mapped memory used for caching
188      */

189     public CountStatistic JavaDoc getSizeMmapCache() {
190         sizeMmapCache.setCount(
191           StatsUtil.getMaxLongStatistic(server,
192                 fileCacheName,"sizeMmapCache"));
193         return (CountStatistic JavaDoc)sizeMmapCache.unmodifiableView();
194     }
195     
196     
197     /**
198      * Return the Maximum Memory Map size to be used for caching
199      */

200     public CountStatistic JavaDoc getMaxMmapCacheSize() {
201         maxMmapCacheSize.setCount(
202            StatsUtil.getMaxLongStatistic(server,
203                 fileCacheName,"maxMmapCacheSize"));
204         return (CountStatistic JavaDoc)maxMmapCacheSize.unmodifiableView();
205     }
206     
207     
208     /**
209      * Return he Number of cache lookup hits
210      */

211     public CountStatistic JavaDoc getCountHits() {
212         countHits.setCount(getAggregateLong("countHits"));
213         return (CountStatistic JavaDoc)countHits.unmodifiableView();
214     }
215     
216     
217     /**
218      * Return the Number of cache lookup misses
219      */

220     public CountStatistic JavaDoc getCountMisses() {
221         countMisses.setCount(getAggregateLong("countMisses"));
222         return (CountStatistic JavaDoc)countMisses.unmodifiableView();
223     }
224     
225     
226     /**
227      * Return the Number of hits on cached file info
228      */

229     public CountStatistic JavaDoc getCountInfoHits() {
230         countInfoHits.setCount(getAggregateLong("countInfoHits"));
231         return (CountStatistic JavaDoc)countInfoHits.unmodifiableView();
232     }
233     
234     
235     /**
236      * The Number of misses on cached file info
237      * @return misses on cache file info
238      */

239     public CountStatistic JavaDoc getCountInfoMisses() {
240         countInfoMisses.setCount(getAggregateLong("countInfoMisses"));
241         return (CountStatistic JavaDoc)countInfoMisses.unmodifiableView();
242     }
243     
244     
245     /**
246      * Return the Number of hits on cached file content
247      */

248     public CountStatistic JavaDoc getCountContentHits() {
249         countContentHits.setCount(getAggregateLong("countContentHits"));
250         return (CountStatistic JavaDoc)countContentHits.unmodifiableView();
251     }
252     
253     
254     /**
255      * Return the Number of misses on cached file content
256      */

257     public CountStatistic JavaDoc getCountContentMisses() {
258         countContentMisses.setCount(getAggregateLong("countContentMisses"));
259         return (CountStatistic JavaDoc)countContentMisses.unmodifiableView();
260     }
261
262     
263     /**
264      * This is an implementation of the mandatory JSR77 Stats
265      * interface method.
266      * Here we simply delegate it to the GenericStatsImpl object
267      * that we have
268      */

269     public Statistic JavaDoc[] getStatistics() {
270         return baseStatsImpl.getStatistics();
271     }
272
273     
274     public Statistic JavaDoc getStatistic( String JavaDoc str ) {
275         return baseStatsImpl.getStatistic( str );
276     }
277
278     
279     public String JavaDoc[] getStatisticNames() {
280         return baseStatsImpl.getStatisticNames();
281     }
282
283    
284     /**
285      * This method initialize statistics.
286      */

287     private void initializeStatistics() {
288         CountStatistic JavaDoc cs = null;
289         
290         //enabled?
291
cs = new CountStatisticImpl("FlagEnabled");
292         flagEnabled = new MutableCountStatisticImpl( cs );
293
294         //seconds Max Age
295
cs = new CountStatisticImpl("SecondsMaxAge");
296         secondsMaxAge = new MutableCountStatisticImpl( cs );
297
298         //count entries
299
cs = new CountStatisticImpl("CountEntries");
300         countEntries = new MutableCountStatisticImpl( cs );
301
302         //maxEntries
303
cs = new CountStatisticImpl("MaxEntries");
304         maxEntries = new MutableCountStatisticImpl( cs );
305
306         //Open Entries
307
cs = new CountStatisticImpl("CountOpenEntries");
308         countOpenEntries = new MutableCountStatisticImpl( cs );
309
310         //Max Open Entries
311
cs = new CountStatisticImpl("MaxOpenEntries");
312         maxOpenEntries = new MutableCountStatisticImpl( cs );
313
314         // heap cache size
315
cs = new CountStatisticImpl("SizeHeapCache");
316         sizeHeapCache = new MutableCountStatisticImpl( cs );
317
318         //max heap cache size
319
cs = new CountStatisticImpl("MaxHeapCacheSize");
320         maxHeapCacheSize = new MutableCountStatisticImpl( cs );
321
322         //Mmap cache size
323
cs = new CountStatisticImpl("SizeMmapCache");
324         sizeMmapCache = new MutableCountStatisticImpl( cs );
325
326         //Max Mmap cache size
327
cs = new CountStatisticImpl("MaxMmapCacheSize");
328         maxMmapCacheSize = new MutableCountStatisticImpl( cs );
329
330         //count hits
331
cs = new CountStatisticImpl("CountHits");
332         countHits = new MutableCountStatisticImpl( cs );
333
334         //count Misses
335
cs = new CountStatisticImpl("CountMisses");
336         countMisses = new MutableCountStatisticImpl( cs );
337
338         //count Info Hits
339
cs = new CountStatisticImpl("CountInfoHits");
340         countInfoHits = new MutableCountStatisticImpl( cs );
341
342         //count Info Misses
343
cs = new CountStatisticImpl("CountInfoMisses");
344         countInfoMisses = new MutableCountStatisticImpl( cs );
345
346         //content hits
347
cs = new CountStatisticImpl("CountContentHits");
348         countContentHits = new MutableCountStatisticImpl( cs );
349
350         //content misses
351
cs = new CountStatisticImpl("CountContentMisses");
352         countContentMisses = new MutableCountStatisticImpl( cs );
353
354     }
355     
356     
357     /**
358      * Get Aggregated int statistics.
359      */

360     private final int getAggregateInt(String JavaDoc attribute){
361         return StatsUtil.getAggregateStatistic(server,fileCacheName,attribute);
362     }
363     
364     
365     /**
366      * Get Aggregated long statistics.
367      */

368     private final long getAggregateLong(String JavaDoc attribute){
369         return StatsUtil.
370                 getAggregateLongStatistic(server,fileCacheName,attribute);
371     }
372 }
373
Popular Tags