PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/webportal/src/main/java/au/org/emii/portal/util/ReloadablePropertiesImpl.java

http://alageospatialportal.googlecode.com/
Java | 124 lines | 78 code | 14 blank | 32 comment | 14 complexity | b6f4ab7783cd28dce2f84211bb76c2df MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5
  6package au.org.emii.portal.util;
  7
  8import au.org.emii.portal.aspect.CheckNotNull;
  9import java.io.File;
 10import java.io.FileInputStream;
 11import java.io.FileNotFoundException;
 12import java.io.IOException;
 13import java.io.InputStream;
 14import java.util.Date;
 15import java.util.Properties;
 16import org.apache.log4j.Logger;
 17
 18/**
 19 *
 20 * @author geoff
 21 */
 22public class ReloadablePropertiesImpl implements ReloadableProperties {
 23
 24    protected Logger logger = Logger.getLogger(getClass());
 25    private String filename = null;
 26    private Properties properties = null;
 27
 28    /**
 29     * Date of last reload or null if properties have never been reloaded
 30     */
 31    protected Date lastReloaded = null;
 32
 33    /**
 34     * Get the properties field - perform an initial load or reread if file
 35     * modified as required
 36     * @return properties field or null if it could not be loaded
 37     */
 38    @Override
 39    public Properties getProperties() {
 40        if (updateNeeded()) {
 41            reloadPropertiesNow();
 42        }
 43        return properties;
 44    }
 45
 46    @Override
 47    public String getFilename() {
 48        return filename;
 49    }
 50
 51    @CheckNotNull
 52    @Override
 53    public void setFilename(String filename) {
 54        this.filename = filename;
 55    }
 56
 57
 58    /**
 59     * Reload properties immediately
 60     */
 61    private void reloadPropertiesNow() {
 62        InputStream is = null;
 63        try {
 64            is = new FileInputStream(filename);
 65            if (is != null) {
 66                Properties newProperties = new Properties();
 67                newProperties.load(is);
 68                lastReloaded = new Date();
 69                properties = newProperties;
 70                logger.debug("properties reloaded");
 71            }
 72        } catch (FileNotFoundException e) {
 73            logger.error("file not found: " + filename);
 74        } catch (IOException e) {
 75            logger.error("error loading properties", e);
 76        } finally {
 77            try {
 78                if (is != null) {
 79                    is.close();
 80                }
 81            }
 82            // discard error closing input stream
 83            catch (IOException e) {}
 84        }
 85    }
 86
 87    /**
 88     * Block execution until safe to read file
 89     * @param filename
 90     * @param lastReloaded
 91     * @param updating
 92     * @param motd
 93     */
 94    protected boolean updateNeeded() {
 95        boolean updateNeeded = false;
 96        if (filename == null) {
 97            logger.error("Internal error inspecting properties file - no filename specified", new NullPointerException());
 98        } else {
 99            File file = new File(filename);
100            if (file.exists() && file.canRead()) {
101                if ((lastReloaded == null) || (file.lastModified() > lastReloaded.getTime())) {
102                    // initial load or update
103                    updateNeeded = true;
104                }
105            } else {
106                logger.error("properties file not found or not readable: " + filename);
107            }
108        }
109        return updateNeeded;
110    }
111
112    /**
113     * Set properties, update lastReloaded timestamp
114     * @param properties
115     */
116    @CheckNotNull
117    @Override
118    public void setProperties(Properties properties) {
119        this.properties = properties;
120        lastReloaded = new Date();
121    }
122
123    
124}