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.SortedProperties;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.util.PropsUtil;
31  import com.liferay.portal.util.PropsValues;
32  
33  import com.mchange.v2.c3p0.ComboPooledDataSource;
34  
35  import java.util.Enumeration;
36  import java.util.Properties;
37  
38  import javax.naming.InitialContext;
39  
40  import javax.sql.DataSource;
41  
42  import org.apache.commons.beanutils.BeanUtils;
43  import org.apache.commons.dbcp.BasicDataSourceFactory;
44  
45  import org.springframework.beans.factory.config.AbstractFactoryBean;
46  
47  import uk.org.primrose.pool.datasource.GenericDataSourceFactory;
48  
49  /**
50   * <a href="DataSourceFactoryBean.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   */
54  public class DataSourceFactoryBean extends AbstractFactoryBean {
55  
56      public Class<?> getObjectType() {
57          return DataSource.class;
58      }
59  
60      public void setPropertyPrefix(String propertyPrefix) {
61          _propertyPrefix = propertyPrefix;
62      }
63  
64      public void setPropertyPrefixLookup(String propertyPrefixLookup) {
65          _propertyPrefix = PropsUtil.get(propertyPrefixLookup);
66      }
67  
68      protected DataSource createDataSourceC3PO(Properties properties)
69          throws Exception {
70  
71          DataSource dataSource = new ComboPooledDataSource();
72  
73          Enumeration<String> enu =
74              (Enumeration<String>)properties.propertyNames();
75  
76          while (enu.hasMoreElements()) {
77              String key = enu.nextElement();
78  
79              String value = properties.getProperty(key);
80  
81              // Map org.apache.commons.dbcp.BasicDataSource to C3PO
82  
83              if (key.equalsIgnoreCase("driverClassName")) {
84                  key = "driverClass";
85              }
86              else if (key.equalsIgnoreCase("url")) {
87                  key = "jdbcUrl";
88              }
89              else if (key.equalsIgnoreCase("username")) {
90                  key = "user";
91              }
92  
93              BeanUtils.setProperty(dataSource, key, value);
94          }
95  
96          return dataSource;
97      }
98  
99      protected DataSource createDataSourceDBCP(Properties properties)
100         throws Exception {
101 
102         return BasicDataSourceFactory.createDataSource(properties);
103     }
104 
105     protected DataSource createDataSourcePrimrose(Properties properties)
106         throws Exception {
107 
108         properties.setProperty("poolName", _propertyPrefix);
109 
110         Enumeration<String> enu =
111             (Enumeration<String>)properties.propertyNames();
112 
113         while (enu.hasMoreElements()) {
114             String key = enu.nextElement();
115 
116             String value = properties.getProperty(key);
117 
118             // Map org.apache.commons.dbcp.BasicDataSource to Primrose
119 
120             if (key.equalsIgnoreCase("driverClassName")) {
121                 key = "driverClass";
122             }
123             else if (key.equalsIgnoreCase("url")) {
124                 key = "driverURL";
125             }
126             else if (key.equalsIgnoreCase("username")) {
127                 key = "user";
128             }
129 
130             properties.setProperty(key, value);
131         }
132 
133         GenericDataSourceFactory genericDataSourceFactory =
134             new GenericDataSourceFactory();
135 
136         return genericDataSourceFactory.loadPool(_propertyPrefix, properties);
137     }
138 
139     protected Object createInstance() throws Exception {
140         Properties properties = PropsUtil.getProperties(_propertyPrefix, true);
141 
142         String jndiName = properties.getProperty("jndi.name");
143 
144         if (Validator.isNotNull(jndiName)) {
145             try {
146                 return JNDIUtil.lookup(new InitialContext(), jndiName);
147             }
148             catch (Exception e) {
149                 _log.error("Unable to lookup " + jndiName, e);
150             }
151         }
152 
153         DataSource dataSource = null;
154 
155         String liferayPoolProvider =
156             PropsValues.JDBC_DEFAULT_LIFERAY_POOL_PROVIDER;
157 
158         if (liferayPoolProvider.equals("c3po")) {
159             dataSource = createDataSourceC3PO(properties);
160         }
161         else if (liferayPoolProvider.equals("dbcp")) {
162             dataSource = createDataSourceDBCP(properties);
163         }
164         else {
165             dataSource = createDataSourcePrimrose(properties);
166         }
167 
168         if (_log.isDebugEnabled()) {
169             _log.debug(
170                 "Creating data source " + dataSource.getClass().getName());
171 
172             SortedProperties sortedProperties = new SortedProperties(
173                 properties);
174 
175             _log.debug("Properties for prefix " + _propertyPrefix);
176 
177             sortedProperties.list(System.out);
178         }
179 
180         return dataSource;
181     }
182 
183     private static Log _log =
184         LogFactoryUtil.getLog(DataSourceFactoryBean.class);
185 
186     private String _propertyPrefix;
187 
188 }