1 4 package org.oddjob.hsql; 5 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Map ; 9 10 import org.hsqldb.Server; 11 import org.hsqldb.ServerConstants; 12 import org.hsqldb.persist.HsqlProperties; 13 14 25 public class HsqldbJob { 26 27 class ServerWithStateNotify extends Server { 28 synchronized protected void setState(int state) { 29 super.setState(state); 30 notifyAll(); 31 } 32 } 33 34 39 private String name; 40 41 49 private Map databases = new HashMap (); 50 51 52 58 private HsqlProperties properties; 59 60 61 private Server server; 62 63 68 public String getName() { 69 return name; 70 } 71 72 77 public void setName(String name) { 78 this.name = name; 79 } 80 81 87 public String getDatabase(String name) { 88 return (String ) databases.get(name); 89 } 90 91 96 public void setDatabase(String name, String path) { 97 if (name == null) { 98 throw new NullPointerException ("Database name can not be null"); 99 } 100 this.databases.put(name, path); 101 } 102 103 109 public void setProperty(String name, String value) { 110 if (properties == null) { 111 properties = new HsqlProperties(); 112 } 113 properties.setProperty(name, value); 114 } 115 116 121 public String getProperty(String name) { 122 if (properties == null) { 123 return null; 124 } 125 return properties.getProperty(name); 126 } 127 128 132 public void start() { 133 134 server = new ServerWithStateNotify(); 135 int dbCount = 0; 136 for (Iterator it = databases.entrySet().iterator(); it.hasNext(); ++dbCount) { 137 Map.Entry entry = (Map.Entry ) it.next(); 138 server.setDatabaseName(dbCount, (String ) entry.getKey()); 139 server.setDatabasePath(dbCount, (String ) entry.getValue()); 140 } 141 142 if (properties != null) { 143 server.setProperties(properties); 144 } 145 server.start(); 146 synchronized (server) { 147 while (server.getState() != ServerConstants.SERVER_STATE_ONLINE) { 148 try { 149 server.wait(); 150 } catch (InterruptedException e) { 151 break; 152 } 153 } 154 } 155 } 156 157 161 public void stop() { 162 server.stop(); 163 synchronized (server) { 164 while (server.getState() != ServerConstants.SERVER_STATE_SHUTDOWN) { 165 try { 166 server.wait(); 167 } catch (InterruptedException e) { 168 break; 169 } 170 } 171 } 172 server= null; 173 properties = null; 174 } 175 176 180 public String toString() { 181 if (name == null) { 182 return "Hsqldb server"; 183 } 184 return name; 185 } 186 } 187 | Popular Tags |