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 @Override
038 public Class<Session> getObjectType() {
039 return Session.class;
040 }
041
042 public void setPropertyPrefix(String propertyPrefix) {
043 _propertyPrefix = propertyPrefix;
044 }
045
046 @Override
047 protected Session createInstance() throws Exception {
048 Properties properties = PropsUtil.getProperties(_propertyPrefix, true);
049
050 String jndiName = properties.getProperty("jndi.name");
051
052 if (Validator.isNotNull(jndiName)) {
053 try {
054 return (Session)JNDIUtil.lookup(new InitialContext(), jndiName);
055 }
056 catch (Exception e) {
057 _log.error("Unable to lookup " + jndiName, e);
058 }
059 }
060
061 Session session = Session.getInstance(properties);
062
063 if (_log.isDebugEnabled()) {
064 session.setDebug(true);
065
066 SortedProperties sortedProperties = new SortedProperties(
067 session.getProperties());
068
069 _log.debug("Properties for prefix " + _propertyPrefix);
070
071 sortedProperties.list(System.out);
072 }
073
074 return session;
075 }
076
077 private static Log _log = LogFactoryUtil.getLog(
078 MailSessionFactoryBean.class);
079
080 private String _propertyPrefix;
081
082 }