KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > dispatch > RunAt


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.dispatch;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.config.types.Period;
33 import com.caucho.log.Log;
34 import com.caucho.util.IntArray;
35 import com.caucho.util.L10N;
36 import com.caucho.util.QDate;
37
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Configuration for a run-at
42  */

43 public class RunAt {
44   static L10N L = new L10N(RunAt.class);
45   private static final Logger JavaDoc log = Log.open(RunAt.class);
46
47   private QDate _cal = QDate.createLocal();
48
49   private long _period = -1;
50   
51   private IntArray _hourTimes;
52   private IntArray _minuteTimes;
53   
54   /**
55    * Creates a new servlet configuration object.
56    */

57   public RunAt()
58   {
59   }
60
61   /**
62    * Adds the text.
63    */

64   public void addText(String JavaDoc runAt)
65     throws ConfigException
66   {
67     configureRunAt(runAt);
68   }
69
70   /**
71    * Sets the period
72    */

73   public void setPeriod(Period period)
74     throws ConfigException
75   {
76     _period = period.getPeriod();
77   }
78
79   /**
80    * Returns the next time.
81    */

82   public long getNextTimeout(long now)
83   {
84     _cal.setGMTTime(now);
85     long zone = _cal.getZoneOffset();
86     
87     if (_period > 0)
88       return Period.periodEnd(now + zone, _period) - zone;
89
90     now = now - now % 60000;
91
92     long local = now + zone;
93     
94     long dayMinutes = (local / 60000) % (24 * 60);
95     long hourMinutes = dayMinutes % 60;
96     
97     long nextDelta = Long.MAX_VALUE;
98
99     for (int i = 0; _hourTimes != null && i < _hourTimes.size(); i++) {
100       long time = _hourTimes.get(i);
101       long delta = (time - dayMinutes + 24 * 60) % (24 * 60);
102
103       if (delta == 0)
104         delta = 24 * 60;
105       
106       if (delta < nextDelta && delta > 0)
107         nextDelta = delta;
108     }
109
110     for (int i = 0; _minuteTimes != null && i < _minuteTimes.size(); i++) {
111       long time = _minuteTimes.get(i);
112       long delta = (time - hourMinutes + 60) % 60;
113       
114       if (delta == 0)
115         delta = 60;
116
117       if (delta < nextDelta && delta > 0)
118         nextDelta = delta;
119     }
120
121     if (nextDelta < Integer.MAX_VALUE)
122       return now + nextDelta * 60000L;
123     else
124       return Long.MAX_VALUE / 2;
125   }
126
127   /**
128    * Configures the run-at time.
129    */

130   private void configureRunAt(String JavaDoc string)
131     throws ConfigException
132   {
133     int len = string.length();
134     char ch = 0;
135     int i = 0;
136     while (true) {
137       for (;
138            i < len &&
139              (Character.isWhitespace(ch = string.charAt(i)) || ch == ',');
140            i++) {
141       }
142
143       if (i >= len)
144         return;
145
146       if (! (ch >= '0' && ch <= '9' || ch == ':'))
147         throw new ConfigException(L.l("illegal run-at time `{0}'. Run-at values are either hour (0:00, 6:30, 12:15) or minute (:15, :30, :45).",
148                                       string));
149       
150       int hour = 0;
151       int minute = 0;
152       boolean hasHour = false;
153       for (; i < len && (ch = string.charAt(i)) >= '0' && ch <= '9'; i++) {
154         hasHour = true;
155         hour = 10 * hour + ch - '0';
156       }
157
158       if (ch == ':') {
159         i++;
160         for (; i < len && (ch = string.charAt(i)) >= '0' && ch <= '9'; i++)
161           minute = 10 * minute + ch - '0';
162
163       }
164
165       if (hasHour) {
166         if (_hourTimes == null)
167           _hourTimes = new IntArray();
168         _hourTimes.add(60 * hour + minute);
169       }
170       else {
171         if (_minuteTimes == null)
172           _minuteTimes = new IntArray();
173         _minuteTimes.add(minute);
174       }
175     }
176   }
177 }
178
Popular Tags