View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19   
20  package org.apache.james.fetchmail;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  
25  import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
26  import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
27  import org.apache.avalon.framework.activity.Disposable;
28  import org.apache.avalon.framework.activity.Initializable;
29  import org.apache.avalon.framework.configuration.Configurable;
30  import org.apache.avalon.framework.configuration.Configuration;
31  import org.apache.avalon.framework.configuration.ConfigurationException;
32  import org.apache.avalon.framework.container.ContainerUtil;
33  import org.apache.avalon.framework.logger.AbstractLogEnabled;
34  import org.apache.avalon.framework.service.ServiceException;
35  import org.apache.avalon.framework.service.ServiceManager;
36  import org.apache.avalon.framework.service.Serviceable;
37  
38  /***
39   *  A class to instantiate and schedule a set of mail fetching tasks
40   *
41   * $Id: FetchScheduler.java 494012 2007-01-08 10:23:58Z norman $
42   *
43   *  @see org.apache.james.fetchmail.FetchMailOriginal#configure(Configuration) FetchMailOriginal
44   *  
45   */
46  public class FetchScheduler
47      extends AbstractLogEnabled
48      implements Serviceable, Configurable, Initializable, Disposable, FetchSchedulerMBean {
49  
50      /***
51       * Configuration object for this service
52       */
53      private Configuration conf;
54  
55      /***
56       * The service manager that allows access to the system services
57       */
58      private ServiceManager m_manager;
59  
60      /***
61       * The scheduler service that is used to trigger fetch tasks.
62       */
63      private TimeScheduler scheduler;
64  
65      /***
66       * Whether this service is enabled.
67       */
68      private volatile boolean enabled = false;
69  
70      private ArrayList theFetchTaskNames = new ArrayList();
71  
72      /***
73       * @see org.apache.avalon.framework.service.Serviceable#service( ServiceManager )
74       */
75      public void service(ServiceManager comp) throws ServiceException
76      {
77          m_manager = comp;
78      }
79  
80      /***
81       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
82       */
83      public void configure(Configuration conf) throws ConfigurationException
84      {
85          this.conf = conf;
86      }
87  
88      /***
89       * @see org.apache.avalon.framework.activity.Initializable#initialize()
90       */
91      public void initialize() throws Exception
92      {
93          enabled = conf.getAttributeAsBoolean("enabled", false);
94          if (enabled)
95          {
96              scheduler = (TimeScheduler) m_manager.lookup(TimeScheduler.ROLE);
97              Configuration[] fetchConfs = conf.getChildren("fetch");
98              for (int i = 0; i < fetchConfs.length; i++)
99              {
100                 FetchMail fetcher = new FetchMail();
101                 Configuration fetchConf = fetchConfs[i];
102                 String fetchTaskName = fetchConf.getAttribute("name");
103                 ContainerUtil.enableLogging(fetcher,getLogger().getChildLogger(fetchTaskName));
104                 ContainerUtil.service(fetcher,m_manager);
105                 ContainerUtil.configure(fetcher,fetchConf);
106                 Integer interval =
107                     new Integer(fetchConf.getChild("interval").getValue());
108                 PeriodicTimeTrigger fetchTrigger =
109                     new PeriodicTimeTrigger(0, interval.intValue());
110 
111                 scheduler.addTrigger(fetchTaskName, fetchTrigger, fetcher);
112                 theFetchTaskNames.add(fetchTaskName);
113             }
114 
115             if (getLogger().isInfoEnabled())
116                 getLogger().info("FetchMail Started");
117             System.out.println("FetchMail Started");
118         }
119         else
120         {
121             if (getLogger().isInfoEnabled())
122                 getLogger().info("FetchMail Disabled");
123             System.out.println("FetchMail Disabled");
124         }
125     }
126 
127     /***
128      * @see org.apache.avalon.framework.activity.Disposable#dispose()
129      */
130     public void dispose()
131     {
132         if (enabled)
133         {
134             getLogger().info("FetchMail dispose...");
135             Iterator nameIterator = theFetchTaskNames.iterator();
136             while (nameIterator.hasNext())
137             {
138                 scheduler.removeTrigger((String) nameIterator.next());
139             }
140             getLogger().info("FetchMail ...dispose end");
141         }
142     }
143     
144     /***
145      * Describes whether this service is enabled by configuration.
146      *
147      * @return is the service enabled.
148      */
149     public final boolean isEnabled() {
150         return enabled;
151     }
152     
153 }