1 11 package org.eclipse.core.internal.refresh; 12 13 import java.util.ArrayList ; 14 import org.eclipse.core.internal.resources.Resource; 15 import org.eclipse.core.internal.utils.Messages; 16 import org.eclipse.core.resources.*; 17 import org.eclipse.core.resources.refresh.IRefreshMonitor; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.core.runtime.jobs.Job; 20 import org.osgi.framework.Bundle; 21 22 35 public class PollingMonitor extends Job implements IRefreshMonitor { 36 39 private static final long MAX_DURATION = 250; 40 43 private static final long HOT_ROOT_DECAY = 90000; 44 47 private static final long MIN_FREQUENCY = 4000; 48 51 private final ArrayList resourceRoots; 52 55 private final ArrayList toRefresh; 56 59 private IResource hotRoot; 60 63 private long hotRootTime; 64 65 private final RefreshManager refreshManager; 66 69 private boolean firstRun = true; 70 71 74 public PollingMonitor(RefreshManager manager) { 75 super(Messages.refresh_pollJob); 76 this.refreshManager = manager; 77 setPriority(Job.DECORATE); 78 setSystem(true); 79 resourceRoots = new ArrayList (); 80 toRefresh = new ArrayList (); 81 } 82 83 86 public synchronized void monitor(IResource root) { 87 resourceRoots.add(root); 88 schedule(MIN_FREQUENCY); 89 } 90 91 94 protected IStatus run(IProgressMonitor monitor) { 95 if (firstRun) { 97 firstRun = false; 98 Bundle bundle = Platform.getBundle(ResourcesPlugin.PI_RESOURCES); 99 long waitStart = System.currentTimeMillis(); 100 while (bundle.getState() == Bundle.STARTING) { 101 try { 102 Thread.sleep(10000); 103 } catch (InterruptedException e) { 104 } 106 if ((System.currentTimeMillis() - waitStart) > 90000) 108 break; 109 } 110 } 111 long time = System.currentTimeMillis(); 112 if (toRefresh.isEmpty()) { 114 beginIteration(); 115 if (RefreshManager.DEBUG) 116 System.out.println(RefreshManager.DEBUG_PREFIX + "New polling iteration on " + toRefresh.size() + " roots"); } 118 final int oldSize = toRefresh.size(); 119 if (RefreshManager.DEBUG) 120 System.out.println(RefreshManager.DEBUG_PREFIX + "started polling"); if (time - hotRootTime > HOT_ROOT_DECAY) 123 hotRoot = null; 124 else if (hotRoot != null && !monitor.isCanceled()) 125 poll(hotRoot); 126 final long loopStart = System.currentTimeMillis(); 128 while (!toRefresh.isEmpty()) { 129 if (monitor.isCanceled()) 130 break; 131 poll((IResource) toRefresh.remove(toRefresh.size() - 1)); 132 if (System.currentTimeMillis() - loopStart > MAX_DURATION) 134 break; 135 } 136 time = System.currentTimeMillis() - time; 137 if (RefreshManager.DEBUG) 138 System.out.println(RefreshManager.DEBUG_PREFIX + "polled " + (oldSize - toRefresh.size()) + " roots in " + time + "ms"); long delay = Math.max(MIN_FREQUENCY, time * 20); 142 if (!getJobManager().isIdle()) 144 delay *= 2; 145 if (RefreshManager.DEBUG) 146 System.out.println(RefreshManager.DEBUG_PREFIX + "rescheduling polling job in: " + delay / 1000 + " seconds"); if (Platform.getBundle(ResourcesPlugin.PI_RESOURCES).getState() == Bundle.ACTIVE) 149 schedule(delay); 150 return Status.OK_STATUS; 151 } 152 153 159 void runOnce() { 160 synchronized (this) { 161 IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); 165 for (int i = 0; i < projects.length; i++) 166 toRefresh.add(projects[i]); 167 } 168 schedule(MIN_FREQUENCY); 169 } 170 171 private void poll(IResource resource) { 172 if (resource.isSynchronized(IResource.DEPTH_INFINITE)) 173 return; 174 if (resource.isLinked() && !((Resource) resource).getStore().fetchInfo().exists()) 176 return; 177 refreshManager.refresh(resource); 179 hotRoot = resource; 180 hotRootTime = System.currentTimeMillis(); 181 if (RefreshManager.DEBUG) 182 System.out.println(RefreshManager.DEBUG_PREFIX + "new hot root: " + resource); } 184 185 188 public boolean shouldRun() { 189 return !resourceRoots.isEmpty() || !toRefresh.isEmpty(); 191 } 192 193 198 private synchronized void beginIteration() { 199 toRefresh.addAll(resourceRoots); 200 if (hotRoot != null) 201 toRefresh.remove(hotRoot); 202 } 203 204 207 public synchronized void unmonitor(IResource resource) { 208 if (resource == null) 209 resourceRoots.clear(); 210 else 211 resourceRoots.remove(resource); 212 if (resourceRoots.isEmpty()) 213 cancel(); 214 } 215 } 216 | Popular Tags |