1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.dao.jdbc.util;
24  
25  import com.liferay.portal.kernel.jndi.JNDIUtil;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.PropertiesUtil;
29  import com.liferay.portal.kernel.util.SortedProperties;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.util.PropsUtil;
32  import com.liferay.portal.util.PropsValues;
33  
34  import com.mchange.v2.c3p0.ComboPooledDataSource;
35  
36  import java.util.Enumeration;
37  import java.util.Properties;
38  
39  import javax.naming.InitialContext;
40  
41  import javax.sql.DataSource;
42  
43  import org.apache.commons.beanutils.BeanUtils;
44  import org.apache.commons.dbcp.BasicDataSourceFactory;
45  
46  import org.springframework.beans.factory.config.AbstractFactoryBean;
47  
48  import uk.org.primrose.pool.datasource.GenericDataSourceFactory;
49  
50  /**
51   * <a href="DataSourceFactoryBean.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   */
55  public class DataSourceFactoryBean extends AbstractFactoryBean {
56  
57      public Object createInstance() throws Exception {
58          Properties properties = _properties;
59  
60          if (properties == null) {
61              properties = PropsUtil.getProperties(_propertyPrefix, true);
62          }
63          else {
64              properties = PropertiesUtil.getProperties(
65                  properties, _propertyPrefix, true);
66          }
67  
68          String jndiName = properties.getProperty("jndi.name");
69  
70          if (Validator.isNotNull(jndiName)) {
71              try {
72                  return JNDIUtil.lookup(new InitialContext(), jndiName);
73              }
74              catch (Exception e) {
75                  _log.error("Unable to lookup " + jndiName, e);
76              }
77          }
78  
79          DataSource dataSource = null;
80  
81          String liferayPoolProvider =
82              PropsValues.JDBC_DEFAULT_LIFERAY_POOL_PROVIDER;
83  
84          if (liferayPoolProvider.equals("c3po")) {
85              dataSource = createDataSourceC3PO(properties);
86          }
87          else if (liferayPoolProvider.equals("dbcp")) {
88              dataSource = createDataSourceDBCP(properties);
89          }
90          else {
91              dataSource = createDataSourcePrimrose(properties);
92          }
93  
94          if (_log.isDebugEnabled()) {
95              _log.debug(
96                  "Creating data source " + dataSource.getClass().getName());
97  
98              SortedProperties sortedProperties = new SortedProperties(
99                  properties);
100 
101             _log.debug("Properties for prefix " + _propertyPrefix);
102 
103             sortedProperties.list(System.out);
104         }
105 
106         return dataSource;
107     }
108 
109     public Class<?> getObjectType() {
110         return DataSource.class;
111     }
112 
113     public void setProperties(Properties properties) {
114         _properties = properties;
115     }
116 
117     public void setPropertyPrefix(String propertyPrefix) {
118         _propertyPrefix = propertyPrefix;
119     }
120 
121     public void setPropertyPrefixLookup(String propertyPrefixLookup) {
122         _propertyPrefix = PropsUtil.get(propertyPrefixLookup);
123     }
124 
125     protected DataSource createDataSourceC3PO(Properties properties)
126         throws Exception {
127 
128         ComboPooledDataSource comboPooledDataSource =
129             new ComboPooledDataSource();
130 
131         comboPooledDataSource.setIdentityToken(_propertyPrefix);
132 
133         Enumeration<String> enu =
134             (Enumeration<String>)properties.propertyNames();
135 
136         while (enu.hasMoreElements()) {
137             String key = enu.nextElement();
138 
139             String value = properties.getProperty(key);
140 
141             // Map org.apache.commons.dbcp.BasicDataSource to C3PO
142 
143             if (key.equalsIgnoreCase("driverClassName")) {
144                 key = "driverClass";
145             }
146             else if (key.equalsIgnoreCase("url")) {
147                 key = "jdbcUrl";
148             }
149             else if (key.equalsIgnoreCase("username")) {
150                 key = "user";
151             }
152 
153             BeanUtils.setProperty(comboPooledDataSource, key, value);
154         }
155 
156         return comboPooledDataSource;
157     }
158 
159     protected DataSource createDataSourceDBCP(Properties properties)
160         throws Exception {
161 
162         return BasicDataSourceFactory.createDataSource(properties);
163     }
164 
165     protected DataSource createDataSourcePrimrose(Properties properties)
166         throws Exception {
167 
168         properties.setProperty("poolName", _propertyPrefix);
169 
170         Enumeration<String> enu =
171             (Enumeration<String>)properties.propertyNames();
172 
173         while (enu.hasMoreElements()) {
174             String key = enu.nextElement();
175 
176             String value = properties.getProperty(key);
177 
178             // Map org.apache.commons.dbcp.BasicDataSource to Primrose
179 
180             if (key.equalsIgnoreCase("driverClassName")) {
181                 key = "driverClass";
182             }
183             else if (key.equalsIgnoreCase("url")) {
184                 key = "driverURL";
185             }
186             else if (key.equalsIgnoreCase("username")) {
187                 key = "user";
188             }
189 
190             properties.setProperty(key, value);
191         }
192 
193         GenericDataSourceFactory genericDataSourceFactory =
194             new GenericDataSourceFactory();
195 
196         return genericDataSourceFactory.loadPool(_propertyPrefix, properties);
197     }
198 
199     private static Log _log =
200         LogFactoryUtil.getLog(DataSourceFactoryBean.class);
201 
202     private Properties _properties;
203     private String _propertyPrefix;
204 
205 }