001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.chat.config;
019:
020: import java.io.BufferedInputStream;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileNotFoundException;
024: import java.io.FileOutputStream;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.OutputStream;
028: import java.util.Properties;
029:
030: import org.columba.chat.config.api.IAccount;
031: import org.columba.chat.config.api.IConfig;
032: import org.columba.core.config.DefaultConfigDirectory;
033: import org.columba.core.io.DiskIO;
034: import org.columba.core.shutdown.ShutdownManager;
035:
036: /**
037: * Configuration handling. Currently, using a Properties object to serialize an
038: * {@link Account}. Default port for the jabber protocol is 5222.
039: *
040: * @author fdietz
041: */
042: public class Config implements IConfig {
043:
044: private File file;
045:
046: private File chatDirectory;
047:
048: private IAccount account;
049:
050: private Properties properties;
051:
052: /**
053: *
054: */
055: public Config() {
056:
057: // get Columba's top-level configuration directory
058: File parentFile = DefaultConfigDirectory.getDefaultPath();
059: ;
060:
061: // create top-level configuration directory
062: chatDirectory = new File(parentFile, "chat");
063: DiskIO.ensureDirectory(chatDirectory);
064:
065: file = new File(chatDirectory, "config.ini");
066:
067: if (file.exists())
068: load();
069: else
070: account = new Account();
071:
072: // persist changes on exit
073: ShutdownManager.getInstance().register(new Runnable() {
074:
075: public void run() {
076: try {
077: save();
078: } catch (Exception e) {
079: e.printStackTrace();
080: }
081: }
082: });
083: }
084:
085: /**
086: * @return Returns the account.
087: */
088: public IAccount getAccount() {
089: return account;
090: }
091:
092: /**
093: * @param account
094: * The account to set.
095: */
096: public void setAccount(Account account) {
097: this .account = account;
098: }
099:
100: /**
101: * Load configuration from disk. Automatically called on application
102: * startup.
103: */
104: private void load() {
105: // use key/value properties file
106: properties = new Properties();
107:
108: // load configuraation
109: try {
110: // open stream to file
111: InputStream is = new BufferedInputStream(
112: new FileInputStream(file));
113: // load properties from stream
114: properties.load(is);
115: // close stream
116: is.close();
117: } catch (FileNotFoundException e) {
118:
119: e.printStackTrace();
120: } catch (IOException e) {
121:
122: e.printStackTrace();
123: }
124:
125: // create account object
126: account = new Account(properties.getProperty("id"));
127:
128: if (properties.getProperty("host") != null)
129: account.setHost(properties.getProperty("host"));
130: else
131: account.setHost("jabber.org");
132:
133: if (properties.getProperty("password") != null)
134: account.setPassword(properties.getProperty("password")
135: .toCharArray());
136:
137: if (properties.getProperty("resource") != null)
138: account.setResource(properties.getProperty("resource"));
139: else
140: account.setResource("Altura");
141:
142: if (properties.getProperty("enable_ssl") != null)
143: account.setEnableSSL(new Boolean(properties
144: .getProperty("enable_ssl")).booleanValue());
145: else
146: account.setEnableSSL(true);
147:
148: if (properties.getProperty("port") != null)
149: account.setPort(new Integer(properties.getProperty("port"))
150: .intValue());
151: else
152: account.setPort(5222);
153:
154: }
155:
156: /**
157: * Save configuration to file. Automatically called by using the system
158: * shutdown hook.
159: *
160: */
161: private void save() {
162: if (properties == null)
163: properties = new Properties();
164:
165: // store account data in properties
166: put("host", account.getHost());
167: put("id", account.getId());
168: if (account.getPassword() != null)
169: put("password", new String(account.getPassword()));
170: put("resource", account.getResource());
171: put("enable_ssl", new Boolean(account.isEnableSSL()).toString());
172: put("port", new Integer(account.getPort()).toString());
173:
174: try {
175: // create stream to file
176: OutputStream os = new FileOutputStream(file);
177: // save properties to file
178: properties.store(os, "account");
179: // close stream
180: os.close();
181: } catch (IOException e1) {
182:
183: e1.printStackTrace();
184: }
185: }
186:
187: private void put(String key, Object value) {
188: if (value != null)
189: properties.put(key, value);
190: }
191:
192: }
|