1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.smartlab.web;
24
25 import java.lang.reflect.Constructor;
26 import java.net.URL;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.TreeMap;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36
37
38
39
40
41
42
43
44
45 public abstract class PropertiesEnumeration extends StringEnumeration {
46
47 private static final long serialVersionUID = 228095287903246251L;
48
49 private static final Class[] TYPES = new Class[] {String.class, String.class};
50
51 private static Map enumerations = new HashMap();
52
53
54
55
56 protected final static Log logger = LogFactory.getLog(PropertiesEnumeration.class);
57
58
59
60
61
62 public PropertiesEnumeration() {
63 super();
64 }
65
66
67
68
69
70
71
72 public PropertiesEnumeration(String code, String display) {
73 super(code, display);
74 }
75
76
77
78
79 public StringEnumeration decode(String code) {
80 if (logger.isDebugEnabled()) {
81 logger.debug("decode(\"" + code + "\") - start");
82 }
83 Map values = (Map)enumerations.get(this.getClass());
84 if (values == null) {
85 synchronized (this.getClass()) {
86 try {
87 String classname = this.getClass().getName();
88 URL url = this.getClass().getResource(
89 classname.substring(classname.lastIndexOf('.') + 1) + ".properties");
90 Properties properties = new Properties();
91 properties.load(url.openStream());
92 Iterator entries = properties.entrySet().iterator();
93 Constructor instantiator = this.getClass().getConstructor(TYPES);
94 values = new TreeMap();
95 while (entries.hasNext()) {
96 Map.Entry entry = (Map.Entry)entries.next();
97 if (logger.isTraceEnabled()) {
98 logger.trace(" adding (" + entry.getKey() + ", " + entry.getValue() + ")");
99 }
100 values.put(entry.getKey(), instantiator.newInstance(new Object[] {entry.getKey(),
101 entry.getValue()}));
102 }
103 enumerations.put(this.getClass(), values);
104 } catch (NoSuchMethodException nsme) {
105 logger.fatal("missing constructor " + this.getClass().getName() + "(String, String)", nsme);
106 } catch (Exception e) {
107 logger.error("decode() - failed", e);
108 }
109 }
110 }
111 return (StringEnumeration)values.get(code);
112 }
113 }