KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > gui > session > GuiSession


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.gui.session;
26
27 import java.io.BufferedWriter JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileWriter JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Hashtable JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 import org.objectweb.cjdbc.common.util.ReadWrite;
38
39 /**
40  * This class defines a GUISession
41  *
42  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
43  * @version 1.0
44  */

45 public class GuiSession
46 {
47   private final boolean saveDatabaseInfoToDisk = true;
48   ArrayList JavaDoc controllerItems;
49   ArrayList JavaDoc configurationFiles;
50   Hashtable JavaDoc databaseItems;
51
52   /**
53    * Creates a new <code>GUISession.java</code> object no recorded values
54    */

55   public GuiSession()
56   {
57     controllerItems = new ArrayList JavaDoc();
58     configurationFiles = new ArrayList JavaDoc();
59     databaseItems = new Hashtable JavaDoc();
60   }
61
62   /**
63    * Save the current gui session into a file
64    *
65    * @param sessionFile the file to save the session in
66    * @throws IOException if writing causes a problem
67    */

68   public void saveSessionToFile(File JavaDoc sessionFile) throws IOException JavaDoc
69   {
70     BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(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   /**
87    * Load a gui session from the give file
88    *
89    * @param sessionFile the file to load the session from
90    * @throws IOException if loading causes a problem
91    */

92   public void loadSessionFromFile(File JavaDoc sessionFile) throws IOException JavaDoc
93   {
94     if (sessionFile.exists())
95     {
96       Properties JavaDoc session = new Properties JavaDoc();
97       session.load(new FileInputStream JavaDoc(sessionFile));
98       Enumeration JavaDoc enume = session.keys();
99       String JavaDoc key;
100       String JavaDoc value;
101       while (enume.hasMoreElements())
102       {
103         key = (String JavaDoc) enume.nextElement();
104         value = (String JavaDoc) 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 JavaDoc(value));
111       }
112     }
113     else
114     {
115       controllerItems.add("0.0.0.0:1090");
116       // URL url = this.getClass().getResource("/virtualdatabase");
117
// System.out.println(url.getFile());
118
// if (url != null)
119
// {
120
// File f = new File(url.getFile());
121
// File[] list = f.listFiles();
122
//
123
// for (int i = 0; i < list.length; i++)
124
// addFileToConfigurationFiles(list[i]);
125
// }
126
}
127   }
128
129   /**
130    * Returns the controllerItems value.
131    *
132    * @return Returns the controllerItems.
133    */

134   public ArrayList JavaDoc getControllerItems()
135   {
136     return controllerItems;
137   }
138
139   /**
140    * Add a controller url to the list of controllers
141    *
142    * @param controller [ipAddress]:[portNumber]
143    */

144   public void addControllerToList(String JavaDoc controller)
145   {
146     if (!controllerItems.contains(controller))
147       controllerItems.add(controller);
148   }
149
150   /**
151    * checkif a controller is in the session
152    *
153    * @param controller [ipAddress]:[portNumber]
154    * @return true if controller is in session
155    */

156   public boolean checkControllerInSession(String JavaDoc controller)
157   {
158     return controllerItems.contains(controller);
159   }
160
161   /**
162    * Get the list of configuration files
163    *
164    * @return the <code>Vector</code> of configuration files
165    */

166   public ArrayList JavaDoc getConfigurationFiles()
167   {
168     return configurationFiles;
169   }
170
171   /**
172    * Add a file to the list of configuration files if it is not already in the
173    * session
174    *
175    * @param newFile to add to the list
176    */

177   public void addFileToConfigurationFiles(File JavaDoc newFile)
178   {
179     if (!configurationFiles.contains(newFile))
180       configurationFiles.add(newFile);
181   }
182
183   /**
184    * Add authentication to session
185    *
186    * @param databaseName name of the virtual database
187    * @param login login name
188    * @param password password associated to the login
189    */

190   public void addDatabaseToSession(String JavaDoc databaseName, String JavaDoc login,
191       String JavaDoc password)
192   {
193     databaseItems.put("database." + databaseName + ".login", login);
194     databaseItems.put("database." + databaseName + ".password", password);
195   }
196
197   /**
198    * Test if user was authenticated for this database
199    *
200    * @param databaseName name of the virtual database
201    * @return true if has been authenticated with success before, false
202    * otherwise
203    */

204   public boolean isAuthenticatedDatabase(String JavaDoc databaseName)
205   {
206     return databaseItems.containsKey("database." + databaseName + ".login");
207   }
208
209   /**
210    * Retrieve the login stored for this database
211    *
212    * @param databaseName name of the virtual database
213    * @return password as a <code>String</code> or null
214    */

215   public String JavaDoc getAuthenticatedDatabaseLogin(String JavaDoc databaseName)
216   {
217     return (String JavaDoc) databaseItems.get("database." + databaseName + ".login");
218   }
219
220   /**
221    * Retrieve the password stored for this database
222    *
223    * @param databaseName name of the virtual database
224    * @return password as a <code>String</code> or null
225    */

226   public String JavaDoc getAuthenticatedDatabasePassword(String JavaDoc databaseName)
227   {
228     return (String JavaDoc) databaseItems.get("database." + databaseName + ".password");
229   }
230
231   /**
232    * Returns the databaseItems value.
233    *
234    * @return Returns the databaseItems.
235    */

236   public Hashtable JavaDoc getDatabaseItems()
237   {
238     return databaseItems;
239   }
240 }
241
Popular Tags