1 28 29 package org.apache.catalina.session; 30 31 32 public class SessionLock { 33 34 private static final String BACKGROUND_LOCK = "background_lock"; 35 private static final String FOREGROUND_LOCK = "foreground_lock"; 36 37 38 public SessionLock() { 39 } 40 41 45 public String getLockType() { 46 return _lockType; 47 } 48 49 54 public void setLockType(String lockType) { 55 _lockType = lockType; 56 } 57 58 62 public int getForegroundRefCount() { 63 return _foregroundRefCount; 64 } 65 66 71 public void setForegroundRefCount(int foregroundRefCount) { 72 _foregroundRefCount = foregroundRefCount; 73 } 74 75 79 public void incrementForegroundRefCount() { 80 _foregroundRefCount++; 81 } 82 83 87 public void decrementForegroundRefCount() { 88 _foregroundRefCount--; 89 } 90 91 95 public boolean isBackgroundLocked() { 96 if(_lockType == null) { 97 return false; 98 } 99 return (_lockType.equals(BACKGROUND_LOCK)); 100 } 101 102 106 public boolean isForegroundLocked() { 107 if(_lockType == null) { 108 return false; 109 } 110 return (_lockType.equals(FOREGROUND_LOCK)); 111 } 112 113 117 public boolean isLocked() { 118 return (_lockType != null); 119 } 120 121 129 public void unlock() { 130 if(!isLocked()) 131 return; 132 if(isBackgroundLocked()) { 133 this.setLockType(null); 134 this.setForegroundRefCount(0); 135 return; 136 } 137 if(isForegroundLocked()) { 138 decrementForegroundRefCount(); 139 if(_foregroundRefCount == 0) { 140 this.setLockType(null); 141 } 142 } 143 } 144 145 152 public void unlockForeground() { 153 if(!isLocked()) 156 return; 157 if(isForegroundLocked()) { 158 decrementForegroundRefCount(); 159 if(_foregroundRefCount == 0) { 160 this.setLockType(null); 161 } 162 } 163 } 164 165 170 public void unlockForegroundCompletely() { 171 if(!isLocked()) 174 return; 175 if(isForegroundLocked()) { 176 this.setForegroundRefCount(0); 177 this.setLockType(null); 178 } 179 } 180 181 186 public void unlockBackground() { 187 if(!isLocked()) 190 return; 191 if(isBackgroundLocked()) { 192 this.setLockType(null); 193 this.setForegroundRefCount(0); 194 return; 195 } 196 } 197 198 207 public synchronized boolean lockForeground() { 208 if(isBackgroundLocked()) { 209 return false; 210 } 211 if(isForegroundLocked()) { 212 incrementForegroundRefCount(); 213 } else { 214 setForegroundRefCount(1); 215 } 216 setLockType(FOREGROUND_LOCK); 217 return true; 218 } 219 220 227 public synchronized boolean lockBackground() { 228 if(isForegroundLocked()) { 229 return false; 230 } 231 setLockType(BACKGROUND_LOCK); 232 setForegroundRefCount(0); 233 return true; 234 } 235 236 239 public String toString() { 240 StringBuffer sb = new StringBuffer (50); 241 sb.append("_lockType= " + _lockType); 242 sb.append("\n" + "foregroundRefCount= " + _foregroundRefCount); 243 return sb.toString(); 244 } 245 246 private String _lockType = null; 247 private int _foregroundRefCount = 0; 248 249 } 250 | Popular Tags |