1 24 25 package com.mckoi.database.jdbcserver; 26 27 import com.mckoi.database.control.DBConfig; 29 import com.mckoi.database.control.DBSystem; 30 import com.mckoi.database.control.DBController; 31 import com.mckoi.database.control.DefaultDBConfig; 32 import com.mckoi.database.DatabaseSystem; 33 import com.mckoi.database.Database; 34 import com.mckoi.database.DatabaseException; 35 import com.mckoi.database.jdbc.LocalBootable; 36 import com.mckoi.database.jdbc.DatabaseCallBack; 37 import com.mckoi.database.jdbc.DatabaseInterface; 38 import com.mckoi.database.jdbc.QueryResponse; 39 import com.mckoi.database.jdbc.ResultPart; 40 import com.mckoi.database.jdbc.SQLQuery; 41 42 import java.io.File ; 43 import java.sql.SQLException ; 44 45 52 53 public class DefaultLocalBootable implements LocalBootable { 54 55 58 private boolean booted = false; 59 60 63 private boolean active = false; 64 65 68 private DBSystem dbsys; 69 70 74 private int connect_id = 0; 75 76 79 private int open_connections = 0; 80 81 84 private Object connection_lock = new Object (); 85 86 92 public DatabaseInterface create(String username, String password, 93 DBConfig config) throws SQLException { 94 95 if (username.equals("") || password.equals("")) { 96 throw new SQLException ("Username and Password must both be set."); 97 } 98 99 if (!booted) { 100 String host_string = "Local/Create/"; 103 104 DBController controller = DBController.getDefault(); 106 dbsys = controller.createDatabase(config, username, password); 107 DatabaseInterface db_interface = 108 new LocalJDBCDatabaseInterface(dbsys.getDatabase(), host_string); 109 110 booted = true; 111 ++open_connections; 112 active = true; 113 114 return db_interface; 115 } 116 117 throw new SQLException ("Database is already created."); 118 119 } 120 121 127 public DatabaseInterface boot(DBConfig config) throws SQLException { 128 if (!booted) { 129 String host_string = "Local/Boot/"; 132 133 DBController controller = DBController.getDefault(); 135 dbsys = controller.startDatabase(config); 136 DatabaseInterface db_interface = 137 new LocalJDBCDatabaseInterface(dbsys.getDatabase(), host_string); 138 139 booted = true; 140 ++open_connections; 141 active = true; 142 143 return db_interface; 144 145 } 146 else { 147 throw new SQLException ("Database was booted more than once."); 148 } 149 } 150 151 157 public boolean checkExists(DBConfig config) throws SQLException { 158 if (!booted) { 159 DBController controller = DBController.getDefault(); 160 return controller.databaseExists(config); 161 } 162 else { 163 throw new SQLException ("The database is already booted."); 164 } 165 } 166 167 171 public boolean isBooted() throws SQLException { 172 return booted; 173 } 174 175 180 public DatabaseInterface connectToJVM() throws SQLException { 181 if (booted) { 182 183 String host_string = "Local/Connection/" + connect_id; 186 187 DatabaseInterface db_interface = 189 new LocalJDBCDatabaseInterface(dbsys.getDatabase(), host_string); 190 191 ++connect_id; 192 ++open_connections; 193 active = true; 194 195 return db_interface; 196 197 } 198 else { 199 throw new SQLException ("The database is not started."); 200 } 201 202 } 203 204 206 210 private class LocalJDBCDatabaseInterface extends JDBCDatabaseInterface { 211 212 boolean closed = false; 213 214 public LocalJDBCDatabaseInterface(Database database, String host_string) { 215 super(database, host_string); 216 } 217 218 220 public void dispose() throws SQLException { 221 if (!closed) { 222 super.dispose(); 223 224 --open_connections; 225 226 if (open_connections <= 0) { 228 dbsys.close(); 231 active = false; 232 booted = false; 233 dbsys = null; 234 } 235 closed = true; 236 } 237 238 } 239 240 242 public void finalize() throws Throwable { 243 super.finalize(); 244 dispose(); 245 } 246 247 } 248 249 } 250 | Popular Tags |