📄 jamesspoolmanager.java
字号:
/*********************************************************************** * Copyright (c) 2000-2004 The Apache Software Foundation. * * All rights reserved. * * ------------------------------------------------------------------- * * Licensed under the Apache License, Version 2.0 (the "License"); you * * may not use this file except in compliance with the License. You * * may obtain a copy of the License at: * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * * implied. See the License for the specific language governing * * permissions and limitations under the License. * ***********************************************************************/package org.apache.james.transport;import org.apache.avalon.cornerstone.services.threads.ThreadManager;//import org.apache.avalon.excalibur.thread.ThreadPool;import org.apache.avalon.framework.activity.Disposable;import org.apache.avalon.framework.activity.Initializable;import org.apache.avalon.framework.component.ComponentException;import org.apache.avalon.framework.component.ComponentManager;import org.apache.avalon.framework.component.Composable;import org.apache.avalon.framework.component.DefaultComponentManager;import org.apache.avalon.framework.component.Component;import org.apache.avalon.framework.configuration.Configurable;import org.apache.avalon.framework.configuration.Configuration;import org.apache.avalon.framework.configuration.ConfigurationException;import org.apache.avalon.framework.logger.AbstractLogEnabled;import org.apache.avalon.framework.context.Context;import org.apache.avalon.framework.context.Contextualizable;import org.apache.james.core.MailImpl;import org.apache.james.services.MailStore;import org.apache.james.services.SpoolRepository;import org.apache.mailet.*;import javax.mail.MessagingException;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;/** * Manages the mail spool. This class is responsible for retrieving * messages from the spool, directing messages to the appropriate * processor, and removing them from the spool when processing is * complete. * * @version CVS $Revision: 1.20.4.15 $ $Date: 2004/04/14 04:36:15 $ */public class JamesSpoolManager extends AbstractLogEnabled implements Composable, Configurable, Initializable, Runnable, Disposable, Component, Contextualizable { private Context context; /** * Whether 'deep debugging' is turned on. */ private final static boolean DEEP_DEBUG = false; /** * System component manager */ private DefaultComponentManager compMgr; /** * The configuration object used by this spool manager. */ private Configuration conf; private SpoolRepository spool; private MailetContext mailetContext; /** * The map of processor names to processors */ private HashMap processors; /** * The number of threads used to move mail through the spool. */ private int numThreads; /** * The ThreadPool containing worker threads. * * This used to be used, but for threads that lived the entire * lifespan of the application. Currently commented out. In * the future, we could use a thread pool to run short-lived * workers, so that we have a smaller number of readers that * accept a message from the spool, and dispatch to a pool of * worker threads that process the message. */ // private ThreadPool workerPool; /** * The ThreadManager from which the thread pool is obtained. */ // private ThreadManager threadManager; /** * Number of active threads */ private int numActive; /** * Spool threads are active */ private boolean active; /** * Spool threads */ private Collection spoolThreads; /** * @see org.apache.avalon.framework.component.Composable#compose(ComponentManager) */ public void compose(ComponentManager comp) throws ComponentException { // threadManager = (ThreadManager)comp.lookup( ThreadManager.ROLE ); compMgr = new DefaultComponentManager(comp); } /** * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration) */ public void configure(Configuration conf) throws ConfigurationException { this.conf = conf; numThreads = conf.getChild("threads").getValueAsInteger(1); } /** * @see org.apache.avalon.framework.activity.Initializable#initialize() */ public void initialize() throws Exception { getLogger().info("JamesSpoolManager init..."); // workerPool = threadManager.getThreadPool( "default" ); MailStore mailstore = (MailStore) compMgr.lookup("org.apache.james.services.MailStore"); spool = mailstore.getInboundSpool(); if (null == spool) { String exceptionMessage = "The mailstore's inbound spool is null. The mailstore is misconfigured"; if (getLogger().isErrorEnabled()) { getLogger().error( exceptionMessage ); } throw new ConfigurationException(exceptionMessage); } if ((DEEP_DEBUG) && (getLogger().isDebugEnabled())) { getLogger().debug("Got spool"); } mailetContext = (MailetContext) compMgr.lookup("org.apache.mailet.MailetContext"); MailetLoader mailetLoader = new MailetLoader(); MatchLoader matchLoader = new MatchLoader(); try { mailetLoader.setLogger(getLogger()); matchLoader.setLogger(getLogger()); mailetLoader.contextualize(context); matchLoader.contextualize(context); mailetLoader.configure(conf.getChild("mailetpackages")); matchLoader.configure(conf.getChild("matcherpackages")); compMgr.put(Resources.MAILET_LOADER, mailetLoader); compMgr.put(Resources.MATCH_LOADER, matchLoader); } catch (ConfigurationException ce) { final String message = "Unable to configure mailet/matcher Loaders: " + ce.getMessage(); if (getLogger().isErrorEnabled()) { getLogger().error( message, ce ); } throw new RuntimeException( message ); } //A processor is a Collection of processors = new HashMap(); final Configuration[] processorConfs = conf.getChildren( "processor" ); for ( int i = 0; i < processorConfs.length; i++ ) { Configuration processorConf = processorConfs[i]; String processorName = processorConf.getAttribute("name"); try { LinearProcessor processor = new LinearProcessor(); setupLogger(processor, processorName); processor.setSpool(spool); processor.initialize(); processors.put(processorName, processor); // If this is the root processor, add the PostmasterAlias // mailet silently to the top if (processorName.equals("root")) { Matcher matcher = matchLoader.getMatcher("All", mailetContext); Mailet mailet = mailetLoader.getMailet("PostmasterAlias", mailetContext, null); processor.add(matcher, mailet); } final Configuration[] mailetConfs = processorConf.getChildren( "mailet" ); // Loop through the mailet configuration, load // all of the matcher and mailets, and add // them to the processor. for ( int j = 0; j < mailetConfs.length; j++ ) { Configuration c = mailetConfs[j]; String mailetClassName = c.getAttribute("class"); String matcherName = c.getAttribute("match"); Mailet mailet = null; Matcher matcher = null; try { matcher = matchLoader.getMatcher(matcherName, mailetContext); //The matcher itself should log that it's been inited. if (getLogger().isInfoEnabled()) { StringBuffer infoBuffer = new StringBuffer(64) .append("Matcher ") .append(matcherName) .append(" instantiated."); getLogger().info(infoBuffer.toString()); } } catch (MessagingException ex) { // **** Do better job printing out exception if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(256) .append("Unable to init matcher ") .append(matcherName) .append(": ") .append(ex.toString()); getLogger().error( errorBuffer.toString(), ex ); if (ex.getNextException() != null) { getLogger().error( "Caused by nested exception: ", ex.getNextException()); } } System.err.println("Unable to init matcher " + matcherName); System.err.println("Check spool manager logs for more details."); //System.exit(1); throw ex; } try { mailet = mailetLoader.getMailet(mailetClassName, mailetContext, c); if (getLogger().isInfoEnabled()) { StringBuffer infoBuffer = new StringBuffer(64) .append("Mailet ") .append(mailetClassName) .append(" instantiated."); getLogger().info(infoBuffer.toString()); } } catch (MessagingException ex) { // **** Do better job printing out exception if (getLogger().isErrorEnabled()) { StringBuffer errorBuffer = new StringBuffer(256) .append("Unable to init mailet ") .append(mailetClassName) .append(": ") .append(ex.toString()); getLogger().error( errorBuffer.toString(), ex );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -