1 25 package org.objectweb.jonas.db.hsqldb; 26 27 import java.io.File ; 28 import java.sql.Connection ; 29 import java.sql.DriverManager ; 30 import java.sql.ResultSet ; 31 import java.sql.Statement ; 32 import java.util.Iterator ; 33 import java.util.List ; 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 49 public class HsqlDBServiceImpl extends AbsDBServiceImpl implements DBService, AbsDBServiceImplMBean { 50 51 54 private List users = null; 55 56 59 private String databaseName = null; 60 61 64 private static final String DEFAULT_PORT = "9001"; 65 66 69 private static final int SLEEP_VALUE = 100; 70 71 74 private static final int MAX_RETRY_NB = 20; 75 76 79 private String portNumber = null; 80 81 84 private Server server = null; 85 86 92 93 protected void initServer(List users, String databaseName, String 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 jBase = JProp.getJonasBase(); 102 103 server = new Server(); 104 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 baseDir = jBase + File.separator + "work" + File.separator + "hsqldb" + File.separator + databaseName; 114 String pString = ""; 115 if (portNumber != null) { 116 pString = ";port=" + portNumber; 117 } 118 String serverProps = "database.0=" + baseDir + ";dbname.0=" + databaseName + pString; 119 server.putPropertiesFromString(serverProps); 120 121 } 122 123 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 int retryNb = 0; 139 while (server.getState() != ServerConstants.SERVER_STATE_ONLINE) { 140 try { 141 Thread.sleep(SLEEP_VALUE); 142 } catch (InterruptedException ie) { 143 getLogger().log(BasicLevel.ERROR, "Can't wait that the service is online", ie); 144 } 145 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 conn = null; 158 Statement 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 e) { 166 throw new ServiceException("Cannot access to HSQL", e); 167 } 168 169 User user = null; 171 String userName = null; 172 String password = null; 173 ResultSet rs = null; 174 for (Iterator 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 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 e) { 191 getLogger().log(BasicLevel.ERROR, "Error while creating/adding user", e); 192 } 193 194 } 195 196 try { 197 st.close(); 198 } catch (Exception e) { 199 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 200 getLogger().log(BasicLevel.DEBUG, "Error while closing statement object", e); 201 } 202 } 203 204 } 205 206 210 protected void doStop() throws ServiceException { 211 super.doStop(); 212 213 server.stop(); 214 } 215 216 } | Popular Tags |