001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2001-2003 Emmanuel Bourg
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019
020 package net.jetrix.config;
021
022 import java.util.Properties;
023
024 /**
025 * A generic configuration.
026 *
027 * @author Emmanuel Bourg
028 * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
029 */
030 public class Configuration
031 {
032 protected Properties props;
033
034 public void setParameter(String name, String value)
035 {
036 if (props == null)
037 {
038 props = new Properties();
039 }
040 props.setProperty(name, value);
041 }
042
043 public int getInt(String key, int defaultValue)
044 {
045 int value;
046
047 try
048 {
049 value = Integer.parseInt(props.getProperty(key));
050 }
051 catch (Exception e)
052 {
053 value = defaultValue;
054 }
055
056 return value;
057 }
058
059 public boolean getBoolean(String key, boolean defaultValue)
060 {
061 boolean value;
062
063 try
064 {
065 String property = props.getProperty(key).trim().toLowerCase();
066 if ("true".equals(property) || "false".equals(property))
067 {
068 value = new Boolean(property).booleanValue();
069 }
070 else
071 {
072 value = defaultValue;
073 }
074
075 }
076 catch (Exception e)
077 {
078 value = defaultValue;
079 }
080
081 return value;
082 }
083
084 public String getString(String key, String defaultValue)
085 {
086 String value = props != null ? props.getProperty(key) : null;
087 return value != null ? value : defaultValue;
088 }
089
090 /**
091 * Return the underlying Properties.
092 *
093 * @since 0.2
094 */
095 public Properties getProperties()
096 {
097 return props;
098 }
099 }