35
35
import java .io .InputStream ;
36
36
import java .sql .DriverManager ;
37
37
import java .util .ArrayList ;
38
- import java .util .Arrays ;
39
38
import java .util .Collections ;
40
39
import java .util .HashMap ;
41
40
import java .util .List ;
47
46
import java .util .stream .Collectors ;
48
47
49
48
import com .mysql .cj .Messages ;
49
+ import com .mysql .cj .conf .PropertyDefinitions .PropertyKey ;
50
50
import com .mysql .cj .exceptions .CJException ;
51
51
import com .mysql .cj .exceptions .ExceptionFactory ;
52
52
import com .mysql .cj .exceptions .InvalidConnectionAttributeException ;
@@ -301,11 +301,11 @@ protected ConnectionUrl(ConnectionUrlParser connStrParser, Properties info) {
301
301
*/
302
302
protected void collectProperties (ConnectionUrlParser connStrParser , Properties info ) {
303
303
// Fill in the properties from the connection string.
304
- this .properties .putAll ( connStrParser . getProperties ( ));
304
+ connStrParser . getProperties (). entrySet (). stream (). forEach ( e -> this .properties .put ( PropertyKey . normalizeCase ( e . getKey ()), e . getValue () ));
305
305
306
306
// Properties passed in override the ones from the connection string.
307
307
if (info != null ) {
308
- info .stringPropertyNames ().stream ().forEach (k -> this .properties .put (k , info .getProperty (k )));
308
+ info .stringPropertyNames ().stream ().forEach (k -> this .properties .put (PropertyKey . normalizeCase ( k ) , info .getProperty (k )));
309
309
}
310
310
311
311
// Collect properties from additional sources.
@@ -360,7 +360,8 @@ protected void expandPropertiesFromConfigFiles(Map<String, String> props) {
360
360
String configFiles = props .get (PropertyDefinitions .PNAME_useConfigs );
361
361
if (!isNullOrEmpty (configFiles )) {
362
362
Properties configProps = getPropertiesFromConfigFiles (configFiles );
363
- configProps .stringPropertyNames ().stream ().filter (k -> !props .containsKey (k )).forEach (k -> props .put (k , configProps .getProperty (k )));
363
+ configProps .stringPropertyNames ().stream ().map (PropertyKey ::normalizeCase ).filter (k -> !props .containsKey (k ))
364
+ .forEach (k -> props .put (k , configProps .getProperty (k )));
364
365
}
365
366
}
366
367
@@ -417,44 +418,47 @@ protected void collectHostsInfo(ConnectionUrlParser connStrParser) {
417
418
* @return a new {@link HostInfo} with all required data
418
419
*/
419
420
protected HostInfo fixHostInfo (HostInfo hi ) {
420
- Map <String , String > hostProps = new TreeMap <>(String . CASE_INSENSITIVE_ORDER );
421
+ Map <String , String > hostProps = new HashMap <>();
421
422
422
- hostProps .putAll (this .properties ); // Add global connection arguments.
423
- hostProps .putAll (hi .getHostProperties ()); // Add/override host specific connection arguments.
424
- hostProps .put (PropertyDefinitions .DBNAME_PROPERTY_KEY , getDatabase ()); // Add the database name
423
+ // Add global connection arguments.
424
+ hostProps .putAll (this .properties );
425
+ // Add/override host specific connection arguments.
426
+ hi .getHostProperties ().entrySet ().stream ().forEach (e -> hostProps .put (PropertyKey .normalizeCase (e .getKey ()), e .getValue ()));
427
+ // Add the database name
428
+ hostProps .put (PropertyKey .DBNAME .getKeyName (), getDatabase ());
425
429
426
- hostProps = preprocessPerTypeHostProperties (hostProps );
430
+ preprocessPerTypeHostProperties (hostProps );
427
431
428
- String host = hostProps .remove (PropertyDefinitions . HOST_PROPERTY_KEY );
432
+ String host = hostProps .remove (PropertyKey . HOST . getKeyName () );
429
433
if (!isNullOrEmpty (hi .getHost ())) {
430
434
host = hi .getHost ();
431
435
} else if (isNullOrEmpty (host )) {
432
436
host = getDefaultHost ();
433
437
}
434
438
435
- String portAsString = hostProps .remove (PropertyDefinitions . PORT_PROPERTY_KEY );
439
+ String portAsString = hostProps .remove (PropertyKey . PORT . getKeyName () );
436
440
int port = hi .getPort ();
437
441
if (port == -1 && !isNullOrEmpty (portAsString )) {
438
442
try {
439
443
port = Integer .valueOf (portAsString );
440
444
} catch (NumberFormatException e ) {
441
445
throw ExceptionFactory .createException (WrongArgumentException .class ,
442
- Messages .getString ("ConnectionString.7" , new Object [] { hostProps .get (PropertyDefinitions . PORT_PROPERTY_KEY ) }), e );
446
+ Messages .getString ("ConnectionString.7" , new Object [] { hostProps .get (PropertyKey . PORT . getKeyName () ) }), e );
443
447
}
444
448
}
445
449
if (port == -1 ) {
446
450
port = getDefaultPort ();
447
451
}
448
452
449
- String user = hostProps .remove (PropertyDefinitions . PNAME_user );
453
+ String user = hostProps .remove (PropertyKey . USER . getKeyName () );
450
454
if (!isNullOrEmpty (hi .getUser ())) {
451
455
user = hi .getUser ();
452
456
} else if (isNullOrEmpty (user )) {
453
457
user = getDefaultUser ();
454
458
}
455
459
456
460
boolean isPasswordless = hi .isPasswordless ();
457
- String password = hostProps .remove (PropertyDefinitions . PNAME_password );
461
+ String password = hostProps .remove (PropertyKey . PASSWORD . getKeyName () );
458
462
if (!isPasswordless ) {
459
463
password = hi .getPassword ();
460
464
} else if (password == null ) {
@@ -465,7 +469,6 @@ protected HostInfo fixHostInfo(HostInfo hi) {
465
469
}
466
470
467
471
expandPropertiesFromConfigFiles (hostProps );
468
- fixKeysCase (hostProps );
469
472
fixProtocolDependencies (hostProps );
470
473
471
474
return buildHostInfo (host , port , user , password , isPasswordless , hostProps );
@@ -479,8 +482,8 @@ protected HostInfo fixHostInfo(HostInfo hi) {
479
482
* @return
480
483
* the processed host properties map
481
484
*/
482
- protected Map < String , String > preprocessPerTypeHostProperties (Map <String , String > hostProps ) {
483
- return hostProps ;
485
+ protected void preprocessPerTypeHostProperties (Map <String , String > hostProps ) {
486
+ // To be overridden in subclasses if needed.
484
487
}
485
488
486
489
/**
@@ -507,7 +510,7 @@ public int getDefaultPort() {
507
510
* @return the default user
508
511
*/
509
512
public String getDefaultUser () {
510
- String user = this .properties .get (PropertyDefinitions . PNAME_user );
513
+ String user = this .properties .get (PropertyKey . USER . getKeyName () );
511
514
return isNullOrEmpty (user ) ? "" : user ;
512
515
}
513
516
@@ -518,39 +521,24 @@ public String getDefaultUser() {
518
521
* @return the default password
519
522
*/
520
523
public String getDefaultPassword () {
521
- String password = this .properties .get (PropertyDefinitions . PNAME_password );
524
+ String password = this .properties .get (PropertyKey . PASSWORD . getKeyName () );
522
525
return isNullOrEmpty (password ) ? "" : password ;
523
526
}
524
527
525
- /**
526
- * Fixes the case for alternate host syntax main properties.
527
- *
528
- * @param hostProps
529
- * the host properties map to fix
530
- */
531
- protected void fixKeysCase (Map <String , String > hostProps ) {
532
- for (String key : Arrays .asList (PropertyDefinitions .PROTOCOL_PROPERTY_KEY , PropertyDefinitions .PATH_PROPERTY_KEY , PropertyDefinitions .TYPE_PROPERTY_KEY ,
533
- PropertyDefinitions .ADDRESS_PROPERTY_KEY , PropertyDefinitions .PRIORITY_PROPERTY_KEY )) {
534
- if (hostProps .containsKey (key )) {
535
- hostProps .put (key , hostProps .remove (key ));
536
- }
537
- }
538
- }
539
-
540
528
/**
541
529
* Fixes the protocol (TCP vs PIPE) dependencies for the given host properties map.
542
530
*
543
531
* @param hostProps
544
532
* the host properties map to fix
545
533
*/
546
534
protected void fixProtocolDependencies (Map <String , String > hostProps ) {
547
- String protocol = hostProps .get (PropertyDefinitions . PROTOCOL_PROPERTY_KEY );
535
+ String protocol = hostProps .get (PropertyKey . PROTOCOL . getKeyName () );
548
536
if (!isNullOrEmpty (protocol ) && protocol .equalsIgnoreCase ("PIPE" )) {
549
537
if (!hostProps .containsKey (PropertyDefinitions .PNAME_socketFactory )) {
550
538
hostProps .put (PropertyDefinitions .PNAME_socketFactory , "com.mysql.cj.protocol.NamedPipeSocketFactory" );
551
539
}
552
- if (hostProps .containsKey (PropertyDefinitions . PATH_PROPERTY_KEY ) && !hostProps .containsKey (PropertyDefinitions .NAMED_PIPE_PROP_NAME )) {
553
- hostProps .put (PropertyDefinitions .NAMED_PIPE_PROP_NAME , hostProps .get (PropertyDefinitions . PATH_PROPERTY_KEY ));
540
+ if (hostProps .containsKey (PropertyKey . PATH . getKeyName () ) && !hostProps .containsKey (PropertyDefinitions .NAMED_PIPE_PROP_NAME )) {
541
+ hostProps .put (PropertyDefinitions .NAMED_PIPE_PROP_NAME , hostProps .get (PropertyKey . PATH . getKeyName () ));
554
542
}
555
543
}
556
544
}
@@ -580,8 +568,7 @@ public String getDatabaseUrl() {
580
568
* @return the database name
581
569
*/
582
570
public String getDatabase () {
583
- return this .properties .containsKey (PropertyDefinitions .DBNAME_PROPERTY_KEY ) ? this .properties .get (PropertyDefinitions .DBNAME_PROPERTY_KEY )
584
- : this .originalDatabase ;
571
+ return this .properties .containsKey (PropertyKey .DBNAME .getKeyName ()) ? this .properties .get (PropertyKey .DBNAME .getKeyName ()) : this .originalDatabase ;
585
572
}
586
573
587
574
/**
@@ -671,30 +658,31 @@ private HostInfo buildHostInfo(String host, int port, String user, String passwo
671
658
Properties props = new Properties ();
672
659
props .putAll (hostProps );
673
660
674
- props .setProperty (PropertyDefinitions . HOST_PROPERTY_KEY , host );
675
- props .setProperty (PropertyDefinitions . PORT_PROPERTY_KEY , String .valueOf (port ));
676
- props .setProperty (PropertyDefinitions . PNAME_user , user );
677
- props .setProperty (PropertyDefinitions . PNAME_password , password );
661
+ props .setProperty (PropertyKey . HOST . getKeyName () , host );
662
+ props .setProperty (PropertyKey . PORT . getKeyName () , String .valueOf (port ));
663
+ props .setProperty (PropertyKey . USER . getKeyName () , user );
664
+ props .setProperty (PropertyKey . PASSWORD . getKeyName () , password );
678
665
679
666
Properties transformedProps = this .propertiesTransformer .transformProperties (props );
680
667
681
- host = transformedProps .getProperty (PropertyDefinitions . HOST_PROPERTY_KEY );
668
+ host = transformedProps .getProperty (PropertyKey . PORT . getKeyName () );
682
669
try {
683
- port = Integer .parseInt (transformedProps .getProperty (PropertyDefinitions . PORT_PROPERTY_KEY ));
670
+ port = Integer .parseInt (transformedProps .getProperty (PropertyKey . PORT . getKeyName () ));
684
671
} catch (NumberFormatException e ) {
685
- throw ExceptionFactory .createException (WrongArgumentException .class ,
686
- Messages .getString ("ConnectionString.8" ,
687
- new Object [] { PropertyDefinitions .PORT_PROPERTY_KEY , transformedProps .getProperty (PropertyDefinitions .PORT_PROPERTY_KEY ) }),
688
- e );
672
+ throw ExceptionFactory .createException (WrongArgumentException .class , Messages .getString ("ConnectionString.8" ,
673
+ new Object [] { PropertyKey .PORT .getKeyName (), transformedProps .getProperty (PropertyKey .PORT .getKeyName ()) }), e );
689
674
}
690
- user = transformedProps .getProperty (PropertyDefinitions . PNAME_user );
691
- password = transformedProps .getProperty (PropertyDefinitions . PNAME_password );
675
+ user = transformedProps .getProperty (PropertyKey . USER . getKeyName () );
676
+ password = transformedProps .getProperty (PropertyKey . PASSWORD . getKeyName () );
692
677
693
- List <String > surplusKeys = Arrays .asList (PropertyDefinitions .HOST_PROPERTY_KEY , PropertyDefinitions .PORT_PROPERTY_KEY ,
694
- PropertyDefinitions .PNAME_user , PropertyDefinitions .PNAME_password );
695
678
Map <String , String > transformedHostProps = new TreeMap <>(String .CASE_INSENSITIVE_ORDER );
696
- transformedProps .stringPropertyNames ().stream ().filter (k -> !surplusKeys .contains (k ))
697
- .forEach (k -> transformedHostProps .put (k , transformedProps .getProperty (k )));
679
+ transformedProps .stringPropertyNames ().stream ().forEach (k -> transformedHostProps .put (k , transformedProps .getProperty (k )));
680
+ // Remove surplus keys.
681
+ transformedHostProps .remove (PropertyKey .HOST .getKeyName ());
682
+ transformedHostProps .remove (PropertyKey .PORT .getKeyName ());
683
+ transformedHostProps .remove (PropertyKey .USER .getKeyName ());
684
+ transformedHostProps .remove (PropertyKey .PASSWORD .getKeyName ());
685
+
698
686
hostProps = transformedHostProps ;
699
687
}
700
688
0 commit comments