1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
26
27
28
29 final class MemcachedConfigurationBuilder {
30
31
32
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
47
48 private final List<AbstractPropertySetter<?>> settersRegistry = new ArrayList<AbstractPropertySetter<?>>();
49
50
51
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
76
77
78
79 public static MemcachedConfigurationBuilder getInstance() {
80 return INSTANCE;
81 }
82
83
84
85
86
87
88 public MemcachedConfiguration parseConfiguration() {
89 return parseConfiguration(getClass().getClassLoader());
90 }
91
92
93
94
95
96
97
98
99
100 public MemcachedConfiguration parseConfiguration(ClassLoader classLoader) {
101 Properties config = new Properties();
102
103
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
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 }