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 CAL10NBundle 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 CAL10NBundle parent;
47
48 public CAL10NBundle(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(CAL10NBundle parent) {
62 this.parent = (parent);
63 }
64
65 public boolean hasChanged() {
66
67 if (hostFile == null) {
68 return false;
69 }
70
71 long now = System.currentTimeMillis();
72 if (now < nextCheck) {
73 return false;
74 } else {
75 nextCheck = now + CHECK_DELAY;
76 if (lastModified != hostFile.lastModified()) {
77 lastModified = hostFile.lastModified();
78 return true;
79 } else {
80 return false;
81 }
82 }
83 }
84
85
86
87
88 public void resetCheckTimes() {
89 nextCheck = 0;
90 lastModified = 0;
91 }
92
93 @Override
94 public Enumeration<String> getKeys() {
95 Hashtable<String, String> ht = new Hashtable<String, String>(map);
96 if(parent != null) {
97 ht.putAll(parent.map);
98 }
99 return ht.keys();
100 }
101
102 @Override
103 protected Object handleGetObject(String key) {
104 if (key == null) {
105 throw new NullPointerException();
106 }
107 Object o = map.get(key);
108 if(o == null && parent != null) {
109 o = parent.handleGetObject(key);
110 }
111 return o;
112 }
113 }