KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > services > scheduler > TimeTriggerFactory


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.cornerstone.services.scheduler;
19
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22
23 /**
24  * Factory for <code>TimeTrigger</code>s.
25  *
26  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
27  */

28 public class TimeTriggerFactory
29 {
30     /**
31      * Create <code>TimeTrigger</code> from configuration.
32      *
33      * @param conf configuration for time trigger
34      */

35     public TimeTrigger createTimeTrigger( final Configuration conf )
36         throws ConfigurationException
37     {
38         final String JavaDoc type = conf.getAttribute( "type" );
39
40         TimeTrigger trigger;
41         if( "periodic".equals( type ) )
42         {
43             final int offset =
44                 conf.getChild( "offset", true ).getValueAsInteger( 0 );
45             final int period =
46                 conf.getChild( "period", true ).getValueAsInteger( -1 );
47
48             trigger = new PeriodicTimeTrigger( offset, period );
49         }
50         else if( "cron".equals( type ) )
51         {
52             final int minute =
53                 conf.getChild( "minute" ).getValueAsInteger( -1 );
54             final int hour =
55                 conf.getChild( "hour" ).getValueAsInteger( -1 );
56             final int day =
57                 conf.getChild( "day" ).getValueAsInteger( -1 );
58             final int month =
59                 conf.getChild( "month" ).getValueAsInteger( -1 );
60             final int year =
61                 conf.getChild( "year" ).getValueAsInteger( -1 );
62             final boolean dayOfWeek =
63                 conf.getChild( "day" ).getAttributeAsBoolean( "week", false );
64
65             trigger = new CronTimeTrigger( minute, hour, day, month, year,
66                                            dayOfWeek );
67         }
68         else
69         {
70             throw new ConfigurationException( "Unknown trigger type" );
71         }
72
73         return trigger;
74     }
75 }
76
Popular Tags