001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.http.support;
016:
017: import java.io.File;
018: import java.io.FileInputStream;
019: import java.net.URL;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.Properties;
024: import org.apache.commons.lang.exception.NestableRuntimeException;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.araneaframework.core.util.ClassLoaderUtil;
028:
029: /**
030: * @author <a href="mailto:ekabanov@webmedia.ee">Jevgeni Kabanov </a>
031: * @since {since}
032: */
033: public class ReloadingPropertyFileResourceBundle extends
034: LocaleAwareResourceBundle {
035:
036: private static final Log log = LogFactory
037: .getLog(ReloadingPropertyFileResourceBundle.class);
038:
039: protected String propertyResource = "resources";
040:
041: protected long lastReloadTime;
042: protected long checkTime;
043: protected long lastModified = 0;
044:
045: protected Map properties = new HashMap();
046:
047: public ReloadingPropertyFileResourceBundle() {
048: }
049:
050: public ReloadingPropertyFileResourceBundle(String propertyResource) {
051: this .propertyResource = propertyResource;
052: }
053:
054: protected Object handleGetObject(String key) {
055: if (key == null)
056: throw new NullPointerException();
057:
058: if (checkTime != -1
059: && System.currentTimeMillis() - lastReloadTime > checkTime) {
060: reloadProperties();
061: }
062:
063: return handleGetCachedObject(key);
064: }
065:
066: private void reloadProperties() {
067: try {
068: String resourceName = propertyResource + "_"
069: + getLocale().getLanguage() + ".properties";
070: URL propertyURL = ClassLoaderUtil
071: .findResource(resourceName);
072:
073: if (propertyURL == null) {
074: resourceName = propertyResource + ".properties";
075: propertyURL = ClassLoaderUtil
076: .findResource(resourceName);
077: }
078:
079: if (propertyURL == null) {
080: log.warn("Localization resources for '"
081: + getLocale().getLanguage()
082: + "' were not found!");
083: return;
084: }
085:
086: File propertyFile = new File(propertyURL.getFile());
087:
088: if (lastModified < propertyFile.lastModified()) {
089:
090: log
091: .debug("Reloading localization data from property file '"
092: + propertyFile + "'.");
093:
094: Properties result = new Properties();
095: result.load(new FileInputStream(propertyURL.getFile()));
096:
097: loadData(result);
098:
099: lastReloadTime = System.currentTimeMillis();
100: lastModified = propertyFile.lastModified();
101: }
102: } catch (RuntimeException re) {
103: throw re;
104: } catch (Exception e) {
105: throw new NestableRuntimeException(e);
106: }
107: }
108:
109: protected Object handleGetCachedObject(String key) {
110: return properties.get(key);
111: }
112:
113: protected void loadData(Properties newProperties) throws Exception {
114: properties = new HashMap();
115:
116: for (Enumeration i = newProperties.propertyNames(); i
117: .hasMoreElements();) {
118: String key = (String) i.nextElement();
119: String value = newProperties.getProperty(key);
120:
121: properties.put(key, value);
122: }
123: }
124:
125: public Enumeration getKeys() {
126: return null;
127: }
128: }
|