最经在研究Tomcat,今天看到JNDI这一章,在测试Mail Session的配置时,发现了一个问题:在使用需要配置密码时,Tomcat会报错。
错误:
root cause
java.lang.NoClassDefFoundError: javax/mail/Authenticator
org.apache.naming.factory.MailSessionFactory.getObjectInstance(MailSessionFactory.java:105)
。。。。。。。。。。。。
root cause
java.lang.ClassNotFoundException: javax.mail.Authenticator
。。。。。。。。。。。。
java.lang.NoClassDefFoundError: javax/mail/Authenticator
org.apache.naming.factory.MailSessionFactory.getObjectInstance(MailSessionFactory.java:105)
。。。。。。。。。。。。
root cause
java.lang.ClassNotFoundException: javax.mail.Authenticator
。。。。。。。。。。。。
(我的Tomcat是6.0.20,JDK1.6update14)
参看Tomcat的源码:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* 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.
*/
package org.apache.naming.factory;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.naming.Name;
import javax.naming.Context;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;
/**
* <p>Factory class that creates a JNDI named JavaMail Session factory,
* which can be used for managing inbound and outbound electronic mail
* messages via JavaMail APIs. All messaging environment properties
* described in the JavaMail Specification may be passed to the Session
* factory; however the following properties are the most commonly used:</p>
* <ul>
* <li>
* <li><strong>mail.smtp.host</strong> - Hostname for outbound transport
* connections. Defaults to <code>localhost</code> if not specified.</li>
* </ul>
*
* <p>This factory can be configured in a <code><DefaultContext></code>
* or <code><Context></code> element in your <code>conf/server.xml</code>
* configuration file. An example of factory configuration is:</p>
* <pre>
* <Resource name="mail/smtp" auth="CONTAINER"
* type="javax.mail.Session"/>
* <ResourceParams name="mail/smtp">
* <parameter>
* <name>factory</name>
* <value>org.apache.naming.factory.MailSessionFactory</value>
* </parameter>
* <parameter>
* <name>mail.smtp.host</name>
* <value>mail.mycompany.com</value>
* </parameter>
* </ResourceParams>
* </pre>
*
* @author Craig R. McClanahan
* @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (Tue, 24 Oct 2006) $
*/
public class MailSessionFactory implements ObjectFactory {
/**
* The Java type for which this factory knows how to create objects.
*/
protected static final String factoryType = "javax.mail.Session";
/**
* Create and return an object instance based on the specified
* characteristics.
*
* @param refObj Reference information containing our parameters, or null
* if there are no parameters
* @param name The name of this object, relative to context, or null
* if there is no name
* @param context The context to which name is relative, or null if name
* is relative to the default initial context
* @param env Environment variables, or null if there are none
*
* @exception Exception if an error occurs during object creation
*/
public Object getObjectInstance(Object refObj, Name name, Context context,
Hashtable env) throws Exception
{
// Return null if we cannot create an object of the requested type
final Reference ref = (Reference) refObj;
if (!ref.getClassName().equals(factoryType))
return (null);
// Create a new Session inside a doPrivileged block, so that JavaMail
// can read its default properties without throwing Security
// exceptions.
//
// Bugzilla 31288, 33077: add support for authentication.
return AccessController.doPrivileged( new PrivilegedAction() {
public Object run() {
// Create the JavaMail properties we will use
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "localhost");
String password = null;
Enumeration attrs = ref.getAll();
while (attrs.hasMoreElements()) {
RefAddr attr = (RefAddr) attrs.nextElement();
if ("factory".equals(attr.getType())) {
continue;
}
if ("password".equals(attr.getType())) {
password = (String) attr.getContent();
continue;
}
props.put(attr.getType(), (String) attr.getContent());
}
Authenticator auth = null;
if (password != null) {
String user = props.getProperty("mail.smtp.user");
if(user == null) {
user = props.getProperty("mail.user");
}
if(user != null) {
final PasswordAuthentication pa = new PasswordAuthentication(user, password);
auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return pa;
}
};
}
}
// Create and return the new Session object
Session session = Session.getInstance(props, auth);
return (session);
}
} );
}
}
第105行的代码是:
return AccessController.doPrivileged( new PrivilegedAction() {
如果不是AccessController的问题,那么我就不晓得问题出在哪了。自己写了个MySessionFactory的类:
package tomcat.jndi.provider;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;
public class MySessionFactory implements ObjectFactory {
public Object getObjectInstance(Object refObj, Name name, Context context,
Hashtable<?, ?> environment) throws Exception {
final Reference ref = (Reference) refObj;
// 创建默认的Properties属性集
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "localhost");
String password = null;
// 遍历所有的设定参数
Enumeration<RefAddr> attrs = ref.getAll();
while (attrs.hasMoreElements()) {
RefAddr attr = (RefAddr) attrs.nextElement();
// 忽略factory参数
if ("factory".equals(attr.getType())) {
continue;
}
// 密码的设定使用“password”
if ("password".equals(attr.getType())) {
password = (String) attr.getContent();
continue;
}
// 其他的参数,如调试、验证等,直接设置在properties中
props.put(attr.getType(), (String) attr.getContent());
}
Authenticator auth = null;
if (password != null) {
// 在properties中寻找用户的邮件信息
String user = props.getProperty("mail.smtp.user");
if (user == null) {
user = props.getProperty("mail.user");
}
if (user != null) {
final PasswordAuthentication pa = new PasswordAuthentication(
user, password);
auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return pa;
}
};
}
}
// Create and return the new Session object
Session session = Session.getInstance(props, auth);
return (session);
}
}
修改META-INF/context.xml文件:
<Resource name="mail/Session" auth="Container"
type="javax.mail.Session"
factory="tomcat.jndi.provider.MySessionFactory"
mail.smtp.auth="true"
mail.smtp.host="smtp.sohu.com"
mail.smtp.user="XXX@sohu.com"
mail.debug="true"
password="XXXXXX"/>
MySessionFactory基本上是仿照Tomcat的MailSessionFactory写的, 测试竟然正常了!!!不知道是不是 AccessController的问题。也不清楚是否是我的配置在某些细微之处错了。先记录一下,研究过AccessController之后,再修改本文!!也欢迎大家和我一起探讨这个问题!!
问题解决了:
从报错的具体细节,推测出是jar包的导入出问题了。删除 application/WEB-INF/lib目录下的mail.jar,在Tomcat/lib目录中添加mail.jar这样就可以使用 MailSessionFactory了!!
再细心地核对了一下Tomcat的说明文档:
写道
Download the JavaMail API. The JavaMail API requires the Java Activation Framework (JAF) API as well. The Java Activation Framework can be downloaded from Sun's site.
This download includes 2 vital libraries for the configuration; activation.jar and mail.jar. Unpackage both distributions and place them into $CATALINA_HOME/lib so that they are available to Tomcat during the initialization of the mail Session Resource. Note: placing these jars in both $CATALINA_HOME/lib and a web application's lib folder will cause an error, so ensure you have them in the $CATALINA_HOME/lib location only.
This download includes 2 vital libraries for the configuration; activation.jar and mail.jar. Unpackage both distributions and place them into $CATALINA_HOME/lib so that they are available to Tomcat during the initialization of the mail Session Resource. Note: placing these jars in both $CATALINA_HOME/lib and a web application's lib folder will cause an error, so ensure you have them in the $CATALINA_HOME/lib location only.
呵呵,是自己看的时候漏掉了。