KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > db > hsqldb > HsqlDBServiceImpl


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: HsqlDBServiceImpl.java,v 1.7 2005/04/28 08:43:24 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.db.hsqldb;
26
27 import java.io.File JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.DriverManager JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.Statement JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.hsqldb.Server;
36 import org.hsqldb.ServerConstants;
37 import org.objectweb.jonas.common.JProp;
38 import org.objectweb.jonas.db.AbsDBServiceImpl;
39 import org.objectweb.jonas.db.AbsDBServiceImplMBean;
40 import org.objectweb.jonas.db.DBService;
41 import org.objectweb.jonas.db.User;
42 import org.objectweb.jonas.service.ServiceException;
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45 /**
46  * Embeds the HSQL database in JOnAS
47  * @author Florent Benoit
48  */

49 public class HsqlDBServiceImpl extends AbsDBServiceImpl implements DBService, AbsDBServiceImplMBean {
50
51     /**
52      * List of users
53      */

54     private List JavaDoc users = null;
55
56     /**
57      * Name of database
58      */

59     private String JavaDoc databaseName = null;
60
61     /**
62      * Default port number
63      */

64     private static final String JavaDoc DEFAULT_PORT = "9001";
65
66     /**
67      * Sleep value
68      */

69     private static final int SLEEP_VALUE = 100;
70
71     /**
72      * Max retry number
73      */

74     private static final int MAX_RETRY_NB = 20;
75
76     /**
77      * port number used
78      */

79     private String JavaDoc portNumber = null;
80
81     /**
82      * HsqlDB server
83      */

84     private Server server = null;
85
86     /**
87      * Create a database with the specified arguments.
88      * @param users user/password (separated by a ":")
89      * @param databaseName name of the database
90      * @param portNumber port number of the database
91      */

92
93     protected void initServer(List JavaDoc users, String JavaDoc databaseName, String JavaDoc portNumber) {
94         this.users = users;
95         if (portNumber != null) {
96             this.portNumber = portNumber;
97         } else {
98             this.portNumber = DEFAULT_PORT;
99         }
100         this.databaseName = databaseName;
101         String JavaDoc jBase = JProp.getJonasBase();
102
103         server = new Server();
104         // Remove all traces if level != DEBUG
105
if (!getLogger().isLoggable(BasicLevel.DEBUG)) {
106             server.setLogWriter(null);
107             server.setErrWriter(null);
108             server.setSilent(true);
109             server.setTrace(false);
110             server.setLogWriter(null);
111         }
112
113         String JavaDoc baseDir = jBase + File.separator + "work" + File.separator + "hsqldb" + File.separator + databaseName;
114         String JavaDoc pString = "";
115         if (portNumber != null) {
116             pString = ";port=" + portNumber;
117         }
118         String JavaDoc serverProps = "database.0=" + baseDir + ";dbname.0=" + databaseName + pString;
119         server.putPropertiesFromString(serverProps);
120
121     }
122
123     /**
124      * Start the service.
125      * @throws ServiceException if the startup failed.
126      */

127     protected void doStart() throws ServiceException {
128         super.doStart();
129         if (getLogger().isLoggable(BasicLevel.INFO)) {
130             getLogger().log(BasicLevel.INFO, "Starting " + server.getProductName() + " " + server.getProductVersion() + " on port " + portNumber);
131         }
132         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
133             getLogger().log(BasicLevel.DEBUG, "serverState=" + server.getState());
134         }
135         server.start();
136
137         // Wait the start
138
int retryNb = 0;
139         while (server.getState() != ServerConstants.SERVER_STATE_ONLINE) {
140             try {
141                 Thread.sleep(SLEEP_VALUE);
142             } catch (InterruptedException JavaDoc ie) {
143                 getLogger().log(BasicLevel.ERROR, "Can't wait that the service is online", ie);
144             }
145             // Error if server state is "SHUTDOWN" during a long period
146
// Maybe strange but 'SHUTDOWN' state seems to be an intermediate state during startup
147
retryNb++;
148             if (server.getState() == ServerConstants.SERVER_STATE_SHUTDOWN && retryNb >= MAX_RETRY_NB) {
149                     throw new ServiceException("The server was shutdown. Enable the traces in trace.properties file to see why the service has failed.");
150             }
151             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
152                 getLogger().log(BasicLevel.DEBUG, "retry=" + retryNb + ", serverState=" + server.getState());
153             }
154         }
155
156         getLogger().log(BasicLevel.INFO, server.getProductName() + " started.");
157         Connection JavaDoc conn = null;
158         Statement JavaDoc st = null;
159         try {
160
161             Class.forName("org.hsqldb.jdbcDriver");
162             conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost:" + portNumber + "/" + databaseName, "sa",
163                     "");
164             st = conn.createStatement();
165         } catch (Exception JavaDoc e) {
166             throw new ServiceException("Cannot access to HSQL", e);
167         }
168
169         // Drop users before recreating it
170
User user = null;
171         String JavaDoc userName = null;
172         String JavaDoc password = null;
173         ResultSet JavaDoc rs = null;
174         for (Iterator JavaDoc it = users.iterator(); it.hasNext();) {
175             user = (User) it.next();
176             try {
177                 password = user.getPassword();
178                 userName = user.getUserName();
179                 getLogger().log(BasicLevel.INFO,
180                         "Dropping and adding user '" + userName + "' with password '" + password + "'.");
181                 try {
182                     rs = st.executeQuery("DROP USER " + userName);
183                 } catch (Exception JavaDoc ee) {
184                     if (getLogger().isLoggable(BasicLevel.DEBUG)) {
185                         getLogger().log(BasicLevel.DEBUG, "User '" + userName + "' doesn't exists", ee);
186                     }
187                 }
188                 rs = st.executeQuery("Create USER " + userName + " PASSWORD " + password + " ADMIN");
189                 rs.close();
190             } catch (Exception JavaDoc e) {
191                 getLogger().log(BasicLevel.ERROR, "Error while creating/adding user", e);
192             }
193
194         }
195
196         try {
197             st.close();
198         } catch (Exception JavaDoc e) {
199             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
200                 getLogger().log(BasicLevel.DEBUG, "Error while closing statement object", e);
201             }
202         }
203
204     }
205
206     /**
207      * Stop the service.
208      * @throws ServiceException if the stop failed.
209      */

210     protected void doStop() throws ServiceException {
211         super.doStop();
212
213         server.stop();
214     }
215
216 }
Popular Tags