View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.core.mail;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.config.property.ConfigContext;
20  import org.springframework.beans.factory.config.AbstractFactoryBean;
21  import org.springframework.mail.javamail.JavaMailSenderImpl;
22  
23  import javax.mail.Authenticator;
24  import javax.mail.PasswordAuthentication;
25  import javax.mail.Session;
26  import java.util.Properties;
27  
28  /**
29   * A factory bean which reads mail-related properties from the Configuration system and
30   * generates a Spring Java Mail Sender instance for use by services that send e-mail.
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   *
34   */
35  @SuppressWarnings("unchecked")
36  public class MailSenderFactoryBean extends AbstractFactoryBean {
37  
38  	protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MailSenderFactoryBean.class);
39  	
40      private static final String MAIL_PREFIX = "mail";
41      private static final String USERNAME_PROPERTY = "mail.smtp.username";
42      private static final String PASSWORD_PROPERTY = "mail.smtp.password";
43      private static final String HOST_PROPERTY = "mail.smtp.host";
44      private static final String PORT_PROPERTY = "mail.smtp.port";
45      private static final String PROTOCOL_PROPERTY = "mail.transport.protocol";
46      private Session mailSession;
47      
48      @Override
49  	protected  Object createInstance() throws Exception {
50  	    // Retrieve "mail.*" properties from the configuration system and construct a Properties object
51  		Properties properties = new Properties();
52  		Properties configProps = ConfigContext.getCurrentContextConfig().getProperties();
53  		LOG.debug("createInstance(): collecting mail properties.");
54  		for (Object keyObj : configProps.keySet()) {
55  		    if (keyObj instanceof String) {
56  		    	String key = (String)keyObj;
57  		    	if (key.startsWith(MAIL_PREFIX)){
58  		    		properties.put(key, configProps.get(key));
59  		    	}
60  		    }
61  		}
62  		
63  		// Construct an appropriate Java Mail Session
64  		// If username and password properties are found, construct a Session with SMTP authentication
65  		String username = properties.getProperty(USERNAME_PROPERTY);
66  		String password = properties.getProperty(PASSWORD_PROPERTY);
67  		if (username != null && password != null) {
68  			mailSession = Session.getInstance(properties, new SimpleAuthenticator(username, password));
69  			LOG.info("createInstance(): Initializing mail session using SMTP authentication.");
70  		} else {
71  			mailSession = Session.getInstance(properties);
72  			LOG.info("createInstance(): Initializing mail session. No SMTP authentication.");
73  		}
74  		
75  		// Construct and return a Spring Java Mail Sender
76  		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
77  		LOG.debug("createInstance(): setting SMTP host.");
78  		mailSender.setHost(properties.getProperty(HOST_PROPERTY));
79  		if (properties.getProperty(PORT_PROPERTY) != null) {
80  			LOG.debug("createInstance(): setting SMTP port.");
81  			int smtpPort = Integer.parseInt(properties.getProperty(PORT_PROPERTY).trim());
82  			mailSender.setPort(smtpPort);
83  		}
84          String protocol = properties.getProperty(PROTOCOL_PROPERTY);
85          if (StringUtils.isNotBlank(protocol)) {
86              LOG.debug("createInstance(): setting mail transport protocol = " + protocol);
87              mailSender.setProtocol(protocol);
88          }
89  		mailSender.setSession(mailSession);
90  		
91  		LOG.info("createInstance(): Mail Sender Factory Bean initialized.");
92  		return mailSender;
93      }
94      
95      private class SimpleAuthenticator extends Authenticator {
96      	
97      	private final PasswordAuthentication passwordAuthentication;
98  
99          private SimpleAuthenticator(String username, String password) {
100         	this.passwordAuthentication = new PasswordAuthentication(username, password);
101         }
102 
103         public PasswordAuthentication getPasswordAuthentication() {
104             return passwordAuthentication;
105         }
106     }
107     
108     @SuppressWarnings("unchecked")
109 	@Override
110     public Class getObjectType() {
111 	return JavaMailSenderImpl.class;
112     }
113 
114 }