1
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
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 DataSource dataSource = new ComboPooledDataSource();
129
130 Enumeration<String> enu =
131 (Enumeration<String>)properties.propertyNames();
132
133 while (enu.hasMoreElements()) {
134 String key = enu.nextElement();
135
136 String value = properties.getProperty(key);
137
138
140 if (key.equalsIgnoreCase("driverClassName")) {
141 key = "driverClass";
142 }
143 else if (key.equalsIgnoreCase("url")) {
144 key = "jdbcUrl";
145 }
146 else if (key.equalsIgnoreCase("username")) {
147 key = "user";
148 }
149
150 BeanUtils.setProperty(dataSource, key, value);
151 }
152
153 return dataSource;
154 }
155
156 protected DataSource createDataSourceDBCP(Properties properties)
157 throws Exception {
158
159 return BasicDataSourceFactory.createDataSource(properties);
160 }
161
162 protected DataSource createDataSourcePrimrose(Properties properties)
163 throws Exception {
164
165 properties.setProperty("poolName", _propertyPrefix);
166
167 Enumeration<String> enu =
168 (Enumeration<String>)properties.propertyNames();
169
170 while (enu.hasMoreElements()) {
171 String key = enu.nextElement();
172
173 String value = properties.getProperty(key);
174
175
177 if (key.equalsIgnoreCase("driverClassName")) {
178 key = "driverClass";
179 }
180 else if (key.equalsIgnoreCase("url")) {
181 key = "driverURL";
182 }
183 else if (key.equalsIgnoreCase("username")) {
184 key = "user";
185 }
186
187 properties.setProperty(key, value);
188 }
189
190 GenericDataSourceFactory genericDataSourceFactory =
191 new GenericDataSourceFactory();
192
193 return genericDataSourceFactory.loadPool(_propertyPrefix, properties);
194 }
195
196 private static Log _log =
197 LogFactoryUtil.getLog(DataSourceFactoryBean.class);
198
199 private Properties _properties;
200 private String _propertyPrefix;
201
202 }