| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.jcs.access.monitor.MonitorAccess |
|
|
| 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 | 0 | 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 | 0 | { |
| 53 | 0 | synchronized ( GroupCacheAccess.class ) |
| 54 | { |
|
| 55 | 0 | if ( this.cacheMgr == null ) |
| 56 | { |
|
| 57 | 0 | this.cacheMgr = CompositeCacheManager.getInstance(); |
| 58 | } |
|
| 59 | 0 | } |
| 60 | 0 | } |
| 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 | 0 | String result = ""; |
| 73 | ||
| 74 | try |
|
| 75 | { |
|
| 76 | 0 | ICache cache = this.cacheMgr.getCache( cacheName ); |
| 77 | ||
| 78 | 0 | if ( key != null ) |
| 79 | { |
|
| 80 | 0 | if ( key.toUpperCase().equals( "ALL" ) ) |
| 81 | { |
|
| 82 | 0 | cache.removeAll(); |
| 83 | ||
| 84 | 0 | if ( log.isDebugEnabled() ) |
| 85 | { |
|
| 86 | 0 | log.debug( "Removed all elements from " + cacheName ); |
| 87 | } |
|
| 88 | 0 | result = "key = " + key; |
| 89 | 0 | } |
| 90 | else |
|
| 91 | { |
|
| 92 | 0 | if ( log.isDebugEnabled() ) |
| 93 | { |
|
| 94 | 0 | log.debug( "key = " + key ); |
| 95 | } |
|
| 96 | 0 | result = "key = " + key; |
| 97 | 0 | StringTokenizer toke = new StringTokenizer( key, "_" ); |
| 98 | ||
| 99 | 0 | while ( toke.hasMoreElements() ) |
| 100 | { |
|
| 101 | 0 | String temp = (String) toke.nextElement(); |
| 102 | 0 | cache.remove( key ); |
| 103 | ||
| 104 | 0 | if ( log.isDebugEnabled() ) |
| 105 | { |
|
| 106 | 0 | log.debug( "Removed " + temp + " from " + cacheName ); |
| 107 | } |
|
| 108 | 0 | } |
| 109 | } |
|
| 110 | 0 | } |
| 111 | else |
|
| 112 | { |
|
| 113 | 0 | result = "key is null"; |
| 114 | } |
|
| 115 | ||
| 116 | } |
|
| 117 | 0 | catch ( Exception e ) |
| 118 | { |
|
| 119 | 0 | log.error( e ); |
| 120 | 0 | } |
| 121 | ||
| 122 | 0 | 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 | 0 | ArrayList data = new ArrayList(); |
| 133 | ||
| 134 | 0 | String[] list = this.cacheMgr.getCacheNames(); |
| 135 | 0 | Arrays.sort( list ); |
| 136 | 0 | for ( int i = 0; i < list.length; i++ ) |
| 137 | { |
|
| 138 | 0 | Hashtable ht = new Hashtable(); |
| 139 | 0 | String name = list[i]; |
| 140 | 0 | ht.put( "name", name ); |
| 141 | ||
| 142 | 0 | ICache cache = this.cacheMgr.getCache( name ); |
| 143 | 0 | int size = cache.getSize(); |
| 144 | 0 | ht.put( "size", Integer.toString( size ) ); |
| 145 | ||
| 146 | 0 | int status = cache.getStatus(); |
| 147 | 0 | 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 | 0 | ht.put( "stat", stat ); |
| 155 | ||
| 156 | 0 | data.add( ht ); |
| 157 | } |
|
| 158 | 0 | return data; |
| 159 | } |
|
| 160 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |