1 package hudson.model; 2 3 import org.kohsuke.stapler.StaplerRequest; 4 import org.kohsuke.stapler.StaplerResponse; 5 6 import javax.servlet.ServletException ; 7 import java.io.IOException ; 8 import java.util.LinkedHashSet ; 9 import java.util.SortedMap ; 10 11 20 public abstract class ViewJob<JobT extends ViewJob<JobT,RunT>, RunT extends Run<JobT,RunT>> 21 extends Job<JobT,RunT> { 22 23 27 private transient long nextUpdate = 0; 28 29 32 protected transient RunMap<RunT> runs = new RunMap<RunT>(); 33 34 private transient boolean notLoaded = true; 35 36 40 private transient volatile boolean reloadingInProgress; 41 42 48 private static final LinkedHashSet <ViewJob> reloadQueue = new LinkedHashSet <ViewJob>(); 49 static final Thread reloadThread = new ReloadThread(); 50 static { 51 reloadThread.start(); 52 } 53 54 protected ViewJob(Hudson parent, String name) { 55 super(parent,name); 56 } 57 58 public boolean isBuildable() { 59 return false; 60 } 61 62 63 public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException { 64 super.onLoad(parent, name); 65 notLoaded = true; 66 } 67 68 protected SortedMap <Integer ,RunT> _getRuns() { 69 if(notLoaded || runs==null) { 70 synchronized(this) { 72 if(runs==null) 73 runs = new RunMap<RunT>(); 74 if(notLoaded) { 75 notLoaded = false; 76 _reload(); 77 } 78 } 79 } 80 if(nextUpdate<System.currentTimeMillis()) { 81 if(!reloadingInProgress) { 82 reloadingInProgress = true; 86 synchronized(reloadQueue) { 87 reloadQueue.add(this); 88 reloadQueue.notify(); 89 } 90 } 91 } 92 return runs; 93 } 94 95 public void removeRun(RunT run) { 96 nextUpdate = 0; 98 } 99 100 private void _reload() { 101 try { 102 reload(); 103 } finally { 104 reloadingInProgress = false; 105 nextUpdate = System.currentTimeMillis()+1000*60; 106 } 107 } 108 109 115 protected abstract void reload(); 116 117 public void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException { 118 super.doConfigSubmit(req,rsp); 119 nextUpdate = 0; 121 } 122 123 124 127 private static final class ReloadThread extends Thread { 128 private ViewJob getNext() throws InterruptedException { 129 synchronized(reloadQueue) { 130 while(reloadQueue.isEmpty()) 131 reloadQueue.wait(); 132 ViewJob job = reloadQueue.iterator().next(); 133 reloadQueue.remove(job); 134 return job; 135 } 136 } 137 138 public void run() { 139 while (true) { 140 try { 141 getNext()._reload(); 142 } catch (InterruptedException e) { 143 return; 145 } catch (Throwable t) { 146 t.printStackTrace(); 148 } 149 } 150 } 151 } 152 153 } 155 | Popular Tags |