View Javadoc
1   /*
2    *    Copyright 2012-2022 the original author or authors.
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    *       https://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 org.mybatis.caches.memcached;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Properties;
23  
24  /**
25   * Converter from the Config to a proper {@link MemcachedConfiguration}.
26   *
27   * @author Simone Tripodi
28   */
29  final class MemcachedConfigurationBuilder {
30  
31    /**
32     * This class instance.
33     */
34    private static final MemcachedConfigurationBuilder INSTANCE = new MemcachedConfigurationBuilder();
35  
36    private static final String SYSTEM_PROPERTY_MEMCACHED_PROPERTIES_FILENAME = "memcached.properties.filename";
37  
38    /**
39     *
40     */
41    private static final String MEMCACHED_RESOURCE = "memcached.properties";
42  
43    private final String memcachedPropertiesFilename;
44  
45    /**
46     * The setters used to extract properties.
47     */
48    private final List<AbstractPropertySetter<?>> settersRegistry = new ArrayList<AbstractPropertySetter<?>>();
49  
50    /**
51     * Hidden constructor, this class can't be instantiated.
52     */
53    private MemcachedConfigurationBuilder() {
54      memcachedPropertiesFilename = System.getProperty(SYSTEM_PROPERTY_MEMCACHED_PROPERTIES_FILENAME, MEMCACHED_RESOURCE);
55  
56      settersRegistry.add(new StringPropertySetter("org.mybatis.caches.memcached.keyprefix", "keyPrefix", "_mybatis_"));
57      settersRegistry.add(new StringPropertySetter("org.mybatis.caches.memcached.username", "username", ""));
58      settersRegistry.add(new StringPropertySetter("org.mybatis.caches.memcached.password", "password", ""));
59  
60      settersRegistry
61          .add(new IntegerPropertySetter("org.mybatis.caches.memcached.expiration", "expiration", 60 * 60 * 24 * 30));
62      settersRegistry.add(new IntegerPropertySetter("org.mybatis.caches.memcached.timeout", "timeout", 5));
63      settersRegistry.add(new TimeUnitSetter());
64  
65      settersRegistry.add(new BooleanPropertySetter("org.mybatis.caches.memcached.asyncget", "usingAsyncGet", false));
66      settersRegistry
67          .add(new BooleanPropertySetter("org.mybatis.caches.memcached.compression", "compressionEnabled", false));
68      settersRegistry.add(new BooleanPropertySetter("org.mybatis.caches.memcached.sasl", "usingSASL", false));
69  
70      settersRegistry.add(new InetSocketAddressListPropertySetter());
71      settersRegistry.add(new ConnectionFactorySetter());
72    }
73  
74    /**
75     * Return this class instance.
76     *
77     * @return this class instance.
78     */
79    public static MemcachedConfigurationBuilder getInstance() {
80      return INSTANCE;
81    }
82  
83    /**
84     * Parses the Config and builds a new {@link MemcachedConfiguration}.
85     *
86     * @return the converted {@link MemcachedConfiguration}.
87     */
88    public MemcachedConfiguration parseConfiguration() {
89      return parseConfiguration(getClass().getClassLoader());
90    }
91  
92    /**
93     * Parses the Config and builds a new {@link MemcachedConfiguration}.
94     *
95     * @param the
96     *          {@link ClassLoader} used to load the {@code memcached.properties} file in classpath.
97     *
98     * @return the converted {@link MemcachedConfiguration}.
99     */
100   public MemcachedConfiguration parseConfiguration(ClassLoader classLoader) {
101     Properties config = new Properties();
102 
103     // load the properties specified from /memcached.properties, if present
104     InputStream input = classLoader.getResourceAsStream(memcachedPropertiesFilename);
105     if (input != null) {
106       try {
107         config.load(input);
108       } catch (IOException e) {
109         throw new RuntimeException("An error occurred while reading classpath property '" + memcachedPropertiesFilename
110             + "', see nested exceptions", e);
111       } finally {
112         try {
113           input.close();
114         } catch (IOException e) {
115           // close quietly
116         }
117       }
118     }
119 
120     MemcachedConfiguration memcachedConfiguration = new MemcachedConfiguration();
121 
122     for (AbstractPropertySetter<?> setter : settersRegistry) {
123       setter.set(config, memcachedConfiguration);
124     }
125 
126     return memcachedConfiguration;
127   }
128 
129 }