KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > chat > config > Config


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.chat.config;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import org.columba.chat.config.api.IAccount;
31 import org.columba.chat.config.api.IConfig;
32 import org.columba.core.config.DefaultConfigDirectory;
33 import org.columba.core.io.DiskIO;
34 import org.columba.core.shutdown.ShutdownManager;
35
36 /**
37  * Configuration handling. Currently, using a Properties object to serialize an
38  * {@link Account}. Default port for the jabber protocol is 5222.
39  *
40  * @author fdietz
41  */

42 public class Config implements IConfig {
43
44     private File JavaDoc file;
45
46     private File JavaDoc chatDirectory;
47
48     private IAccount account;
49
50     private Properties JavaDoc properties;
51
52     /**
53      *
54      */

55     public Config() {
56
57         // get Columba's top-level configuration directory
58
File JavaDoc parentFile = DefaultConfigDirectory.getDefaultPath();;
59
60         // create top-level configuration directory
61
chatDirectory = new File JavaDoc(parentFile, "chat");
62         DiskIO.ensureDirectory(chatDirectory);
63
64         file = new File JavaDoc(chatDirectory, "config.ini");
65
66         if (file.exists())
67             load();
68         else
69             account = new Account();
70
71         // persist changes on exit
72
ShutdownManager.getInstance().register(new Runnable JavaDoc() {
73
74             public void run() {
75                 try {
76                     save();
77                 } catch (Exception JavaDoc e) {
78                     e.printStackTrace();
79                 }
80             }
81         });
82     }
83
84     /**
85      * @return Returns the account.
86      */

87     public IAccount getAccount() {
88         return account;
89     }
90
91     /**
92      * @param account
93      * The account to set.
94      */

95     public void setAccount(Account account) {
96         this.account = account;
97     }
98
99     /**
100      * Load configuration from disk. Automatically called on application
101      * startup.
102      */

103     private void load() {
104         // use key/value properties file
105
properties = new Properties JavaDoc();
106
107         // load configuraation
108
try {
109             // open stream to file
110
InputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
111             // load properties from stream
112
properties.load(is);
113             // close stream
114
is.close();
115         } catch (FileNotFoundException JavaDoc e) {
116
117             e.printStackTrace();
118         } catch (IOException JavaDoc e) {
119
120             e.printStackTrace();
121         }
122
123         // create account object
124
account = new Account(properties.getProperty("id"));
125
126         if (properties.getProperty("host") != null)
127             account.setHost(properties.getProperty("host"));
128         else
129             account.setHost("jabber.org");
130
131         if (properties.getProperty("password") != null)
132             account.setPassword(properties.getProperty("password")
133                     .toCharArray());
134
135         if (properties.getProperty("resource") != null)
136             account.setResource(properties.getProperty("resource"));
137         else
138             account.setResource("Altura");
139
140         if (properties.getProperty("enable_ssl") != null)
141             account.setEnableSSL(new Boolean JavaDoc(properties
142                     .getProperty("enable_ssl")).booleanValue());
143         else
144             account.setEnableSSL(true);
145
146         if (properties.getProperty("port") != null)
147             account.setPort(new Integer JavaDoc(properties.getProperty("port"))
148                     .intValue());
149         else
150             account.setPort(5222);
151
152     }
153
154     /**
155      * Save configuration to file. Automatically called by using the system
156      * shutdown hook.
157      *
158      */

159     private void save() {
160         if (properties == null)
161             properties = new Properties JavaDoc();
162
163         // store account data in properties
164
put("host", account.getHost());
165         put("id", account.getId());
166         if (account.getPassword() != null)
167             put("password", new String JavaDoc(account.getPassword()));
168         put("resource", account.getResource());
169         put("enable_ssl", new Boolean JavaDoc(account.isEnableSSL()).toString());
170         put("port", new Integer JavaDoc(account.getPort()).toString());
171
172         try {
173             // create stream to file
174
OutputStream JavaDoc os = new FileOutputStream JavaDoc(file);
175             // save properties to file
176
properties.store(os, "account");
177             // close stream
178
os.close();
179         } catch (IOException JavaDoc e1) {
180
181             e1.printStackTrace();
182         }
183     }
184
185     private void put(String JavaDoc key, Object JavaDoc value) {
186         if (value != null)
187             properties.put(key, value);
188     }
189
190 }
Popular Tags