KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > TimeBuild


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.sourcecontrols;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.Calendar JavaDoc;
41 import java.util.Date JavaDoc;
42 import java.util.Hashtable JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
45
46 import net.sourceforge.cruisecontrol.Builder;
47 import net.sourceforge.cruisecontrol.CruiseControlException;
48 import net.sourceforge.cruisecontrol.Modification;
49 import net.sourceforge.cruisecontrol.util.DateUtil;
50 import net.sourceforge.cruisecontrol.util.ValidationHelper;
51
52 import org.apache.log4j.Logger;
53
54 /**
55  * Provide a "time" using hhmm format that specifies when a build should be
56  * triggered. Once one successful build occurs, no more occur. If a build occurs
57  * successfully via other means as the time threshold is crossed then this build
58  * won't occur.
59  *
60  * The is useful when you need a project to be built on a time basis despite no
61  * changes to source control.
62  *
63  * @author <a HREF="mailto:epugh@opensourceconnections.com">Eric Pugh </a>
64  * @version $Id: TimeBuild.java,v 1.8 2006/03/19 12:02:11 jchyip Exp $
65  */

66 public class TimeBuild extends FakeUserSourceControl {
67     private static final Logger LOG = Logger.getLogger(TimeBuild.class);
68
69     private int time = Builder.NOT_SET;
70
71     private Hashtable JavaDoc properties = new Hashtable JavaDoc();
72
73     /**
74      * The threshold time to cross that starts triggering a build
75      *
76      * @param timeString
77      * The time in hhmm format
78      */

79     public void setTime(String JavaDoc timeString) {
80         time = Integer.parseInt(timeString);
81     }
82
83     public Map JavaDoc getProperties() {
84         return properties;
85     }
86
87     public void validate() throws CruiseControlException {
88         ValidationHelper.assertFalse(time == Builder.NOT_SET, "the 'time' attribute is mandatory");
89     }
90
91     /**
92      * Check if TimeBuild "time" threshold has passed with out a successful
93      * build. If so, trigger the build.
94      *
95      * @param lastBuild
96      * date of last build
97      * @param now
98      * current time
99      */

100     public List JavaDoc getModifications(Date JavaDoc lastBuild, Date JavaDoc now) {
101         LOG.debug("LastBuild:" + lastBuild + ", now:" + now);
102         List JavaDoc modifications = new ArrayList JavaDoc();
103
104         /*
105          *
106          * if now and lastbuild occur on the same day, only trigger a build when
107          * lastbuildtime is before 'time' and 'time' is before nowtime
108          *
109          * if now and lastbuild do not occur on the same day, only trigger a
110          * build when nowtime is after 'time'
111          *
112          *
113          */

114         // TODO trigger at time, not just after it
115
int lastBuildTime = DateUtil.getTimeFromDate(lastBuild);
116         int nowTime = DateUtil.getTimeFromDate(now);
117         if (onSameDay(lastBuild, now)) {
118             if (lastBuildTime < time && time < nowTime) {
119                 modifications.add(getMod(now));
120             }
121         } else {
122             if (nowTime > time) {
123                 modifications.add(getMod(now));
124             }
125         }
126
127         return modifications;
128     }
129
130     private boolean onSameDay(Date JavaDoc date1, Date JavaDoc date2) {
131         Calendar JavaDoc calendar = Calendar.getInstance();
132         calendar.setTime(date1);
133         int day1 = calendar.get(Calendar.DAY_OF_MONTH);
134         calendar.setTime(date2);
135         int day2 = calendar.get(Calendar.DAY_OF_MONTH);
136         return day1 == day2;
137     }
138
139     private Modification getMod(Date JavaDoc now) {
140         Modification mod = new Modification("always");
141         Modification.ModifiedFile modfile = mod.createModifiedFile("time build", "time build");
142         modfile.action = "change";
143         mod.userName = getUserName();
144         Calendar JavaDoc nowTimeBuild = Calendar.getInstance();
145         nowTimeBuild.setTime(now);
146         final int modifHour = this.time / 100;
147         final int modifMinute = this.time - modifHour * 100;
148         nowTimeBuild.set(Calendar.HOUR_OF_DAY, modifHour);
149         nowTimeBuild.set(Calendar.MINUTE, modifMinute);
150         nowTimeBuild.set(Calendar.MILLISECOND, 0);
151         mod.modifiedTime = nowTimeBuild.getTime();
152         mod.comment = "";
153         return mod;
154     }
155
156     public String JavaDoc toString() {
157         return getUserName() + ", " + time;
158     }
159
160 }
161
Popular Tags