1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package ch.qos.cal10n.util;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.Reader;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
29 import java.util.Map;
30 import java.util.ResourceBundle;
31 import java.util.concurrent.ConcurrentHashMap;
32
33
34
35
36
37
38 public class CAL10NResourceBundle extends ResourceBundle {
39
40 static long CHECK_DELAY = 10 * 60 * 1000;
41
42 Map<String, String> map = new ConcurrentHashMap<String, String>();
43 File hostFile;
44 volatile long nextCheck;
45 long lastModified;
46 CAL10NResourceBundle parent;
47
48 public CAL10NResourceBundle(Reader r, File file)
49 throws IOException {
50 read(r);
51 this.hostFile = file;
52 nextCheck = System.currentTimeMillis() + CHECK_DELAY;
53 }
54
55 void read(Reader r) throws IOException {
56 Parser p = new Parser(r, map);
57 p.parseAndPopulate();
58 }
59
60
61 public void setParent(CAL10NResourceBundle parent) {
62 this.parent = (parent);
63
64 }
65
66 public boolean hasChanged() {
67
68 if (hostFile == null) {
69 return false;
70 }
71
72 long now = System.currentTimeMillis();
73 if (now < nextCheck) {
74 return false;
75 } else {
76 nextCheck = now + CHECK_DELAY;
77 if (lastModified != hostFile.lastModified()) {
78 lastModified = hostFile.lastModified();
79 return true;
80 } else {
81 return false;
82 }
83 }
84 }
85
86
87
88
89 public void resetCheckTimes() {
90 nextCheck = 0;
91 lastModified = 0;
92 }
93
94 @Override
95 public Enumeration<String> getKeys() {
96 Hashtable<String, String> ht = new Hashtable<String, String>(map);
97 if(parent != null) {
98 ht.putAll(parent.map);
99 }
100 return ht.keys();
101 }
102
103 @Override
104 protected Object handleGetObject(String key) {
105 if (key == null) {
106 throw new NullPointerException();
107 }
108 Object o = map.get(key);
109 if(o == null && parent != null) {
110 o = parent.handleGetObject(key);
111 }
112 return o;
113 }
114 }