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.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
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
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
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 }