PropertiesFileHandler.java

  1. /*
  2.  * This software was designed and created by Jason Carroll.
  3.  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
  4.  * The author can be reached at jcarroll@cowsultants.com
  5.  * ITracker website: http://www.cowsultants.com
  6.  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it only under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  */

  18. package org.itracker.model.util;

  19. import org.apache.log4j.Logger;

  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.util.Properties;

  23. public class PropertiesFileHandler {
  24.     private Properties props;
  25.     private final Logger logger;

  26.     public PropertiesFileHandler() {
  27.         this.logger = Logger.getLogger(getClass());
  28.         props = new Properties();
  29.     }

  30.     public PropertiesFileHandler(String resource) {
  31.         this();
  32.         addProperties(resource);
  33.     }

  34.     public void addProperties(String resource) {
  35.         if (resource == null || resource.equals("") || !resource.endsWith(".properties")) {
  36.             if (logger.isInfoEnabled()) {
  37.                 logger.info("addProperties: skip " + resource);
  38.             }
  39.             return;
  40.         }

  41.         try {
  42.             InputStream is = getClass().getResourceAsStream(resource);
  43.             if (is != null) {
  44.                 props.load(is);
  45.             } else {
  46.                 logger.debug("No properties resource, " + resource + " was found.");
  47.             }
  48.         } catch (IOException ioe) {
  49.             logger.warn("Could not load properties resource: " + resource, ioe);
  50.         }
  51.     }

  52.     public Properties getProperties() {
  53.         return (Properties) props.clone();
  54.     }

  55.     public String getProperty(String name) {
  56.         return props.getProperty(name);
  57.     }

  58.     public boolean hasProperty(String name) {
  59.         return props.containsKey(name);
  60.     }

  61.     public boolean hasProperties() {
  62.         return (props.size() > 0 ? true : false);
  63.     }
  64. }