1 package org.columba.chat.config; 19 20 import java.io.BufferedInputStream ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.util.Properties ; 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 42 public class Config implements IConfig { 43 44 private File file; 45 46 private File chatDirectory; 47 48 private IAccount account; 49 50 private Properties properties; 51 52 55 public Config() { 56 57 File parentFile = DefaultConfigDirectory.getDefaultPath();; 59 60 chatDirectory = new File (parentFile, "chat"); 62 DiskIO.ensureDirectory(chatDirectory); 63 64 file = new File (chatDirectory, "config.ini"); 65 66 if (file.exists()) 67 load(); 68 else 69 account = new Account(); 70 71 ShutdownManager.getInstance().register(new Runnable () { 73 74 public void run() { 75 try { 76 save(); 77 } catch (Exception e) { 78 e.printStackTrace(); 79 } 80 } 81 }); 82 } 83 84 87 public IAccount getAccount() { 88 return account; 89 } 90 91 95 public void setAccount(Account account) { 96 this.account = account; 97 } 98 99 103 private void load() { 104 properties = new Properties (); 106 107 try { 109 InputStream is = new BufferedInputStream (new FileInputStream (file)); 111 properties.load(is); 113 is.close(); 115 } catch (FileNotFoundException e) { 116 117 e.printStackTrace(); 118 } catch (IOException e) { 119 120 e.printStackTrace(); 121 } 122 123 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 (properties 142 .getProperty("enable_ssl")).booleanValue()); 143 else 144 account.setEnableSSL(true); 145 146 if (properties.getProperty("port") != null) 147 account.setPort(new Integer (properties.getProperty("port")) 148 .intValue()); 149 else 150 account.setPort(5222); 151 152 } 153 154 159 private void save() { 160 if (properties == null) 161 properties = new Properties (); 162 163 put("host", account.getHost()); 165 put("id", account.getId()); 166 if (account.getPassword() != null) 167 put("password", new String (account.getPassword())); 168 put("resource", account.getResource()); 169 put("enable_ssl", new Boolean (account.isEnableSSL()).toString()); 170 put("port", new Integer (account.getPort()).toString()); 171 172 try { 173 OutputStream os = new FileOutputStream (file); 175 properties.store(os, "account"); 177 os.close(); 179 } catch (IOException e1) { 180 181 e1.printStackTrace(); 182 } 183 } 184 185 private void put(String key, Object value) { 186 if (value != null) 187 properties.put(key, value); 188 } 189 190 } | Popular Tags |