1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.ibatis.jpetstore.persistence;
18
19 import java.io.File;
20 import java.io.FileWriter;
21 import java.io.IOException;
22
23 import javax.servlet.ServletContext;
24 import javax.servlet.ServletContextEvent;
25 import javax.servlet.ServletContextListener;
26
27 public class LocalHsqldbConfigurator implements ServletContextListener
28 {
29 public void contextInitialized(ServletContextEvent sce)
30 {
31 ServletContext context = sce.getServletContext();
32 try
33 {
34 File propertiesFile = new File(context.getRealPath("WEB-INF/classes/properties/database.properties"));
35 if (propertiesFile.exists())
36 {
37 context.log("LocalHsqldbConfigurator: database.properties already exists");
38 return;
39 }
40 String dbPath = context.getRealPath("/WEB-INF/db/jpetstore.script");
41 FileWriter output = new FileWriter(propertiesFile);
42 output.write("driver=org.hsqldb.jdbcDriver\n");
43 output.write("url=jdbc:hsqldb:"+dbPath.substring(0,dbPath.length()-(".script".length())).replace('//','/')+"\n");
44 output.write("username=sa\n");
45 output.write("password=\n");
46 output.close();
47 context.log("LocalHsqldbConfigurator: database.properties created");
48 }
49 catch (IOException e)
50 {
51 context.log("LocalHsqldbConfigurator: failed to create database.properties",e);
52 }
53 }
54
55 public void contextDestroyed(ServletContextEvent sce)
56 {
57 }
58 }