1 11 package org.eclipse.core.internal.resources; 12 13 import org.eclipse.core.internal.utils.Messages; 14 import org.eclipse.core.internal.utils.Policy; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.IResourceStatus; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.jobs.*; 20 21 32 public class WorkManager implements IManager { 33 45 class NotifyRule implements ISchedulingRule { 46 public boolean contains(ISchedulingRule rule) { 47 return (rule instanceof IResource) || rule.getClass().equals(NotifyRule.class); 48 } 49 50 public boolean isConflicting(ISchedulingRule rule) { 51 return contains(rule); 52 } 53 } 54 55 59 private final ThreadLocal checkInFailed = new ThreadLocal (); 60 63 private boolean hasBuildChanges = false; 64 private IJobManager jobManager; 65 69 private final ILock lock; 70 71 74 private int nestedOperations = 0; 75 76 private NotifyRule notifyRule = new NotifyRule(); 77 78 private boolean operationCanceled = false; 79 80 83 private int preparedOperations = 0; 84 private Workspace workspace; 85 86 public WorkManager(Workspace workspace) { 87 this.workspace = workspace; 88 this.jobManager = Job.getJobManager(); 89 this.lock = jobManager.newLock(); 90 } 91 92 99 public int beginUnprotected() { 100 int depth = lock.getDepth(); 101 for (int i = 0; i < depth; i++) 102 lock.release(); 103 return depth; 104 } 105 106 110 public void checkIn(ISchedulingRule rule, IProgressMonitor monitor) throws CoreException { 111 boolean success = false; 112 try { 113 if (workspace.isTreeLocked()) { 114 String msg = Messages.resources_cannotModify; 115 throw new ResourceException(IResourceStatus.WORKSPACE_LOCKED, null, msg, null); 116 } 117 jobManager.beginRule(rule, monitor); 118 lock.acquire(); 119 incrementPreparedOperations(); 120 success = true; 121 } finally { 122 if (!success) 124 checkInFailed.set(Boolean.TRUE); 125 } 126 } 127 128 136 public boolean checkInFailed(ISchedulingRule rule) { 137 if (checkInFailed.get() != null) { 138 checkInFailed.set(null); 140 if (!workspace.isTreeLocked()) 142 jobManager.endRule(rule); 143 return true; 144 } 145 return false; 146 } 147 148 151 public synchronized void checkOut(ISchedulingRule rule) { 152 decrementPreparedOperations(); 153 rebalanceNestedOperations(); 154 if (preparedOperations == 0) 156 operationCanceled = hasBuildChanges = false; 157 try { 158 lock.release(); 159 } finally { 160 jobManager.endRule(rule); 162 } 163 } 164 165 170 private void decrementPreparedOperations() { 171 preparedOperations--; 172 } 173 174 179 public void endUnprotected(int depth) { 180 for (int i = 0; i < depth; i++) 181 lock.acquire(); 182 } 183 184 187 ILock getLock() { 188 return lock; 189 } 190 191 194 public ISchedulingRule getNotifyRule() { 195 return notifyRule; 196 } 197 198 203 public synchronized int getPreparedOperationDepth() { 204 return preparedOperations; 205 } 206 207 212 void incrementNestedOperations() { 213 nestedOperations++; 214 } 215 216 221 private void incrementPreparedOperations() { 222 preparedOperations++; 223 } 224 225 231 boolean isBalanced() { 232 return nestedOperations == preparedOperations; 233 } 234 235 239 public boolean isLockAlreadyAcquired() { 240 boolean result = false; 241 try { 242 boolean success = lock.acquire(0L); 243 if (success) { 244 result = lock.getDepth() > 1; 247 lock.release(); 248 } 249 } catch (InterruptedException e) { 250 } 252 return result; 253 } 254 255 260 public void operationCanceled() { 261 operationCanceled = true; 262 } 263 264 270 public void rebalanceNestedOperations() { 271 nestedOperations = preparedOperations; 272 } 273 274 278 public void setBuild(boolean hasChanges) { 279 hasBuildChanges = hasBuildChanges || hasChanges; 280 } 281 282 286 public boolean shouldBuild() { 287 if (hasBuildChanges) { 288 if (operationCanceled) 289 return Policy.buildOnCancel; 290 return true; 291 } 292 return false; 293 } 294 295 public void shutdown(IProgressMonitor monitor) { 296 } 298 299 public void startup(IProgressMonitor monitor) { 300 } 302 } 303 | Popular Tags |