KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > fetchmail > FetchScheduler


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

17  
18 package org.apache.james.fetchmail;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
24 import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
25 import org.apache.avalon.framework.activity.Disposable;
26 import org.apache.avalon.framework.activity.Initializable;
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.logger.AbstractLogEnabled;
31 import org.apache.avalon.framework.service.ServiceException;
32 import org.apache.avalon.framework.service.ServiceManager;
33 import org.apache.avalon.framework.service.Serviceable;
34
35 /**
36  * A class to instantiate and schedule a set of mail fetching tasks
37  *
38  * $Id: FetchScheduler.java,v 1.8.2.4 2004/02/18 21:47:26 hilmer Exp $
39  *
40  * @see org.apache.james.fetchmail.FetchMailOriginal#configure(Configuration) FetchMailOriginal
41  *
42  */

43 public class FetchScheduler
44     extends AbstractLogEnabled
45     implements Serviceable, Configurable, Initializable, Disposable, FetchSchedulerMBean {
46
47     /**
48      * Configuration object for this service
49      */

50     private Configuration conf;
51
52     /**
53      * The service manager that allows access to the system services
54      */

55     private ServiceManager m_manager;
56
57     /**
58      * The scheduler service that is used to trigger fetch tasks.
59      */

60     private TimeScheduler scheduler;
61
62     /**
63      * Whether this service is enabled.
64      */

65     private volatile boolean enabled = false;
66
67     private ArrayList JavaDoc theFetchTaskNames = new ArrayList JavaDoc();
68
69     /**
70      * @see org.apache.avalon.framework.service.Serviceable#service( ServiceManager )
71      */

72     public void service(ServiceManager comp) throws ServiceException
73     {
74         m_manager = comp;
75     }
76
77     /**
78      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
79      */

80     public void configure(Configuration conf) throws ConfigurationException
81     {
82         this.conf = conf;
83     }
84
85     /**
86      * @see org.apache.avalon.framework.activity.Initializable#initialize()
87      */

88     public void initialize() throws Exception JavaDoc
89     {
90         enabled = conf.getAttributeAsBoolean("enabled", false);
91         if (enabled)
92         {
93             scheduler = (TimeScheduler) m_manager.lookup(TimeScheduler.ROLE);
94             Configuration[] fetchConfs = conf.getChildren("fetch");
95             for (int i = 0; i < fetchConfs.length; i++)
96             {
97                 FetchMail fetcher = new FetchMail();
98                 Configuration fetchConf = fetchConfs[i];
99                 String JavaDoc fetchTaskName = fetchConf.getAttribute("name");
100                 fetcher.enableLogging(
101                     getLogger().getChildLogger(fetchTaskName));
102                 fetcher.service(m_manager);
103                 fetcher.configure(fetchConf);
104                 Integer JavaDoc interval =
105                     new Integer JavaDoc(fetchConf.getChild("interval").getValue());
106                 PeriodicTimeTrigger fetchTrigger =
107                     new PeriodicTimeTrigger(0, interval.intValue());
108
109                 scheduler.addTrigger(fetchTaskName, fetchTrigger, fetcher);
110                 theFetchTaskNames.add(fetchTaskName);
111             }
112
113             if (getLogger().isInfoEnabled())
114                 getLogger().info("FetchMail Started");
115             System.out.println("FetchMail Started");
116         }
117         else
118         {
119             if (getLogger().isInfoEnabled())
120                 getLogger().info("FetchMail Disabled");
121             System.out.println("FetchMail Disabled");
122         }
123     }
124
125     /**
126      * @see org.apache.avalon.framework.activity.Disposable#dispose()
127      */

128     public void dispose()
129     {
130         if (enabled)
131         {
132             getLogger().info("FetchMail dispose...");
133             Iterator JavaDoc nameIterator = theFetchTaskNames.iterator();
134             while (nameIterator.hasNext())
135             {
136                 scheduler.removeTrigger((String JavaDoc) nameIterator.next());
137             }
138             getLogger().info("FetchMail ...dispose end");
139         }
140     }
141     
142     /**
143      * Describes whether this service is enabled by configuration.
144      *
145      * @return is the service enabled.
146      */

147     public final boolean isEnabled() {
148         return enabled;
149     }
150     
151 }
152
Popular Tags