KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > cmp > init > InitDatabaseServlet


1 package example.cmp.init;
2
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5
6 import javax.naming.*;
7
8 import javax.sql.*;
9 import java.sql.*;
10
11 import java.util.*;
12 import java.io.*;
13
14 // to flush caches
15
// import com.caucho.ejb.admin.EJBAdmin;
16

17
18 /**
19  * Initializes the database for use with <code>examples</code> packages.
20  * <pre>
21  * You need to make sure that the init-parameter <code>jdbc-ref</code> points
22  * to a correctly configured database pool.
23  * <pre>
24  * The init-parameter <code>schema</code> points to the SQL schema file used
25  * by <code>InitDatabaseServlet</code>. If you experience
26  * <code>SQLException</code>s, you may have to adapt the syntax in the file to
27  * your database.
28  */

29 public class InitDatabaseServlet extends HttpServlet {
30   private int errorCount = 0;
31
32   public void service(HttpServletRequest req, HttpServletResponse res)
33     throws java.io.IOException
34   {
35     PrintWriter out = res.getWriter();
36
37     String schemaFile = req.getParameter("schema_file");
38     if (schemaFile == null)
39       schemaFile = "/WEB-INF/sql/default.sql";
40
41     String jdbcRef = req.getParameter("jdbc_ref");
42     if (jdbcRef == null)
43       jdbcRef = "java:comp/env/jdbc/test";
44
45     out.println("schema_file: " + schemaFile);
46     out.println("jdbc_ref: " + jdbcRef);
47     out.println();
48
49     Collection collection = getTextlinesFromResource(schemaFile);
50     Iterator it = collection.iterator();
51
52     DataSource source = null;
53     Connection con = null;
54     
55     try {
56       Context env = new InitialContext();
57       source = (DataSource) env.lookup(jdbcRef);
58     } catch(NamingException ne) {
59       out.println("Can't find resource " + jdbcRef + ". Check the configuration to");
60       out.println("make sure there's a resource-ref defining the database.");
61       out.println();
62       out.println(ne);
63
64       getServletContext().log("", ne);
65       return;
66     }
67
68     try {
69       con = source.getConnection();
70     } catch (SQLException e) {
71       out.println("Can't connect to the database at " + jdbcRef + ". Check the configuration to");
72       out.println("make sure the database is configured properly.");
73       out.println();
74       out.println(e);
75
76       getServletContext().log("", e);
77       return;
78     }
79
80     try {
81       Statement s = con.createStatement();
82
83       while (it.hasNext()) {
84         String sql = (String) it.next();
85         try {
86           s.executeQuery(sql);
87         } catch(SQLException sqle) {
88           out.println("'" + sql + "' was not accepted by the database.");
89           out.println(sqle);
90           out.println();
91         } finally {
92         }
93       }
94
95       s.close();
96     } catch(SQLException sqle) {
97       sqle.printStackTrace(out);
98     } finally {
99       if (con != null) {
100         try {
101           out.println("closing connection");
102           con.close();
103         } catch(Exception e) {
104           out.println("could not commit to the database" );
105           
106           getServletContext().log("", e);
107         }
108       }
109     }
110
111     /*
112     EJBAdmin admin = (EJBAdmin) getServletContext().getAttribute("caucho.ejb.admin");
113     if (admin != null)
114       admin.invalidateCache();
115     */

116
117     out.println("Initialization complete");
118   }
119
120   /**
121    * Produces a <code>Collection</Collection> of <code>String</code>s of
122    * lines from a textfile. The textfile must be a resource of the webapp.
123    */

124   private Collection getTextlinesFromResource(String filename)
125     throws IOException
126   {
127     ArrayList list = new ArrayList(50);
128
129     InputStream in = getServletContext().getResourceAsStream(filename);
130
131     if (in == null)
132       throw new FileNotFoundException(filename);
133     
134     BufferedReader br = new BufferedReader(new InputStreamReader(in));
135     
136     String line = "";
137     
138     String inputLine;
139     while ((inputLine = br.readLine()) != null) {
140       inputLine = inputLine.trim();
141
142       if (! inputLine.equals("")) {
143         if (inputLine.endsWith(";")) {
144           inputLine = inputLine.substring(0, inputLine.length() - 1);
145
146           line += inputLine;
147           list.add(line);
148           line = "";
149         }
150         else if (! inputLine.startsWith("#"))
151           line += inputLine;
152       }
153     }
154     
155     return list;
156   }
157 }
158
Popular Tags