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.constructs;
18  
19  import java.util.Properties;
20  
21  import net.sf.ehcache.Ehcache;
22  
23  /***
24   * An abstract factory for creating decorated Ehcache instances. Implementing classes should provide their own
25   * concrete factory extending this factory.
26   * 
27   * @author Abhishek Sanoujam
28   */
29  public abstract class CacheDecoratorFactory {
30  
31      /***
32       * Dash string : "-"
33       */
34      public static final String DASH = "-";
35  
36      /***
37       * Creates a decorated {@link Ehcache} using the properties specified for configuring the decorator.
38       * <p />
39       * If the returned decorated cache has the same name as the underlying cache, then the original cache will be replaced by this new
40       * decorated cache in the CacheManager.
41       * 
42       * @param cache
43       *            a reference to the owning cache
44       * @param properties
45       *            implementation specific properties configured as delimiter
46       *            separated name value pairs in ehcache.xml
47       * @return a decorated Ehcache
48       */
49      public abstract Ehcache createDecoratedEhcache(Ehcache cache, Properties properties);
50  
51      /***
52       * This method is called when the factory is specified for the defaultCache in the config.
53       * Create the decorated {@link Ehcache} using the properties specified.
54       * <p />
55       * If the returned decorated cache has the same name as the underlying cache, then the original cache will be replaced by this new
56       * decorated cache in the CacheManager.
57       * 
58       * @param cache
59       *            a reference to the owning cache
60       * @param properties
61       *            implementation specific properties configured as delimiter
62       *            separated name value pairs in ehcache.xml
63       * @return a decorated Ehcache
64       */
65      public abstract Ehcache createDefaultDecoratedEhcache(Ehcache cache, Properties properties);
66  
67      /***
68       * Utility method to generate name of decorated cache to be created using factory specified in defaultCache.
69       * 
70       * @param cache
71       *            the underlying cache
72       * @param cacheNameSuffix
73       *            Name to be used as suffix. This is normally provided as a property in the decorator config properties. If this parameter
74       *            is null or empty string, cache.getName() is returned
75       * @return Name to be used for the new decorated cache in the form of cache.getName() + "-" + cacheNameSuffix or cache.getName() if
76       *         cacheNameSuffix is null
77       */
78      public static String generateDefaultDecoratedCacheName(Ehcache cache, String cacheNameSuffix) {
79          if (cacheNameSuffix == null || cacheNameSuffix.trim().length() == 0) {
80              return cache.getName();
81          }
82          return cache.getName() + DASH + cacheNameSuffix;
83      }
84  
85  }