001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: HsqlDBServiceImpl.java 9372 2006-08-07 09:34:09Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.db.hsqldb;
025:
026: import java.io.File;
027: import java.sql.Connection;
028: import java.sql.DriverManager;
029: import java.sql.ResultSet;
030: import java.sql.Statement;
031: import java.util.Iterator;
032: import java.util.List;
033:
034: import org.hsqldb.Server;
035: import org.hsqldb.ServerConstants;
036: import org.objectweb.jonas.common.JProp;
037: import org.objectweb.jonas.db.AbsDBServiceImpl;
038: import org.objectweb.jonas.db.AbsDBServiceImplMBean;
039: import org.objectweb.jonas.db.DBService;
040: import org.objectweb.jonas.db.User;
041: import org.objectweb.jonas.service.ServiceException;
042: import org.objectweb.util.monolog.api.BasicLevel;
043:
044: /**
045: * Embeds the HSQL database in JOnAS
046: * @author Florent Benoit
047: */
048: public class HsqlDBServiceImpl extends AbsDBServiceImpl implements
049: DBService, AbsDBServiceImplMBean {
050:
051: /**
052: * List of users
053: */
054: private List users = null;
055:
056: /**
057: * Name of database
058: */
059: private String databaseName = null;
060:
061: /**
062: * Default port number
063: */
064: private static final String DEFAULT_PORT = "9001";
065:
066: /**
067: * Sleep value
068: */
069: private static final int SLEEP_VALUE = 100;
070:
071: /**
072: * Max retry number
073: */
074: private static final int MAX_RETRY_NB = 20;
075:
076: /**
077: * port number used
078: */
079: private String portNumber = null;
080:
081: /**
082: * HsqlDB server
083: */
084: private Server server = null;
085:
086: /**
087: * Create a database with the specified arguments.
088: * @param users user/password (separated by a ":")
089: * @param databaseName name of the database
090: * @param portNumber port number of the database
091: */
092:
093: protected void initServer(List users, String databaseName,
094: String portNumber) {
095: this .users = users;
096: if (portNumber != null) {
097: this .portNumber = portNumber;
098: } else {
099: this .portNumber = DEFAULT_PORT;
100: }
101: this .databaseName = databaseName;
102: String jBase = JProp.getJonasBase();
103:
104: server = new Server();
105: // Remove all traces if level != DEBUG
106: if (!getLogger().isLoggable(BasicLevel.DEBUG)) {
107: server.setLogWriter(null);
108: server.setErrWriter(null);
109: server.setSilent(true);
110: server.setTrace(false);
111: server.setLogWriter(null);
112: } else {
113: // Enable all traces : verbose mode (as user needs DEBUG)
114: server.setSilent(false);
115: server.setTrace(true);
116: }
117:
118: String baseDir = jBase + File.separator + "work"
119: + File.separator + "hsqldb" + File.separator
120: + databaseName;
121: String pString = "";
122: if (portNumber != null) {
123: pString = ";port=" + portNumber;
124: }
125: String serverProps = "database.0=" + baseDir + ";dbname.0="
126: + databaseName + pString;
127: server.putPropertiesFromString(serverProps);
128:
129: }
130:
131: /**
132: * Start the service.
133: * @throws ServiceException if the startup failed.
134: */
135: protected void doStart() throws ServiceException {
136: super .doStart();
137: if (getLogger().isLoggable(BasicLevel.INFO)) {
138: getLogger().log(
139: BasicLevel.INFO,
140: "Starting " + server.getProductName() + " "
141: + server.getProductVersion() + " on port "
142: + portNumber);
143: }
144: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
145: getLogger().log(BasicLevel.DEBUG,
146: "serverState=" + server.getState());
147: }
148: server.start();
149:
150: // Wait the start
151: int retryNb = 0;
152: while (server.getState() != ServerConstants.SERVER_STATE_ONLINE) {
153: try {
154: Thread.sleep(SLEEP_VALUE);
155: } catch (InterruptedException ie) {
156: getLogger().log(BasicLevel.ERROR,
157: "Can't wait that the service is online", ie);
158: }
159: // Error if server state is "SHUTDOWN" during a long period
160: // Maybe strange but 'SHUTDOWN' state seems to be an intermediate state during startup
161: retryNb++;
162: if (server.getState() == ServerConstants.SERVER_STATE_SHUTDOWN
163: && retryNb >= MAX_RETRY_NB) {
164: Throwable t = server.getServerError();
165: throw new ServiceException("The server was shutdown :",
166: t);
167: }
168: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
169: getLogger().log(
170: BasicLevel.DEBUG,
171: "retry=" + retryNb + ", serverState="
172: + server.getState());
173: }
174: }
175:
176: getLogger().log(BasicLevel.INFO,
177: server.getProductName() + " started.");
178: Connection conn = null;
179: Statement st = null;
180: try {
181:
182: Class.forName("org.hsqldb.jdbcDriver");
183: conn = DriverManager.getConnection(
184: "jdbc:hsqldb:hsql://localhost:" + portNumber + "/"
185: + databaseName, "sa", "");
186: st = conn.createStatement();
187: } catch (Exception e) {
188: throw new ServiceException("Cannot access to HSQL", e);
189: }
190:
191: // Drop users before recreating it
192: User user = null;
193: String userName = null;
194: String password = null;
195: ResultSet rs = null;
196: for (Iterator it = users.iterator(); it.hasNext();) {
197: user = (User) it.next();
198: try {
199: password = user.getPassword();
200: userName = user.getUserName();
201: getLogger()
202: .log(
203: BasicLevel.INFO,
204: "Dropping and adding user '" + userName
205: + "' with password '"
206: + password + "'.");
207: try {
208: rs = st.executeQuery("DROP USER " + userName);
209: } catch (Exception ee) {
210: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
211: getLogger().log(
212: BasicLevel.DEBUG,
213: "User '" + userName
214: + "' doesn't exists", ee);
215: }
216: }
217: rs = st.executeQuery("Create USER " + userName
218: + " PASSWORD " + password + " ADMIN");
219: rs.close();
220: } catch (Exception e) {
221: getLogger().log(BasicLevel.ERROR,
222: "Error while creating/adding user", e);
223: }
224:
225: }
226:
227: try {
228: st.close();
229: } catch (Exception e) {
230: if (getLogger().isLoggable(BasicLevel.DEBUG)) {
231: getLogger().log(BasicLevel.DEBUG,
232: "Error while closing statement object", e);
233: }
234: }
235:
236: }
237:
238: /**
239: * Stop the service.
240: * @throws ServiceException if the stop failed.
241: */
242: protected void doStop() throws ServiceException {
243: super.doStop();
244:
245: server.shutdown();
246: }
247:
248: }
|