001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }