View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.loader;
18  
19  import net.sf.ehcache.AbstractCacheTest;
20  import net.sf.ehcache.CacheManager;
21  import net.sf.ehcache.Ehcache;
22  import net.sf.ehcache.Element;
23  
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  /***
29   * Written for Dead-lock poc
30   *
31   * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
32   * @version $Id: CacheHelper.html 13146 2011-08-01 17:12:39Z oletizi $
33   */
34  public final class CacheHelper {
35  
36      private static Map managers = new HashMap();
37  
38  
39      /***
40       * Utility class
41       */
42      private CacheHelper() {
43          //noop
44      }
45  
46  
47      /***
48       * Initialises the CacheHelper
49       */
50      public static void init() {
51          managers = new HashMap();
52      }
53  
54      /***
55       * @param cacheManagerUrl
56       * @param cacheName
57       * @return
58       */
59      public static Ehcache getCache(String cacheManagerUrl, String cacheName) {
60          CacheManager mgr = (CacheManager) managers.get(cacheManagerUrl);
61  
62          if (mgr == null) {
63              mgr = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + cacheManagerUrl);
64              //Requires the config file to be in the classpath, which is not how we test specific configs in these tests
65              //mgr = new CacheManager(Thread.currentThread().getContextClassLoader().getResourceAsStream(cacheManagerUrl));
66              managers.put(cacheManagerUrl, mgr);
67          }
68  
69          return mgr.getEhcache(cacheName);
70      }
71  
72      /***
73       * @param cacheManagerUrl
74       * @param cacheName
75       * @param key
76       * @return
77       */
78      public static Object get(String cacheManagerUrl, String cacheName, String key) {
79          return get(cacheManagerUrl, cacheName, key, null);
80      }
81  
82      /***
83       * @param cacheManagerUrl
84       * @param cacheName
85       * @param key
86       * @param arguments
87       * @return
88       */
89      public static Object get(String cacheManagerUrl, String cacheName, String key, Object arguments) {
90          Ehcache cache = getCache(cacheManagerUrl, cacheName);
91          Element elem = cache.getWithLoader(key, null, arguments);
92          return elem.getObjectValue();
93      }
94  
95      /***
96       *
97       */
98      public static void shutdown() {
99          for (Iterator iter = managers.values().iterator(); iter.hasNext();) {
100             ((CacheManager) iter.next()).shutdown();
101         }
102     }
103 
104 }