1 package org.apache.jcs.access.monitor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.Serializable;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Hashtable;
26 import java.util.StringTokenizer;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.jcs.access.GroupCacheAccess;
31 import org.apache.jcs.engine.CacheConstants;
32 import org.apache.jcs.engine.behavior.ICache;
33 import org.apache.jcs.engine.control.CompositeCacheManager;
34
35 /***
36 * Exposes the simple monitoring methods to the public in a simple manner.
37 */
38 public class MonitorAccess
39 implements Serializable
40 {
41 /*** Don't change. */
42 private static final long serialVersionUID = 1002037665133774391L;
43
44 /*** The logger. */
45 private static final Log log = LogFactory.getLog( MonitorAccess.class );
46
47 /*** Description of the Field */
48 protected CompositeCacheManager cacheMgr;
49
50 /*** Constructor for the MonitorAccess object */
51 public MonitorAccess()
52 {
53 synchronized ( GroupCacheAccess.class )
54 {
55 if ( this.cacheMgr == null )
56 {
57 this.cacheMgr = CompositeCacheManager.getInstance();
58 }
59 }
60 }
61
62 /***
63 * Removes all.
64 * <p>
65 * @param cacheName
66 * @param key
67 * @return an informative message about what was deleted.
68 */
69 public String delete( String cacheName, String key )
70 {
71
72 String result = "";
73
74 try
75 {
76 ICache cache = this.cacheMgr.getCache( cacheName );
77
78 if ( key != null )
79 {
80 if ( key.toUpperCase().equals( "ALL" ) )
81 {
82 cache.removeAll();
83
84 if ( log.isDebugEnabled() )
85 {
86 log.debug( "Removed all elements from " + cacheName );
87 }
88 result = "key = " + key;
89 }
90 else
91 {
92 if ( log.isDebugEnabled() )
93 {
94 log.debug( "key = " + key );
95 }
96 result = "key = " + key;
97 StringTokenizer toke = new StringTokenizer( key, "_" );
98
99 while ( toke.hasMoreElements() )
100 {
101 String temp = (String) toke.nextElement();
102 cache.remove( key );
103
104 if ( log.isDebugEnabled() )
105 {
106 log.debug( "Removed " + temp + " from " + cacheName );
107 }
108 }
109 }
110 }
111 else
112 {
113 result = "key is null";
114 }
115
116 }
117 catch ( Exception e )
118 {
119 log.error( e );
120 }
121
122 return result;
123 }
124
125 /***
126 * Gives basic info on all the regions. Better to use getStats.
127 * <p>
128 * @return list of hashtables with keys (name,size,stat)
129 */
130 public ArrayList overview()
131 {
132 ArrayList data = new ArrayList();
133
134 String[] list = this.cacheMgr.getCacheNames();
135 Arrays.sort( list );
136 for ( int i = 0; i < list.length; i++ )
137 {
138 Hashtable ht = new Hashtable();
139 String name = list[i];
140 ht.put( "name", name );
141
142 ICache cache = this.cacheMgr.getCache( name );
143 int size = cache.getSize();
144 ht.put( "size", Integer.toString( size ) );
145
146 int status = cache.getStatus();
147 String stat = status == CacheConstants.STATUS_ALIVE
148 ? "ALIVE"
149 : status == CacheConstants.STATUS_DISPOSED
150 ? "DISPOSED"
151 : status == CacheConstants.STATUS_ERROR
152 ? "ERROR"
153 : "UNKNOWN";
154 ht.put( "stat", stat );
155
156 data.add( ht );
157 }
158 return data;
159 }
160 }