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   * @author Abhishek Sanoujam
25   */
26  public class MockDecoratorFactory extends CacheDecoratorFactory {
27  
28      @Override
29      public Ehcache createDecoratedEhcache(Ehcache cache, Properties properties) {
30          return new MockDecoratorFactoryCache(cache, properties, false);
31      }
32  
33      /***
34       * {@inheritDoc}
35       */
36      @Override
37      public Ehcache createDefaultDecoratedEhcache(Ehcache cache, Properties properties) {
38          return new MockDecoratorFactoryCache(cache, properties, true);
39      }
40  
41      public static class MockDecoratorFactoryCache extends EhcacheDecoratorAdapter {
42  
43          private final Properties properties;
44          private final String name;
45  
46          public MockDecoratorFactoryCache(Ehcache underlyingCache, Properties properties, boolean forDefaultCache) {
47              super(underlyingCache);
48              this.properties = properties;
49              String tmpName = properties.getProperty("name");
50              if (forDefaultCache) {
51                  if (tmpName == null || tmpName.trim().equals("")) {
52                      tmpName = underlyingCache.getName();
53                  } else {
54                      tmpName = CacheDecoratorFactory.generateDefaultDecoratedCacheName(underlyingCache, tmpName);
55                  }
56              }
57              this.name = tmpName;
58          }
59  
60          public Properties getProperties() {
61              return properties;
62          }
63  
64          @Override
65          public String getName() {
66              return this.name;
67          }
68  
69          @Override
70          public String toString() {
71              return "DecoratedTestEhcache[name=" + name + "]";
72          }
73  
74      }
75  
76  }