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