001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.fetchmail;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022:
023: import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
024: import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
025: import org.apache.avalon.framework.activity.Disposable;
026: import org.apache.avalon.framework.activity.Initializable;
027: import org.apache.avalon.framework.configuration.Configurable;
028: import org.apache.avalon.framework.configuration.Configuration;
029: import org.apache.avalon.framework.configuration.ConfigurationException;
030: import org.apache.avalon.framework.container.ContainerUtil;
031: import org.apache.avalon.framework.logger.AbstractLogEnabled;
032: import org.apache.avalon.framework.service.ServiceException;
033: import org.apache.avalon.framework.service.ServiceManager;
034: import org.apache.avalon.framework.service.Serviceable;
035:
036: /**
037: * A class to instantiate and schedule a set of mail fetching tasks
038: *
039: * $Id: FetchScheduler.java 494012 2007-01-08 10:23:58Z norman $
040: *
041: * @see org.apache.james.fetchmail.FetchMailOriginal#configure(Configuration) FetchMailOriginal
042: *
043: */
044: public class FetchScheduler extends AbstractLogEnabled implements
045: Serviceable, Configurable, Initializable, Disposable,
046: FetchSchedulerMBean {
047:
048: /**
049: * Configuration object for this service
050: */
051: private Configuration conf;
052:
053: /**
054: * The service manager that allows access to the system services
055: */
056: private ServiceManager m_manager;
057:
058: /**
059: * The scheduler service that is used to trigger fetch tasks.
060: */
061: private TimeScheduler scheduler;
062:
063: /**
064: * Whether this service is enabled.
065: */
066: private volatile boolean enabled = false;
067:
068: private ArrayList theFetchTaskNames = new ArrayList();
069:
070: /**
071: * @see org.apache.avalon.framework.service.Serviceable#service( ServiceManager )
072: */
073: public void service(ServiceManager comp) throws ServiceException {
074: m_manager = comp;
075: }
076:
077: /**
078: * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
079: */
080: public void configure(Configuration conf)
081: throws ConfigurationException {
082: this .conf = conf;
083: }
084:
085: /**
086: * @see org.apache.avalon.framework.activity.Initializable#initialize()
087: */
088: public void initialize() throws Exception {
089: enabled = conf.getAttributeAsBoolean("enabled", false);
090: if (enabled) {
091: scheduler = (TimeScheduler) m_manager
092: .lookup(TimeScheduler.ROLE);
093: Configuration[] fetchConfs = conf.getChildren("fetch");
094: for (int i = 0; i < fetchConfs.length; i++) {
095: FetchMail fetcher = new FetchMail();
096: Configuration fetchConf = fetchConfs[i];
097: String fetchTaskName = fetchConf.getAttribute("name");
098: ContainerUtil.enableLogging(fetcher, getLogger()
099: .getChildLogger(fetchTaskName));
100: ContainerUtil.service(fetcher, m_manager);
101: ContainerUtil.configure(fetcher, fetchConf);
102: Integer interval = new Integer(fetchConf.getChild(
103: "interval").getValue());
104: PeriodicTimeTrigger fetchTrigger = new PeriodicTimeTrigger(
105: 0, interval.intValue());
106:
107: scheduler.addTrigger(fetchTaskName, fetchTrigger,
108: fetcher);
109: theFetchTaskNames.add(fetchTaskName);
110: }
111:
112: if (getLogger().isInfoEnabled())
113: getLogger().info("FetchMail Started");
114: System.out.println("FetchMail Started");
115: } else {
116: if (getLogger().isInfoEnabled())
117: getLogger().info("FetchMail Disabled");
118: System.out.println("FetchMail Disabled");
119: }
120: }
121:
122: /**
123: * @see org.apache.avalon.framework.activity.Disposable#dispose()
124: */
125: public void dispose() {
126: if (enabled) {
127: getLogger().info("FetchMail dispose...");
128: Iterator nameIterator = theFetchTaskNames.iterator();
129: while (nameIterator.hasNext()) {
130: scheduler.removeTrigger((String) nameIterator.next());
131: }
132: getLogger().info("FetchMail ...dispose end");
133: }
134: }
135:
136: /**
137: * Describes whether this service is enabled by configuration.
138: *
139: * @return is the service enabled.
140: */
141: public final boolean isEnabled() {
142: return enabled;
143: }
144:
145: }
|