1 24 25 package org.objectweb.cjdbc.console.gui.session; 26 27 import java.io.BufferedWriter ; 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileWriter ; 31 import java.io.IOException ; 32 import java.util.ArrayList ; 33 import java.util.Enumeration ; 34 import java.util.Hashtable ; 35 import java.util.Properties ; 36 37 import org.objectweb.cjdbc.common.util.ReadWrite; 38 39 45 public class GuiSession 46 { 47 private final boolean saveDatabaseInfoToDisk = true; 48 ArrayList controllerItems; 49 ArrayList configurationFiles; 50 Hashtable databaseItems; 51 52 55 public GuiSession() 56 { 57 controllerItems = new ArrayList (); 58 configurationFiles = new ArrayList (); 59 databaseItems = new Hashtable (); 60 } 61 62 68 public void saveSessionToFile(File sessionFile) throws IOException 69 { 70 BufferedWriter writer = new BufferedWriter (new FileWriter (sessionFile)); 71 if (saveDatabaseInfoToDisk) 72 { 73 writer.write("### DATABASES ###" 74 + System.getProperty("line.separator")); 75 writer.write(ReadWrite.write(databaseItems, false)); 76 } 77 writer.write("### CONTROLLERS ###" 78 + System.getProperty("line.separator")); 79 writer.write(ReadWrite.write(controllerItems, "controller", false)); 80 writer.write("### CONFIGURATION FILES ###" 81 + System.getProperty("line.separator")); 82 writer.write(ReadWrite.write(configurationFiles, "configuration", false)); 83 writer.close(); 84 } 85 86 92 public void loadSessionFromFile(File sessionFile) throws IOException 93 { 94 if (sessionFile.exists()) 95 { 96 Properties session = new Properties (); 97 session.load(new FileInputStream (sessionFile)); 98 Enumeration enume = session.keys(); 99 String key; 100 String value; 101 while (enume.hasMoreElements()) 102 { 103 key = (String ) enume.nextElement(); 104 value = (String ) session.get(key); 105 if (key.startsWith("controller")) 106 controllerItems.add(value); 107 else if (key.startsWith("database")) 108 databaseItems.put(key, value); 109 else if (key.startsWith("configuration")) 110 addFileToConfigurationFiles(new File (value)); 111 } 112 } 113 else 114 { 115 controllerItems.add("0.0.0.0:1090"); 116 } 127 } 128 129 134 public ArrayList getControllerItems() 135 { 136 return controllerItems; 137 } 138 139 144 public void addControllerToList(String controller) 145 { 146 if (!controllerItems.contains(controller)) 147 controllerItems.add(controller); 148 } 149 150 156 public boolean checkControllerInSession(String controller) 157 { 158 return controllerItems.contains(controller); 159 } 160 161 166 public ArrayList getConfigurationFiles() 167 { 168 return configurationFiles; 169 } 170 171 177 public void addFileToConfigurationFiles(File newFile) 178 { 179 if (!configurationFiles.contains(newFile)) 180 configurationFiles.add(newFile); 181 } 182 183 190 public void addDatabaseToSession(String databaseName, String login, 191 String password) 192 { 193 databaseItems.put("database." + databaseName + ".login", login); 194 databaseItems.put("database." + databaseName + ".password", password); 195 } 196 197 204 public boolean isAuthenticatedDatabase(String databaseName) 205 { 206 return databaseItems.containsKey("database." + databaseName + ".login"); 207 } 208 209 215 public String getAuthenticatedDatabaseLogin(String databaseName) 216 { 217 return (String ) databaseItems.get("database." + databaseName + ".login"); 218 } 219 220 226 public String getAuthenticatedDatabasePassword(String databaseName) 227 { 228 return (String ) databaseItems.get("database." + databaseName + ".password"); 229 } 230 231 236 public Hashtable getDatabaseItems() 237 { 238 return databaseItems; 239 } 240 } 241 | Popular Tags |