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;
037:
038: import java.util.Calendar;
039: import java.util.Date;
040:
041: import net.sourceforge.cruisecontrol.util.DateUtil;
042: import net.sourceforge.cruisecontrol.util.PerDayScheduleItem;
043: import net.sourceforge.cruisecontrol.util.ValidationHelper;
044:
045: /**
046: * Used by <code>Schedule</code> to define periods of time when CruiseControl should not even attempt a build. Useful
047: * for making sure CruiseControl does not run during server backup times, etc.
048: *
049: * @author Alden Almagro
050: */
051: public class PauseBuilder extends PerDayScheduleItem {
052:
053: private static final long serialVersionUID = 866530246895228766L;
054:
055: private int startTime = PerDayScheduleItem.NOT_SET;
056: private int endTime = PerDayScheduleItem.NOT_SET;
057:
058: public void validate() throws CruiseControlException {
059: ValidationHelper.assertFalse(startTime < 0,
060: "'starttime' is a required attribute on PauseBuilder");
061:
062: ValidationHelper.assertFalse(endTime < 0,
063: "'endtime' is a required attribute on PauseBuilder");
064:
065: ValidationHelper
066: .assertFalse(
067: getDay() == INVALID_NAME_OF_DAY,
068: "setDay attribute on PauseBuilder requires english name for day of week (case insensitive)");
069: }
070:
071: public void setStartTime(int time) {
072: startTime = time;
073: }
074:
075: public void setEndTime(int time) {
076: endTime = time;
077: }
078:
079: public int getStartTime() {
080: return startTime;
081: }
082:
083: public int getEndTime() {
084: return endTime;
085: }
086:
087: /**
088: * Determine if the build is paused at the given time.
089: *
090: * @param date
091: * Date set to current date/time
092: * @return true if the build is paused at date
093: */
094: public boolean isPaused(Date date) {
095: Calendar now = Calendar.getInstance();
096: now.setTime(date);
097: int currentDay = now.get(Calendar.DAY_OF_WEEK);
098: int currentTime = DateUtil.getTimeFromDate(date);
099:
100: int builderDay = getDay();
101: boolean isValidDay = ((builderDay < 0) || (builderDay == currentDay));
102:
103: if (startTime < endTime) {
104: return (startTime <= currentTime && currentTime <= endTime && isValidDay);
105: }
106:
107: return ((startTime <= currentTime && (builderDay < 0 || builderDay == currentDay)) || (currentTime <= endTime && (builderDay < 0 || builderDay == (currentDay - 1))));
108: }
109:
110: }
|