I needed to build a new task for work. We needed to make sure that the builds are only run during specific times, so we don't impact our users. In practice, we will be able to override this by adding an unless property to the target.
Attached is a fairly robust task that does this. I included the documentation right away as well. Any comments? Thanks Scott PS. I gave a presentation on Ant to my coworkers yesterday. I think I garnered some more interest. Cool. :)
Index: index.html =================================================================== RCS file: /home/cvspublic/jakarta-ant/docs/index.html,v retrieving revision 1.195 diff -w -b -u -r1.195 index.html --- index.html 2001/01/25 10:25:01 1.195 +++ index.html 2001/01/27 04:36:41 @@ -5503,6 +5503,7 @@ <li><a href="#sound">Sound</a></li> <li><a href="#telnet">Telnet</a></li> <li><a href="#vssget">VssGet</a></li> + <li><a href="#buildwindow">BuildWindow</a></li> </ul> <hr> <h2><a name="cab">Cab</a></h2> @@ -6528,6 +6529,51 @@ <i>me</i> and the password <i>mypassword</i>. It will recursively get the files which are labeled <i>Release1</i> and write them to the local directory <i>C:\mysrc\myproject</i>. The local files will be writable.</p> +<hr> +<h2><a name="buildwindow">BuildWindow</a></h2> +<h3>Description</h3> +Task to validate current time in list of time windows. +<h3>Parameters</h3> +The BuildWindow task only uses nested elements currently. + +<h3>Nested Elements</h3> +The BuildWindow task supports a window nested element which allows a start time, stop time, and weekdays that those times apply to, to be specified + +<table border="1" cellpadding="2" cellspacing="0"> + <tr> + <th>Attribute</th> + <th>Values</th> + <th>Required</th> + </tr> + <tr> + <td>start</td> + <td rowspan="2">Time in format <i>HH:MM</i> </td> + <td>No</td> + </tr> + <tr> + <td>stop</td> + <td>No</td> + </tr> + <tr> + <td>weekdays</td> + <td>A list of one or more comma seperated days<br> <u>Su,Mo,Tu,We,Th,Fr,Sa</u></td> + <td>No</td> + </tr> + </tr> +</table> +<p>Note that at least one of these attributes must be present</o> +<h3>Examples</h3> +<blockquote> +<pre> +<BuildWindow> + <window stop="8:00" /> + <window stop="17:00" /> + <window start="11:30" stop="12:00" /> + <window weekdays="Sa,Su" /> +</BuildWindow> +</pre> +</blockquote> +<p>This allows builds anytime before 8am, anytime after 5pm, between 11:30am and noon, and all day saturday and sunday. </p> <hr> <h2><a name="buildevents">Build Events</a></h2>
/* * The Apache Software License, Version 1.1 * * Copyright (c) 2000 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Ant", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.tools.ant.taskdefs.optional; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import java.io.*; import java.lang.*; import java.util.*; import java.text.*; /** * Class to allow builds only during build windows * * @author Scott Carlson<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> * @version $Revision: 1.5 $ */ public class BuildWindow extends Task { /** * The list of build windows for this session */ private Vector windows = new Vector(); /** * Check the list of windows for one that * Surrounds the current time */ public void execute() throws BuildException { Enumeration e = windows.elements(); while (e!= null && e.hasMoreElements()) { Window w = (Window) e.nextElement(); if (w.checkTime() == true) return; } throw new BuildException("Not in a specified build window"); } /** * Add a new window to our list */ public Window createWindow() { Window w = new Window(); windows.add(w); return w; } /** * This class actually holds the window */ public class Window { Calendar startTime = null; Calendar stopTime = null; Boolean[] days; public void setWeekdays(String s) throws BuildException { days = new Boolean[7]; StringTokenizer st = new StringTokenizer(s,","); if (st == null || st.countTokens() == 0) { throw new BuildException("Weekdays attribute must not be a zero length value"); } while (st != null && st.hasMoreTokens()) { String day = st.nextToken(); if (day.compareTo("Su") == 0) days[Calendar.SUNDAY] = Boolean.TRUE; else if (day.compareTo("Mo") == 0) days[Calendar.MONDAY] = Boolean.TRUE; else if (day.compareTo("Tu") == 0) days[Calendar.TUESDAY] = Boolean.TRUE; else if (day.compareTo("We") == 0) days[Calendar.WEDNESDAY] = Boolean.TRUE; else if (day.compareTo("Th") == 0) days[Calendar.THURSDAY] = Boolean.TRUE; else if (day.compareTo("Fr") == 0) days[Calendar.FRIDAY] = Boolean.TRUE; else if (day.compareTo("Sa") == 0) days[Calendar.SATURDAY] = Boolean.TRUE; else throw new BuildException("Weekday attribute must be one or more of Su,Mo,Tu,We,Th,Fr,Sa"); } } public boolean checkTime() throws BuildException { Calendar rightNow = Calendar.getInstance(); /** Validate that the window is valid */ if (startTime != null && stopTime != null && startTime.after(stopTime)) { throw new BuildException("Startime must be before stop time"); } /** Both times can't be null unless a date was specified */ if (startTime == null && stopTime == null && days == null) { throw new BuildException("A startTime and/or stopTime must be set"); } /** Check to see if this window is for today */ if ( days != null && days[rightNow.get(Calendar.DAY_OF_WEEK)] == null) { return false; } /** Check to see if builds are valid all day */ if (startTime == null && stopTime == null && days != null) { if (days[rightNow.get(Calendar.DAY_OF_WEEK)] != null) return true; } // if StartTime is not set, then just make sure we // are before the end of the window if (startTime == null && rightNow.before(stopTime)) { return true; } // if StopTime is not set, then just make sure we // are after the startTime if (stopTime == null && rightNow.after(startTime)) { return true; } // Otherwise we must be between the windows if (rightNow.before(startTime) || rightNow.after(stopTime)) return false; return true; } public void setStart(String s) throws BuildException { startTime = Calendar.getInstance(); parseTime(s, startTime); } public void setStop(String s) throws BuildException { stopTime = Calendar.getInstance(); parseTime(s, stopTime); } public void parseTime(String s, Calendar cal) throws BuildException { StringTokenizer st = new StringTokenizer(s, ":"); if (st.countTokens() != 2) { throw new BuildException("Invalid time format"); } cal.set(Calendar.HOUR_OF_DAY, new Integer(st.nextToken()).intValue()); cal.set(Calendar.MINUTE, new Integer(st.nextToken()).intValue()); } } }
