1111import com .avaje .ebean .EbeanServer ;
1212import com .avaje .ebean .EbeanServerFactory ;
1313import com .avaje .ebean .config .ServerConfig ;
14+ import com .zaxxer .hikari .HikariConfig ;
15+ import com .zaxxer .hikari .HikariDataSource ;
1416import org .avaje .datasource .DataSourceConfig ;
1517import org .osgl .$ ;
1618import org .osgl .logging .LogManager ;
2224import javax .persistence .Entity ;
2325import javax .persistence .Id ;
2426import javax .persistence .PersistenceException ;
27+ import javax .sql .DataSource ;
2528import java .io .File ;
2629import java .lang .annotation .Annotation ;
2730import java .lang .reflect .ParameterizedType ;
@@ -45,6 +48,9 @@ public final class EbeanService extends DbService {
4548
4649 private ServerConfig serverConfig ;
4750
51+ // the datasource for low level JDBC API usage
52+ private DataSource ds ;
53+
4854 private static Set <Class <?>> modelTypes = C .newSet ();
4955
5056 public EbeanService (final String dbId , final App app , final Map <String , Object > config ) {
@@ -129,22 +135,27 @@ public EbeanServer ebean() {
129135 return ebean ;
130136 }
131137
138+ public DataSource ds () {
139+ return ds ;
140+ }
141+
132142 private ServerConfig serverConfig (String id , Map <String , Object > conf ) {
133143 ServerConfig sc = new ServerConfig ();
134144 sc .setName (id );
135145 Properties properties = new Properties ();
136146 properties .putAll (conf );
137147 sc .loadFromProperties (properties );
138148
139- DataSourceConfig dsc = datasourceConfig (conf );
140- sc .setDataSourceConfig (dsc );
149+ HikariDataSource dataSource = dataSource (conf );
150+ sc .setDataSource (dataSource );
151+ ds = dataSource ;
141152
142153 boolean noddl = false ;
143154 String ddlGenerate = (String ) conf .get ("ddl.generate" );
144155 if (null != ddlGenerate ) {
145156 sc .setDdlGenerate (Boolean .parseBoolean (ddlGenerate ));
146157 } else if (Act .isDev ()) {
147- String url = dsc . getUrl ();
158+ String url = dataSource . getJdbcUrl ();
148159 if (url .startsWith ("jdbc:h2:" )) {
149160 String file = url .substring ("jdbc:h2:" .length ()) + ".mv.db" ;
150161 File _file = new File (file );
@@ -176,13 +187,83 @@ private ServerConfig serverConfig(String id, Map<String, Object> conf) {
176187 return sc ;
177188 }
178189
179- private DataSourceConfig datasourceConfig (Map <String , Object > conf ) {
180- Properties properties = new Properties ();
181- properties .putAll (conf );
182- DataSourceConfig dsc = new DataSourceConfig ();
183- dsc .loadSettings (properties , "" );
184- ensureDefaultDatasourceConfig (dsc );
185- return dsc ;
190+ private HikariDataSource dataSource (Map <String , Object > conf ) {
191+ HikariConfig hc = new HikariConfig ();
192+ for (Map .Entry <String , Object > entry : conf .entrySet ()) {
193+ String key = entry .getKey ();
194+ Object val = entry .getValue ();
195+ if ("username" .equals (key )) {
196+ hc .setUsername (S .string (val ));
197+ } else if ("password" .equals (key )) {
198+ hc .setPassword (S .string (val ));
199+ } else if ("url" .equals (key ) || "jdbcUrl" .equals (key )) {
200+ hc .setJdbcUrl (S .string (val ));
201+ } else if ("maximumPoolSize" .equals (key )) {
202+ hc .setMaximumPoolSize (Integer .parseInt (S .string (val )));
203+ } else if ("autoCommit" .equals (key )) {
204+ hc .setAutoCommit (Boolean .valueOf (S .string (val )));
205+ } else if ("idleTimeout" .equals (key )) {
206+ hc .setIdleTimeout (Long .parseLong (S .string (val )));
207+ } else if ("maxLifetime" .equals (key )) {
208+ hc .setMaxLifetime (Long .parseLong (S .string (val )));
209+ } else if ("connectionTimeout" .equals (key )) {
210+ hc .setConnectionTimeout (Long .parseLong (S .string (val )));
211+ } else if ("minimumIdle" .equals (key )) {
212+ hc .setMinimumIdle (Integer .parseInt (S .string (val )));
213+ } else if ("poolName" .equals (key )) {
214+ hc .setPoolName (S .string (val ));
215+ } else if ("driverClassName" .equals (key )) {
216+ hc .setDriverClassName (S .string (val ));
217+ } else {
218+ hc .addDataSourceProperty (entry .getKey (), entry .getValue ());
219+ }
220+ }
221+ ensureDefaultDatasourceConfig (hc );
222+ return new HikariDataSource (hc );
223+ }
224+
225+ private void ensureDefaultDatasourceConfig (HikariConfig dsc ) {
226+ String username = dsc .getUsername ();
227+ if (null == username ) {
228+ logger .warn ("No data source user configuration specified. Will use the default 'sa' user" );
229+ username = "sa" ;
230+ }
231+ dsc .setUsername (username );
232+
233+ String password = dsc .getPassword ();
234+ if (null == password ) {
235+ password = "" ;
236+ }
237+ dsc .setPassword (password );
238+
239+ String url = dsc .getJdbcUrl ();
240+ if (null == url ) {
241+ logger .warn ("No database URL configuration specified. Will use the default h2 inmemory test database" );
242+ url = "jdbc:h2:mem:tests" ;
243+ }
244+ dsc .setJdbcUrl (url );
245+
246+
247+ String driver = dsc .getDriverClassName ();
248+ if (null == driver ) {
249+ if (url .contains ("mysql" )) {
250+ driver = "com.mysql.jdbc.Driver" ;
251+ } else if (url .contains ("postgresql" )) {
252+ driver = "org.postgresql.Driver" ;
253+ } else if (url .contains ("jdbc:h2:" )) {
254+ driver = "org.h2.Driver" ;
255+ } else if (url .contains ("jdbc:oracle" )) {
256+ driver = "oracle.jdbc.OracleDriver" ;
257+ } else if (url .contains ("sqlserver" )) {
258+ driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver" ;
259+ } else if (url .contains ("jdbc:db2" )) {
260+ driver = "com.ibm.db2.jcc.DB2Driver" ;
261+ } else {
262+ throw E .invalidConfiguration ("JDBC driver needs to be configured for datasource: %s" , id ());
263+ }
264+ logger .warn ("JDBC driver not configured, system automatically set to: " + driver );
265+ }
266+ dsc .setDriverClassName (driver );
186267 }
187268
188269 private void ensureDefaultDatasourceConfig (DataSourceConfig dsc ) {
0 commit comments