KickJava   Java API By Example, From Geeks To Geeks.

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


1 /***********************************************************************
2  * Copyright (c) 2000-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.fetchpop;
19 import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
20 import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
21 import org.apache.avalon.framework.activity.Disposable;
22 import org.apache.avalon.framework.activity.Initializable;
23 import org.apache.avalon.framework.component.Component;
24 import org.apache.avalon.framework.component.ComponentException;
25 import org.apache.avalon.framework.component.ComponentManager;
26 import org.apache.avalon.framework.component.Composable;
27 import org.apache.avalon.framework.component.DefaultComponentManager;
28 import org.apache.avalon.framework.configuration.Configurable;
29 import org.apache.avalon.framework.configuration.Configuration;
30 import org.apache.avalon.framework.configuration.ConfigurationException;
31 import org.apache.avalon.framework.logger.AbstractLogEnabled;
32 import org.apache.james.services.MailServer;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 /**
38  * A class to instantiate and schedule a set of POP mail fetching tasks<br>
39  * <br>$Id: FetchScheduler.java,v 1.4.4.4 2004/03/15 03:54:16 noel Exp $
40  * @see org.apache.james.fetchpop.FetchPOP#configure(Configuration) FetchPOP
41  *
42  */

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

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

55     private ComponentManager compMgr;
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.component.Composable#compose(ComponentManager)
71      */

72     public void compose(ComponentManager comp) throws ComponentException {
73         compMgr = comp;
74     }
75
76     /**
77      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
78      */

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

86     public void initialize() throws Exception JavaDoc {
87         enabled = conf.getAttributeAsBoolean("enabled", false);
88         if (enabled) {
89             scheduler = (TimeScheduler) compMgr.lookup(TimeScheduler.ROLE);
90             Configuration[] fetchConfs = conf.getChildren("fetch");
91             for (int i = 0; i < fetchConfs.length; i++) {
92                 FetchPOP fp = new FetchPOP();
93                 Configuration fetchConf = fetchConfs[i];
94                 String JavaDoc fetchTaskName = fetchConf.getAttribute("name");
95                 fp.enableLogging(getLogger().getChildLogger(fetchTaskName));
96                 fp.compose(compMgr);
97                 fp.configure(fetchConf);
98                 Integer JavaDoc interval = new Integer JavaDoc(fetchConf.getChild("interval").getValue());
99                 PeriodicTimeTrigger fetchTrigger = new PeriodicTimeTrigger(0, interval.intValue());
100                 scheduler.addTrigger(fetchTaskName, fetchTrigger, fp);
101                 theFetchTaskNames.add(fetchTaskName);
102             }
103             getLogger().info("Fetch POP Started");
104             System.out.println("Fetch POP Started ");
105         } else {
106             getLogger().info("Fetch POP Disabled");
107             System.out.println("Fetch POP Disabled");
108         }
109     }
110
111     /**
112      * @see org.apache.avalon.framework.activity.Disposable#dispose()
113      */

114     public void dispose() {
115         if (enabled) {
116             getLogger().info( "Fetch POP dispose..." );
117             Iterator JavaDoc nameIterator = theFetchTaskNames.iterator();
118             while (nameIterator.hasNext()) {
119                 scheduler.removeTrigger((String JavaDoc)nameIterator.next());
120             }
121
122             getLogger().info( "Fetch POP ...dispose end" );
123         }
124     }
125
126     /**
127      * Describes whether this service is enabled by configuration.
128      *
129      * @return is the service enabled.
130      */

131     public final boolean isEnabled() {
132         return enabled;
133     }
134
135 }
136
Popular Tags