1 9 package org.ozoneDB; 10 11 import org.ozoneDB.core.DbRemote.DbClient; 12 import org.ozoneDB.core.DbRemote.DbLocalClient; 13 import org.ozoneDB.core.Env; 14 import org.ozoneDB.tools.Install; 15 import org.ozoneDB.util.OzoneDebugLevel; 16 17 import java.io.PrintWriter ; 18 import java.io.StringWriter ; 19 import java.util.Hashtable ; 20 21 22 32 public final class LocalDatabase extends ExternalDatabase { 33 34 public Env env; 35 36 public String userName; 37 38 39 public LocalDatabase() { 40 } 41 42 43 53 public synchronized String create( String dirName ) throws Exception { 54 Setup setup = new Setup( env ); 56 return create( dirName, setup ); 57 } 58 59 60 69 public synchronized String create( String dirName, Setup setup ) throws Exception { 70 StringWriter writer = new StringWriter (); 71 PrintWriter logFile = new PrintWriter ( writer, true ); 72 Install.createDB( dirName, setup, logFile ); 73 return writer.toString(); 74 } 75 76 79 public void open( String dirName) throws Exception { 80 String username = System.getProperty( "user.name" ); 81 open( dirName, null, username, username ); 82 } 83 84 93 public void open(String dirName, int debugLevel) throws Exception { 94 String level = OzoneDebugLevel.toLevel(debugLevel, OzoneDebugLevel.INFO).toString(); 95 open(dirName, level); 96 } 97 98 105 public void open( String dirName, String debugLevel ) throws Exception { 106 String username = System.getProperty( "user.name" ); 107 open( dirName, debugLevel, username, username ); 108 } 109 110 119 public void open( String dirName, String debugLevel, String userName, String password ) throws Exception { 120 Hashtable props = new Hashtable (); 121 props.put( PROP_DIR, dirName ); 122 props.put( PROP_USER, userName ); 123 props.put( PROP_PASSWD, password ); 124 if (debugLevel != null) { 125 props.put( PROP_DEBUG, debugLevel ); 126 } 127 128 open( props ); 129 } 130 131 132 protected synchronized void open( Hashtable props ) throws Exception { 133 try { 134 super.open( props ); 135 136 String dirName = (String )props.get( PROP_DIR ); 137 userName = (String )props.get( PROP_USER ); 138 String debugLevel = ((String )props.get( PROP_DEBUG )); 140 141 env = new Env( dirName, debugLevel); 142 143 if (env.userManager.userForName( userName ) == null) { 146 env.userManager.newUser( userName, userName.hashCode() ); 147 } 148 env.startDeadlockRecognition(); 149 } 150 catch (Exception e) { 151 close(); 152 throw e; 153 } 154 } 155 156 157 public boolean isOpen() throws Exception { 158 return env != null; 159 } 160 161 162 public synchronized void close() throws Exception { 163 super.close(); 164 if (env != null) { 165 try { 166 env.shutdown(); 167 } 168 finally { 169 env = null; 170 } 171 } 172 } 173 174 175 protected DbClient newConnection() throws org.ozoneDB.core.UserManagerException,org.ozoneDB.PermissionDeniedException { 176 return new DbLocalClient(this,env,userName); 177 } 178 179 180 public String toString() { 181 return "LocalDatabase"; 182 } 183 184 185 protected void finalize() throws Exception { 186 close(); 187 } 188 189 193 public boolean exists(String dbDir) { 194 return Install.dbExists(dbDir); 195 } 196 197 } 198 | Popular Tags |