1 package org.tigris.scarab.services.cache; 2 3 48 49 import java.io.Serializable ; 50 import java.util.HashMap ; 51 import java.util.Iterator ; 52 import java.util.Map ; 53 import java.util.WeakHashMap ; 54 55 import org.apache.avalon.framework.activity.Initializable; 56 import org.apache.avalon.framework.logger.AbstractLogEnabled; 57 import org.apache.avalon.framework.service.ServiceManager; 58 import org.apache.avalon.framework.service.Serviceable; 59 import org.apache.fulcrum.InitializationException; 60 import org.apache.fulcrum.pool.PoolService; 61 import org.tigris.scarab.tools.localization.L10NKeySet; 62 import org.tigris.scarab.util.Log; 63 import org.tigris.scarab.util.ScarabRuntimeException; 64 65 72 public class DefaultScarabCacheService 73 extends AbstractLogEnabled implements ScarabCacheService, Serviceable, Initializable 74 { 75 76 77 private Map maps; 79 private Class keyClass; 80 81 private PoolService poolService; 82 private ServiceManager manager; 83 84 public DefaultScarabCacheService() 85 { 86 } 87 88 public Map getMapImpl() 89 { 90 Thread t = Thread.currentThread(); 91 Map map = (Map )maps.get(t); 92 if (map == null) 93 { 94 map = new HashMap (); 95 maps.put(t, map); 96 } 97 98 return map; 99 } 100 101 public void clearImpl() 102 { 103 Map map = (Map )maps.get(Thread.currentThread()); 104 if (map != null) 105 { 106 Iterator i = map.keySet().iterator(); 107 while (i.hasNext()) 108 { 109 poolService.putInstance(i.next()); 110 } 111 map.clear(); 112 } 113 } 114 115 public Object getImpl(Serializable instanceOrClass, String method) 116 { 117 Object result = null; 118 try 119 { 120 ScarabCacheKey key = 121 (ScarabCacheKey)poolService.getInstance(keyClass); 122 key.init(instanceOrClass, method); 123 result = getMapImpl().get(key); 124 } 125 catch (Exception e) 126 { 127 Log.get().error(e); 128 } 129 return result; 130 } 131 132 public Object getImpl(Serializable instanceOrClass, String method, 133 Serializable arg1) 134 { 135 Object result = null; 136 try 137 { 138 ScarabCacheKey key = 139 (ScarabCacheKey)poolService.getInstance(keyClass); 140 key.init(instanceOrClass, method, arg1); 141 result = getMapImpl().get(key); 142 } 143 catch (Exception e) 144 { 145 Log.get().error(e); 146 } 147 return result; 148 } 149 150 public Object getImpl(Serializable instanceOrClass, String method, 151 Serializable arg1, Serializable arg2) 152 { 153 Object result = null; 154 try 155 { 156 ScarabCacheKey key = 157 (ScarabCacheKey)poolService.getInstance(keyClass); 158 key.init(instanceOrClass, method, arg1, arg2); 159 result = getMapImpl().get(key); 160 } 161 catch (Exception e) 162 { 163 Log.get().error(e); 164 } 165 return result; 166 } 167 168 public Object getImpl(Serializable instanceOrClass, String method, 169 Serializable arg1, Serializable arg2, 170 Serializable arg3) 171 { 172 Object result = null; 173 try 174 { 175 ScarabCacheKey key = 176 (ScarabCacheKey)poolService.getInstance(keyClass); 177 key.init(instanceOrClass, method, arg1, arg2, arg3); 178 result = getMapImpl().get(key); 179 } 180 catch (Exception e) 181 { 182 Log.get().error(e); 183 } 184 return result; 185 } 186 187 public Object getImpl(Serializable [] keys) 188 { 189 Object result = null; 190 try 191 { 192 ScarabCacheKey key = 193 (ScarabCacheKey)poolService.getInstance(keyClass); 194 key.init(keys); 195 result = getMapImpl().get(key); 196 } 197 catch (Exception e) 198 { 199 Log.get().error(e); 200 } 201 return result; 202 } 203 204 public void putImpl(Object value, Serializable instanceOrClass, 205 String method) 206 { 207 try 208 { 209 ScarabCacheKey key = 210 (ScarabCacheKey)poolService.getInstance(keyClass); 211 key.init(instanceOrClass, method); 212 getMapImpl().put(key, value); 213 } 214 catch (Exception e) 215 { 216 Log.get().error(e); 217 } 218 } 219 220 public void putImpl(Object value, Serializable instanceOrClass, 221 String method, Serializable arg1) 222 { 223 try 224 { 225 ScarabCacheKey key = 226 (ScarabCacheKey)poolService.getInstance(keyClass); 227 key.init(instanceOrClass, method, arg1); 228 getMapImpl().put(key, value); 229 } 230 catch (Exception e) 231 { 232 Log.get().error(e); 233 } 234 } 235 236 public void putImpl(Object value, Serializable instanceOrClass, 237 String method, Serializable arg1, Serializable arg2) 238 { 239 try 240 { 241 ScarabCacheKey key = 242 (ScarabCacheKey)poolService.getInstance(keyClass); 243 key.init(instanceOrClass, method, arg1, arg2); 244 getMapImpl().put(key, value); 245 } 246 catch (Exception e) 247 { 248 Log.get().error(e); 249 } 250 } 251 252 public void putImpl(Object value, Serializable instanceOrClass, 253 String method, Serializable arg1, Serializable arg2, 254 Serializable arg3) 255 { 256 try 257 { 258 ScarabCacheKey key = 259 (ScarabCacheKey)poolService.getInstance(keyClass); 260 key.init(instanceOrClass, method, arg1, arg2, arg3); 261 getMapImpl().put(key, value); 262 } 263 catch (Exception e) 264 { 265 Log.get().error(e); 266 } 267 } 268 269 public void putImpl(Object value, Serializable [] keys) 270 { 271 try 272 { 273 ScarabCacheKey key = 274 (ScarabCacheKey)poolService.getInstance(keyClass); 275 key.init(keys); 276 getMapImpl().put(key, value); 277 } 278 catch (Exception e) 279 { 280 Log.get().error(e); 281 } 282 } 283 284 285 289 public void service(ServiceManager manager) 290 { 291 this.manager = manager; 292 } 293 294 301 public void initialize() throws Exception 302 { 303 try 304 { 305 poolService = (PoolService) manager.lookup(PoolService.ROLE); 306 } 307 catch (Exception e) 308 { 309 throw new ScarabRuntimeException( 310 L10NKeySet.ExceptionScarabCacheService, e); 311 } 312 maps = new WeakHashMap (); 313 try 314 { 315 keyClass = Class 316 .forName("org.tigris.scarab.services.cache.ScarabCacheKey"); 317 } 318 catch (Exception x) 319 { 320 throw new InitializationException( 321 "Failed to initialize ScarabCache",x); } 323 324 } 325 } 326 | Popular Tags |