QED Administrator Console

com/quadcap/app/qed/AdminSession.java

Go to the documentation of this file.
00001 package com.quadcap.app.qed; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.File; 00042 00043 import java.util.Enumeration; 00044 import java.util.Hashtable; 00045 import java.util.Properties; 00046 import java.util.Vector; 00047 00048 //-//#ifdef JDK11 00049 //-import com.quadcap.io.URLDecoder; 00050 //#else 00051 import java.net.URLDecoder; 00052 //#endif 00053 00054 import java.sql.Connection; 00055 import java.sql.DriverManager; 00056 import java.sql.SQLException; 00057 00058 import javax.servlet.ServletException; 00059 00060 import javax.servlet.http.HttpServletRequest; 00061 00062 import com.quadcap.sql.Database; 00063 00064 import com.quadcap.jdbc.JdbcDriver; 00065 00066 import com.quadcap.util.ConfigString; 00067 import com.quadcap.util.Debug; 00068 00069 /** 00070 * A JavaBean which encapsulates administrative access to QED databases running 00071 * in this JVM. 00072 * 00073 * @author Stan Bailes 00074 */ 00075 public class AdminSession { 00076 String page = "login.jsp"; 00077 JdbcDriver qedDriver = null; 00078 boolean isAuthenticated = false; 00079 String auth = "admin"; 00080 ConfigString adminUser; 00081 ConfigString adminPassword; 00082 Hashtable connections = new Hashtable(); 00083 00084 /** 00085 * Construct a new admin session 00086 */ 00087 public AdminSession() { 00088 adminUser = ConfigString.find("qed.admin.user", null); 00089 adminPassword = ConfigString.find("qed.admin.password", null); 00090 if (adminUser.getValue() == null) { 00091 Debug.println(0, "**** Since no configuration value was " + 00092 "supplied for the config variable 'qed.admin.user'" + 00093 ", authentication is disabled"); 00094 isAuthenticated = true; 00095 } 00096 } 00097 00098 /*{com.quadcap.app.qed.AdminSession.xml-0} 00099 * 00100 */ 00101 00102 /** 00103 * Return true if this session has been authenticated 00104 */ 00105 public boolean isAuthenticated() { return isAuthenticated; } 00106 00107 /** 00108 * Remember which page we're displaying in the body frame 00109 */ 00110 public void setPage(String page) { this.page = page; } 00111 00112 /** 00113 * Return the last page visited in the body frame 00114 */ 00115 public String getPage() { return page; } 00116 00117 /** 00118 * Return the QED JDBC Driver instance. There should be only one. 00119 */ 00120 public JdbcDriver getDriver() { 00121 if (qedDriver == null) { 00122 try { 00123 Class.forName("com.quadcap.jdbc.JdbcDriver"); 00124 } catch (Exception e) { 00125 Debug.print(e); 00126 throw new RuntimeException("Can't get QED driver"); 00127 } 00128 Enumeration e = DriverManager.getDrivers(); 00129 while (e.hasMoreElements()) { 00130 Object obj = e.nextElement(); 00131 if (obj instanceof JdbcDriver) { 00132 qedDriver = (JdbcDriver)obj; 00133 break; 00134 } 00135 } 00136 } 00137 return qedDriver; 00138 } 00139 00140 /** 00141 * Return an enumeration of strings containing the names of all open 00142 * databases. 00143 */ 00144 public Enumeration getDatabaseNames() { 00145 JdbcDriver driver = getDriver(); 00146 if (driver == null) { 00147 return (new Vector()).elements(); 00148 } 00149 return driver.getDatabaseNames(); 00150 } 00151 00152 /** 00153 * Return the opened database with the specified name. 00154 */ 00155 private Database findDatabase(String name) { 00156 JdbcDriver driver = getDriver(); 00157 if (driver == null) return null; 00158 return driver.getDatabase(name); 00159 } 00160 00161 /** 00162 * Return the opened database with the specified name. 00163 */ 00164 public Database getDatabase(String name) throws SQLException { 00165 Database db = null; 00166 Connection conn = getConnection(name); 00167 if (conn != null) { 00168 if (conn instanceof com.quadcap.jdbc.Connection) { 00169 db = ((com.quadcap.jdbc.Connection)conn).getDatabase(); 00170 } 00171 } 00172 return db; 00173 } 00174 00175 /** 00176 * Return a new connection for the specified database. 00177 */ 00178 public Connection getConnection(String name, 00179 boolean create, boolean force) 00180 throws SQLException 00181 { 00182 name = new File(name).getAbsolutePath(); 00183 Connection conn = (Connection)connections.get(name); 00184 if (conn == null) { 00185 Database db = findDatabase(name); 00186 if (db == null) { 00187 Properties props = new Properties(); 00188 if (create) props.put("create", "true"); 00189 if (force) props.put("force", "true"); 00190 props.put("user", auth); 00191 String url = "jdbc:qed:" + name; 00192 conn = getDriver().connect(url, props); 00193 } else { 00194 conn = getDriver().makeConnection(db, adminUser.getValue(), 00195 adminPassword.getValue()); 00196 } 00197 if (conn != null) { 00198 connections.put(name, conn); 00199 } 00200 } 00201 return conn; 00202 } 00203 00204 /** 00205 * Return a new connection for the specified database. 00206 */ 00207 public Connection getConnection(String name) throws SQLException { 00208 return getConnection(name, false, false); 00209 } 00210 00211 /** 00212 * Close all open connections held by this session 00213 */ 00214 void closeConnections() { 00215 Enumeration e = connections.elements(); 00216 while (e.hasMoreElements()) { 00217 try { 00218 ((Connection)e.nextElement()).close(); 00219 } catch (Throwable t) {} 00220 } 00221 connections = new Hashtable(); 00222 } 00223 00224 /** 00225 * jsp helper to build radio button checklists. 00226 */ 00227 public static String isSel(String sel, String val) { 00228 if (sel != null && val != null && sel.equals(val)) { 00229 return " checked "; 00230 } else { 00231 return ""; 00232 } 00233 } 00234 00235 /** 00236 * jsp helper to build radio button checklists. 00237 */ 00238 public static String isSet(int pos, int val) { 00239 if (((val >> pos) & 1) == 1) { 00240 return " checked "; 00241 } else { 00242 return ""; 00243 } 00244 } 00245 00246 /** 00247 * jsp helper to format time from backup time (minutes since midnite) 00248 */ 00249 static char[] digits = {'0','1','2','3','4','5','6','7','8','9'}; 00250 public static String lz2(int num) { 00251 if (num < 10) { 00252 return "0" + digits[num]; 00253 } else { 00254 return "" + digits[(num/10)%10] + digits[num%10]; 00255 } 00256 } 00257 00258 /** 00259 * Login action 00260 */ 00261 private void login(Properties p) { 00262 String admin = adminUser.getValue(); 00263 if (admin != null) { 00264 String user = p.getProperty("user"); 00265 if (user != null && user.equals(admin)) { 00266 String pass = p.getProperty("password"); 00267 String adminpass = adminPassword.getValue(); 00268 if (pass != null && 00269 (adminpass == null || pass.equals(adminpass))) { 00270 isAuthenticated = true; 00271 this.auth = admin; 00272 } 00273 } 00274 } 00275 } 00276 00277 /** 00278 * Open action 00279 */ 00280 private void open(Properties p) throws SQLException { 00281 String name = p.getProperty("dbName"); 00282 name = new File(name).getAbsolutePath(); 00283 boolean create = 00284 p.getProperty("create", "false").equalsIgnoreCase("true"); 00285 boolean force = 00286 p.getProperty("force", "false").equalsIgnoreCase("true"); 00287 getConnection(name, create, force); 00288 } 00289 00290 /** 00291 * Close action 00292 */ 00293 private void close(Properties p) { 00294 String name = p.getProperty("dbName"); 00295 name = new File(name).getAbsolutePath(); 00296 Connection conn = (Connection)connections.get(name); 00297 if (conn != null) { 00298 try { 00299 conn.close(); 00300 } catch (Throwable t) { 00301 Debug.print(t); 00302 } 00303 connections.remove(name); 00304 } 00305 } 00306 00307 /** 00308 * Logout action 00309 */ 00310 private void logout(Properties p) { 00311 if (adminUser.getValue() != null) { 00312 isAuthenticated = false; 00313 closeConnections(); 00314 } 00315 } 00316 00317 /** 00318 * The main request handler. Determine which action is to be invoked 00319 * and dispatch the appropriate routine. 00320 * 00321 * @param req the http request 00322 * @exception ServletException may be thrown 00323 */ 00324 public void handleRequest(HttpServletRequest req) 00325 throws ServletException, SQLException 00326 { 00327 String action = req.getParameter("action"); 00328 if (action != null) { 00329 Properties p = getRequestProperties(req); 00330 Debug.println(4, "admin(" + action + "): " + p); 00331 if (action.equals("login")) { 00332 login(p); 00333 } else if (action.equals("logout")) { 00334 logout(p); 00335 } else if (!isAuthenticated()) { 00336 throw new ServletException("not logged in"); 00337 } else if (action.equals("open")) { 00338 open(p); 00339 } else if (action.equals("close")) { 00340 close(p); 00341 } else { 00342 throw new ServletException("bad action"); 00343 } 00344 } else if (!isAuthenticated()) { 00345 throw new ServletException("not logged in"); 00346 } 00347 } 00348 00349 /** 00350 * Build a Properties object containing all of the request parameters. 00351 * 00352 * @param req the http request 00353 * @return the request parameters 00354 */ 00355 public Properties getRequestProperties(HttpServletRequest req) { 00356 Properties p = new Properties(); 00357 Enumeration e = req.getParameterNames(); 00358 while (e.hasMoreElements()) { 00359 String name = (String)e.nextElement(); 00360 String val = req.getParameter(name); 00361 try { 00362 val = URLDecoder.decode(val); 00363 } catch (Throwable ex) {} 00364 p.put(name, val); 00365 } 00366 return p; 00367 } 00368 00369 }