View Javadoc

1   // 
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   //All rights reserved. 
4   //   
5   //Redistribution and use in source and binary forms, with or without modification,  
6   //are permitted provided that the following conditions are met: 
7   //   
8   //* Redistributions of source code must retain the above copyright notice,  
9   //this list of conditions and the following disclaimer. 
10  //* Redistributions in binary form must reproduce the above copyright notice,  
11  //this list of conditions and the following disclaimer in the documentation  
12  //and/or other materials provided with the distribution. 
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //nor the names of its contributors may be used to endorse or promote products  
15  //derived from this software without specific prior written permission. 
16  // 
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  //POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.scheduler;
30  
31  import java.text.ParseException;
32  import java.util.ArrayList;
33  import java.util.Collections;
34  import java.util.Date;
35  import java.util.StringTokenizer;
36  
37  /**
38   * A schedule that allows you to execute jobs at specific moments of time.
39   *
40   * <p>The configuration of the schedule is expected to be semicolon separated
41   * list of dates. Each date must adhere to the pattern used by the
42   * Scheduler (see {@link AbstractScheduler#DATE_FORMAT_DEFAULT}).</p>
43   *
44   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
45   * @version $Id: AtSchedule.java,v 1.3 2005/02/10 17:46:58 rafal Exp $
46   */
47  public class AtSchedule
48      implements Schedule
49  {
50      // constants /////////////////////////////////////////////////////////////
51  
52      /*** The schedule type. */
53      public static final String TYPE = "at";
54  
55      // instance variables ////////////////////////////////////////////////////
56  
57      /*** Scheduler */
58      private AbstractScheduler scheduler;
59  
60      /*** Stored configuration. */
61      private String config;
62      
63      /*** Parsed configuration. */
64      private Date[] runTime;
65  
66      /***
67       * package private constructor.
68       */
69      AtSchedule()
70      {
71          // limited access
72      }
73  
74      /***
75       * {@inheritDoc}
76       */
77      public void init(AbstractScheduler scheduler, String config)
78          throws InvalidScheduleException
79      {
80          this.scheduler = scheduler;
81          setConfig(config);
82      }
83  
84      /***
85       * {@inheritDoc}
86       */
87      public String getType()
88      {
89          return TYPE;
90      }
91  
92      /***
93       * {@inheritDoc}
94       */
95      public String getConfig()
96      {
97          return config;
98      }
99  
100     /***
101      * {@inheritDoc}
102      */
103     public void setConfig(String config)
104         throws InvalidScheduleException
105     {
106         this.config = config;
107         
108         StringTokenizer st = new StringTokenizer(config, ";");
109         ArrayList temp = new ArrayList();
110         try
111         {
112             while(st.hasMoreTokens())
113             {
114                 temp.add(scheduler.getDateFormat().parse(st.nextToken()));
115             }
116         }
117         catch(ParseException e)
118         {
119             throw new InvalidScheduleException("invalid date format", e);
120         }
121         Collections.sort(temp);
122         runTime = new Date[temp.size()];
123         temp.toArray(runTime);
124     }
125 
126     /***
127      * {@inheritDoc}
128      */
129     public boolean atStartup()
130     {
131         return false;
132     }
133 
134     /***
135      * {@inheritDoc}
136      */
137     public Date getNextRunTime(Date currentTime, Date lastRunTime)
138     {
139         Date next = null;
140         for(int i=0; i<runTime.length; i++)
141         {
142             if(currentTime.compareTo(runTime[i]) < 0)
143             {
144                 next = runTime[i];
145                 break;
146             }
147         }
148         return next;
149     }
150 }