KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > app > qed > AdminSession


1 package com.quadcap.app.qed;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.File JavaDoc;
42
43 import java.util.Enumeration JavaDoc;
44 import java.util.Hashtable JavaDoc;
45 import java.util.Properties JavaDoc;
46 import java.util.Vector JavaDoc;
47
48 //-//#ifdef JDK11
49
//-import com.quadcap.io.URLDecoder;
50
//#else
51
import java.net.URLDecoder JavaDoc;
52 //#endif
53

54 import java.sql.Connection JavaDoc;
55 import java.sql.DriverManager JavaDoc;
56 import java.sql.SQLException JavaDoc;
57
58 import javax.servlet.ServletException JavaDoc;
59
60 import javax.servlet.http.HttpServletRequest JavaDoc;
61
62 import com.quadcap.sql.Database;
63
64 import com.quadcap.jdbc.JdbcDriver;
65
66 import com.quadcap.util.ConfigString;
67 import com.quadcap.util.Debug;
68
69 /**
70  * A JavaBean which encapsulates administrative access to QED databases running
71  * in this JVM.
72  *
73  * @author Stan Bailes
74  */

75 public class AdminSession {
76     String JavaDoc page = "login.jsp";
77     JdbcDriver qedDriver = null;
78     boolean isAuthenticated = false;
79     String JavaDoc auth = "admin";
80     ConfigString adminUser;
81     ConfigString adminPassword;
82     Hashtable JavaDoc connections = new Hashtable JavaDoc();
83
84     /**
85      * Construct a new admin session
86      */

87     public AdminSession() {
88     adminUser = ConfigString.find("qed.admin.user", null);
89     adminPassword = ConfigString.find("qed.admin.password", null);
90     if (adminUser.getValue() == null) {
91             Debug.println(0, "**** Since no configuration value was " +
92                           "supplied for the config variable 'qed.admin.user'" +
93                           ", authentication is disabled");
94         isAuthenticated = true;
95     }
96     }
97
98     /*{com.quadcap.app.qed.AdminSession.xml-0}
99      *
100      */

101     
102     /**
103      * Return true if this session has been authenticated
104      */

105     public boolean isAuthenticated() { return isAuthenticated; }
106
107     /**
108      * Remember which page we're displaying in the body frame
109      */

110     public void setPage(String JavaDoc page) { this.page = page; }
111
112     /**
113      * Return the last page visited in the body frame
114      */

115     public String JavaDoc getPage() { return page; }
116
117     /**
118      * Return the QED JDBC Driver instance. There should be only one.
119      */

120     public JdbcDriver getDriver() {
121     if (qedDriver == null) {
122         try {
123         Class.forName("com.quadcap.jdbc.JdbcDriver");
124         } catch (Exception JavaDoc e) {
125         Debug.print(e);
126         throw new RuntimeException JavaDoc("Can't get QED driver");
127         }
128         Enumeration JavaDoc e = DriverManager.getDrivers();
129         while (e.hasMoreElements()) {
130         Object JavaDoc obj = e.nextElement();
131         if (obj instanceof JdbcDriver) {
132             qedDriver = (JdbcDriver)obj;
133             break;
134         }
135         }
136     }
137     return qedDriver;
138     }
139     
140     /**
141      * Return an enumeration of strings containing the names of all open
142      * databases.
143      */

144     public Enumeration JavaDoc getDatabaseNames() {
145     JdbcDriver driver = getDriver();
146     if (driver == null) {
147         return (new Vector JavaDoc()).elements();
148     }
149     return driver.getDatabaseNames();
150     }
151
152     /**
153      * Return the opened database with the specified name.
154      */

155     private Database findDatabase(String JavaDoc name) {
156     JdbcDriver driver = getDriver();
157     if (driver == null) return null;
158     return driver.getDatabase(name);
159     }
160
161     /**
162      * Return the opened database with the specified name.
163      */

164     public Database getDatabase(String JavaDoc name) throws SQLException JavaDoc {
165     Database db = null;
166     Connection JavaDoc conn = getConnection(name);
167     if (conn != null) {
168         if (conn instanceof com.quadcap.jdbc.Connection) {
169         db = ((com.quadcap.jdbc.Connection)conn).getDatabase();
170         }
171     }
172     return db;
173     }
174
175     /**
176      * Return a new connection for the specified database.
177      */

178     public Connection JavaDoc getConnection(String JavaDoc name,
179                     boolean create, boolean force)
180     throws SQLException JavaDoc
181     {
182     name = new File JavaDoc(name).getAbsolutePath();
183     Connection JavaDoc conn = (Connection JavaDoc)connections.get(name);
184     if (conn == null) {
185         Database db = findDatabase(name);
186         if (db == null) {
187         Properties JavaDoc props = new Properties JavaDoc();
188         if (create) props.put("create", "true");
189         if (force) props.put("force", "true");
190         props.put("user", auth);
191         String JavaDoc url = "jdbc:qed:" + name;
192         conn = getDriver().connect(url, props);
193         } else {
194         conn = getDriver().makeConnection(db, adminUser.getValue(),
195                                                   adminPassword.getValue());
196         }
197         if (conn != null) {
198         connections.put(name, conn);
199         }
200     }
201     return conn;
202     }
203
204     /**
205      * Return a new connection for the specified database.
206      */

207     public Connection JavaDoc getConnection(String JavaDoc name) throws SQLException JavaDoc {
208     return getConnection(name, false, false);
209     }
210
211     /**
212      * Close all open connections held by this session
213      */

214     void closeConnections() {
215     Enumeration JavaDoc e = connections.elements();
216     while (e.hasMoreElements()) {
217         try {
218         ((Connection JavaDoc)e.nextElement()).close();
219         } catch (Throwable JavaDoc t) {}
220     }
221     connections = new Hashtable JavaDoc();
222     }
223
224     /**
225      * jsp helper to build radio button checklists.
226      */

227     public static String JavaDoc isSel(String JavaDoc sel, String JavaDoc val) {
228     if (sel != null && val != null && sel.equals(val)) {
229         return " checked ";
230     } else {
231         return "";
232     }
233     }
234
235     /**
236      * jsp helper to build radio button checklists.
237      */

238     public static String JavaDoc isSet(int pos, int val) {
239     if (((val >> pos) & 1) == 1) {
240         return " checked ";
241     } else {
242         return "";
243     }
244     }
245
246     /**
247      * jsp helper to format time from backup time (minutes since midnite)
248      */

249     static char[] digits = {'0','1','2','3','4','5','6','7','8','9'};
250     public static String JavaDoc lz2(int num) {
251     if (num < 10) {
252         return "0" + digits[num];
253     } else {
254         return "" + digits[(num/10)%10] + digits[num%10];
255     }
256     }
257
258     /**
259      * Login action
260      */

261     private void login(Properties JavaDoc p) {
262     String JavaDoc admin = adminUser.getValue();
263     if (admin != null) {
264         String JavaDoc user = p.getProperty("user");
265         if (user != null && user.equals(admin)) {
266         String JavaDoc pass = p.getProperty("password");
267         String JavaDoc adminpass = adminPassword.getValue();
268         if (pass != null &&
269             (adminpass == null || pass.equals(adminpass))) {
270             isAuthenticated = true;
271             this.auth = admin;
272         }
273         }
274     }
275     }
276
277     /**
278      * Open action
279      */

280     private void open(Properties JavaDoc p) throws SQLException JavaDoc {
281     String JavaDoc name = p.getProperty("dbName");
282     name = new File JavaDoc(name).getAbsolutePath();
283     boolean create =
284         p.getProperty("create", "false").equalsIgnoreCase("true");
285     boolean force =
286         p.getProperty("force", "false").equalsIgnoreCase("true");
287     getConnection(name, create, force);
288     }
289
290     /**
291      * Close action
292      */

293     private void close(Properties JavaDoc p) {
294     String JavaDoc name = p.getProperty("dbName");
295     name = new File JavaDoc(name).getAbsolutePath();
296     Connection JavaDoc conn = (Connection JavaDoc)connections.get(name);
297     if (conn != null) {
298         try {
299         conn.close();
300         } catch (Throwable JavaDoc t) {
301                 Debug.print(t);
302             }
303         connections.remove(name);
304     }
305     }
306
307     /**
308      * Logout action
309      */

310     private void logout(Properties JavaDoc p) {
311     if (adminUser.getValue() != null) {
312         isAuthenticated = false;
313         closeConnections();
314     }
315     }
316
317     /**
318      * The main request handler. Determine which action is to be invoked
319      * and dispatch the appropriate routine.
320      *
321      * @param req the http request
322      * @exception ServletException may be thrown
323      */

324     public void handleRequest(HttpServletRequest JavaDoc req)
325     throws ServletException JavaDoc, SQLException JavaDoc
326     {
327     String JavaDoc action = req.getParameter("action");
328     if (action != null) {
329         Properties JavaDoc p = getRequestProperties(req);
330             Debug.println(4, "admin(" + action + "): " + p);
331         if (action.equals("login")) {
332         login(p);
333         } else if (action.equals("logout")) {
334         logout(p);
335         } else if (!isAuthenticated()) {
336         throw new ServletException JavaDoc("not logged in");
337         } else if (action.equals("open")) {
338         open(p);
339         } else if (action.equals("close")) {
340         close(p);
341         } else {
342         throw new ServletException JavaDoc("bad action");
343         }
344     } else if (!isAuthenticated()) {
345         throw new ServletException JavaDoc("not logged in");
346     }
347     }
348
349     /**
350      * Build a Properties object containing all of the request parameters.
351      *
352      * @param req the http request
353      * @return the request parameters
354      */

355     public Properties JavaDoc getRequestProperties(HttpServletRequest JavaDoc req) {
356     Properties JavaDoc p = new Properties JavaDoc();
357     Enumeration JavaDoc e = req.getParameterNames();
358     while (e.hasMoreElements()) {
359         String JavaDoc name = (String JavaDoc)e.nextElement();
360         String JavaDoc val = req.getParameter(name);
361         try {
362         val = URLDecoder.decode(val);
363         } catch (Throwable JavaDoc ex) {}
364         p.put(name, val);
365     }
366     return p;
367     }
368
369 }
370
Popular Tags