KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > scheduler > SingleScheduleProvider


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.varia.scheduler;
23
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import javax.management.JMException JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MalformedObjectNameException JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32
33 /**
34  * This Provider adds a single Schedule to the Schedule Manager
35  * but you can create more than one of this MBeans and each will
36  * add a different Schedule even when the use the same Target.
37  * ATTENTION: This is the provider you used in the older Scheduler
38  * when you used a MBean as target.
39  *
40  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
41  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
42  * @version $Revision: 43909 $
43  */

44 public class SingleScheduleProvider extends AbstractScheduleProvider
45    implements SingleScheduleProviderMBean
46 {
47
48    // -------------------------------------------------------------------------
49
// Constants
50
// -------------------------------------------------------------------------
51

52    // -------------------------------------------------------------------------
53
// Members
54
// -------------------------------------------------------------------------
55

56    private ObjectName JavaDoc mSchedulableMBean;
57    private String JavaDoc mSchedulableMBeanMethod;
58    private String JavaDoc mSchedulableMBeanMethodName;
59    private String JavaDoc[] mMethodSignature = new String JavaDoc[ 0 ];
60    
61    private SimpleDateFormat JavaDoc mDateFormatter;
62    private Date JavaDoc mStartDate;
63    private String JavaDoc mStartDateString;
64    private long mSchedulePeriod;
65    private long mInitialRepetitions;
66    
67    /** The ID of the Schedule used later to remove it later */
68    private int mScheduleID;
69    
70    // -------------------------------------------------------------------------
71
// Constructors
72
// -------------------------------------------------------------------------
73

74    /**
75     * Default (no-args) Constructor
76     */

77    public SingleScheduleProvider()
78    {
79    }
80    
81    // -------------------------------------------------------------------------
82
// SchedulerMBean Methods
83
// -------------------------------------------------------------------------
84

85    public void startProviding() throws JMException JavaDoc
86    {
87       mScheduleID = addSchedule(
88          mSchedulableMBean,
89          mSchedulableMBeanMethodName,
90          mMethodSignature,
91          mStartDate,
92          mSchedulePeriod,
93          (int) mInitialRepetitions
94       );
95    }
96    
97    public void stopProviding()
98    {
99       try
100       {
101          removeSchedule( mScheduleID );
102       }
103       catch (JMException JavaDoc jme)
104       {
105          log.error( "Could not remove Schedule in stop providing", jme );
106       }
107    }
108    
109    /**
110     * @jmx:managed-attribute
111     *
112     * @return the ObjectName of the Target MBean for the timer notifications
113     */

114    public ObjectName JavaDoc getTargetName()
115    {
116       return mSchedulableMBean;
117    }
118    
119    /**
120     * Sets the fully qualified JMX MBean Object Name of the Schedulable MBean to be called.
121     *
122     * @jmx:managed-attribute
123     *
124     * @param pTargetObjectName JMX MBean Object Name which should be called.
125     * @throws IllegalArgumentException If the given value is an valid Object Name.
126     */

127    public void setTargetName(ObjectName JavaDoc pTargetObjectName)
128    {
129       if (pTargetObjectName == null)
130       {
131          throw new IllegalArgumentException JavaDoc("Schedulable MBean must be specified");
132       }
133       else
134       {
135          this.mSchedulableMBean = pTargetObjectName;
136       }
137    }
138    
139    /**
140     * @return Method description of the target MBean to be called
141     *
142     * @jmx:managed-attribute
143     */

144    public String JavaDoc getTargetMethod()
145    {
146       return mSchedulableMBeanMethod;
147    }
148    
149    /**
150     * Sets the method name to be called on the Schedulable MBean. It can optionally be
151     * followed by an opening bracket, list of attributes (see below) and a closing bracket.
152     * The list of attributes can contain:
153     * <ul>
154     * <li>NOTIFICATION which will be replaced by the timers notification instance
155     * (javax.management.Notification)</li>
156     * <li>DATE which will be replaced by the date of the notification call
157     * (java.util.Date)</li>
158     * <li>REPETITIONS which will be replaced by the number of remaining repetitions
159     * (long)</li>
160     * <li>SCHEDULER_NAME which will be replaced by the Object Name of the Scheduler
161     * (javax.management.ObjectName)</li>
162     * <li>any full qualified Class name which the Scheduler will be set a "null" value
163     * for it</li>
164     * </ul>
165     * <br>
166     * An example could be: "doSomething( NOTIFICATION, REPETITIONS, java.lang.String )"
167     * where the Scheduler will pass the timer's notification instance, the remaining
168     * repetitions as int and a null to the MBean's doSomething() method which must
169     * have the following signature: doSomething( javax.management.Notification, long,
170     * java.lang.String ).
171     *
172     * @jmx:managed-attribute
173     *
174     * @param pTargetMethod Name of the method to be called optional followed
175     * by method arguments (see above).
176     *
177     * @throws IllegalArgumentException If the given value is not of the right
178     * format
179     */

180    public void setTargetMethod(String JavaDoc pTargetMethod) throws IllegalArgumentException JavaDoc
181    {
182       if (pTargetMethod == null)
183       {
184          mSchedulableMBeanMethod = null;
185          return;
186       }
187       int lIndex = pTargetMethod.indexOf('(');
188       String JavaDoc lMethodName = "";
189       if( lIndex < 0 )
190       {
191          lMethodName = pTargetMethod.trim();
192          mMethodSignature = new String JavaDoc[ 0 ];
193       }
194       else
195       if( lIndex > 0 )
196       {
197          lMethodName = pTargetMethod.substring(0, lIndex).trim();
198       }
199       if (lMethodName.equals(""))
200       {
201          lMethodName = "perform";
202       }
203       if (lIndex >= 0)
204       {
205          int lIndex2 = pTargetMethod.indexOf(')');
206          if (lIndex2 < lIndex)
207          {
208             throw new IllegalArgumentException JavaDoc("Schedulable MBean Method: closing bracket must be after opening bracket");
209          }
210          if (lIndex2 < pTargetMethod.length() - 1)
211          {
212             String JavaDoc lRest = pTargetMethod.substring(lIndex2 + 1).trim();
213             if (lRest.length() > 0)
214             {
215                throw new IllegalArgumentException JavaDoc("Schedulable MBean Method: nothing should be after closing bracket");
216             }
217          }
218          String JavaDoc lArguments = pTargetMethod.substring(lIndex + 1, lIndex2).trim();
219          if (lArguments.equals(""))
220          {
221             mMethodSignature = new String JavaDoc[0];
222          }
223          else
224          {
225             StringTokenizer JavaDoc lTokenizer = new StringTokenizer JavaDoc(lArguments, ",");
226             mMethodSignature = new String JavaDoc[lTokenizer.countTokens()];
227             for (int i = 0; lTokenizer.hasMoreTokens(); i++)
228             {
229                mMethodSignature[i] = lTokenizer.nextToken().trim();
230             }
231          }
232       }
233       mSchedulableMBeanMethodName = lMethodName;
234       mSchedulableMBeanMethod = pTargetMethod;
235    }
236    
237    /**
238     * @jmx:managed-attribute
239     *
240     * @return Schedule Period between two scheduled calls in Milliseconds. It will always
241     * be bigger than 0 except it returns -1 then the schedule is stopped.
242     */

243    public long getPeriod()
244    {
245       return mSchedulePeriod;
246    }
247
248    /**
249     * Sets the Schedule Period between two scheduled call.
250     *
251     * @jmx:managed-attribute
252     *
253     * @param pPeriod Time between to scheduled calls (after the initial call) in Milliseconds.
254     * This value must be bigger than 0.
255     *
256     * @throws IllegalArgumentException If the given value is less or equal than 0
257     */

258    public void setPeriod(long pPeriod)
259    {
260       if (pPeriod <= 0)
261       {
262          throw new IllegalArgumentException JavaDoc("Schedulable Period may be not less or equals than 0");
263       }
264       mSchedulePeriod = pPeriod;
265    }
266
267    /**
268     * @jmx:managed-attribute
269     *
270     * @return the date format
271     */

272    public String JavaDoc getDateFormat()
273    {
274       if (mDateFormatter == null)
275          mDateFormatter = new SimpleDateFormat JavaDoc();
276       
277       return mDateFormatter.toPattern();
278    }
279    
280    /**
281     * Sets the date format used to parse date/times
282     *
283     * @jmx:managed-attribute
284     *
285     * @param dateFormat The date format when empty or null the locale is used to parse dates
286     */

287    public void setDateFormat(String JavaDoc dateFormat)
288    {
289       if (dateFormat == null || dateFormat.trim().length() == 0)
290          mDateFormatter = new SimpleDateFormat JavaDoc();
291       else
292          mDateFormatter = new SimpleDateFormat JavaDoc(dateFormat);
293    }
294    
295    /**
296     * @jmx:managed-attribute
297     *
298     * @return Date (and time) of the first scheduled. For value see {@link #setInitialStartDate}
299     * method.
300     */

301    public String JavaDoc getStartDate()
302    {
303       return mStartDateString;
304    }
305    
306    /**
307     * Sets the first scheduled call. If the date is in the past the scheduler tries to find the
308     * next available start date.
309     *
310     * @jmx:managed-attribute
311     *
312     * @param pStartDate Date when the initial call is scheduled. It can be either:
313     * <ul>
314     * <li>
315     * NOW: date will be the current date (new Date()) plus 1 seconds
316     * </li><li>
317     * Date as String able to be parsed by SimpleDateFormat with default format
318     * </li><li>
319     * Date as String parsed using the date format attribute
320     * </li><li>
321     * Milliseconds since 1/1/1970
322     * </li>
323     * </ul>
324     * If the date is in the past the Scheduler
325     * will search a start date in the future with respect to the initial repe-
326     * titions and the period between calls. This means that when you restart
327     * the MBean (restarting JBoss etc.) it will start at the next scheduled
328     * time. When no start date is available in the future the Scheduler will
329     * not start.<br>
330     * Example: if you start your Schedulable everyday at Noon and you restart
331     * your JBoss server then it will start at the next Noon (the same if started
332     * before Noon or the next day if start after Noon).
333     */

334    public void setStartDate(String JavaDoc pStartDate)
335    {
336       mStartDateString = pStartDate == null ? "" : pStartDate.trim();
337       if (mStartDateString.equals(""))
338       {
339          mStartDate = new Date JavaDoc(0);
340       }
341       else if (mStartDateString.equals("NOW"))
342       {
343          mStartDate = new Date JavaDoc(new Date JavaDoc().getTime() + 1000);
344       }
345       else
346       {
347          try
348          {
349             long lDate = new Long JavaDoc(pStartDate).longValue();
350             mStartDate = new Date JavaDoc(lDate);
351          }
352          catch (Exception JavaDoc e)
353          {
354             try
355             {
356                if (mDateFormatter == null)
357                {
358                   mDateFormatter = new SimpleDateFormat JavaDoc();
359                }
360                mStartDate = mDateFormatter.parse(mStartDateString);
361             }
362             catch (Exception JavaDoc e2)
363             {
364                log.error ("Could not parse given date string: " + mStartDateString, e2);
365                throw new IllegalArgumentException JavaDoc("Schedulable Date is not of correct format");
366             }
367          }
368       }
369       log.debug("Initial Start Date is set to: " + mStartDate);
370    }
371
372    /**
373     * @jmx:managed-attribute
374     *
375     * @return Number of scheduled calls initially. If -1 then there is not limit.
376     */

377    public long getRepetitions()
378    {
379       return mInitialRepetitions;
380    }
381
382    /**
383     * Sets the initial number of scheduled calls.
384     *
385     * @jmx:managed-attribute
386     *
387     * @param pNumberOfCalls Initial Number of scheduled calls. If -1 then the number
388     * is unlimted.
389     *
390     * @throws IllegalArgumentException If the given value is less or equal than 0
391     */

392    public void setRepetitions(long pNumberOfCalls)
393    {
394       if (pNumberOfCalls <= 0)
395       {
396          pNumberOfCalls = -1;
397       }
398       mInitialRepetitions = pNumberOfCalls;
399    }
400    
401    // -------------------------------------------------------------------------
402
// Methods
403
// -------------------------------------------------------------------------
404

405    public ObjectName JavaDoc getObjectName(MBeanServer JavaDoc pServer, ObjectName JavaDoc pName)
406       throws MalformedObjectNameException JavaDoc
407    {
408       return pName == null ? SingleScheduleProviderMBean.OBJECT_NAME : pName;
409    }
410 }
411
Popular Tags