001 package dk.deepthought.sidious.util;
002 
003 import java.io.File;
004 import java.io.FileInputStream;
005 import java.io.IOException;
006 import java.util.Properties;
007 
008 import org.apache.commons.logging.Log;
009 import org.apache.commons.logging.LogFactory;
010 
011 import dk.deepthought.sidious.supportsystem.SuperLinkID;
012 
013 /**
014  * This class retrieves the appropriate properties for a given <code>Rule</code>.
015  
016  @author Deepthought
017  
018  */
019 public class RuleProperty {
020     /**
021      * Logger for this class
022      */
023     private static final Log logger = LogFactory.getLog(RuleProperty.class);
024 
025     /**
026      * Internal properties file.
027      */
028     private final Properties properties;
029 
030     /**
031      * The path to the folder where properties files for the <code>Rules</code>
032      * are stored.
033      */
034     private static final String RULES_PROPERTIES_PATH = ".";
035 
036     /**
037      * Constructs a new RuleProperty from the input class name.
038      
039      @param className
040      *            the name of the class
041      */
042     public RuleProperty(String className) {
043         if (className == null || className.equals("")) {
044             logger.error("RuleProperty(String className=" + className
045                     ") - Not valid input=null");
046             throw new IllegalArgumentException("null not valid input");
047         }
048         properties = readProperties(className);
049     }
050 
051     /**
052      * Constructs a new RuleProperty from the input <code>Properties</code>.
053      
054      @param properties
055      *            the properties
056      */
057     public RuleProperty(Properties properties) {
058         if (properties == null || properties.isEmpty()) {
059             logger.error("RuleProperty(String properties=" + properties
060                     ") - Not valid input");
061             throw new IllegalArgumentException("not valid input");
062         }
063         this.properties = properties;
064     }
065 
066     /**
067      * Method returns the <code>Properties</code> object specified by the
068      * input <code>name</code>.
069      
070      @param prefixName
071      *            prefix of the properties file
072      @return the properties of the input object name
073      */
074     private Properties readProperties(String prefixName) {
075         if (prefixName == null || prefixName.equals("")) {
076             logger.error("getProperties(String prefixName=" + prefixName
077                     ") - input not valid");
078             throw new IllegalArgumentException("Not valid input=" + prefixName);
079         }
080         Properties properties = new Properties();
081         try {
082             FileInputStream in = new FileInputStream(RULES_PROPERTIES_PATH
083                     + File.separator + prefixName + ".properties");
084             properties.load(in);
085             in.close();
086         catch (IOException e) {
087             logger.error("getProperties(String prefixName=" + prefixName
088                     ") - IOException, properties not read", e);
089             if (logger.isDebugEnabled()) {
090                 logger.debug("Empty properties returned");
091             }
092         }
093         return properties;
094     }
095 
096     /**
097      * Method returns the value of the specified key.
098      
099      @param key
100      *            the key
101      @param defaultValue
102      *            the default value, if retrieved value was not a
103      *            <code>double</code>
104      @return the retrieved value
105      */
106     public double getFloat(String key, double defaultValue) {
107         if (key == null) {
108             logger.error("getFloat(String key=null, double defaultValue="
109                     + defaultValue + ") - null changed to empty string");
110             key = "";
111         }
112         double f = defaultValue;
113         try {
114             f = Float.parseFloat(properties.getProperty(key, String
115                     .valueOf(defaultValue)));
116         catch (NumberFormatException e) {
117             logger.error("getFloat(String key=" + key + ", double defaultValue="
118                     + defaultValue + ") - Value of key was not a double");
119         }
120         return f;
121     }
122 
123     /**
124      * Method returns the value of the specified key.
125      <p>
126      * NOTE: The <code>RuleProperty.getFloat()</code> is utilized for retrieval.
127      
128      @param key
129      *            the key
130      @param defaultValue
131      *            the default value, if retrieved value was not an
132      *            <code>int</code>
133      @return the retrieved value
134      */
135     public int getInt(String key, int defaultValue) {
136         if (logger.isDebugEnabled()) {
137             logger.debug("getInt(String key=" + key + ", int defaultValue="
138                     + defaultValue + ") - start - calling getFloat()");
139         }
140         int returnint = (intgetFloat(key, (doubledefaultValue);
141         if (logger.isDebugEnabled()) {
142             logger.debug("getInt(String key=" + key + ", int defaultValue="
143                     + defaultValue + ") - end - return value=" + returnint);
144         }
145         return returnint;
146     }
147 
148     /**
149      * Method returns the value of the specified key.
150      
151      @param key
152      *            the key
153      @return the retrieved value
154      */
155     public SuperLinkID getID(String key) {
156         if (key == null) {
157             logger.error("getID(String key=null) - null changed to empty string");
158             key = "";
159         }
160         return new SuperLinkID(properties.getProperty(key));
161     }
162 
163 }