00001 package com.quadcap.app.qed;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00049
00050
00051
import java.net.URLDecoder;
00052
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
00071
00072
00073
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
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
00099
00100
00101
00102
00103
00104
00105 public boolean isAuthenticated() {
return isAuthenticated; }
00106
00107
00108
00109
00110 public void setPage(String page) {
this.page = page; }
00111
00112
00113
00114
00115 public String
getPage() {
return page; }
00116
00117
00118
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
00142
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
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
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
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
00206
00207 public Connection getConnection(String name)
throws SQLException {
00208
return getConnection(name,
false,
false);
00209 }
00210
00211
00212
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
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
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
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
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
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
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
00309
00310 private void logout(Properties p) {
00311
if (
adminUser.getValue() != null) {
00312 isAuthenticated =
false;
00313
closeConnections();
00314 }
00315 }
00316
00317
00318
00319
00320
00321
00322
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
00351
00352
00353
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 }