KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > triggers > Trigger


1 package hudson.triggers;
2
3 import antlr.ANTLRException;
4 import hudson.ExtensionPoint;
5 import hudson.model.AbstractProject;
6 import hudson.model.Action;
7 import hudson.model.Build;
8 import hudson.model.Describable;
9 import hudson.model.FingerprintCleanupThread;
10 import hudson.model.Hudson;
11 import hudson.model.Project;
12 import hudson.model.WorkspaceCleanupThread;
13 import hudson.model.Item;
14 import hudson.scheduler.CronTab;
15 import hudson.scheduler.CronTabList;
16
17 import java.io.InvalidObjectException JavaDoc;
18 import java.io.ObjectStreamException JavaDoc;
19 import java.util.Calendar JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.GregorianCalendar JavaDoc;
22 import java.util.Timer JavaDoc;
23 import java.util.TimerTask JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 /**
28  * Triggers a {@link Build}.
29  *
30  * <p>
31  * To register a custom {@link Trigger} from a plugin,
32  * add it to {@link Triggers#TRIGGERS}.
33  *
34  * @author Kohsuke Kawaguchi
35  */

36 public abstract class Trigger<J extends Item> implements Describable<Trigger<?>>, ExtensionPoint {
37
38     /**
39      * Called when a {@link Trigger} is loaded into memory and started.
40      *
41      * @param project
42      * given so that the persisted form of this object won't have to have a back pointer.
43      * @param newInstance
44      * True if this is a newly created trigger first attached to the {@link Project}.
45      * False if this is invoked for a {@link Project} loaded from disk.
46      */

47     public void start(J project, boolean newInstance) {
48         this.job = project;
49     }
50
51     /**
52      * Executes the triggered task.
53      *
54      * This method is invoked when {@link #Trigger(String)} is used
55      * to create an instance, and the crontab matches the current time.
56      */

57     protected void run() {}
58
59     /**
60      * Called before a {@link Trigger} is removed.
61      * Under some circumstances, this may be invoked more than once for
62      * a given {@link Trigger}, so be prepared for that.
63      *
64      * <p>
65      * When the configuration is changed for a project, all triggers
66      * are removed once and then added back.
67      */

68     public void stop() {}
69
70     /**
71      * Returns an action object if this {@link Trigger} has an action
72      * to contribute to a {@link Project}.
73      */

74     public Action getProjectAction() {
75         return null;
76     }
77
78     public abstract TriggerDescriptor getDescriptor();
79
80
81
82     protected final String JavaDoc spec;
83     protected transient CronTabList tabs;
84     protected transient J job;
85
86     /**
87      * Creates a new {@link Trigger} that gets {@link #run() run}
88      * periodically. This is useful when your trigger does
89      * some polling work.
90      */

91     protected Trigger(String JavaDoc cronTabSpec) throws ANTLRException {
92         this.spec = cronTabSpec;
93         this.tabs = CronTabList.create(cronTabSpec);
94     }
95
96     /**
97      * Creates a new {@link Trigger} without using cron.
98      */

99     protected Trigger() {
100         this.spec = "";
101         this.tabs = new CronTabList(Collections.<CronTab>emptyList());
102     }
103
104     /**
105      * Gets the crontab specification.
106      *
107      * If you are not using cron service, just ignore it.
108      */

109     public final String JavaDoc getSpec() {
110         return spec;
111     }
112
113     private Object JavaDoc readResolve() throws ObjectStreamException JavaDoc {
114         try {
115             tabs = CronTabList.create(spec);
116         } catch (ANTLRException e) {
117             InvalidObjectException JavaDoc x = new InvalidObjectException JavaDoc(e.getMessage());
118             x.initCause(e);
119             throw x;
120         }
121         return this;
122     }
123
124
125     /**
126      * Runs every minute to check {@link TimerTrigger} and schedules build.
127      */

128     private static class Cron extends TimerTask JavaDoc {
129         private final Calendar JavaDoc cal = new GregorianCalendar JavaDoc();
130
131         public void run() {
132             LOGGER.fine("cron checking "+cal.getTime().toLocaleString());
133
134             try {
135                 Hudson inst = Hudson.getInstance();
136                 for (AbstractProject<?,?> p : inst.getAllItems(AbstractProject.class)) {
137                     for (Trigger t : p.getTriggers().values()) {
138                         LOGGER.fine("cron checking "+p.getName());
139                         if(t.tabs.check(cal)) {
140                             LOGGER.fine("cron triggered "+p.getName());
141                             t.run();
142                         }
143                     }
144                 }
145             } catch (Throwable JavaDoc e) {
146                 LOGGER.log(Level.WARNING,"Cron thread throw an exception",e);
147                 // bug in the code. Don't let the thread die.
148
e.printStackTrace();
149             }
150
151             cal.add(Calendar.MINUTE,1);
152         }
153     }
154
155     private static final Logger JavaDoc LOGGER = Logger.getLogger(Trigger.class.getName());
156
157     /**
158      * This timer is available for all the components inside Hudson to schedule
159      * some work.
160      */

161     public static final Timer JavaDoc timer = new Timer JavaDoc(); // "Hudson cron thread"); -- this is a new constructor since 1.5
162

163     public static void init() {
164         timer.scheduleAtFixedRate(new Cron(), 1000*60, 1000*60/*every minute*/);
165
166         // clean up fingerprint once a day
167
long HOUR = 1000*60*60;
168         long DAY = HOUR*24;
169         timer.scheduleAtFixedRate(new FingerprintCleanupThread(),DAY,DAY);
170         timer.scheduleAtFixedRate(new WorkspaceCleanupThread(),DAY+4*HOUR,DAY);
171     }
172 }
173
Popular Tags