1 9 package org.ozoneDB.odmg; 10 11 import java.util.*; 12 import org.odmg.*; 13 import org.ozoneDB.ExternalDatabase; 14 import org.ozoneDB.LocalDatabase; 15 import org.ozoneDB.RemoteDatabase; 16 import org.ozoneDB.OzoneProxy; 17 import org.ozoneDB.OzoneRemote; 18 import org.ozoneDB.OzoneCompatible; 19 20 21 28 public class OzoneODMGDatabase implements EnhDatabase { 29 30 private ExternalDatabase db; 31 32 private OzoneODMG factory; 33 34 private int accessMode = NOT_OPEN; 35 36 37 public OzoneODMGDatabase( OzoneODMG _factory ) { 38 factory = _factory; 39 } 40 41 42 public ExternalDatabase underlying() { 43 return db; 44 } 45 46 47 protected int mode() { 48 return accessMode; 49 } 50 51 52 56 public synchronized void open( String _url, int _accessMode ) throws ODMGException { 57 if (db != null) { 58 throw new DatabaseOpenException( "Database is already open." ); 59 } 60 61 switch (_accessMode) { 62 case OPEN_READ_ONLY: { 63 break; 64 } 65 case OPEN_READ_WRITE: { 66 break; 67 } 68 case OPEN_EXCLUSIVE: { 69 throw new ODMGRuntimeException( "OPEN_EXCLUSIVE not supported." ); 70 } 71 default: 72 throw new ODMGRuntimeException( "Illegal open mode." ); 73 } 74 75 try { 76 db = ExternalDatabase.openDatabase( _url ); 77 } catch (Exception e) { 78 throw new DatabaseNotFoundException( e.toString() ); 79 } 80 81 accessMode = _accessMode; 82 factory.databaseOpened( this ); 83 } 84 85 86 public void close() throws ODMGException { 87 if (db == null) { 88 throw new DatabaseClosedException( "Database not open." ); 89 } 90 91 try { 92 db.close(); 93 } catch (Exception e) { 94 throw new ODMGException( e.toString() ); 95 } finally { 96 db = null; 97 accessMode = NOT_OPEN; 98 factory.databaseClosed( this ); 99 } 100 } 101 102 103 protected void finalize() throws Throwable { 104 if (db != null) { 105 close(); 106 } 107 } 108 109 110 117 public void makePersistent( Object object ) { 118 if (OzoneODMGTransaction.current() == null) { 119 throw new TransactionNotInProgressException( "Thread has not joined a transaction." ); 120 } 121 122 if (db == null) { 123 throw new DatabaseClosedException( "Database not open." ); 124 } 125 126 if (object instanceof OzoneCompatible) { 129 throw new ODMGRuntimeException( object.getClass().getName() 130 + ": create a persistent instance via createPersistent." ); 131 } 132 133 if (object instanceof OzoneProxy) { 135 return; 136 } 137 } 138 139 140 151 public Object createPersistent( Class cl ) { 152 if (OzoneODMGTransaction.current() == null) { 153 throw new TransactionNotInProgressException( "Thread has not joined a transaction." ); 154 } 155 156 if (db == null) { 157 throw new DatabaseClosedException( "Database not open." ); 158 } 159 160 if (cl.isAssignableFrom( OzoneCompatible.class )) { 161 throw new ClassNotPersistenceCapableException( cl.getName() ); 162 } 163 164 try { 165 return db.createObject( cl.getName(), ExternalDatabase.Public, null ); 166 } catch (Exception e) { 167 throw new ODMGRuntimeException( e.toString() ); 168 } 169 } 170 171 172 public void deletePersistent( Object object ) { 173 if (OzoneODMGTransaction.current() == null) { 174 throw new TransactionNotInProgressException( "Thread has not joined a transaction." ); 175 } 176 177 if (db == null) { 178 throw new DatabaseClosedException( "Database not open." ); 179 } 180 181 if (!(object instanceof OzoneProxy)) { 182 throw new ObjectNotPersistentException( object.getClass().getName() ); 183 } 184 185 try { 186 db.deleteObject( (OzoneRemote)object ); 187 } catch (Exception e) { 188 throw new ODMGRuntimeException( e.toString() ); 189 } 190 } 191 192 193 public void bind( Object object, String name ) { 194 if (OzoneODMGTransaction.current() == null) { 195 throw new TransactionNotInProgressException( "Thread has not joined a transaction." ); 196 } 197 198 if (db == null) { 199 throw new DatabaseClosedException( "Database not open." ); 200 } 201 202 if (!(object instanceof OzoneProxy)) { 203 throw new ClassNotPersistenceCapableException( object.getClass().getName() ); 204 } 205 206 try { 207 db.nameObject( (OzoneRemote)object, name ); 208 } catch (Exception e) { 209 throw new ODMGRuntimeException( e.toString() ); 210 } 211 } 212 213 214 public void unbind( String name ) throws ObjectNameNotFoundException { 215 if (OzoneODMGTransaction.current() == null) { 216 throw new TransactionNotInProgressException( "Thread has not joined a transaction." ); 217 } 218 219 if (db == null) { 220 throw new DatabaseClosedException( "Database not open." ); 221 } 222 223 Object obj = lookup( name ); 224 try { 225 db.nameObject( (OzoneRemote)obj, null ); 226 } catch (Exception e) { 227 throw new ODMGRuntimeException( e.toString() ); 228 } 229 } 230 231 232 public Object lookup( String name ) throws ObjectNameNotFoundException { 233 if (OzoneODMGTransaction.current() == null) { 234 throw new TransactionNotInProgressException( "Thread has not joined a transaction." ); 235 } 236 237 if (db == null) { 238 throw new DatabaseClosedException( "Database not open." ); 239 } 240 241 Object result; 242 try { 243 result = db.objectForName( name ); 244 } catch (Exception e) { 245 throw new ODMGRuntimeException( e.toString() ); 246 } 247 248 if (result == null) { 249 throw new ObjectNameNotFoundException( name ); 250 } 251 252 return result; 253 } 254 255 256 public boolean containsObject( Object obj ) { 257 if (OzoneODMGTransaction.current() == null) { 258 throw new TransactionNotInProgressException( "Thread has not joined a transaction." ); 259 } 260 261 if (db == null) { 262 throw new DatabaseClosedException( "Database not open." ); 263 } 264 265 if (!(obj instanceof OzoneProxy)) { 266 throw new ClassNotPersistenceCapableException( obj.getClass().getName() ); 267 } 268 269 OzoneProxy proxy = (OzoneProxy)obj; 270 return proxy.link == db; 271 } 272 273 } 274 | Popular Tags |