View Javadoc
1   /**
2    * This file Copyright (c) 2017-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.cache.ehcache3.configuration;
35  
36  import java.util.Collection;
37  
38  import org.ehcache.config.Builder;
39  import org.ehcache.config.CacheConfiguration;
40  import org.ehcache.config.EvictionAdvisor;
41  import org.ehcache.config.ResourcePools;
42  import org.ehcache.config.builders.CacheConfigurationBuilder;
43  import org.ehcache.core.config.ExpiryUtils;
44  import org.ehcache.expiry.Expiry;
45  import org.ehcache.expiry.ExpiryPolicy;
46  import org.ehcache.spi.service.ServiceConfiguration;
47  
48  /**
49   * Copy of {@link org.ehcache.core.config.BaseCacheConfiguration} with default ctor and setters/getters which are needed by N2B.
50   * @param <K> the key type for the cache
51   * @param <V> the value type for the cache
52   */
53  public class EhCache3ConfigurationBuilder<K, V> implements CacheConfiguration<K, V>, Builder<CacheConfiguration<K, V>> {
54  
55      private Collection<ServiceConfiguration<?>> serviceConfigurations;
56      private Class<K> keyType;
57      private Class<V> valueType;
58      private ClassLoader classLoader;
59      private EvictionAdvisor<? super K, ? super V> evictionAdvisor;
60      private ExpiryPolicy<? super K, ? super V> expiry = new EhCache3Expiry<>();
61      private Builder<ResourcePools> resourcePoolsBuilder = new Ehcache3ResourcePoolsBuilder();
62  
63      @Override
64      public Collection<ServiceConfiguration<?>> getServiceConfigurations() {
65          return serviceConfigurations;
66      }
67  
68      public void setServiceConfigurations(Collection<ServiceConfiguration<?>> serviceConfigurations) {
69          this.serviceConfigurations = serviceConfigurations;
70      }
71  
72      @Override
73      public Class<K> getKeyType() {
74          return keyType;
75      }
76  
77      public void setKeyType(Class<K> keyType) {
78          this.keyType = keyType;
79      }
80  
81      @Override
82      public Class<V> getValueType() {
83          return valueType;
84      }
85  
86      public void setValueType(Class<V> valueType) {
87          this.valueType = valueType;
88      }
89  
90      @Override
91      public EvictionAdvisor<? super K, ? super V> getEvictionAdvisor() {
92          return evictionAdvisor;
93      }
94  
95      public void setEvictionAdvisor(EvictionAdvisor<? super K, ? super V> evictionAdvisor) {
96          this.evictionAdvisor = evictionAdvisor;
97      }
98  
99      @Override
100     public ClassLoader getClassLoader() {
101         return classLoader;
102     }
103 
104     public void setClassLoader(ClassLoader classLoader) {
105         this.classLoader = classLoader;
106     }
107 
108     @Override
109     public ExpiryPolicy<? super K, ? super V> getExpiryPolicy() {
110         return expiry;
111     }
112 
113     public void setExpiry(ExpiryPolicy<? super K, ? super V> expiry) {
114         this.expiry = expiry;
115     }
116 
117     @Override
118     public Expiry<? super K, ? super V> getExpiry() {
119         return ExpiryUtils.convertToExpiry(getExpiryPolicy());
120     }
121 
122     @Override
123     public ResourcePools getResourcePools() {
124         return resourcePoolsBuilder.build();
125     }
126 
127     public Builder<ResourcePools> getResourcePoolsBuilder() {
128         return resourcePoolsBuilder;
129     }
130 
131     public void setResourcePoolsBuilder(Builder<ResourcePools> resourcePoolsBuilder) {
132         this.resourcePoolsBuilder = resourcePoolsBuilder;
133     }
134 
135     @Override
136     public CacheConfiguration<K, V> build() {
137         return CacheConfigurationBuilder.newCacheConfigurationBuilder(getKeyType(), getValueType(), getResourcePools())
138                 .withExpiry(getExpiryPolicy())
139                 .withClassLoader(getClassLoader())
140                 .withEvictionAdvisor(getEvictionAdvisor())
141                 .withResourcePools(getResourcePoolsBuilder().build())
142                 .build();
143     }
144 }