View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
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  package de.smartics.properties.spi.config.cache;
17  
18  import java.util.Set;
19  
20  import de.smartics.properties.resource.util.AbstractServiceFactory;
21  
22  /**
23   * Creates instances of the {@link CacheManager}.
24   * <p>
25   * Returns the manager for caches. The manager class is registered in
26   * <code>META-INF/services/de.smartics.properties.spi.config.cache.CacheManager</code>
27   * .
28   * </p>
29   * <p>
30   * Issues a warning if more than one implementation is found. Returns an
31   * instance of the first implementation found. If no type is specified, returns
32   * an instance of {@link AmnesiaCacheManager} per default.
33   * </p>
34   */
35  public final class CacheManagerFactory extends
36      AbstractServiceFactory<CacheManager>
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    /**
43     * The singleton.
44     */
45    private static final CacheManagerFactory INSTANCE = new CacheManagerFactory();
46  
47    /**
48     * The cache manager singleton.
49     */
50    private static final CacheManager MANAGER;
51  
52    // --- members --------------------------------------------------------------
53  
54    // ****************************** Initializer *******************************
55  
56    static
57    {
58      MANAGER = INSTANCE.create(InMemoryCacheManager.class);
59    }
60  
61    // ****************************** Constructors ******************************
62  
63    /**
64     * Default constructor.
65     */
66    private CacheManagerFactory()
67    {
68      super(CacheManager.class);
69    }
70  
71    // ****************************** Inner Classes *****************************
72  
73    // ********************************* Methods ********************************
74  
75    // --- init -----------------------------------------------------------------
76  
77    // --- get&set --------------------------------------------------------------
78  
79    // --- business -------------------------------------------------------------
80  
81    /**
82     * Returns the instance of the manager.
83     *
84     * @return the instance of the manager. Never <code>null</code>.
85     */
86    public static CacheManager getManager()
87    {
88      return MANAGER;
89    }
90  
91    /**
92     * Removes all keys from all caches.
93     */
94    public static void clearAll()
95    {
96      getManager().clearAll();
97    }
98  
99    /**
100    * Clears the cache with the given name.
101    *
102    * @param cacheName the name of the cache to be cleared.
103    */
104   public static void clear(final String cacheName)
105   {
106     getManager().clear(cacheName);
107   }
108 
109   /**
110    * Clears and stops the cache.
111    *
112    * @param cacheName the name of the cache to be cleared and stopped.
113    */
114   public static void discard(final String cacheName)
115   {
116     getManager().discard(cacheName);
117   }
118 
119   /**
120    * Stops all caches, rendering the manager in a stopped state.
121    */
122   public static void stopAll()
123   {
124     getManager().stopAll();
125   }
126 
127   /**
128    * Stops the cache with the given name.
129    *
130    * @param cacheName the name of the cache to stop.
131    */
132   public static void stop(final String cacheName)
133   {
134     getManager().stop(cacheName);
135   }
136 
137   /**
138    * Returns the cache with the given name. The cache is already started.
139    *
140    * @param cacheName the name of the cache to fetch.
141    * @return the requested cache already started.
142    */
143   public static UnawareCache<?, ?> getPropertiesCache(final String cacheName)
144   {
145     final UnawareCache<?, ?> cache = getManager().getPropertiesCache(cacheName);
146     return cache;
147   }
148 
149   /**
150    * Returns the cache with the given name. The cache is already started.
151    *
152    * @param cacheName the name of the cache to fetch.
153    * @return the requested cache already started.
154    */
155   public static Cache<?, ?> getConfigurationsCache(final String cacheName)
156   { // TODO TK ask RR generics
157     final Cache<?, ?> cache = getManager().getConfigurationsCache(cacheName);
158     return cache;
159   }
160 
161   /**
162    * Returns the set of cache names.
163    *
164    * @return the set of cache names.
165    */
166   public static Set<String> getCacheNames()
167   {
168     return getManager().getCacheNames();
169   }
170 
171   // --- object basics --------------------------------------------------------
172 
173 }