/*
 * 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.pop3server;

import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.james.services.MailServer;
import org.apache.james.services.UsersRepository;
import org.apache.james.services.UsersStore;

/**
 * Input to POP3 Hander. Holds reference to POP3 Configuration and
 * Components needed by POP3 Handler.
 *
 * There is one POP3HandlerParams per POP3Server and one POP3Config
 * for n POP3Handler. POP3Handler is a very lightweight object.
 */
class POP3HandlerParams {
    /**
     * The default timeout for the connection
     */
    private static int DEFAULT_TIMEOUT = 1800000;

    // config params
    private final int lengthReset;
    private final int timeout;
    private final String helloName;

    // needed components.
    private final MailServer mailServer;
    private final UsersRepository users;
    private final TimeScheduler scheduler;

    POP3HandlerParams(Configuration configuration,ComponentManager componentManager) 
        throws ConfigurationException, ComponentException
    {
        // figure out the configuration.
        
        this.lengthReset = configuration.getChild("lengthReset")
            .getValueAsInteger(20000);
        this.timeout = configuration.getChild( "connectiontimeout" )
            .getValueAsInteger( DEFAULT_TIMEOUT );

        // figure out helloName (greeting)
        String hostName = null;

        try {
            hostName = InetAddress.getLocalHost().getHostName();
        } catch  (UnknownHostException ue) {
            // Default to localhost if we can't get the local host name.
            hostName = "localhost";
        }
        Configuration helloConf = configuration.getChild("helloName");
        boolean autodetect = helloConf.getAttributeAsBoolean("autodetect", true);

        this.helloName = autodetect ? hostName : helloConf.getValue("localhost");

        // figure out the needed components
        this.mailServer = (MailServer)componentManager.
            lookup( "org.apache.james.services.MailServer" );
        UsersStore usersStore = (UsersStore)componentManager.
            lookup( "org.apache.james.services.UsersStore" );
        this.users = usersStore.getRepository("LocalUsers");
        this.scheduler = (TimeScheduler)componentManager.
            lookup( "org.apache.avalon.cornerstone.services.scheduler.TimeScheduler" );
    }

    /**
     * Get the value of scheduler.
     * @return value of scheduler.
     */
    public TimeScheduler getScheduler() {
        return scheduler;
    }
    
    /**
     * Get the value of users.
     * @return value of users.
     */
    public UsersRepository getUsers() {
        return users;
    }
    
    /**
     * Get the value of lengthReset.
     * @return value of lengthReset.
     */
    public int getLengthReset() {
        return lengthReset;
    }
    /**
     * Get the value of timeout.
     * @return value of timeout.
     */
    public int getTimeout() {
        return timeout;
    }
    
    /**
     * Get the value of helloName.
     * @return value of helloName.
     */
    public String getHelloName() {
        return helloName;
    }
    
    /**
     * Get the value of mailServer.
     * @return value of mailServer.
     */
    public MailServer getMailServer() {
        return mailServer;
    }
}

