View Javadoc

1   package org.kuali.ole.deliver.batch;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.deliver.loan.LoanProcessor;
5   import org.kuali.rice.core.api.config.property.ConfigContext;
6   import org.kuali.rice.core.mail.MailSenderFactoryBean;
7   import org.springframework.beans.factory.config.AbstractFactoryBean;
8   import org.springframework.mail.javamail.JavaMailSenderImpl;
9   
10  import javax.mail.Authenticator;
11  import javax.mail.PasswordAuthentication;
12  import javax.mail.Session;
13  import java.util.Properties;
14  
15  /**
16   * Created with IntelliJ IDEA.
17   * User: maheswarang
18   * Date: 4/23/13
19   * Time: 6:49 PM
20   * To change this template use File | Settings | File Templates.
21   */
22  public class OleMailSenderFactoryBean extends AbstractFactoryBean {
23      protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleMailSenderFactoryBean.class);
24      private static final String MAIL_PREFIX = "mail";
25      private static final String USERNAME_PROPERTY = "mail.smtp.username";
26      private static final String PASSWORD_PROPERTY = "mail.smtp.password";
27      private static final String HOST_PROPERTY = "mail.smtp.host";
28      private static final String PORT_PROPERTY = "mail.smtp.port";
29      private static final String PROTOCOL_PROPERTY = "mail.transport.protocol";
30      private Session mailSession;
31      private String host;
32      private String port;
33      private String userName;
34      private String password;
35      private LoanProcessor loanProcessor = new LoanProcessor();
36  
37      public LoanProcessor getLoanProcessor() {
38          return loanProcessor;
39      }
40  
41      public void setLoanProcessor(LoanProcessor loanProcessor) {
42          this.loanProcessor = loanProcessor;
43      }
44  
45      public String getHost() {
46          return host;
47      }
48  
49      public void setHost(String host) {
50          this.host = host;
51      }
52  
53      public String getPort() {
54          return port;
55      }
56  
57      public void setPort(String port) {
58          this.port = port;
59      }
60  
61      public String getUserName() {
62          return userName;
63      }
64  
65      public void setUserName(String userName) {
66          this.userName = userName;
67      }
68  
69      public String getPassword() {
70          return password;
71      }
72  
73      public void setPassword(String password) {
74          this.password = password;
75      }
76  
77      @Override
78      protected  Object createInstance() throws Exception {
79          // Retrieve "mail.*" properties from the configuration system and construct a Properties object
80          Properties properties = new Properties();
81          Properties configProps = ConfigContext.getCurrentContextConfig().getProperties();
82          LOG.debug("createInstance(): collecting mail properties.");
83          for (Object keyObj : configProps.keySet()) {
84              if (keyObj instanceof String) {
85                  String key = (String)keyObj;
86                  if (key.startsWith(MAIL_PREFIX)){
87                      properties.put(key, configProps.get(key));
88                  }
89              }
90          }
91  
92          // Construct an appropriate Java Mail Session
93          // If username and password properties are found, construct a Session with SMTP authentication
94  
95  
96          if(host==null || (host!=null && host.trim().isEmpty())){
97             host =properties.getProperty(HOST_PROPERTY);
98          }
99          if(port==null || (port!=null && port.trim().isEmpty())){
100             port =properties.getProperty(PORT_PROPERTY);
101         }
102         if((userName==null || (userName!=null && userName.trim().isEmpty())) || ((password==null || (password!=null && password.trim().isEmpty())))){
103             userName =properties.getProperty(USERNAME_PROPERTY);
104             password =properties.getProperty(PASSWORD_PROPERTY);
105         }
106 
107 
108 
109        /* String username = properties.getProperty(USERNAME_PROPERTY);
110         String password = properties.getProperty(PASSWORD_PROPERTY);*/
111         if (userName != null && password != null) {
112             mailSession = Session.getInstance(properties, new SimpleAuthenticator(userName, password));
113             LOG.info("createInstance(): Initializing mail session using SMTP authentication.");
114         } else {
115             mailSession = Session.getInstance(properties);
116             LOG.info("createInstance(): Initializing mail session. No SMTP authentication.");
117         }
118 
119         // Construct and return a Spring Java Mail Sender
120         JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
121         LOG.debug("createInstance(): setting SMTP host.");
122         mailSender.setHost(host);
123         if (port != null) {
124             LOG.debug("createInstance(): setting SMTP port.");
125             int smtpPort = Integer.parseInt(port.trim());
126             mailSender.setPort(smtpPort);
127         }
128         String protocol = properties.getProperty(PROTOCOL_PROPERTY);
129         if (StringUtils.isNotBlank(protocol)) {
130             LOG.debug("createInstance(): setting mail transport protocol = " + protocol);
131             mailSender.setProtocol(protocol);
132         }
133         mailSender.setSession(mailSession);
134 
135         LOG.info("createInstance(): Mail Sender Factory Bean initialized.");
136         return mailSender;
137     }
138 
139     private class SimpleAuthenticator extends Authenticator {
140 
141         private final PasswordAuthentication passwordAuthentication;
142 
143         private SimpleAuthenticator(String username, String password) {
144             this.passwordAuthentication = new PasswordAuthentication(username, password);
145         }
146 
147         public PasswordAuthentication getPasswordAuthentication() {
148             return passwordAuthentication;
149         }
150     }
151     @SuppressWarnings("unchecked")
152     @Override
153     public Class getObjectType() {
154         return JavaMailSenderImpl.class;
155     }
156 
157 
158 }