001: package com.quadcap.app.qed;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.File;
042:
043: import java.util.Enumeration;
044: import java.util.Hashtable;
045: import java.util.Properties;
046: import java.util.Vector;
047:
048: //-//#ifdef JDK11
049: //-import com.quadcap.io.URLDecoder;
050: //#else
051: import java.net.URLDecoder; //#endif
052:
053: import java.sql.Connection;
054: import java.sql.DriverManager;
055: import java.sql.SQLException;
056:
057: import javax.servlet.ServletException;
058:
059: import javax.servlet.http.HttpServletRequest;
060:
061: import com.quadcap.sql.Database;
062:
063: import com.quadcap.jdbc.JdbcDriver;
064:
065: import com.quadcap.util.ConfigString;
066: import com.quadcap.util.Debug;
067:
068: /**
069: * A JavaBean which encapsulates administrative access to QED databases running
070: * in this JVM.
071: *
072: * @author Stan Bailes
073: */
074: public class AdminSession {
075: String page = "login.jsp";
076: JdbcDriver qedDriver = null;
077: boolean isAuthenticated = false;
078: String auth = "admin";
079: ConfigString adminUser;
080: ConfigString adminPassword;
081: Hashtable connections = new Hashtable();
082:
083: /**
084: * Construct a new admin session
085: */
086: public AdminSession() {
087: adminUser = ConfigString.find("qed.admin.user", null);
088: adminPassword = ConfigString.find("qed.admin.password", null);
089: if (adminUser.getValue() == null) {
090: Debug
091: .println(
092: 0,
093: "**** Since no configuration value was "
094: + "supplied for the config variable 'qed.admin.user'"
095: + ", authentication is disabled");
096: isAuthenticated = true;
097: }
098: }
099:
100: /*{com.quadcap.app.qed.AdminSession.xml-0}
101: *
102: */
103:
104: /**
105: * Return true if this session has been authenticated
106: */
107: public boolean isAuthenticated() {
108: return isAuthenticated;
109: }
110:
111: /**
112: * Remember which page we're displaying in the body frame
113: */
114: public void setPage(String page) {
115: this .page = page;
116: }
117:
118: /**
119: * Return the last page visited in the body frame
120: */
121: public String getPage() {
122: return page;
123: }
124:
125: /**
126: * Return the QED JDBC Driver instance. There should be only one.
127: */
128: public JdbcDriver getDriver() {
129: if (qedDriver == null) {
130: try {
131: Class.forName("com.quadcap.jdbc.JdbcDriver");
132: } catch (Exception e) {
133: Debug.print(e);
134: throw new RuntimeException("Can't get QED driver");
135: }
136: Enumeration e = DriverManager.getDrivers();
137: while (e.hasMoreElements()) {
138: Object obj = e.nextElement();
139: if (obj instanceof JdbcDriver) {
140: qedDriver = (JdbcDriver) obj;
141: break;
142: }
143: }
144: }
145: return qedDriver;
146: }
147:
148: /**
149: * Return an enumeration of strings containing the names of all open
150: * databases.
151: */
152: public Enumeration getDatabaseNames() {
153: JdbcDriver driver = getDriver();
154: if (driver == null) {
155: return (new Vector()).elements();
156: }
157: return driver.getDatabaseNames();
158: }
159:
160: /**
161: * Return the opened database with the specified name.
162: */
163: private Database findDatabase(String name) {
164: JdbcDriver driver = getDriver();
165: if (driver == null)
166: return null;
167: return driver.getDatabase(name);
168: }
169:
170: /**
171: * Return the opened database with the specified name.
172: */
173: public Database getDatabase(String name) throws SQLException {
174: Database db = null;
175: Connection conn = getConnection(name);
176: if (conn != null) {
177: if (conn instanceof com.quadcap.jdbc.Connection) {
178: db = ((com.quadcap.jdbc.Connection) conn).getDatabase();
179: }
180: }
181: return db;
182: }
183:
184: /**
185: * Return a new connection for the specified database.
186: */
187: public Connection getConnection(String name, boolean create,
188: boolean force) throws SQLException {
189: name = new File(name).getAbsolutePath();
190: Connection conn = (Connection) connections.get(name);
191: if (conn == null) {
192: Database db = findDatabase(name);
193: if (db == null) {
194: Properties props = new Properties();
195: if (create)
196: props.put("create", "true");
197: if (force)
198: props.put("force", "true");
199: props.put("user", auth);
200: String url = "jdbc:qed:" + name;
201: conn = getDriver().connect(url, props);
202: } else {
203: conn = getDriver().makeConnection(db,
204: adminUser.getValue(), adminPassword.getValue());
205: }
206: if (conn != null) {
207: connections.put(name, conn);
208: }
209: }
210: return conn;
211: }
212:
213: /**
214: * Return a new connection for the specified database.
215: */
216: public Connection getConnection(String name) throws SQLException {
217: return getConnection(name, false, false);
218: }
219:
220: /**
221: * Close all open connections held by this session
222: */
223: void closeConnections() {
224: Enumeration e = connections.elements();
225: while (e.hasMoreElements()) {
226: try {
227: ((Connection) e.nextElement()).close();
228: } catch (Throwable t) {
229: }
230: }
231: connections = new Hashtable();
232: }
233:
234: /**
235: * jsp helper to build radio button checklists.
236: */
237: public static String isSel(String sel, String val) {
238: if (sel != null && val != null && sel.equals(val)) {
239: return " checked ";
240: } else {
241: return "";
242: }
243: }
244:
245: /**
246: * jsp helper to build radio button checklists.
247: */
248: public static String isSet(int pos, int val) {
249: if (((val >> pos) & 1) == 1) {
250: return " checked ";
251: } else {
252: return "";
253: }
254: }
255:
256: /**
257: * jsp helper to format time from backup time (minutes since midnite)
258: */
259: static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7',
260: '8', '9' };
261:
262: public static String lz2(int num) {
263: if (num < 10) {
264: return "0" + digits[num];
265: } else {
266: return "" + digits[(num / 10) % 10] + digits[num % 10];
267: }
268: }
269:
270: /**
271: * Login action
272: */
273: private void login(Properties p) {
274: String admin = adminUser.getValue();
275: if (admin != null) {
276: String user = p.getProperty("user");
277: if (user != null && user.equals(admin)) {
278: String pass = p.getProperty("password");
279: String adminpass = adminPassword.getValue();
280: if (pass != null
281: && (adminpass == null || pass.equals(adminpass))) {
282: isAuthenticated = true;
283: this .auth = admin;
284: }
285: }
286: }
287: }
288:
289: /**
290: * Open action
291: */
292: private void open(Properties p) throws SQLException {
293: String name = p.getProperty("dbName");
294: name = new File(name).getAbsolutePath();
295: boolean create = p.getProperty("create", "false")
296: .equalsIgnoreCase("true");
297: boolean force = p.getProperty("force", "false")
298: .equalsIgnoreCase("true");
299: getConnection(name, create, force);
300: }
301:
302: /**
303: * Close action
304: */
305: private void close(Properties p) {
306: String name = p.getProperty("dbName");
307: name = new File(name).getAbsolutePath();
308: Connection conn = (Connection) connections.get(name);
309: if (conn != null) {
310: try {
311: conn.close();
312: } catch (Throwable t) {
313: Debug.print(t);
314: }
315: connections.remove(name);
316: }
317: }
318:
319: /**
320: * Logout action
321: */
322: private void logout(Properties p) {
323: if (adminUser.getValue() != null) {
324: isAuthenticated = false;
325: closeConnections();
326: }
327: }
328:
329: /**
330: * The main request handler. Determine which action is to be invoked
331: * and dispatch the appropriate routine.
332: *
333: * @param req the http request
334: * @exception ServletException may be thrown
335: */
336: public void handleRequest(HttpServletRequest req)
337: throws ServletException, SQLException {
338: String action = req.getParameter("action");
339: if (action != null) {
340: Properties p = getRequestProperties(req);
341: Debug.println(4, "admin(" + action + "): " + p);
342: if (action.equals("login")) {
343: login(p);
344: } else if (action.equals("logout")) {
345: logout(p);
346: } else if (!isAuthenticated()) {
347: throw new ServletException("not logged in");
348: } else if (action.equals("open")) {
349: open(p);
350: } else if (action.equals("close")) {
351: close(p);
352: } else {
353: throw new ServletException("bad action");
354: }
355: } else if (!isAuthenticated()) {
356: throw new ServletException("not logged in");
357: }
358: }
359:
360: /**
361: * Build a Properties object containing all of the request parameters.
362: *
363: * @param req the http request
364: * @return the request parameters
365: */
366: public Properties getRequestProperties(HttpServletRequest req) {
367: Properties p = new Properties();
368: Enumeration e = req.getParameterNames();
369: while (e.hasMoreElements()) {
370: String name = (String) e.nextElement();
371: String val = req.getParameter(name);
372: try {
373: val = URLDecoder.decode(val);
374: } catch (Throwable ex) {
375: }
376: p.put(name, val);
377: }
378: return p;
379: }
380:
381: }
|