Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

GuiSession.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.console.gui.session;
00026 
00027 import java.io.BufferedWriter;
00028 import java.io.File;
00029 import java.io.FileInputStream;
00030 import java.io.FileWriter;
00031 import java.io.IOException;
00032 import java.util.ArrayList;
00033 import java.util.Enumeration;
00034 import java.util.Hashtable;
00035 import java.util.Properties;
00036 
00037 import org.objectweb.cjdbc.common.util.ReadWrite;
00038 
00039 /**
00040  * This class defines a GUISession
00041  * 
00042  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00043  * @version 1.0
00044  */
00045 public class GuiSession
00046 {
00047   private final boolean saveDatabaseInfoToDisk = true;
00048   ArrayList             controllerItems;
00049   ArrayList             configurationFiles;
00050   Hashtable             databaseItems;
00051 
00052   /**
00053    * Creates a new <code>GUISession.java</code> object no recorded values
00054    */
00055   public GuiSession()
00056   {
00057     controllerItems = new ArrayList();
00058     configurationFiles = new ArrayList();
00059     databaseItems = new Hashtable();
00060   }
00061 
00062   /**
00063    * Save the current gui session into a file
00064    * 
00065    * @param sessionFile the file to save the session in
00066    * @throws IOException if writing causes a problem
00067    */
00068   public void saveSessionToFile(File sessionFile) throws IOException
00069   {
00070     BufferedWriter writer = new BufferedWriter(new FileWriter(sessionFile));
00071     if (saveDatabaseInfoToDisk)
00072     {
00073       writer.write("### DATABASES           ###"
00074           + System.getProperty("line.separator"));
00075       writer.write(ReadWrite.write(databaseItems, false));
00076     }
00077     writer.write("### CONTROLLERS         ###"
00078         + System.getProperty("line.separator"));
00079     writer.write(ReadWrite.write(controllerItems, "controller", false));
00080     writer.write("### CONFIGURATION FILES ###"
00081         + System.getProperty("line.separator"));
00082     writer.write(ReadWrite.write(configurationFiles, "configuration", false));
00083     writer.close();
00084   }
00085 
00086   /**
00087    * Load a gui session from the give file
00088    * 
00089    * @param sessionFile the file to load the session from
00090    * @throws IOException if loading causes a problem
00091    */
00092   public void loadSessionFromFile(File sessionFile) throws IOException
00093   {
00094     if (sessionFile.exists())
00095     {
00096       Properties session = new Properties();
00097       session.load(new FileInputStream(sessionFile));
00098       Enumeration enume = session.keys();
00099       String key;
00100       String value;
00101       while (enume.hasMoreElements())
00102       {
00103         key = (String) enume.nextElement();
00104         value = (String) session.get(key);
00105         if (key.startsWith("controller"))
00106           controllerItems.add(value);
00107         else if (key.startsWith("database"))
00108           databaseItems.put(key, value);
00109         else if (key.startsWith("configuration"))
00110           addFileToConfigurationFiles(new File(value));
00111       }
00112     }
00113     else
00114     {
00115       controllerItems.add("0.0.0.0:1090");
00116       //      URL url = this.getClass().getResource("/virtualdatabase");
00117       //      System.out.println(url.getFile());
00118       //      if (url != null)
00119       //      {
00120       //        File f = new File(url.getFile());
00121       //        File[] list = f.listFiles();
00122       //        
00123       //        for (int i = 0; i < list.length; i++)
00124       //          addFileToConfigurationFiles(list[i]);
00125       //      }
00126     }
00127   }
00128 
00129   /**
00130    * Returns the controllerItems value.
00131    * 
00132    * @return Returns the controllerItems.
00133    */
00134   public ArrayList getControllerItems()
00135   {
00136     return controllerItems;
00137   }
00138 
00139   /**
00140    * Add a controller url to the list of controllers
00141    * 
00142    * @param controller [ipAddress]:[portNumber]
00143    */
00144   public void addControllerToList(String controller)
00145   {
00146     if (!controllerItems.contains(controller))
00147       controllerItems.add(controller);
00148   }
00149 
00150   /**
00151    * checkif a controller is in the session
00152    * 
00153    * @param controller [ipAddress]:[portNumber]
00154    * @return true if controller is in session
00155    */
00156   public boolean checkControllerInSession(String controller)
00157   {
00158     return controllerItems.contains(controller);
00159   }
00160 
00161   /**
00162    * Get the list of configuration files
00163    * 
00164    * @return the <code>Vector</code> of configuration files
00165    */
00166   public ArrayList getConfigurationFiles()
00167   {
00168     return configurationFiles;
00169   }
00170 
00171   /**
00172    * Add a file to the list of configuration files if it is not already in the
00173    * session
00174    * 
00175    * @param newFile to add to the list
00176    */
00177   public void addFileToConfigurationFiles(File newFile)
00178   {
00179     if (!configurationFiles.contains(newFile))
00180       configurationFiles.add(newFile);
00181   }
00182 
00183   /**
00184    * Add authentication to session
00185    * 
00186    * @param databaseName name of the virtual database
00187    * @param login login name
00188    * @param password password associated to the login
00189    */
00190   public void addDatabaseToSession(String databaseName, String login,
00191       String password)
00192   {
00193     databaseItems.put("database." + databaseName + ".login", login);
00194     databaseItems.put("database." + databaseName + ".password", password);
00195   }
00196 
00197   /**
00198    * Test if user was authenticated for this database
00199    * 
00200    * @param databaseName name of the virtual database
00201    * @return true if has been authenticated with success before, false
00202    *         otherwise
00203    */
00204   public boolean isAuthenticatedDatabase(String databaseName)
00205   {
00206     return databaseItems.containsKey("database." + databaseName + ".login");
00207   }
00208 
00209   /**
00210    * Retrieve the login stored for this database
00211    * 
00212    * @param databaseName name of the virtual database
00213    * @return password as a <code>String</code> or null
00214    */
00215   public String getAuthenticatedDatabaseLogin(String databaseName)
00216   {
00217     return (String) databaseItems.get("database." + databaseName + ".login");
00218   }
00219 
00220   /**
00221    * Retrieve the password stored for this database
00222    * 
00223    * @param databaseName name of the virtual database
00224    * @return password as a <code>String</code> or null
00225    */
00226   public String getAuthenticatedDatabasePassword(String databaseName)
00227   {
00228     return (String) databaseItems.get("database." + databaseName + ".password");
00229   }
00230 
00231   /**
00232    * Returns the databaseItems value.
00233    * 
00234    * @return Returns the databaseItems.
00235    */
00236   public Hashtable getDatabaseItems()
00237   {
00238     return databaseItems;
00239   }
00240 }

Generated on Mon Apr 11 22:01:32 2005 for C-JDBC by  doxygen 1.3.9.1