001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
033     * @author Brian Wing Shun Chan
034     */
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(
049                            _propertyPrefix, true);
050    
051                    String jndiName = properties.getProperty("jndi.name");
052    
053                    if (Validator.isNotNull(jndiName)) {
054                            try {
055                                    return (Session)JNDIUtil.lookup(new InitialContext(), jndiName);
056                            }
057                            catch (Exception e) {
058                                    _log.error("Unable to lookup " + jndiName, e);
059                            }
060                    }
061    
062                    Session session = Session.getInstance(properties);
063    
064                    if (_log.isDebugEnabled()) {
065                            session.setDebug(true);
066    
067                            SortedProperties sortedProperties = new SortedProperties(
068                                    session.getProperties());
069    
070                            _log.debug("Properties for prefix " + _propertyPrefix);
071    
072                            sortedProperties.list(System.out);
073                    }
074    
075                    return session;
076            }
077    
078            private static Log _log = LogFactoryUtil.getLog(
079                    MailSessionFactoryBean.class);
080    
081            private String _propertyPrefix;
082    
083    }