|                                                                                                              1
 24
 25  package com.mckoi.database;
 26
 27  import com.mckoi.debug.*;
 28
 29
 36
 37  public final class LockHandle {
 38
 39
 42    private Lock[] lock_list;
 43
 44
 47    private int lock_index;
 48
 49
 52    private boolean unlocked;
 53
 54
 57    private final DebugLogger debug;
 58
 59
 63    LockHandle(int lock_count, DebugLogger logger) {
 64      this.debug = logger;
 65      lock_list = new Lock[lock_count];
 66      lock_index = 0;
 67      unlocked = false;
 68    }
 69
 70
 75    void addLock(Lock lock) {
 76      lock_list[lock_index] = lock;
 77      ++lock_index;
 78    }
 79
 80
 86    void unlockAll() {
 87      if (!unlocked) {
 88        for (int i = lock_list.length - 1; i >= 0; --i) {
 89          lock_list[i].release();
 90        }
 91        unlocked = true;
 92      }
 93    }
 94
 95
 105   public void checkAccess(DataTable table, int access_type) {
 106     for (int i = lock_list.length - 1; i >= 0; --i) {
 107       Lock l = lock_list[i];
 108       if (l.getTable() == table) {
 109         l.checkAccess(access_type);
 110         return;
 111       }
 112     }
 113     throw new RuntimeException
  ( 114         "The given DataTable was not found in the lock list for this handle");
 115   }
 116
 117
 123   public void finalize() {
 124     if (!unlocked) {
 125       unlockAll();
 126       debug.write(Lvl.ERROR, this, "Finalize released a table lock - " +
 127         "This indicates that there is a serious error.  Locks should " +
 128         "only have a very short life span.  The 'unlockAll' method should " +
 129         "have been called before finalization.  " + toString());
 130     }
 131   }
 132
 133   public String
  toString() { 134     StringBuffer
  str = new StringBuffer  ("LockHandle: "); 135     for (int i = 0; i < lock_list.length; ++i) {
 136       str.append(lock_list[i].toString());
 137     }
 138     return new String
  (str); 139   }
 140
 141 }
 142
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |