001: /*
002: * Copyright (C) 2004 Nicky BRAMANTE
003: *
004: * This file is part of FreeQueryBuilder
005: *
006: * FreeQueryBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Send questions or suggestions to nickyb@interfree.it
021: */
022:
023: package it.frb.admin;
024:
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.io.ObjectInputStream;
030: import java.io.ObjectOutputStream;
031:
032: import java.util.Enumeration;
033: import java.util.Hashtable;
034: import java.util.Properties;
035: import java.util.StringTokenizer;
036: import java.util.Vector;
037:
038: public class UserSession {
039: private static String oldadminfile = System
040: .getProperty("user.home")
041: + File.separator + ".fqb_jdbc_admin";
042: private static String userfile = System.getProperty("user.home")
043: + File.separator + ".freereportbuilder";
044:
045: protected final static String VERSION = "version";
046:
047: protected final static String JDBC_PROFILES = "profile.names";
048: protected final static String JDBC_DRIVERS = "driver.names";
049:
050: protected final static String HISTORY_COMMANDS = "histroy.commands";
051: protected final static String HISTORY_MODELS = "histroy.models";
052: protected final static String HISTORY_MODELS_PLUS = "histroy.models.plus";
053:
054: protected static Hashtable jdbc;
055: protected static Hashtable generic;
056: protected static Hashtable histroy;
057:
058: public static void load() {
059: jdbc = new Hashtable();
060: histroy = new Hashtable();
061: generic = new Hashtable();
062:
063: try {
064: File file = new File(userfile);
065: if (file.exists()) {
066: FileInputStream istream = new FileInputStream(userfile);
067: ObjectInputStream p = new ObjectInputStream(istream);
068:
069: jdbc = (Hashtable) p.readObject();
070: histroy = (Hashtable) p.readObject();
071:
072: if (istream.available() > 0)
073: generic = (Hashtable) p.readObject();
074:
075: istream.close();
076: } else {
077: jdbc.put(JDBC_DRIVERS, "JDBC-ODBC Bridge");
078: jdbc.put("JDBC-ODBC Bridge.file", "$CLASSPATH");
079: jdbc.put("JDBC-ODBC Bridge.driver",
080: "sun.jdbc.odbc.JdbcOdbcDriver");
081: jdbc.put("JDBC-ODBC Bridge.example", "jdbc:odbc:<DSN>");
082:
083: jdbc.put(JDBC_PROFILES, "<editme>");
084: jdbc.put("<editme>.drv", "$JDBC-ODBC Bridge");
085: jdbc.put("<editme>.uid", "<enter>");
086: jdbc.put("<editme>.url", "jdbc:odbc:<DSN>");
087: }
088:
089: if (!histroy.containsKey(HISTORY_COMMANDS))
090: histroy.put(HISTORY_COMMANDS, new Vector());
091: if (!histroy.containsKey(HISTORY_MODELS))
092: histroy.put(HISTORY_MODELS, new Vector());
093: if (!histroy.containsKey(HISTORY_MODELS_PLUS))
094: histroy.put(HISTORY_MODELS_PLUS, new Vector());
095:
096: File oldfile = new File(oldadminfile);
097: if (oldfile.exists()) {
098: FileInputStream istream = new FileInputStream(oldfile);
099: Properties old = new Properties();
100: old.load(istream);
101: istream.close();
102:
103: for (Enumeration e = old.propertyNames(); e
104: .hasMoreElements();) {
105: String propertyName = e.nextElement().toString();
106: jdbc.put(propertyName, old
107: .getProperty(propertyName));
108: }
109:
110: oldfile.renameTo(file);
111: UserSession.save();
112: }
113: } catch (IOException e) {
114: e.printStackTrace();
115: } catch (ClassNotFoundException e) {
116: e.printStackTrace();
117: }
118: }
119:
120: protected static void save() {
121: generic.put(VERSION, UIUtilities.MAJOR_VERSION + "."
122: + UIUtilities.MINOR_VERSION);
123:
124: try {
125: File file = new File(userfile);
126: if (!file.exists())
127: file.createNewFile();
128:
129: FileOutputStream ostream = new FileOutputStream(file);
130: ObjectOutputStream p = new ObjectOutputStream(ostream);
131:
132: p.writeObject(jdbc);
133: p.writeObject(histroy);
134: p.writeObject(generic);
135:
136: p.flush();
137: ostream.close();
138: } catch (IOException e) {
139: e.printStackTrace();
140: }
141: }
142:
143: public static Object[] listProfile() {
144: Vector name = new Vector();
145: String declared_profiles = UserSession.jdbc.get(
146: UserSession.JDBC_PROFILES).toString();
147: StringTokenizer k = new StringTokenizer(declared_profiles, ";");
148:
149: k = new StringTokenizer(declared_profiles, ";");
150:
151: while (k.hasMoreTokens()) {
152: name.add((String) k.nextToken());
153: }
154:
155: return name.toArray();
156: }
157: }
|