/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. */
  24. package hudson.triggers;
  25. import static hudson.Util.fixNull;
  26. import hudson.model.BuildableItem;
  27. import hudson.model.Cause;
  28. import hudson.model.Item;
  29. import hudson.scheduler.CronTabList;
  30. import hudson.util.FormValidation;
  31. import hudson.Extension;
  32. import org.kohsuke.stapler.DataBoundConstructor;
  33. import org.kohsuke.stapler.QueryParameter;
  34. import antlr.ANTLRException;
  35. /**
  36. * {@link Trigger} that runs a job periodically.
  37. *
  38. * @author Kohsuke Kawaguchi
  39. */
  40. public class TimerTrigger extends Trigger<BuildableItem> {
  41. @DataBoundConstructor
  42. public TimerTrigger(String spec) throws ANTLRException {
  43. super(spec);
  44. }
  45. @Override
  46. public void run() {
  47. job.scheduleBuild(0, new TimerTriggerCause());
  48. }
  49. @Extension
  50. public static class DescriptorImpl extends TriggerDescriptor {
  51. public boolean isApplicable(Item item) {
  52. return item instanceof BuildableItem;
  53. }
  54. public String getDisplayName() {
  55. return Messages.TimerTrigger_DisplayName();
  56. }
  57. // backward compatibility
  58. public FormValidation doCheck(@QueryParameter String value) {
  59. return doCheckSpec(value);
  60. }
  61. /**
  62. * Performs syntax check.
  63. */
  64. public FormValidation doCheckSpec(@QueryParameter String value) {
  65. try {
  66. String msg = CronTabList.create(fixNull(value)).checkSanity();
  67. if(msg!=null) return FormValidation.warning(msg);
  68. return FormValidation.ok();
  69. } catch (ANTLRException e) {
  70. return FormValidation.error(e.getMessage());
  71. }
  72. }
  73. }
  74. public static class TimerTriggerCause extends Cause {
  75. @Override
  76. public String getShortDescription() {
  77. return Messages.TimerTrigger_TimerTriggerCause_ShortDescription();
  78. }
  79. @Override
  80. public boolean equals(Object o) {
  81. return o instanceof TimerTriggerCause;
  82. }
  83. @Override
  84. public int hashCode() {
  85. return 5;
  86. }
  87. }
  88. }