📄 james.java
字号:
/**************************************************************** * Licensed to the Apache Software Foundation (ASF) under one * * or more contributor license agreements. See the NOTICE file * * distributed with this work for additional information * * regarding copyright ownership. The ASF licenses this file * * to you 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;import org.apache.avalon.cornerstone.services.store.Store;import org.apache.avalon.framework.activity.Initializable;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.configuration.DefaultConfiguration;import org.apache.avalon.framework.container.ContainerUtil;import org.apache.avalon.framework.context.Context;import org.apache.avalon.framework.context.Contextualizable;import org.apache.avalon.framework.context.DefaultContext;import org.apache.avalon.framework.logger.AbstractLogEnabled;import org.apache.avalon.framework.logger.Logger;import org.apache.avalon.framework.service.DefaultServiceManager;import org.apache.avalon.framework.service.ServiceException;import org.apache.avalon.framework.service.ServiceManager;import org.apache.avalon.framework.service.Serviceable;import org.apache.commons.collections.ReferenceMap;import org.apache.james.context.AvalonContextUtilities;import org.apache.james.core.MailHeaders;import org.apache.james.core.MailImpl;import org.apache.james.core.MailetConfigImpl;import org.apache.james.services.DNSServer;import org.apache.james.services.MailRepository;import org.apache.james.services.MailServer;import org.apache.james.services.SpoolRepository;import org.apache.james.services.UsersRepository;import org.apache.james.services.UsersStore;import org.apache.james.transport.mailets.LocalDelivery;import org.apache.james.userrepository.DefaultJamesUser;import org.apache.mailet.Mail;import org.apache.mailet.MailAddress;import org.apache.mailet.Mailet;import org.apache.mailet.MailetContext;import org.apache.mailet.RFC2822Headers;import javax.mail.Address;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.io.ByteArrayInputStream;import java.io.InputStream;import java.io.SequenceInputStream;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Collection;import java.util.Date;import java.util.Enumeration;import java.util.HashSet;import java.util.Hashtable;import java.util.Iterator;import java.util.Locale;import java.util.Map;import java.util.Vector;/** * Core class for JAMES. Provides three primary services: * <br> 1) Instantiates resources, such as user repository, and protocol * handlers * <br> 2) Handles interactions between components * <br> 3) Provides container services for Mailets * * * @version This is $Revision: 494012 $ */public class James extends AbstractLogEnabled implements Contextualizable, Serviceable, Configurable, JamesMBean, Initializable, MailServer, MailetContext { /** * The software name and version */ private final static String SOFTWARE_NAME_VERSION = Constants.SOFTWARE_NAME + " " + Constants.SOFTWARE_VERSION; /** * The component manager used both internally by James and by Mailets. */ private DefaultServiceManager compMgr; //Components shared /** * TODO: Investigate what this is supposed to do. Looks like it * was supposed to be the Mailet context. */ private DefaultContext context; /** * The top level configuration object for this server. */ private Configuration conf; /** * The logger used by the Mailet API. */ private Logger mailetLogger = null; /** * The mail store containing the inbox repository and the spool. */ private Store store; /** * The store containing the local user repository. */ private UsersStore usersStore; /** * The spool used for processing mail handled by this server. */ private SpoolRepository spool; /** * The root URL used to get mailboxes from the repository */ private String inboxRootURL; /** * The user repository for this mail server. Contains all the users with inboxes * on this server. */ private UsersRepository localusers; /** * The collection of domain/server names for which this instance of James * will receive and process mail. */ private Collection serverNames; /** * Whether to ignore case when looking up user names on this server */ private boolean ignoreCase; /** * The number of mails generated. Access needs to be synchronized for * thread safety and to ensure that all threads see the latest value. */ private static long count; /** * The address of the postmaster for this server */ private MailAddress postmaster; /** * A map used to store mailboxes and reduce the cost of lookup of individual * mailboxes. */ private Map mailboxes; //Not to be shared! /** * A hash table of server attributes * These are the MailetContext attributes */ private Hashtable attributes = new Hashtable(); /** * The Avalon context used by the instance */ protected Context myContext; /** * Currently used by storeMail to avoid code duplication (we moved store logic to that mailet). * TODO We should remove this and its initialization when we remove storeMail method. */ protected Mailet localDeliveryMailet; /** * @see org.apache.avalon.framework.context.Contextualizable#contextualize(Context) */ public void contextualize(final Context context) { this.myContext = context; } /** * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager) */ public void service(ServiceManager comp) { compMgr = new DefaultServiceManager(comp); mailboxes = new ReferenceMap(); } /** * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration) */ public void configure(Configuration conf) { this.conf = conf; } /** * @see org.apache.avalon.framework.activity.Initializable#initialize() */ public void initialize() throws Exception { getLogger().info("JAMES init..."); // TODO: This should retrieve a more specific named thread pool from // Context that is set up in server.xml try { store = (Store) compMgr.lookup( Store.ROLE ); } catch (Exception e) { if (getLogger().isWarnEnabled()) { getLogger().warn("Can't get Store: " + e); } } if (getLogger().isDebugEnabled()) { getLogger().debug("Using Store: " + store.toString()); } try { spool = (SpoolRepository) compMgr.lookup( SpoolRepository.ROLE ); } catch (Exception e) { if (getLogger().isWarnEnabled()) { getLogger().warn("Can't get spoolRepository: " + e); } } if (getLogger().isDebugEnabled()) { getLogger().debug("Using SpoolRepository: " + spool.toString()); } try { usersStore = (UsersStore) compMgr.lookup( UsersStore.ROLE ); } catch (Exception e) { if (getLogger().isWarnEnabled()) { getLogger().warn("Can't get Store: " + e); } } if (getLogger().isDebugEnabled()) { getLogger().debug("Using UsersStore: " + usersStore.toString()); } String hostName = null; try { hostName = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException ue) { hostName = "localhost"; } context = new DefaultContext(); context.put("HostName", hostName); getLogger().info("Local host is: " + hostName); // Get the domains and hosts served by this instance serverNames = new HashSet(); Configuration serverConf = conf.getChild("servernames"); if (serverConf.getAttributeAsBoolean("autodetect") && (!hostName.equals("localhost"))) { serverNames.add(hostName.toLowerCase(Locale.US)); } final Configuration[] serverNameConfs = conf.getChild( "servernames" ).getChildren( "servername" ); for ( int i = 0; i < serverNameConfs.length; i++ ) { serverNames.add( serverNameConfs[i].getValue().toLowerCase(Locale.US)); if (serverConf.getAttributeAsBoolean("autodetectIP", true)) { try { /* This adds the IP address(es) for each host to support * support <user@address-literal> - RFC 2821, sec 4.1.3. * It might be proper to use the actual IP addresses * available on this server, but we can't do that * without NetworkInterface from JDK 1.4. Because of * Virtual Hosting considerations, we may need to modify * this to keep hostname and IP associated, rather than * just both in the set. */ InetAddress[] addrs = InetAddress.getAllByName(serverNameConfs[i].getValue()); for (int j = 0; j < addrs.length ; j++) { serverNames.add(addrs[j].getHostAddress()); } } catch(Exception genericException) { getLogger().error("Cannot get IP address(es) for " + serverNameConfs[i].getValue()); } } } if (serverNames.isEmpty()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -