1 25 26 package org.objectweb.easybeans.component.hsqldb; 27 28 import java.io.File ; 29 import java.sql.Connection ; 30 import java.sql.DriverManager ; 31 import java.sql.ResultSet ; 32 import java.sql.SQLException ; 33 import java.sql.Statement ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 37 import org.hsqldb.DatabaseManager; 38 import org.hsqldb.Server; 39 import org.hsqldb.ServerConstants; 40 import org.objectweb.easybeans.component.api.EZBComponentException; 41 import org.objectweb.easybeans.component.itf.EmbeddedDBComponent; 42 import org.objectweb.easybeans.log.JLog; 43 import org.objectweb.easybeans.log.JLogFactory; 44 45 49 public class HSQLDBComponent implements EmbeddedDBComponent { 50 51 54 private static JLog logger = JLogFactory.getLog(HSQLDBComponent.class); 55 56 59 private List <User> users = null; 60 61 64 private String databaseName = null; 65 66 69 private static final String DEFAULT_PORT = "9001"; 70 71 74 private static final int SLEEP_VALUE = 100; 75 76 79 private static final int MAX_RETRY_NB = 20; 80 81 84 private String portNumber = null; 85 86 89 private Server server = null; 90 91 95 public HSQLDBComponent() { 96 this.portNumber = DEFAULT_PORT; 97 } 98 99 103 @SuppressWarnings ("unchecked") 104 public void init() throws EZBComponentException { 105 106 server = new Server(); 107 if (!logger.isDebugEnabled()) { 109 server.setLogWriter(null); 110 server.setErrWriter(null); 111 server.setSilent(true); 112 server.setTrace(false); 113 server.setLogWriter(null); 114 } else { 115 server.setSilent(false); 117 server.setTrace(true); 118 } 119 120 String baseDir = System.getProperty("java.io.tmpdir") + File.separator + "easybeans" + File.separator + "hsqldb" 121 + File.separator + databaseName; 122 String pString = ""; 123 if (portNumber != null) { 124 pString = ";port=" + portNumber; 125 } 126 String serverProps = "database.0=" + baseDir + ";dbname.0=" + databaseName + pString; 127 logger.debug("Server properties = {0}", serverProps); 128 server.putPropertiesFromString(serverProps); 129 130 try { 131 Class.forName("org.hsqldb.jdbcDriver"); 132 } catch (ClassNotFoundException e) { 133 throw new EZBComponentException("Cannot access to HSQL Driver 'org.hsqldb.jdbcDriver'.", e); 134 } 135 136 } 137 138 142 @SuppressWarnings ("boxing") 143 public void start() throws EZBComponentException { 144 145 logger.info("Starting ''{0}'' ''{1}'' on port ''{2}''", server.getProductName(), server.getProductVersion(), portNumber); 146 147 server.start(); 148 149 int retryNb = 0; 151 while (server.getState() != ServerConstants.SERVER_STATE_ONLINE) { 152 try { 153 Thread.sleep(SLEEP_VALUE); 154 } catch (InterruptedException ie) { 155 logger.error("Cannot wait that the service is online", ie); 156 } 157 retryNb++; 161 if (server.getState() == ServerConstants.SERVER_STATE_SHUTDOWN && retryNb >= MAX_RETRY_NB) { 162 Throwable t = server.getServerError(); 163 throw new EZBComponentException("Cannot start the server. The server has not started and is shutdown.", t); 164 } 165 logger.debug("retry= {0}, serverState= {1}", retryNb, server.getState()); 166 } 167 168 String connURL = "jdbc:hsqldb:hsql://localhost:" + portNumber + "/" + databaseName; 169 logger.info("{0} started with URL {1}", server.getProductName(), connURL); 170 171 Connection conn = null; 172 Statement st = null; 173 174 try { 175 conn = DriverManager.getConnection(connURL, "sa", ""); 176 st = conn.createStatement(); 177 } catch (SQLException e) { 178 throw new EZBComponentException("Cannot access to HSQL", e); 179 } 180 181 User user = null; 183 String userName = null; 184 String password = null; 185 ResultSet rs = null; 186 for (Iterator it = users.iterator(); it.hasNext();) { 187 user = (User) it.next(); 188 try { 189 password = user.getPassword(); 190 userName = user.getUserName(); 191 logger.debug("Dropping and adding user {0} with password {1}.", userName, password); 192 try { 193 rs = st.executeQuery("DROP USER " + userName); 194 } catch (SQLException e) { 195 logger.debug("User {0} doesn't exists", userName, e); 196 } 197 rs = st.executeQuery("Create USER " + userName + " PASSWORD " + password + " ADMIN"); 198 rs.close(); 199 } catch (SQLException e) { 200 logger.error("Error while creating/adding user", e); 201 } 202 203 } 204 205 try { 206 st.close(); 207 } catch (SQLException e) { 208 logger.error("Error while closing statement object", e); 209 } 210 211 } 212 213 217 public List <User> getUsers() { 218 return this.users; 219 } 220 221 225 public void setUsers(final List <User> users) { 226 this.users = users; 227 } 228 229 234 public void stop() throws EZBComponentException { 235 server.shutdown(); 236 DatabaseManager.getTimer().shutDown(); 237 } 238 239 243 public void setPortNumber(final String portNumber) { 244 this.portNumber = portNumber; 245 } 246 247 251 public void setDatabaseName(final String databaseName) { 252 this.databaseName = databaseName; 253 } 254 255 } 256 | Popular Tags |