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  
21   
22  package org.apache.james.fetchmail;
23  
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  
27  import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
28  import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
29  import org.apache.avalon.framework.activity.Disposable;
30  import org.apache.avalon.framework.activity.Initializable;
31  import org.apache.avalon.framework.configuration.Configurable;
32  import org.apache.avalon.framework.configuration.Configuration;
33  import org.apache.avalon.framework.configuration.ConfigurationException;
34  import org.apache.avalon.framework.container.ContainerUtil;
35  import org.apache.avalon.framework.logger.AbstractLogEnabled;
36  import org.apache.avalon.framework.service.ServiceException;
37  import org.apache.avalon.framework.service.ServiceManager;
38  import org.apache.avalon.framework.service.Serviceable;
39  
40  /**
41   *  A class to instantiate and schedule a set of mail fetching tasks
42   *
43   * $Id: FetchScheduler.java 521427 2007-03-22 19:57:00Z bago $
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      public void setScheduler(TimeScheduler scheduler) {
73          this.scheduler = scheduler;
74      }
75  
76      /**
77       * @see org.apache.avalon.framework.service.Serviceable#service( ServiceManager )
78       */
79      public void service(ServiceManager comp) throws ServiceException
80      {
81          m_manager = comp;
82      }
83  
84      /**
85       * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
86       */
87      public void configure(Configuration conf) throws ConfigurationException
88      {
89          this.conf = conf;
90      }
91  
92      /**
93       * @see org.apache.avalon.framework.activity.Initializable#initialize()
94       */
95      public void initialize() throws Exception
96      {
97          enabled = conf.getAttributeAsBoolean("enabled", false);
98          if (enabled)
99          {
100             TimeScheduler scheduler = (TimeScheduler) m_manager.lookup(TimeScheduler.ROLE);
101             setScheduler(scheduler);
102 
103             Configuration[] fetchConfs = conf.getChildren("fetch");
104             for (int i = 0; i < fetchConfs.length; i++)
105             {
106                 // read configuration
107                 Configuration fetchConf = fetchConfs[i];
108                 String fetchTaskName = fetchConf.getAttribute("name");
109                 Integer interval = new Integer(fetchConf.getChild("interval").getValue());
110 
111                 FetchMail fetcher = new FetchMail();
112                 
113                 // avalon specific initialization
114                 ContainerUtil.enableLogging(fetcher,getLogger().getChildLogger(fetchTaskName));
115                 ContainerUtil.service(fetcher,m_manager);
116                 ContainerUtil.configure(fetcher,fetchConf);
117 
118                 // initialize scheduling
119                 PeriodicTimeTrigger fetchTrigger =
120                         new PeriodicTimeTrigger(0, interval.intValue());
121                 scheduler.addTrigger(fetchTaskName, fetchTrigger, fetcher);
122                 theFetchTaskNames.add(fetchTaskName);
123             }
124 
125             if (getLogger().isInfoEnabled()) getLogger().info("FetchMail Started");
126             System.out.println("FetchMail Started");
127         }
128         else
129         {
130             if (getLogger().isInfoEnabled()) getLogger().info("FetchMail Disabled");
131             System.out.println("FetchMail Disabled");
132         }
133     }
134 
135     /**
136      * @see org.apache.avalon.framework.activity.Disposable#dispose()
137      */
138     public void dispose()
139     {
140         if (enabled)
141         {
142             getLogger().info("FetchMail dispose...");
143             Iterator nameIterator = theFetchTaskNames.iterator();
144             while (nameIterator.hasNext())
145             {
146                 scheduler.removeTrigger((String) nameIterator.next());
147             }
148             getLogger().info("FetchMail ...dispose end");
149         }
150     }
151     
152     /**
153      * Describes whether this service is enabled by configuration.
154      *
155      * @return is the service enabled.
156      */
157     public final boolean isEnabled() {
158         return enabled;
159     }
160     
161 }