1 package com.quadcap.app.qed; 2 3 40 41 import java.io.File ; 42 43 import java.util.Enumeration ; 44 import java.util.Hashtable ; 45 import java.util.Properties ; 46 import java.util.Vector ; 47 48 import java.net.URLDecoder ; 52 54 import java.sql.Connection ; 55 import java.sql.DriverManager ; 56 import java.sql.SQLException ; 57 58 import javax.servlet.ServletException ; 59 60 import javax.servlet.http.HttpServletRequest ; 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 75 public class AdminSession { 76 String page = "login.jsp"; 77 JdbcDriver qedDriver = null; 78 boolean isAuthenticated = false; 79 String auth = "admin"; 80 ConfigString adminUser; 81 ConfigString adminPassword; 82 Hashtable connections = new Hashtable (); 83 84 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 101 102 105 public boolean isAuthenticated() { return isAuthenticated; } 106 107 110 public void setPage(String page) { this.page = page; } 111 112 115 public String getPage() { return page; } 116 117 120 public JdbcDriver getDriver() { 121 if (qedDriver == null) { 122 try { 123 Class.forName("com.quadcap.jdbc.JdbcDriver"); 124 } catch (Exception e) { 125 Debug.print(e); 126 throw new RuntimeException ("Can't get QED driver"); 127 } 128 Enumeration e = DriverManager.getDrivers(); 129 while (e.hasMoreElements()) { 130 Object obj = e.nextElement(); 131 if (obj instanceof JdbcDriver) { 132 qedDriver = (JdbcDriver)obj; 133 break; 134 } 135 } 136 } 137 return qedDriver; 138 } 139 140 144 public Enumeration getDatabaseNames() { 145 JdbcDriver driver = getDriver(); 146 if (driver == null) { 147 return (new Vector ()).elements(); 148 } 149 return driver.getDatabaseNames(); 150 } 151 152 155 private Database findDatabase(String name) { 156 JdbcDriver driver = getDriver(); 157 if (driver == null) return null; 158 return driver.getDatabase(name); 159 } 160 161 164 public Database getDatabase(String name) throws SQLException { 165 Database db = null; 166 Connection 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 178 public Connection getConnection(String name, 179 boolean create, boolean force) 180 throws SQLException 181 { 182 name = new File (name).getAbsolutePath(); 183 Connection conn = (Connection )connections.get(name); 184 if (conn == null) { 185 Database db = findDatabase(name); 186 if (db == null) { 187 Properties props = new Properties (); 188 if (create) props.put("create", "true"); 189 if (force) props.put("force", "true"); 190 props.put("user", auth); 191 String 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 207 public Connection getConnection(String name) throws SQLException { 208 return getConnection(name, false, false); 209 } 210 211 214 void closeConnections() { 215 Enumeration e = connections.elements(); 216 while (e.hasMoreElements()) { 217 try { 218 ((Connection )e.nextElement()).close(); 219 } catch (Throwable t) {} 220 } 221 connections = new Hashtable (); 222 } 223 224 227 public static String isSel(String sel, String val) { 228 if (sel != null && val != null && sel.equals(val)) { 229 return " checked "; 230 } else { 231 return ""; 232 } 233 } 234 235 238 public static String isSet(int pos, int val) { 239 if (((val >> pos) & 1) == 1) { 240 return " checked "; 241 } else { 242 return ""; 243 } 244 } 245 246 249 static char[] digits = {'0','1','2','3','4','5','6','7','8','9'}; 250 public static String 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 261 private void login(Properties p) { 262 String admin = adminUser.getValue(); 263 if (admin != null) { 264 String user = p.getProperty("user"); 265 if (user != null && user.equals(admin)) { 266 String pass = p.getProperty("password"); 267 String 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 280 private void open(Properties p) throws SQLException { 281 String name = p.getProperty("dbName"); 282 name = new File (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 293 private void close(Properties p) { 294 String name = p.getProperty("dbName"); 295 name = new File (name).getAbsolutePath(); 296 Connection conn = (Connection )connections.get(name); 297 if (conn != null) { 298 try { 299 conn.close(); 300 } catch (Throwable t) { 301 Debug.print(t); 302 } 303 connections.remove(name); 304 } 305 } 306 307 310 private void logout(Properties p) { 311 if (adminUser.getValue() != null) { 312 isAuthenticated = false; 313 closeConnections(); 314 } 315 } 316 317 324 public void handleRequest(HttpServletRequest req) 325 throws ServletException , SQLException 326 { 327 String action = req.getParameter("action"); 328 if (action != null) { 329 Properties 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 ("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 ("bad action"); 343 } 344 } else if (!isAuthenticated()) { 345 throw new ServletException ("not logged in"); 346 } 347 } 348 349 355 public Properties getRequestProperties(HttpServletRequest req) { 356 Properties p = new Properties (); 357 Enumeration e = req.getParameterNames(); 358 while (e.hasMoreElements()) { 359 String name = (String )e.nextElement(); 360 String val = req.getParameter(name); 361 try { 362 val = URLDecoder.decode(val); 363 } catch (Throwable ex) {} 364 p.put(name, val); 365 } 366 return p; 367 } 368 369 } 370 | Popular Tags |