View Javadoc
1   /*
2    * Copyright 2004 Diogo Quintela (dquintela@users.sourceforge.net)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.whatsnew.config;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileNotFoundException;
21  import java.io.IOException;
22  
23  import java.util.MissingResourceException;
24  import java.util.Properties;
25  import java.util.ResourceBundle;
26  
27  
28  /***
29   * <p>
30   * Config file wrapper. Supports user given properties file
31   * </p>
32   *
33   * @author <a href="mailto:dquintela@users.sourceforge.net">Diogo Quintela</a>
34   * @version $Id: Config.java,v 1.1 2004/05/13 01:22:36 dquintela Exp $
35   */
36  public class Config {
37      /*** Bundle name/location */
38      private static final String CONFIG_NAME = "net.sf.whatsnew.config.config";  //$NON-NLS-1$
39  
40      /*** Resource bundle for config options */
41      private static final ResourceBundle MESSAGES_BUNDLE = ResourceBundle
42              .getBundle(CONFIG_NAME);
43  
44      /*** Resource bundle for user set options! Overrides default options */
45      private Properties confProps;
46  
47      /***
48       * Creates a new Config object.
49       */
50      public Config() {
51          confProps = new Properties();
52      }
53  
54      /***
55       * Creates a new Config object.
56       *
57       * @param configProperties The external properties files to load
58       *
59       * @throws FileNotFoundException If file doesn't exist
60       * @throws IOException in case of IO error
61       */
62      public Config(File configProperties)
63      throws FileNotFoundException, IOException {
64          this();
65          confProps.load(new FileInputStream(configProperties));
66      }
67  
68      /***
69       * Obtain config entry based on key value
70       *
71       * @param key The key to lookup for
72       *
73       * @return The config entry
74       */
75      public String getString(String key) {
76          try {
77              return confProps.getProperty(key, MESSAGES_BUNDLE.getString(key));
78          } catch (MissingResourceException e) {
79              return '!' + key + '!';
80          }
81      }
82  }
83  // EOF