View Javadoc

1   package org.apache.jcs.access.monitor;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // some junk to return for a synchronous call
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 }