KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > controller > cache > CacheKeyFactory


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.controller.cache;
17
18 import java.util.*;
19 import com.jdon.util.Debug;
20 /**
21   * different cached object, there is different cache key.
22   * CacheKeyFactory is for creating cache key for different object
23   *
24  *
25  * @author banq
26  * @version 1.0
27  * @see {@link com.jdon.model.cache.ModelCacheKeyFactory}
28  * {@link com.jdon.model.query.cache.BlockCacheKeyFactory}
29  */

30 public abstract class CacheKeyFactory {
31   public final static String JavaDoc module = CacheKeyFactory.class.getName();
32
33   protected CacheManager cacheManager;
34
35   public CacheKeyFactory(CacheManager cacheManager){
36     this.cacheManager = cacheManager;
37   }
38
39   public CacheKey createCacheKey(Object JavaDoc dataKey, String JavaDoc typeName) {
40     CacheKey cacheKey = createCacheKeyImp(dataKey, typeName);
41     if (dataKey != null)
42        cacheManager.getCacheKeyMap().put(cacheKey.toString(), dataKey.toString());
43     return cacheKey;
44   }
45
46   public abstract CacheKey createCacheKeyImp(Object JavaDoc dataKey, String JavaDoc typeName);
47
48
49   public void removeCacheKey(String JavaDoc cacheKeyStr){
50      cacheManager.getCacheKeyMap().remove(cacheKeyStr);
51   }
52
53   /**
54     * look up all cachekeys that is equals to dataKey
55     * @param dataKey
56     * @return Collection
57     */

58    public synchronized Collection getAllCacheKey(Object JavaDoc dataKey){
59
60      String JavaDoc cacheKey = null;
61      String JavaDoc dataTmpKey = null;
62      Collection list = new ArrayList();
63
64      Map map = cacheManager.getCacheKeyMap();
65      Iterator iter = map.keySet().iterator();
66      while(iter.hasNext()){
67         cacheKey = (String JavaDoc)iter.next();
68         dataTmpKey = (String JavaDoc)map.get(cacheKey);
69         if (dataTmpKey.equals(dataKey)){
70            list.add(cacheKey);
71         }
72      }
73      return list;
74    }
75
76
77 }
78
Popular Tags