View Javadoc

1   /*
2    * JarInspector - Copyright (C) 2004 Che Inc., Rosario Argentina
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Library General Public
6    * License as published by the Free Software Foundation; either
7    * version 2 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Library General Public License for more details.
13   *
14   * You should have received a copy of the GNU Library General Public
15   * License along with this library; if not, write to the Free
16   * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17   */
18  
19  package inc.che.common.config;
20  
21  // Imports
22  import inc.che.common.resource.ResourceManager;
23  import inc.che.common.resource.StringResources;
24  import inc.che.common.string.StringUtil;
25  
26  import java.io.File;
27  import java.io.FileInputStream;
28  import java.io.FileOutputStream;
29  import java.io.IOException;
30  import java.util.Date;
31  import java.util.Iterator;
32  import java.util.Properties;
33  
34  import org.apache.log4j.Logger;
35  
36  /***
37   *  <b>Lädt uns Speichert Konfigurationsparameter aus einer Propertydatei</b>
38   * @version $Id: PropertyConfigLoader.java,v 1.1 2005/03/06 12:56:50 stevemcmee Exp $
39   * @author <address> Steve McMee &lt;stevemcmee@users.sourceforge.net&gt;</address>
40   */
41  
42  public class PropertyConfigLoader extends ConfigLoader {
43  
44      /*** CVS ID of this file */
45      public static final String CVS_ID =
46          "$Id: PropertyConfigLoader.java,v 1.1 2005/03/06 12:56:50 stevemcmee Exp $";
47  
48      /***
49       * logger instance for this class
50       */
51  
52      private static Logger log = Logger.getLogger(PropertyConfigLoader.class);
53  
54      /***
55      * The ResourceManager
56      */
57      private static ResourceManager resourceManager =
58          ResourceManager.getResourceManager(StringResources.TEXT_RESOURCES);
59  
60      /***
61       * stores Properties into a File
62       *@param configFile File holds the properties
63       *@param properties Properties to store
64       */
65      public void storeProperties(File configFile, Properties properties)
66          throws IOException {
67          if (log.isDebugEnabled()) {
68              log.debug(
69                  "storeProperties (" + configFile + "," + properties + ")");
70          }
71          properties.store(
72              new FileOutputStream(configFile),
73              resourceManager.getText(
74                  "props_store_header",
75                  new String[] {
76                      new Date().toString(),
77                      System.getProperty("user.name")}));
78      }
79  
80      public Properties loadProperties(File configFile) throws IOException {
81          if (log.isDebugEnabled()) {
82              log.debug("loadProperties(" + configFile + ")");
83          }
84          Properties erg = new Properties();
85          Properties props = new Properties();
86          props.load(new FileInputStream(configFile));
87          // Ersetze Systemproperties
88          Properties sysProps = System.getProperties();
89          String value;
90          String key;
91          String sysKey;
92          for (Iterator it = props.keySet().iterator(); it.hasNext();) {
93              key = (String) it.next();
94              value = props.getProperty(key);
95              value = StringUtil.replaceSystemProperties(value);
96              erg.setProperty(key, value);
97          }
98          return erg;
99      }
100 
101 }