1 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.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 43 public class FetchScheduler 44 extends AbstractLogEnabled 45 implements Serviceable, Configurable, Initializable, Disposable, FetchSchedulerMBean { 46 47 50 private Configuration conf; 51 52 55 private ServiceManager m_manager; 56 57 60 private TimeScheduler scheduler; 61 62 65 private volatile boolean enabled = false; 66 67 private ArrayList theFetchTaskNames = new ArrayList (); 68 69 72 public void service(ServiceManager comp) throws ServiceException 73 { 74 m_manager = comp; 75 } 76 77 80 public void configure(Configuration conf) throws ConfigurationException 81 { 82 this.conf = conf; 83 } 84 85 88 public void initialize() throws Exception 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 fetchTaskName = fetchConf.getAttribute("name"); 100 fetcher.enableLogging( 101 getLogger().getChildLogger(fetchTaskName)); 102 fetcher.service(m_manager); 103 fetcher.configure(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 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 147 public final boolean isEnabled() { 148 return enabled; 149 } 150 151 } 152 | Popular Tags |