Package org.apache.james.transport

Source Code of org.apache.james.transport.MailetLoader

/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.james.transport;

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.james.core.MailetConfigImpl;
import org.apache.mailet.Mailet;
import org.apache.mailet.MailetContext;
import org.apache.mailet.MailetException;

import javax.mail.MessagingException;
import java.util.Vector;

/**
* @author Serge Knystautas <sergek@lokitech.com>
* @author Federico Barbieri <scoobie@systemy.it>
*/
public class MailetLoader implements Component, Configurable {

    private Configuration conf;
    private Vector mailetPackages;

    public void configure(Configuration conf) throws ConfigurationException {
        mailetPackages = new Vector();
        mailetPackages.addElement("");
        final Configuration[] pkgConfs = conf.getChildren( "mailetpackage" );
        for ( int i = 0; i < pkgConfs.length; i++ )
        {
            Configuration c = pkgConfs[i];
            String packageName = c.getValue();
            if (!packageName.endsWith(".")) {
                packageName += ".";
            }
            mailetPackages.addElement(packageName);
        }
    }

    public Mailet getMailet(String mailetName, MailetContext context, Configuration configuration)
        throws MessagingException {
        try {
            for (int i = 0; i < mailetPackages.size(); i++) {
                String className = (String)mailetPackages.elementAt(i) + mailetName;
                try {
                    MailetConfigImpl configImpl = new MailetConfigImpl();
                    configImpl.setMailetName(mailetName);
                    configImpl.setConfiguration(configuration);
                    configImpl.setMailetContext(context);

                    Mailet mailet = (Mailet) Class.forName(className).newInstance();
                    mailet.init(configImpl);
                    return mailet;
                } catch (ClassNotFoundException cnfe) {
                    //do this so we loop through all the packages
                }
            }
            throw new ClassNotFoundException("Requested mailet not found: " + mailetName + ".  looked in " + mailetPackages.toString());
        } catch (MessagingException me) {
            throw me;
        } catch (Exception e) {
            throw new MailetException("Could not load mailet (" + mailetName + ")", e);
        }
    }
}
TOP

Related Classes of org.apache.james.transport.MailetLoader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.