|
1 | 1 | /** |
2 | | - * Copyright 2012-2015 the original author or authors. |
| 2 | + * Copyright 2012-2017 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
32 | 32 | */ |
33 | 33 | abstract class AbstractPropertySetter<T> { |
34 | 34 |
|
35 | | - /** |
36 | | - * 'propertyName'='writermethod' index of {@link MemcachedConfiguration} |
37 | | - * properties. |
38 | | - */ |
39 | | - private static Map<String, Method> WRITERS = new HashMap<String, Method>(); |
40 | | - |
41 | | - static { |
42 | | - try { |
43 | | - BeanInfo memcachedConfigInfo = Introspector.getBeanInfo(MemcachedConfiguration.class); |
44 | | - for (PropertyDescriptor descriptor : memcachedConfigInfo.getPropertyDescriptors()) { |
45 | | - WRITERS.put(descriptor.getName(), descriptor.getWriteMethod()); |
46 | | - } |
47 | | - } catch (IntrospectionException e) { |
48 | | - // handle quietly |
49 | | - } |
| 35 | + /** |
| 36 | + * 'propertyName'='writermethod' index of {@link MemcachedConfiguration} |
| 37 | + * properties. |
| 38 | + */ |
| 39 | + private static Map<String, Method> WRITERS = new HashMap<String, Method>(); |
| 40 | + |
| 41 | + static { |
| 42 | + try { |
| 43 | + BeanInfo memcachedConfigInfo = Introspector.getBeanInfo(MemcachedConfiguration.class); |
| 44 | + for (PropertyDescriptor descriptor : memcachedConfigInfo.getPropertyDescriptors()) { |
| 45 | + WRITERS.put(descriptor.getName(), descriptor.getWriteMethod()); |
| 46 | + } |
| 47 | + } catch (IntrospectionException e) { |
| 48 | + // handle quietly |
50 | 49 | } |
51 | | - |
52 | | - /** |
53 | | - * The Config property key. |
54 | | - */ |
55 | | - private final String propertyKey; |
56 | | - |
57 | | - /** |
58 | | - * The {@link MemcachedConfiguration} property name. |
59 | | - */ |
60 | | - private final String propertyName; |
61 | | - |
62 | | - /** |
63 | | - * The {@link MemcachedConfiguration} property method writer. |
64 | | - */ |
65 | | - private final Method propertyWriterMethod; |
66 | | - |
67 | | - /** |
68 | | - * The default value used if something goes wrong during the conversion or |
69 | | - * the property is not set in the config. |
70 | | - */ |
71 | | - private final T defaultValue; |
72 | | - |
73 | | - /** |
74 | | - * Build a new property setter. |
75 | | - * |
76 | | - * @param propertyKey the Config property key. |
77 | | - * @param propertyName the {@link MemcachedConfiguration} property name. |
78 | | - * @param defaultValue the property default value. |
79 | | - */ |
80 | | - public AbstractPropertySetter(final String propertyKey, final String propertyName, final T defaultValue) { |
81 | | - this.propertyKey = propertyKey; |
82 | | - this.propertyName = propertyName; |
83 | | - |
84 | | - this.propertyWriterMethod = WRITERS.get(propertyName); |
85 | | - if (this.propertyWriterMethod == null) { |
86 | | - throw new RuntimeException("Class '" |
87 | | - + MemcachedConfiguration.class.getName() |
88 | | - + "' doesn't contain a property '" |
89 | | - + propertyName |
90 | | - + "'"); |
91 | | - } |
92 | | - |
93 | | - this.defaultValue = defaultValue; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * The Config property key. |
| 54 | + */ |
| 55 | + private final String propertyKey; |
| 56 | + |
| 57 | + /** |
| 58 | + * The {@link MemcachedConfiguration} property name. |
| 59 | + */ |
| 60 | + private final String propertyName; |
| 61 | + |
| 62 | + /** |
| 63 | + * The {@link MemcachedConfiguration} property method writer. |
| 64 | + */ |
| 65 | + private final Method propertyWriterMethod; |
| 66 | + |
| 67 | + /** |
| 68 | + * The default value used if something goes wrong during the conversion or |
| 69 | + * the property is not set in the config. |
| 70 | + */ |
| 71 | + private final T defaultValue; |
| 72 | + |
| 73 | + /** |
| 74 | + * Build a new property setter. |
| 75 | + * |
| 76 | + * @param propertyKey the Config property key. |
| 77 | + * @param propertyName the {@link MemcachedConfiguration} property name. |
| 78 | + * @param defaultValue the property default value. |
| 79 | + */ |
| 80 | + public AbstractPropertySetter(final String propertyKey, final String propertyName, final T defaultValue) { |
| 81 | + this.propertyKey = propertyKey; |
| 82 | + this.propertyName = propertyName; |
| 83 | + |
| 84 | + this.propertyWriterMethod = WRITERS.get(propertyName); |
| 85 | + if (this.propertyWriterMethod == null) { |
| 86 | + throw new RuntimeException( |
| 87 | + "Class '" + MemcachedConfiguration.class.getName() + "' doesn't contain a property '" + propertyName + "'"); |
94 | 88 | } |
95 | 89 |
|
96 | | - /** |
97 | | - * Extract a property from the, converts and puts it to the |
98 | | - * {@link MemcachedConfiguration}. |
99 | | - * |
100 | | - * @param config the Config |
101 | | - * @param memcachedConfiguration the {@link MemcachedConfiguration} |
102 | | - */ |
103 | | - public final void set(Properties config, MemcachedConfiguration memcachedConfiguration) { |
104 | | - String propertyValue = config.getProperty(propertyKey); |
105 | | - T value; |
106 | | - |
107 | | - try { |
108 | | - value = this.convert(propertyValue); |
109 | | - if (value == null) { |
110 | | - value = defaultValue; |
111 | | - } |
112 | | - } catch (Exception e) { |
113 | | - value = defaultValue; |
114 | | - } |
115 | | - |
116 | | - try { |
117 | | - propertyWriterMethod.invoke(memcachedConfiguration, value); |
118 | | - } catch (Exception e) { |
119 | | - throw new RuntimeException("Impossible to set property '" |
120 | | - + propertyName |
121 | | - + "' with value '" |
122 | | - + value |
123 | | - + "', extracted from ('" |
124 | | - + propertyKey |
125 | | - + "'=" |
126 | | - + propertyValue |
127 | | - + ")", e); |
128 | | - } |
| 90 | + this.defaultValue = defaultValue; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Extract a property from the, converts and puts it to the |
| 95 | + * {@link MemcachedConfiguration}. |
| 96 | + * |
| 97 | + * @param config the Config |
| 98 | + * @param memcachedConfiguration the {@link MemcachedConfiguration} |
| 99 | + */ |
| 100 | + public final void set(Properties config, MemcachedConfiguration memcachedConfiguration) { |
| 101 | + String propertyValue = config.getProperty(propertyKey); |
| 102 | + T value; |
| 103 | + |
| 104 | + try { |
| 105 | + value = this.convert(propertyValue); |
| 106 | + if (value == null) { |
| 107 | + value = defaultValue; |
| 108 | + } |
| 109 | + } catch (Exception e) { |
| 110 | + value = defaultValue; |
129 | 111 | } |
130 | 112 |
|
131 | | - /** |
132 | | - * Convert a string representation to a proper Java Object. |
133 | | - * |
134 | | - * @param value the value has to be converted. |
135 | | - * @return the converted value. |
136 | | - * @throws Exception if any error occurs. |
137 | | - */ |
138 | | - protected abstract T convert(String value) throws Exception; |
| 113 | + try { |
| 114 | + propertyWriterMethod.invoke(memcachedConfiguration, value); |
| 115 | + } catch (Exception e) { |
| 116 | + throw new RuntimeException("Impossible to set property '" + propertyName + "' with value '" + value |
| 117 | + + "', extracted from ('" + propertyKey + "'=" + propertyValue + ")", e); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Convert a string representation to a proper Java Object. |
| 123 | + * |
| 124 | + * @param value the value has to be converted. |
| 125 | + * @return the converted value. |
| 126 | + * @throws Exception if any error occurs. |
| 127 | + */ |
| 128 | + protected abstract T convert(String value) throws Exception; |
139 | 129 |
|
140 | 130 | } |
0 commit comments