PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/triggers/TimerTrigger.java

http://github.com/hudson/hudson
Java | 101 lines | 56 code | 13 blank | 32 comment | 1 complexity | 043be7987fc3aa8e50c8e37131218473 MD5 | raw file
  1/*
  2 * The MIT License
  3 * 
  4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jean-Baptiste Quenot, Martin Eigenbrodt
  5 * 
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 * 
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 * 
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.triggers;
 25
 26import static hudson.Util.fixNull;
 27import hudson.model.BuildableItem;
 28import hudson.model.Cause;
 29import hudson.model.Item;
 30import hudson.scheduler.CronTabList;
 31import hudson.util.FormValidation;
 32import hudson.Extension;
 33
 34import org.kohsuke.stapler.DataBoundConstructor;
 35import org.kohsuke.stapler.QueryParameter;
 36
 37import antlr.ANTLRException;
 38
 39/**
 40 * {@link Trigger} that runs a job periodically.
 41 *
 42 * @author Kohsuke Kawaguchi
 43 */
 44public class TimerTrigger extends Trigger<BuildableItem> {
 45
 46    @DataBoundConstructor
 47    public TimerTrigger(String spec) throws ANTLRException {
 48        super(spec);
 49    }
 50
 51    @Override
 52    public void run() {
 53        job.scheduleBuild(0, new TimerTriggerCause());
 54    }
 55
 56    @Extension
 57    public static class DescriptorImpl extends TriggerDescriptor {
 58        public boolean isApplicable(Item item) {
 59            return item instanceof BuildableItem;
 60        }
 61
 62        public String getDisplayName() {
 63            return Messages.TimerTrigger_DisplayName();
 64        }
 65
 66        // backward compatibility
 67        public FormValidation doCheck(@QueryParameter String value) {
 68            return doCheckSpec(value);
 69        }
 70        
 71        /**
 72         * Performs syntax check.
 73         */
 74        public FormValidation doCheckSpec(@QueryParameter String value) {
 75            try {
 76                String msg = CronTabList.create(fixNull(value)).checkSanity();
 77                if(msg!=null)   return FormValidation.warning(msg);
 78                return FormValidation.ok();
 79            } catch (ANTLRException e) {
 80                return FormValidation.error(e.getMessage());
 81            }
 82        }
 83    }
 84    
 85    public static class TimerTriggerCause extends Cause {
 86        @Override
 87        public String getShortDescription() {
 88            return Messages.TimerTrigger_TimerTriggerCause_ShortDescription();
 89        }
 90
 91        @Override
 92        public boolean equals(Object o) {
 93            return o instanceof TimerTriggerCause;
 94        }
 95
 96        @Override
 97        public int hashCode() {
 98            return 5;
 99        }
100    }
101}