File Source: MailProvider.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.business;
20
21 import java.util.Properties;
22 import javax.mail.MessagingException;
23 import javax.mail.NoSuchProviderException;
24 import javax.mail.Session;
25 import javax.mail.Transport;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.roller.weblogger.business.startup.StartupException;
32 import org.apache.roller.weblogger.config.WebloggerConfig;
33
34
35 /**
36 * Encapsulates Roller mail configuration, returns mail sessions.
37 */
38 public class MailProvider {
39
/*
P/P * Method: org.apache.roller.weblogger.business.MailProvider__static_init
*
* Postconditions:
* init'ed(log)
*/
40 private static final Log log = LogFactory.getLog(MailProvider.class);
41
/*
P/P * Method: org.apache.roller.weblogger.business.MailProvider$ConfigurationType__static_init
*
* Postconditions:
* $VALUES == &new MailProvider$ConfigurationType[](MailProvider$ConfigurationType__static_init#3)
* JNDI_NAME == &new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#1)
* $VALUES[0] == &new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#1)
* MAIL_PROPERTIES == &new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#2)
* $VALUES[1] == &new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#2)
* new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#1) num objects == 1
* new MailProvider$ConfigurationType(MailProvider$ConfigurationType__static_init#2) num objects == 1
* new MailProvider$ConfigurationType[](MailProvider$ConfigurationType__static_init#3) num objects == 1
* $VALUES.length == 2
*/
42 private enum ConfigurationType {JNDI_NAME, MAIL_PROPERTIES; }
43
44 private Session session = null;
45
46 private ConfigurationType type = ConfigurationType.JNDI_NAME;
47
48 private String jndiName = null;
49 private String mailHostname = null;
50 private int mailPort = -1;
51 private String mailUsername = null;
52 private String mailPassword = null;
53
54
/*
P/P * Method: void org.apache.roller.weblogger.business.MailProvider()
*
* Preconditions:
* org/apache/roller/weblogger/config/WebloggerConfig.config != null
* org/apache/roller/weblogger/config/WebloggerConfig.log != null
*
* Presumptions:
* javax.mail.Session:getDefaultInstance(...)@92 != null
* javax.naming.Context:lookup(...)@79 != null
* org.apache.commons.logging.LogFactory:getLog(...)@40 != null
*
* Postconditions:
* init'ed(this.jndiName)
* init'ed(this.mailHostname)
* init'ed(this.mailPassword)
* (soft) init'ed(this.mailPort)
* init'ed(this.mailUsername)
* this.session != null
* 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)}
*
* Test Vectors:
* java.lang.String:equals(...)@58: {0}, {1}
*/
55 public MailProvider() throws StartupException {
56
57 String connectionTypeString = WebloggerConfig.getProperty("mail.configurationType");
58 if ("properties".equals(connectionTypeString)) {
59 type = ConfigurationType.MAIL_PROPERTIES;
60 }
61 jndiName = WebloggerConfig.getProperty("mail.jndi.name");
62 mailHostname = WebloggerConfig.getProperty("mail.hostname");
63 mailUsername = WebloggerConfig.getProperty("mail.username");
64 mailPassword = WebloggerConfig.getProperty("mail.password");
65 try {
66 String portString = WebloggerConfig.getProperty("mail.port");
67 if (portString != null) {
68 mailPort = Integer.parseInt(portString);
69 }
70 } catch (Throwable t) {
71 log.warn("mail server port not a valid integer, ignoring");
72 }
73
74 // init and connect now so we fail early
75 if (type == ConfigurationType.JNDI_NAME) {
76 String name = "java:comp/env/" + jndiName;
77 try {
78 Context ctx = (Context) new InitialContext();
79 session = (Session) ctx.lookup(name);
80 } catch (NamingException ex) {
81 throw new StartupException("ERROR looking up mail-session with JNDI name: " + name);
82 }
83 } else {
84 Properties props = new Properties();
85 props.put("mail.smtp.host", mailHostname);
86 if (mailUsername != null && mailPassword != null) {
87 props.put("mail.smtp.auth", "true");
88 }
89 if (mailPort != -1) {
90 props.put("mail.smtp.port", ""+mailPort);
91 }
92 session = Session.getDefaultInstance(props, null);
93 }
94
95 try {
96 Transport transport = getTransport();
97 transport.close();
98 } catch (Throwable t) {
99 throw new StartupException("ERROR connecting to mail server", t);
100 }
101
102 }
103
104
105 /**
106 * Get a mail Session.
107 */
108 public Session getSession() {
/*
P/P * Method: Session getSession()
*
* Preconditions:
* init'ed(this.session)
*
* Postconditions:
* return_value == this.session
* init'ed(return_value)
*/
109 return session;
110 }
111
112
113 /**
114 * Create and connect to transport, caller is responsible for closing transport.
115 */
116 public Transport getTransport() throws NoSuchProviderException, MessagingException {
117
/*
P/P * Method: Transport getTransport()
*
* Preconditions:
* this.session != null
* init'ed(this.type)
* (soft) init'ed(this.mailHostname)
* (soft) init'ed(this.mailPassword)
* (soft) init'ed(this.mailPort)
* (soft) init'ed(this.mailUsername)
*
* Presumptions:
* javax.mail.Session:getTransport(...)@122 != null
* javax.mail.Session:getTransport(...)@132 != null
*
* Postconditions:
* (soft) return_value != null
*
* Test Vectors:
* this.mailPassword: Addr_Set{null}, Inverse{null}
* this.mailPort: {-1}, {-231..-2, 0..232-1}
* this.mailUsername: Addr_Set{null}, Inverse{null}
*/
118 Transport transport = null;
119
120 if (type == ConfigurationType.MAIL_PROPERTIES) {
121 // Configure transport ourselves using mail properties
122 transport = session.getTransport("smtp");
123 if (mailUsername != null && mailPassword != null && mailPort != -1) {
124 transport.connect(mailHostname, mailPort, mailUsername, mailPassword);
125 } else if (mailUsername != null && mailPassword != null) {
126 transport.connect(mailHostname, mailUsername, mailPassword);
127 } else {
128 transport.connect();
129 }
130 } else {
131 // Assume container set things up properly
132 transport = session.getTransport();
133 transport.connect();
134 }
135
136 return transport;
137 }
138
139 }
SofCheck Inspector Build Version : 2.18479
| MailProvider.java |
2009-Jan-02 14:25:36 |
| MailProvider.class |
2009-Sep-04 03:12:30 |
| MailProvider$ConfigurationType.class |
2009-Sep-04 03:12:30 |