001
014
015 package com.liferay.mail.util;
016
017 import com.liferay.portal.kernel.jndi.JNDIUtil;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.SortedProperties;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.util.PropsUtil;
023
024 import java.util.Properties;
025
026 import javax.mail.Session;
027
028 import javax.naming.InitialContext;
029
030 import org.springframework.beans.factory.config.AbstractFactoryBean;
031
032
035 public class MailSessionFactoryBean extends AbstractFactoryBean<Session> {
036
037 public Class<Session> getObjectType() {
038 return Session.class;
039 }
040
041 public void setPropertyPrefix(String propertyPrefix) {
042 _propertyPrefix = propertyPrefix;
043 }
044
045 protected Session createInstance() throws Exception {
046 Properties properties = PropsUtil.getProperties(
047 _propertyPrefix, true);
048
049 String jndiName = properties.getProperty("jndi.name");
050
051 if (Validator.isNotNull(jndiName)) {
052 try {
053 return (Session)JNDIUtil.lookup(new InitialContext(), jndiName);
054 }
055 catch (Exception e) {
056 _log.error("Unable to lookup " + jndiName, e);
057 }
058 }
059
060 Session session = Session.getInstance(properties);
061
062 if (_log.isDebugEnabled()) {
063 session.setDebug(true);
064
065 SortedProperties sortedProperties = new SortedProperties(
066 session.getProperties());
067
068 _log.debug("Properties for prefix " + _propertyPrefix);
069
070 sortedProperties.list(System.out);
071 }
072
073 return session;
074 }
075
076 private static Log _log = LogFactoryUtil.getLog(
077 MailSessionFactoryBean.class);
078
079 private String _propertyPrefix;
080
081 }