001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.sourcecontrols;
037:
038: import java.util.ArrayList;
039: import java.util.Calendar;
040: import java.util.Date;
041: import java.util.List;
042:
043: import net.sourceforge.cruisecontrol.Builder;
044: import net.sourceforge.cruisecontrol.CruiseControlException;
045: import net.sourceforge.cruisecontrol.Modification;
046: import net.sourceforge.cruisecontrol.util.DateUtil;
047: import net.sourceforge.cruisecontrol.util.ValidationHelper;
048:
049: import org.apache.log4j.Logger;
050:
051: /**
052: * Provide a "time" using hhmm format that specifies when a build should be
053: * triggered. Once one successful build occurs, no more occur. If a build occurs
054: * successfully via other means as the time threshold is crossed then this build
055: * won't occur.
056: *
057: * The is useful when you need a project to be built on a time basis despite no
058: * changes to source control.
059: *
060: * @author <a href="mailto:epugh@opensourceconnections.com">Eric Pugh </a>
061: * @version $Id: TimeBuild.java 3115 2007-06-22 16:30:48Z pauljulius $
062: */
063: public class TimeBuild extends FakeUserSourceControl {
064: private static final Logger LOG = Logger.getLogger(TimeBuild.class);
065:
066: private int time = Builder.NOT_SET;
067:
068: /**
069: * The threshold time to cross that starts triggering a build
070: *
071: * @param timeString
072: * The time in hhmm format
073: */
074: public void setTime(String timeString) {
075: time = Integer.parseInt(timeString);
076: }
077:
078: public void validate() throws CruiseControlException {
079: ValidationHelper.assertFalse(time == Builder.NOT_SET,
080: "the 'time' attribute is mandatory");
081: }
082:
083: /**
084: * Check if TimeBuild "time" threshold has passed with out a successful
085: * build. If so, trigger the build.
086: *
087: * @param lastBuild
088: * date of last build
089: * @param now
090: * current time
091: */
092: public List getModifications(Date lastBuild, Date now) {
093: LOG.debug("LastBuild:" + lastBuild + ", now:" + now);
094: List modifications = new ArrayList();
095:
096: /*
097: *
098: * if now and lastbuild occur on the same day, only trigger a build when
099: * lastbuildtime is before 'time' and 'time' is before nowtime
100: *
101: * if now and lastbuild do not occur on the same day, only trigger a
102: * build when nowtime is after 'time'
103: *
104: *
105: */
106: // TODO trigger at time, not just after it
107: int lastBuildTime = DateUtil.getTimeFromDate(lastBuild);
108: int nowTime = DateUtil.getTimeFromDate(now);
109: if (onSameDay(lastBuild, now)) {
110: if (lastBuildTime < time && time < nowTime) {
111: modifications.add(getMod(now));
112: }
113: } else {
114: if (nowTime > time | lastBuildTime < time) {
115: modifications.add(getMod(now));
116: }
117: }
118:
119: if (!modifications.isEmpty()) {
120: getSourceControlProperties().modificationFound();
121: }
122:
123: return modifications;
124: }
125:
126: private boolean onSameDay(Date date1, Date date2) {
127: Calendar calendar = Calendar.getInstance();
128: calendar.setTime(date1);
129: int day1 = calendar.get(Calendar.DAY_OF_MONTH);
130: calendar.setTime(date2);
131: int day2 = calendar.get(Calendar.DAY_OF_MONTH);
132: return day1 == day2;
133: }
134:
135: private Modification getMod(Date now) {
136: Modification mod = new Modification("always");
137: Modification.ModifiedFile modfile = mod.createModifiedFile(
138: "time build", "time build");
139: modfile.action = "change";
140: mod.userName = getUserName();
141: Calendar nowTimeBuild = Calendar.getInstance();
142: nowTimeBuild.setTime(now);
143: final int modifHour = this .time / 100;
144: final int modifMinute = this .time - modifHour * 100;
145: nowTimeBuild.set(Calendar.HOUR_OF_DAY, modifHour);
146: nowTimeBuild.set(Calendar.MINUTE, modifMinute);
147: nowTimeBuild.set(Calendar.MILLISECOND, 0);
148: mod.modifiedTime = nowTimeBuild.getTime();
149: mod.comment = "";
150: return mod;
151: }
152:
153: public String toString() {
154: return getUserName() + ", " + time;
155: }
156:
157: }
|