1 65 66 67 package org.hsqldb; 68 69 import java.io.DataInputStream ; 70 import java.io.IOException ; 71 import java.io.PrintWriter ; 72 73 import javax.servlet.ServletConfig ; 74 import javax.servlet.ServletException ; 75 import javax.servlet.ServletOutputStream ; 76 import javax.servlet.http.HttpServletRequest ; 77 import javax.servlet.http.HttpServletResponse ; 78 79 import org.hsqldb.persist.HsqlProperties; 80 import org.hsqldb.rowio.RowInputBinary; 81 import org.hsqldb.rowio.RowOutputBinary; 82 83 88 128 public class Servlet extends javax.servlet.http.HttpServlet { 129 130 private static final int BUFFER_SIZE = 256; 131 private String dbType; 132 private String dbPath; 133 private String errorStr; 134 private RowOutputBinary rowOut; 135 private RowInputBinary rowIn; 136 private int iQueries; 137 138 144 public void init(ServletConfig config) { 145 146 try { 147 super.init(config); 148 149 rowOut = new RowOutputBinary(BUFFER_SIZE); 150 rowIn = new RowInputBinary(rowOut); 151 } catch (ServletException e) { 152 log(e.toString()); 153 } 154 155 String dbStr = getInitParameter("hsqldb.server.database"); 156 157 if (dbStr == null) { 158 dbStr = "."; 159 } 160 161 String useWebInfStr = 163 getInitParameter("hsqldb.server.use_web-inf_path"); 164 165 if (!dbStr.equals(".") && "true".equalsIgnoreCase(useWebInfStr)) { 166 dbStr = getServletContext().getRealPath("/") + "WEB-INF" + dbStr; 167 } 168 169 HsqlProperties dbURL = DatabaseURL.parseURL(dbStr, false); 171 172 log("Database filename = " + dbStr); 173 174 if (dbURL == null) { 175 errorStr = "Bad Database name"; 176 } else { 177 dbPath = dbURL.getProperty("database"); 178 dbType = dbURL.getProperty("connection_type"); 179 180 try { 181 182 DatabaseManager.getDatabase(dbType, dbPath, dbURL); 184 } catch (HsqlException e) { 185 errorStr = e.getMessage(); 186 } 187 } 188 189 log(errorStr); 190 log("Initialization completed."); 191 } 192 193 private static long lModified = 0; 194 195 203 protected long getLastModified(HttpServletRequest req) { 204 205 return lModified++; 208 } 209 210 220 public void doGet(HttpServletRequest request, 221 HttpServletResponse response) 222 throws IOException , ServletException { 223 224 String query = request.getQueryString(); 225 226 if ((query == null) || (query.length() == 0)) { 227 response.setContentType("text/html"); 228 229 response.setHeader("Pragma", "no-cache"); 232 233 PrintWriter out = response.getWriter(); 234 235 out.println( 236 "<html><head><title>HSQL Database Engine Servlet</title>"); 237 out.println("</head><body><h1>HSQL Database Engine Servlet</h1>"); 238 out.println("The servlet is running.<p>"); 239 240 if (errorStr == null) { 241 out.println("The database is also running.<p>"); 242 out.println("Database name: " + dbType + dbPath + "<p>"); 243 out.println("Queries processed: " + iQueries + "<p>"); 244 } else { 245 out.println("<h2>The database is not running!</h2>"); 246 out.println("The error message is:<p>"); 247 out.println(errorStr); 248 } 249 250 out.println("</body></html>"); 251 } 252 } 253 254 264 public void doPost(HttpServletRequest request, 265 HttpServletResponse response) 266 throws IOException , ServletException { 267 268 synchronized (this) { 269 DataInputStream inStream = null; 270 ServletOutputStream outStream = null; 271 272 try { 273 274 inStream = new DataInputStream (request.getInputStream()); 278 279 Result resultIn = Result.read(rowIn, inStream); 280 Result resultOut; 281 282 if (resultIn.mode == ResultConstants.SQLCONNECT) { 283 try { 284 Session session = DatabaseManager.newSession(dbType, 285 dbPath, resultIn.getMainString(), 286 resultIn.getSubString(), null); 287 288 resultOut = new Result(ResultConstants.UPDATECOUNT); 289 resultOut.sessionID = session.getId(); 290 } catch (HsqlException e) { 291 resultOut = new Result(e, null); 292 } 293 } else { 294 int dbId = resultIn.databaseID; 295 int sessionId = resultIn.sessionID; 296 Session session = DatabaseManager.getSession(dbId, 297 sessionId); 298 299 resultOut = session.execute(resultIn); 300 } 301 302 rowOut.reset(); 303 resultOut.write(rowOut); 304 305 response.setContentType("application/octet-stream"); 307 response.setContentLength(rowOut.size()); 308 309 outStream = response.getOutputStream(); 311 312 outStream.write(rowOut.getOutputStream().getBuffer(), 0, 313 rowOut.getOutputStream().size()); 314 315 iQueries++; 316 } catch (HsqlException e) {} 317 finally { 318 if (outStream != null) { 319 outStream.close(); 320 } 321 322 if (inStream != null) { 323 inStream.close(); 324 } 325 } 326 } 327 328 } 330 } 331 | Popular Tags |