View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }