2323import java .io .InputStream ;
2424import java .net .HttpURLConnection ;
2525import java .net .URL ;
26- import java .sql .Connection ;
2726import java .sql .SQLException ;
2827import java .sql .Savepoint ;
2928import java .time .Clock ;
3736import java .util .Set ;
3837import java .util .concurrent .ConcurrentHashMap ;
3938import java .util .function .Function ;
39+ import java .util .function .Supplier ;
4040import java .util .stream .Collectors ;
4141import java .util .stream .Stream ;
4242import java .util .zip .ZipInputStream ;
@@ -413,6 +413,7 @@ private boolean downloadCourtTables(
413413 /** Returns true if successful, false if not successful */
414414 public boolean updateAll (String baseUrl , FilingReviewMDEPort filingPort , CodeDatabase cd )
415415 throws SQLException , IOException , JAXBException {
416+ cd .setAutoCommit (false );
416417 HeaderSigner signer = new HeaderSigner (this .pathToKeystore , this .x509Password );
417418 if (!downloadSystemTables (baseUrl , cd , signer )) {
418419 log .warn (
@@ -496,6 +497,7 @@ public boolean replaceAll(String baseUrl, FilingReviewMDEPort filingPort, CodeDa
496497 public boolean replaceSome (
497498 String baseUrl , FilingReviewMDEPort filingPort , CodeDatabase cd , List <String > locs )
498499 throws SQLException , IOException , JAXBException {
500+ cd .setAutoCommit (false );
499501 HeaderSigner signer = new HeaderSigner (this .pathToKeystore , this .x509Password );
500502 log .info ("Downloading system tables for {}" , cd .getDomain ());
501503 boolean success = downloadSystemTables (baseUrl , cd , signer );
@@ -598,11 +600,14 @@ public boolean downloadIndiv(List<String> args, String jurisdiction, TylerEnv en
598600 }
599601
600602 public static boolean executeCommand (
601- CodeDatabase cd , String jurisdiction , TylerEnv env , List <String > args , String x509Password ) {
603+ Supplier <CodeDatabase > cdSupplier ,
604+ String jurisdiction ,
605+ TylerEnv env ,
606+ List <String > args ,
607+ String x509Password ) {
602608 SoapX509CallbackHandler .setX509Password (x509Password );
603609 String command = args .get (0 );
604610 try {
605- cd .setAutoCommit (false );
606611 String codesSite = TylerClients .getTylerServerRootUrl (jurisdiction , env );
607612 FilingReviewMDEPort filingPort =
608613 loginWithTyler (
@@ -612,11 +617,12 @@ public static boolean executeCommand(
612617 System .getenv ("TYLER_USER_PASSWORD" ));
613618 CodeUpdater cu = new CodeUpdater (System .getenv ("PATH_TO_KEYSTORE" ), x509Password );
614619 if (command .equalsIgnoreCase ("replaceall" )) {
615- return cu .replaceAll (codesSite , filingPort , cd );
620+ return cu .replaceAll (codesSite , filingPort , cdSupplier . get () );
616621 } else if (command .equalsIgnoreCase ("replacesome" )) {
617- return cu .replaceSome (codesSite , filingPort , cd , args .subList (1 , args .size ()));
622+ return cu .replaceSome (
623+ codesSite , filingPort , cdSupplier .get (), args .subList (1 , args .size ()));
618624 } else if (command .equalsIgnoreCase ("refresh" )) {
619- return cu .updateAll (codesSite , filingPort , cd );
625+ return cu .updateAll (codesSite , filingPort , cdSupplier . get () );
620626 } else if (command .equalsIgnoreCase ("downloadIndiv" )) {
621627 return cu .downloadIndiv (args , jurisdiction , env );
622628 } else {
@@ -629,6 +635,25 @@ public static boolean executeCommand(
629635 }
630636 }
631637
638+ /** Should just be called from main. */
639+ private static CodeDatabase makeCodeDatabase (String jurisdiction , TylerEnv env ) {
640+ try {
641+ DataSource ds =
642+ DatabaseCreator .makeDataSource (
643+ System .getenv ("POSTGRES_URL" ),
644+ Integer .parseInt (System .getenv ("POSTGRES_PORT" )),
645+ System .getenv ("POSTGRES_CODES_DB" ),
646+ System .getenv ("POSTGRES_USER" ),
647+ System .getenv ("POSTGRES_PASSWORD" ),
648+ 5 ,
649+ 100 );
650+
651+ return CodeDatabase .fromDS (jurisdiction , env , ds );
652+ } catch (Exception ex ) {
653+ throw new RuntimeException (ex );
654+ }
655+ }
656+
632657 /**
633658 * Run with:
634659 *
@@ -645,30 +670,19 @@ public static void main(String[] args) throws Exception {
645670 log .error ("Need to pass in a subprogram: downloadIndiv, or refresh" );
646671 System .exit (1 );
647672 }
648- DataSource ds =
649- DatabaseCreator .makeDataSource (
650- System .getenv ("POSTGRES_URL" ),
651- Integer .parseInt (System .getenv ("POSTGRES_PORT" )),
652- System .getenv ("POSTGRES_CODES_DB" ),
653- System .getenv ("POSTGRES_USER" ),
654- System .getenv ("POSTGRES_PASSWORD" ),
655- 5 ,
656- 100 );
657673
658674 List <String > jurisdictions = List .of (System .getenv ("TYLER_JURISDICTIONS" ).split (" " ));
659675 var env = TylerEnv .parse (System .getenv ("TYLER_ENV" ));
660676 for (String jurisdiction : jurisdictions ) {
661677 // Reusing USER for Jurisdiction, SESSION for the court / location, and REQUEST for the table
662678 // name.
663679 MDC .put (MDCWrappers .USER_ID , jurisdiction );
664- try (Connection conn = ds .getConnection ()) {
665- executeCommand (
666- new CodeDatabase (jurisdiction , env , conn ),
667- jurisdiction ,
668- env ,
669- List .of (args ),
670- System .getenv ("X509_PASSWORD" ));
671- }
680+ executeCommand (
681+ () -> makeCodeDatabase (jurisdiction , env ),
682+ jurisdiction ,
683+ env ,
684+ List .of (args ),
685+ System .getenv ("X509_PASSWORD" ));
672686 }
673687 }
674688}
0 commit comments