001    /**
002     * Copyright (c) 2000-2013 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.portal.dao.jdbc.spring;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataSourceFactoryUtil;
018    import com.liferay.portal.kernel.util.PropertiesUtil;
019    import com.liferay.portal.util.PropsUtil;
020    
021    import java.util.Properties;
022    
023    import javax.sql.DataSource;
024    
025    import org.springframework.beans.factory.config.AbstractFactoryBean;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class DataSourceFactoryBean extends AbstractFactoryBean<DataSource> {
031    
032            @Override
033            public DataSource createInstance() throws Exception {
034                    Properties properties = _properties;
035    
036                    if (properties == null) {
037                            properties = PropsUtil.getProperties(_propertyPrefix, true);
038                    }
039                    else {
040                            properties = PropertiesUtil.getProperties(
041                                    properties, _propertyPrefix, true);
042                    }
043    
044                    return DataSourceFactoryUtil.initDataSource(properties);
045            }
046    
047            @Override
048            public void destroyInstance(DataSource dataSource) throws Exception {
049                    DataSourceFactoryUtil.destroyDataSource(dataSource);
050            }
051    
052            @Override
053            public Class<DataSource> getObjectType() {
054                    return DataSource.class;
055            }
056    
057            public void setProperties(Properties properties) {
058                    _properties = properties;
059            }
060    
061            public void setPropertyPrefix(String propertyPrefix) {
062                    _propertyPrefix = propertyPrefix;
063            }
064    
065            public void setPropertyPrefixLookup(String propertyPrefixLookup) {
066                    _propertyPrefix = PropsUtil.get(propertyPrefixLookup);
067            }
068    
069            private Properties _properties;
070            private String _propertyPrefix;
071    
072    }