//# 0 errors, 162 messages
//#
/*
    //#MailProvider.java:1:1: class: org.apache.roller.weblogger.business.MailProvider$ConfigurationType
    //#MailProvider.java:1:1: class: org.apache.roller.weblogger.business.MailProvider
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  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.  For additional information regarding
 * copyright in this work, please see the NOTICE file in the top level
 * directory of this distribution.
 */

package org.apache.roller.weblogger.business;

import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.business.startup.StartupException;
import org.apache.roller.weblogger.config.WebloggerConfig;


/**
 * Encapsulates Roller mail configuration, returns mail sessions.
 */
public class MailProvider {
    
    private static final Log log = LogFactory.getLog(MailProvider.class);
    //#MailProvider.java:40: method: org.apache.roller.weblogger.business.MailProvider.org.apache.roller.weblogger.business.MailProvider__static_init
    //#MailProvider.java:40: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.MailProvider
    //#    method: org.apache.roller.weblogger.business.MailProvider__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(org.apache.roller.weblogger.business.MailProvider__static_init): __Descendant_Table[org/apache/roller/weblogger/business/MailProvider]
    //#output(org.apache.roller.weblogger.business.MailProvider__static_init): __Dispatch_Table.getSession()Ljavax/mail/Session;
    //#output(org.apache.roller.weblogger.business.MailProvider__static_init): __Dispatch_Table.getTransport()Ljavax/mail/Transport;
    //#output(org.apache.roller.weblogger.business.MailProvider__static_init): log
    //#post(org.apache.roller.weblogger.business.MailProvider__static_init): __Descendant_Table[org/apache/roller/weblogger/business/MailProvider] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.business.MailProvider__static_init): __Dispatch_Table.getSession()Ljavax/mail/Session; == &getSession
    //#post(org.apache.roller.weblogger.business.MailProvider__static_init): __Dispatch_Table.getTransport()Ljavax/mail/Transport; == &getTransport
    //#post(org.apache.roller.weblogger.business.MailProvider__static_init): init'ed(log)
    //#MailProvider.java:40: end of method: org.apache.roller.weblogger.business.MailProvider.org.apache.roller.weblogger.business.MailProvider__static_init
    
    private enum ConfigurationType {JNDI_NAME, MAIL_PROPERTIES; }
    //#MailProvider.java:42: method: MailProvider$ConfigurationType[] org.apache.roller.weblogger.business.MailProvider$ConfigurationType.values()
    //#input(MailProvider$ConfigurationType[] values()): $VALUES
    //#input(MailProvider$ConfigurationType[] values()): $VALUES.length
    //#input(MailProvider$ConfigurationType[] values()): $VALUES[0..2]
    //#output(MailProvider$ConfigurationType[] values()): new MailProvider$ConfigurationType[](values#1) num objects
    //#output(MailProvider$ConfigurationType[] values()): return_value.length
    //#output(MailProvider$ConfigurationType[] values()): return_value[0..2]
    //#output(MailProvider$ConfigurationType[] values()): return_value
    //#new obj(MailProvider$ConfigurationType[] values()): new MailProvider$ConfigurationType[](values#1)
    //#pre[1] (MailProvider$ConfigurationType[] values()): (soft) init'ed($VALUES[0..2])
    //#post(MailProvider$ConfigurationType[] values()): return_value == &new MailProvider$ConfigurationType[](values#1)
    //#post(MailProvider$ConfigurationType[] values()): new MailProvider$ConfigurationType[](values#1) num objects == 1
    //#post(MailProvider$ConfigurationType[] values()): return_value.length == 2
    //#post(MailProvider$ConfigurationType[] values()): return_value[0..2] == One-of{$VALUES[0..2], undefined}
    //#MailProvider.java:42: end of method: MailProvider$ConfigurationType[] org.apache.roller.weblogger.business.MailProvider$ConfigurationType.values()
    //#MailProvider.java:42: method: MailProvider$ConfigurationType org.apache.roller.weblogger.business.MailProvider$ConfigurationType.valueOf(String)
    //#input(MailProvider$ConfigurationType valueOf(String)): __Descendant_Table[org/apache/roller/weblogger/business/MailProvider$ConfigurationType]
    //#input(MailProvider$ConfigurationType valueOf(String)): __Descendant_Table[others]
    //#input(MailProvider$ConfigurationType valueOf(String)): name
    //#output(MailProvider$ConfigurationType valueOf(String)): return_value
    //#presumption(MailProvider$ConfigurationType valueOf(String)): java.lang.Enum:valueOf(...).__Tag@42 == org/apache/roller/weblogger/business/MailProvider$ConfigurationType
    //#post(MailProvider$ConfigurationType valueOf(String)): init'ed(return_value)
    //#MailProvider.java:42: end of method: MailProvider$ConfigurationType org.apache.roller.weblogger.business.MailProvider$ConfigurationType.valueOf(String)
    //#MailProvider.java:42: method: void org.apache.roller.weblogger.business.MailProvider$ConfigurationType.org.apache.roller.weblogger.business.MailProvider$ConfigurationType(String, int)
    //#input(void org.apache.roller.weblogger.business.MailProvider$ConfigurationType(String, int)): Param_1
    //#input(void org.apache.roller.weblogger.business.MailProvider$ConfigurationType(String, int)): Param_2
    //#input(void org.apache.roller.weblogger.business.MailProvider$ConfigurationType(String, int)): this
    //#MailProvider.java:42: end of method: void org.apache.roller.weblogger.business.MailProvider$ConfigurationType.org.apache.roller.weblogger.business.MailProvider$ConfigurationType(String, int)
    //#MailProvider.java:42: method: org.apache.roller.weblogger.business.MailProvider$ConfigurationType.org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): $VALUES
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): JNDI_NAME
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): MAIL_PROPERTIES
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): __Descendant_Table[org/apache/roller/weblogger/business/MailProvider$ConfigurationType]
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#1) num objects
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): JNDI_NAME.__Tag
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#2) num objects
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): MAIL_PROPERTIES.__Tag
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): new MailProvider$ConfigurationType[](MailProvider$ConfigurationType__static_init#3) num objects
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): $VALUES.length
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): $VALUES[0]
    //#output(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): $VALUES[1]
    //#new obj(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#1)
    //#new obj(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#2)
    //#new obj(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): new MailProvider$ConfigurationType[](MailProvider$ConfigurationType__static_init#3)
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): $VALUES == &new MailProvider$ConfigurationType[](MailProvider$ConfigurationType__static_init#3)
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): JNDI_NAME == &new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#1)
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): $VALUES[0] == &new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#1)
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): MAIL_PROPERTIES == &new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#2)
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): $VALUES[1] == &new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#2)
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): __Descendant_Table[org/apache/roller/weblogger/business/MailProvider$ConfigurationType] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#1) num objects == 1
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#2) num objects == 1
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): new MailProvider$ConfigurationType[](MailProvider$ConfigurationType__static_init#3) num objects == 1
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): JNDI_NAME.__Tag == org/apache/roller/weblogger/business/MailProvider$ConfigurationType
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): MAIL_PROPERTIES.__Tag == org/apache/roller/weblogger/business/MailProvider$ConfigurationType
    //#post(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): $VALUES.length == 2
    //#unanalyzed(org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init): Effects-of-calling:java.lang.Enum
    //#MailProvider.java:42: end of method: org.apache.roller.weblogger.business.MailProvider$ConfigurationType.org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init
    
    private Session session = null;
    
    private ConfigurationType type = ConfigurationType.JNDI_NAME;
    
    private String jndiName = null;
    private String mailHostname = null;
    private int    mailPort = -1;
    private String mailUsername = null;
    private String mailPassword = null;

    
    public MailProvider() throws StartupException {
    //#MailProvider.java:55: method: void org.apache.roller.weblogger.business.MailProvider.org.apache.roller.weblogger.business.MailProvider()
    //#input(void org.apache.roller.weblogger.business.MailProvider()): ""._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): "="._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): "ERROR looking up mail-session with JNDI name: "._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): "Fetching property ["._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): "]"._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): "java:comp.env."._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): "mail.configurationType"._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): "mail.hostname"._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): "mail.jndi.name"._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): "mail.password"._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): "mail.port"._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): "mail.username"._tainted
    //#input(void org.apache.roller.weblogger.business.MailProvider()): __Descendant_Table[org/apache/roller/weblogger/business/MailProvider]
    //#input(void org.apache.roller.weblogger.business.MailProvider()): __Descendant_Table[others]
    //#input(void org.apache.roller.weblogger.business.MailProvider()): __Dispatch_Table.getTransport()Ljavax/mail/Transport;
    //#input(void org.apache.roller.weblogger.business.MailProvider()): log
    //#input(void org.apache.roller.weblogger.business.MailProvider()): org/apache/roller/weblogger/business/MailProvider$ConfigurationType.JNDI_NAME
    //#input(void org.apache.roller.weblogger.business.MailProvider()): org/apache/roller/weblogger/business/MailProvider$ConfigurationType.MAIL_PROPERTIES
    //#input(void org.apache.roller.weblogger.business.MailProvider()): org/apache/roller/weblogger/business/startup/StartupException.java.util.Collections.EMPTY_LIST
    //#input(void org.apache.roller.weblogger.business.MailProvider()): org/apache/roller/weblogger/config/WebloggerConfig.config
    //#input(void org.apache.roller.weblogger.business.MailProvider()): org/apache/roller/weblogger/config/WebloggerConfig.log
    //#input(void org.apache.roller.weblogger.business.MailProvider()): this
    //#input(void org.apache.roller.weblogger.business.MailProvider()): this.__Tag
    //#output(void org.apache.roller.weblogger.business.MailProvider()): this.jndiName
    //#output(void org.apache.roller.weblogger.business.MailProvider()): this.mailHostname
    //#output(void org.apache.roller.weblogger.business.MailProvider()): this.mailPassword
    //#output(void org.apache.roller.weblogger.business.MailProvider()): this.mailPort
    //#output(void org.apache.roller.weblogger.business.MailProvider()): this.mailUsername
    //#output(void org.apache.roller.weblogger.business.MailProvider()): this.session
    //#output(void org.apache.roller.weblogger.business.MailProvider()): this.type
    //#pre[1] (void org.apache.roller.weblogger.business.MailProvider()): org/apache/roller/weblogger/config/WebloggerConfig.config != null
    //#pre[2] (void org.apache.roller.weblogger.business.MailProvider()): org/apache/roller/weblogger/config/WebloggerConfig.log != null
    //#pre[4] (void org.apache.roller.weblogger.business.MailProvider()): this.__Tag == org/apache/roller/weblogger/business/MailProvider
    //#presumption(void org.apache.roller.weblogger.business.MailProvider()): javax.mail.Session:getDefaultInstance(...)@92 != null
    //#presumption(void org.apache.roller.weblogger.business.MailProvider()): javax.naming.Context:lookup(...)@79 != null
    //#presumption(void org.apache.roller.weblogger.business.MailProvider()): org.apache.commons.logging.LogFactory:getLog(...)@40 != null
    //#post(void org.apache.roller.weblogger.business.MailProvider()): init'ed(this.jndiName)
    //#post(void org.apache.roller.weblogger.business.MailProvider()): init'ed(this.mailHostname)
    //#post(void org.apache.roller.weblogger.business.MailProvider()): init'ed(this.mailPassword)
    //#post(void org.apache.roller.weblogger.business.MailProvider()): (soft) init'ed(this.mailPort)
    //#post(void org.apache.roller.weblogger.business.MailProvider()): init'ed(this.mailUsername)
    //#post(void org.apache.roller.weblogger.business.MailProvider()): this.session != null
    //#post(void org.apache.roller.weblogger.business.MailProvider()): this.type in Addr_Set{&org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init.new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#1),&org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init.new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#2)}
    //#unanalyzed(void org.apache.roller.weblogger.business.MailProvider()): Effects-of-calling:javax.mail.Session:getTransport
    //#unanalyzed(void org.apache.roller.weblogger.business.MailProvider()): Effects-of-calling:javax.mail.Transport:connect
    //#unanalyzed(void org.apache.roller.weblogger.business.MailProvider()): Effects-of-calling:java.lang.StringBuilder
    //#unanalyzed(void org.apache.roller.weblogger.business.MailProvider()): Effects-of-calling:java.lang.StringBuilder:append
    //#unanalyzed(void org.apache.roller.weblogger.business.MailProvider()): Effects-of-calling:java.lang.StringBuilder:toString
    //#unanalyzed(void org.apache.roller.weblogger.business.MailProvider()): Effects-of-calling:java.lang.String:trim
    //#unanalyzed(void org.apache.roller.weblogger.business.MailProvider()): Effects-of-calling:org.apache.commons.logging.Log:debug
    //#unanalyzed(void org.apache.roller.weblogger.business.MailProvider()): Effects-of-calling:java.util.Properties:getProperty
    //#unanalyzed(void org.apache.roller.weblogger.business.MailProvider()): Effects-of-calling:org.apache.roller.weblogger.WebloggerException
    //#unanalyzed(void org.apache.roller.weblogger.business.MailProvider()): Effects-of-calling:org.apache.roller.RollerException
    //#test_vector(void org.apache.roller.weblogger.business.MailProvider()): java.lang.String:equals(...)@58: {0}, {1}
        
        String connectionTypeString = WebloggerConfig.getProperty("mail.configurationType"); 
        if ("properties".equals(connectionTypeString)) {
            type = ConfigurationType.MAIL_PROPERTIES;
        }
        jndiName =     WebloggerConfig.getProperty("mail.jndi.name");
        mailHostname = WebloggerConfig.getProperty("mail.hostname");
        mailUsername = WebloggerConfig.getProperty("mail.username");
        mailPassword = WebloggerConfig.getProperty("mail.password");
        try {
            String portString = WebloggerConfig.getProperty("mail.port");
            if (portString != null) {
                mailPort = Integer.parseInt(portString);
            }
        } catch (Throwable t) {
            log.warn("mail server port not a valid integer, ignoring");
    //#MailProvider.java:71: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:warn(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.business.MailProvider
    //#    method: void org.apache.roller.weblogger.business.MailProvider()
    //#    unanalyzed callee: void org.apache.commons.logging.Log:warn(Object)
        }
        
        // init and connect now so we fail early
        if (type == ConfigurationType.JNDI_NAME) {            
            String name = "java:comp/env/" + jndiName;
            try {
                Context ctx = (Context) new InitialContext();
                session = (Session) ctx.lookup(name);
            } catch (NamingException ex) {
                throw new StartupException("ERROR looking up mail-session with JNDI name: " + name);
            }
        } else {
            Properties props = new Properties();
            props.put("mail.smtp.host", mailHostname);
            if (mailUsername != null && mailPassword != null) {
                props.put("mail.smtp.auth", "true");   
            }
            if (mailPort != -1) {
                props.put("mail.smtp.port", ""+mailPort);
            }
            session = Session.getDefaultInstance(props, null);
        }
        
        try {
            Transport transport = getTransport();
            transport.close();
        } catch (Throwable t) {
            throw new StartupException("ERROR connecting to mail server", t);
        }
        
    }
    //#MailProvider.java:102: end of method: void org.apache.roller.weblogger.business.MailProvider.org.apache.roller.weblogger.business.MailProvider()
    
    
    /**
     * Get a mail Session.
     */
    public Session getSession() {
        return session;
    //#MailProvider.java:109: method: Session org.apache.roller.weblogger.business.MailProvider.getSession()
    //#input(Session getSession()): this
    //#input(Session getSession()): this.session
    //#output(Session getSession()): return_value
    //#pre[2] (Session getSession()): init'ed(this.session)
    //#post(Session getSession()): return_value == this.session
    //#post(Session getSession()): init'ed(return_value)
    //#MailProvider.java:109: end of method: Session org.apache.roller.weblogger.business.MailProvider.getSession()
    }
    
    
    /**
     * Create and connect to transport, caller is responsible for closing transport.
     */
    public Transport getTransport() throws NoSuchProviderException, MessagingException {
        
        Transport transport = null;
    //#MailProvider.java:118: method: Transport org.apache.roller.weblogger.business.MailProvider.getTransport()
    //#MailProvider.java:118: Warning: unused assignment
    //#    unused assignment into transport
    //#    severity: SUPPRESSED
    //#    class: org.apache.roller.weblogger.business.MailProvider
    //#    method: Transport getTransport()
    //#    Attribs:  Uncertain
    //#input(Transport getTransport()): org/apache/roller/weblogger/business/MailProvider$ConfigurationType.MAIL_PROPERTIES
    //#input(Transport getTransport()): this
    //#input(Transport getTransport()): this.mailHostname
    //#input(Transport getTransport()): this.mailPassword
    //#input(Transport getTransport()): this.mailPort
    //#input(Transport getTransport()): this.mailUsername
    //#input(Transport getTransport()): this.session
    //#input(Transport getTransport()): this.type
    //#output(Transport getTransport()): return_value
    //#pre[6] (Transport getTransport()): this.session != null
    //#pre[7] (Transport getTransport()): init'ed(this.type)
    //#pre[2] (Transport getTransport()): (soft) init'ed(this.mailHostname)
    //#pre[3] (Transport getTransport()): (soft) init'ed(this.mailPassword)
    //#pre[4] (Transport getTransport()): (soft) init'ed(this.mailPort)
    //#pre[5] (Transport getTransport()): (soft) init'ed(this.mailUsername)
    //#presumption(Transport getTransport()): javax.mail.Session:getTransport(...)@122 != null
    //#presumption(Transport getTransport()): javax.mail.Session:getTransport(...)@132 != null
    //#post(Transport getTransport()): (soft) return_value != null
    //#test_vector(Transport getTransport()): this.mailPassword: Addr_Set{null}, Inverse{null}
    //#test_vector(Transport getTransport()): this.mailPort: {-1}, {-2_147_483_648..-2, 0..4_294_967_295}
    //#test_vector(Transport getTransport()): this.mailUsername: Addr_Set{null}, Inverse{null}
        
        if (type == ConfigurationType.MAIL_PROPERTIES) {
            // Configure transport ourselves using mail properties
            transport = session.getTransport("smtp"); 
            if (mailUsername != null && mailPassword != null && mailPort != -1) {
                transport.connect(mailHostname, mailPort, mailUsername, mailPassword); 
            } else if (mailUsername != null && mailPassword != null) {
                transport.connect(mailHostname, mailUsername, mailPassword); 
            } else {
                transport.connect();
            }
        } else {
            // Assume container set things up properly
            transport = session.getTransport(); 
            transport.connect();
        }
        
        return transport;
    //#MailProvider.java:136: end of method: Transport org.apache.roller.weblogger.business.MailProvider.getTransport()
    }
    
}
    //#MailProvider.java:: end of class: org.apache.roller.weblogger.business.MailProvider$ConfigurationType
    //#MailProvider.java:: end of class: org.apache.roller.weblogger.business.MailProvider
