1616
1717package org .labkey .test ;
1818
19+ import org .apache .commons .lang3 .StringUtils ;
1920import org .apache .http .HttpHost ;
2021import org .apache .http .HttpResponse ;
2122import org .apache .http .auth .AuthScope ;
7374import java .security .KeyManagementException ;
7475import java .security .KeyStoreException ;
7576import java .security .NoSuchAlgorithmException ;
77+ import java .util .Arrays ;
7678import java .util .Collections ;
7779import java .util .Date ;
7880import java .util .HashMap ;
7981import java .util .Map ;
8082import java .util .Properties ;
8183import java .util .Random ;
82- import java .util .Set ;
8384
8485/**
8586 * Static methods for getting properties of and communicating with a running LabKey server
@@ -285,31 +286,53 @@ else if (url.indexOf("/") == 0)
285286
286287 public enum DatabaseType
287288 {
288- PostgreSQL ("postgres " , "pg" ),
289- MicrosoftSQLServer ("sqlserver" , "mssql" , "jtds " );
289+ PostgreSQL ("org.postgresql.Driver " , "pg" , "postgres " ),
290+ MicrosoftSQLServer ("com.microsoft. sqlserver.jdbc.SQLServerDriver " , "net.sourceforge.jtds.jdbc.Driver" , " mssql" , "sqlserver " );
290291
291- private final Set <String > typeNames ;
292+ private static final Map <String , DatabaseType > DATABASE_TYPE_MAP ;
292293
293- DatabaseType ( String ... typeNames )
294+ static
294295 {
295- this .typeNames = Set .of (typeNames );
296+ Map <String , DatabaseType > tempMap = new HashMap <>();
297+ Arrays .stream (values ())
298+ .forEach (dt -> Arrays .stream (dt ._typeKeys )
299+ .forEach (name -> tempMap .put (name , dt )));
300+ DATABASE_TYPE_MAP = Collections .unmodifiableMap (tempMap );
301+ }
302+
303+ private final String [] _typeKeys ;
304+
305+ DatabaseType (String ... typeKeys )
306+ {
307+ _typeKeys = typeKeys ;
308+ }
309+
310+ static @ Nullable DatabaseType get (String driverClassName )
311+ {
312+ return DATABASE_TYPE_MAP .get (driverClassName );
296313 }
297314 }
298315
299316 public static DatabaseType getDatabaseType ()
300317 {
301- String databaseType = getServerProperty ("databaseType" );
318+ String typeKey = getServerProperty ("databaseType" );
319+ if (StringUtils .isBlank (typeKey ))
320+ {
321+ // Use driver class name from 'config.properties' if 'databaseType' isn't specified
322+ typeKey = getServerProperty ("jdbcDriverClassName" );
323+ }
302324
303- if (null == databaseType )
304- throw new IllegalStateException ("Can't determine database type: databaseType property is not set" );
325+ if (StringUtils .isBlank (typeKey ))
326+ {
327+ throw new IllegalStateException ("Can't determine database type: Neither 'jdbcDriverClassName' nor 'databaseType' property is not set" );
328+ }
305329
306- if (DatabaseType .PostgreSQL .typeNames .contains (databaseType ))
307- return DatabaseType .PostgreSQL ;
330+ DatabaseType dt = DatabaseType .get (typeKey );
308331
309- if (DatabaseType . MicrosoftSQLServer . typeNames . contains ( databaseType ) )
310- return DatabaseType . MicrosoftSQLServer ;
332+ if (null == dt )
333+ throw new IllegalStateException ( "Unknown database type: " + typeKey ) ;
311334
312- throw new IllegalStateException ( "Unknown database type: " + databaseType ) ;
335+ return dt ;
313336 }
314337
315338 private static String getServerProperty (String property )
@@ -335,11 +358,6 @@ private static String getServerProperty(String property)
335358 return val ;
336359 }
337360
338- public static String getDatabaseVersion ()
339- {
340- return getServerProperty ("databaseVersion" );
341- }
342-
343361 public static String getBaseUrlWithoutContextPath ()
344362 {
345363 int defaultPort = getTargetServer ().startsWith ("https" ) ? 443 : 80 ;
0 commit comments