View Javadoc

1   /*
2    * Copyright (c) 2009 QOS.ch All rights reserved.
3    * 
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   * 
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   * 
14   * THE SOFTWARE IS PROVIDED "AS  IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
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   * @author Ceki Gülcü
36   *
37   */
38  public class CAL10NResourceBundle extends ResourceBundle {
39  
40    static long CHECK_DELAY = 10 * 60 * 1000; // 10 minutes delay
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      //super.setParent(parent);
64    }
65  
66    public boolean hasChanged() {
67      // if the host file is unknown, no point in a check
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     * WARNING: Used for testing purposes. Do not invoke directly in user code.
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 }