KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > LocalConfig


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.client;
21
22 import java.util.Properties JavaDoc;
23 import java.io.*;
24
25 import org.lucane.common.Logging;
26
27 /**
28  * A local configuration system.
29  * It can be used to store a few string values that are not crucial.
30  * You should not store important or mandatory keys here, as the file
31  * is store on the local client.
32  *
33  * It can be compared to the cookie system in a web environment.
34  */

35 public class LocalConfig
36 {
37     static final String JavaDoc CONFIG_DIRECTORY = "etc/localconfig/";
38
39     //-- attributes
40
private String JavaDoc app;
41     private String JavaDoc path;
42     private Properties JavaDoc properties;
43     
44     /**
45      * Constructor.
46      * Create the Properties and load the applicative configuration
47      *
48      * @param app the application name
49      */

50     public LocalConfig(String JavaDoc app)
51     {
52         this.app = app;
53         this.path = System.getProperty("user.dir") + '/' + CONFIG_DIRECTORY +
54             Client.getInstance().getMyInfos().getName() + '/' + app + ".properties";
55         this.path = this.path.replace('\\', '/');
56         
57         this.properties = new Properties JavaDoc();
58         
59         try {
60             this.properties.load(new FileInputStream(this.path));
61         } catch(FileNotFoundException fnfe) {
62             //no file yet
63
} catch(IOException ioe) {
64             Logging.getLogger().warning("Error when loading LocalConfig for " + app);
65             ioe.printStackTrace();
66         }
67     }
68     
69     /**
70      * Set a value in the localconfig
71      *
72      * @param key the key
73      * @param value the value
74      */

75     public void set(String JavaDoc key, String JavaDoc value)
76     {
77         this.properties.setProperty(key, value);
78     }
79     
80     /**
81      * Set a value in the localconfig
82      *
83      * @param key the key
84      * @param value the value
85      */

86     public void set(String JavaDoc key, int value)
87     {
88         set(key, String.valueOf(value));
89     }
90     
91     /**
92      * Get a value from the localconfig
93      *
94      * @param key the key
95      * @return the value
96      */

97     public String JavaDoc get(String JavaDoc key)
98     {
99         return this.properties.getProperty(key);
100     }
101     
102     /**
103      * Get a value from the localconfig
104      *
105      * @param key the key
106      * @param value the value returned if the key has no value
107      * @return the value
108      */

109     public int getInt(String JavaDoc key, int value)
110     {
111         try {
112             return Integer.parseInt(get(key));
113         } catch(Exception JavaDoc e) {}
114         
115         return value;
116     }
117     
118     /**
119      * Get a value from the localconfig
120      *
121      * @param key the key
122      * @return the value
123      */

124     public int getInt(String JavaDoc key)
125     {
126         return getInt(key, 0);
127     }
128     
129     /**
130      * Get a value from the localconfig
131      *
132      * @param key the key
133      * @param defaultValue the default value
134      * @return the value, or the default if value is null
135      */

136     public String JavaDoc get(String JavaDoc key, String JavaDoc defaultValue)
137     {
138         return this.properties.getProperty(key, defaultValue);
139     }
140     
141     /**
142      * Save the localconfig to a file
143      */

144     public void save()
145     throws IOException
146     {
147         this.properties.store(new FileOutputStream(this.path), this.app);
148     }
149 }
Popular Tags