View Javadoc

1   /************************************************************************
2    * Copyright (c) 2003-2006 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;
21  import java.util.Iterator;
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.container.ContainerUtil;
31  import org.apache.avalon.framework.logger.AbstractLogEnabled;
32  import org.apache.avalon.framework.service.ServiceException;
33  import org.apache.avalon.framework.service.ServiceManager;
34  import org.apache.avalon.framework.service.Serviceable;
35  
36  /***
37   *  A class to instantiate and schedule a set of mail fetching tasks
38   *
39   * $Id: FetchScheduler.java 365582 2006-01-03 08:51:21 +0000 (mar, 03 gen 2006) bago $
40   *
41   *  @see org.apache.james.fetchmail.FetchMailOriginal#configure(Configuration) FetchMailOriginal
42   *  
43   */
44  public class FetchScheduler
45      extends AbstractLogEnabled
46      implements Serviceable, Configurable, Initializable, Disposable, FetchSchedulerMBean {
47  
48      /***
49       * Configuration object for this service
50       */
51      private Configuration conf;
52  
53      /***
54       * The service manager that allows access to the system services
55       */
56      private ServiceManager m_manager;
57  
58      /***
59       * The scheduler service that is used to trigger fetch tasks.
60       */
61      private TimeScheduler scheduler;
62  
63      /***
64       * Whether this service is enabled.
65       */
66      private volatile boolean enabled = false;
67  
68      private ArrayList theFetchTaskNames = new ArrayList();
69  
70      /***
71       * @see org.apache.avalon.framework.service.Serviceable#service( ServiceManager )
72       */
73      public void service(ServiceManager comp) throws ServiceException
74      {
75          m_manager = comp;
76      }
77  
78      /***
79       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
80       */
81      public void configure(Configuration conf) throws ConfigurationException
82      {
83          this.conf = conf;
84      }
85  
86      /***
87       * @see org.apache.avalon.framework.activity.Initializable#initialize()
88       */
89      public void initialize() throws Exception
90      {
91          enabled = conf.getAttributeAsBoolean("enabled", false);
92          if (enabled)
93          {
94              scheduler = (TimeScheduler) m_manager.lookup(TimeScheduler.ROLE);
95              Configuration[] fetchConfs = conf.getChildren("fetch");
96              for (int i = 0; i < fetchConfs.length; i++)
97              {
98                  FetchMail fetcher = new FetchMail();
99                  Configuration fetchConf = fetchConfs[i];
100                 String fetchTaskName = fetchConf.getAttribute("name");
101                 ContainerUtil.enableLogging(fetcher,getLogger().getChildLogger(fetchTaskName));
102                 ContainerUtil.service(fetcher,m_manager);
103                 ContainerUtil.configure(fetcher,fetchConf);
104                 Integer interval =
105                     new Integer(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 nameIterator = theFetchTaskNames.iterator();
134             while (nameIterator.hasNext())
135             {
136                 scheduler.removeTrigger((String) 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 }