You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jdo-commits@db.apache.org by ti...@apache.org on 2023/03/09 19:35:01 UTC

[db-jdo] branch main updated: Removed nested try-catch (#69)

This is an automated email from the ASF dual-hosted git repository.

tilmannz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/db-jdo.git


The following commit(s) were added to refs/heads/main by this push:
     new 14473c8a Removed nested try-catch (#69)
14473c8a is described below

commit 14473c8a66e9e35dee03376110ceae3110a7e7db
Author: Tilmann <zo...@gmx.de>
AuthorDate: Thu Mar 9 20:34:55 2023 +0100

    Removed nested try-catch (#69)
---
 api/src/main/java/javax/jdo/JDOHelper.java         | 30 ++++-----
 .../java/org/apache/jdo/exectck/InstallSchema.java | 73 ++++++++++++----------
 2 files changed, 56 insertions(+), 47 deletions(-)

diff --git a/api/src/main/java/javax/jdo/JDOHelper.java b/api/src/main/java/javax/jdo/JDOHelper.java
index 73c19e18..52926f61 100644
--- a/api/src/main/java/javax/jdo/JDOHelper.java
+++ b/api/src/main/java/javax/jdo/JDOHelper.java
@@ -1647,25 +1647,27 @@ public class JDOHelper implements Constants {
      */
     ArrayList<Throwable> exceptions = new ArrayList<>();
     int numberOfJDOEnhancers = 0;
+    Enumeration<URL> urls = null;
     try {
-      Enumeration<URL> urls = getResources(loader, Constants.SERVICE_LOOKUP_ENHANCER_RESOURCE_NAME);
-      if (urls != null) {
-        while (urls.hasMoreElements()) {
-          numberOfJDOEnhancers++;
-          try {
-            String enhancerClassName = getClassNameFromURL(urls.nextElement());
-            Class<?> enhancerClass = forName(enhancerClassName, true, ctrLoader);
-            return (JDOEnhancer) enhancerClass.newInstance();
-          } catch (Throwable ex) {
-            // remember exceptions from failed enhancer invocations
-            exceptions.add(ex);
-          }
-        }
-      }
+      urls = getResources(loader, Constants.SERVICE_LOOKUP_ENHANCER_RESOURCE_NAME);
     } catch (Throwable ex) {
       exceptions.add(ex);
     }
 
+    if (urls != null) {
+      while (urls.hasMoreElements()) {
+        numberOfJDOEnhancers++;
+        try {
+          String enhancerClassName = getClassNameFromURL(urls.nextElement());
+          Class<?> enhancerClass = forName(enhancerClassName, true, ctrLoader);
+          return (JDOEnhancer) enhancerClass.newInstance();
+        } catch (Throwable ex) {
+          // remember exceptions from failed enhancer invocations
+          exceptions.add(ex);
+        }
+      }
+    }
+
     throw new JDOFatalUserException(
         MSG.msg("EXC_GetEnhancerNoValidEnhancerAvailable", numberOfJDOEnhancers),
         exceptions.toArray(new Throwable[exceptions.size()]));
diff --git a/exectck/src/main/java/org/apache/jdo/exectck/InstallSchema.java b/exectck/src/main/java/org/apache/jdo/exectck/InstallSchema.java
index 220a7a33..fd37b94f 100644
--- a/exectck/src/main/java/org/apache/jdo/exectck/InstallSchema.java
+++ b/exectck/src/main/java/org/apache/jdo/exectck/InstallSchema.java
@@ -33,14 +33,12 @@ import org.apache.maven.plugins.annotations.Parameter;
 public class InstallSchema extends AbstractTCKMojo {
 
   private static final String DB_DIR_NAME = "database"; // NOI18N
-
+  /** List of mappings required by the current configuration */
+  protected Collection<String> mappings = new HashSet<>();
   /** Location of TCK generated output. */
   @Parameter(property = "jdo.tck.doInstallSchema", defaultValue = "true", required = true)
   private boolean doInstallSchema;
 
-  /** List of mappings required by the current configuration */
-  protected Collection<String> mappings = new HashSet<String>();
-
   @Override
   public void execute() throws MojoExecutionException {
 
@@ -174,7 +172,7 @@ public class InstallSchema extends AbstractTCKMojo {
       finalizeDB(db, dbDirName);
 
       System.out.println("*> See diagnostic output in " + dbLogsDir + ".");
-      System.out.println("");
+      System.out.println();
     }
   }
 
@@ -250,38 +248,47 @@ public class InstallSchema extends AbstractTCKMojo {
             "*> connection passwd needs to be specified, see file " + sqlFileName);
       }
       List<String> stmts = loader.getStatements();
+      executeSQLStatements(url, user, passwd, stmts, outFileName);
+    } catch (IOException ex) {
+      throw new MojoExecutionException(
+          "*> Failed to load file " + sqlFileName + " " + ex.getLocalizedMessage());
+    }
+  }
 
-      try (Connection conn = DriverManager.getConnection(url, user, passwd);
-          Statement stmt = conn.createStatement();
-          PrintWriter outfile = new PrintWriter(outFileName)) {
-        outfile.println("url=" + url + " user=" + user + " password=" + passwd);
-        for (String s : stmts) {
-          try {
-            outfile.print("loading: " + s);
-            stmt.execute(s);
-            outfile.println(" success");
-          } catch (SQLException ex) {
-            outfile.println(" failure " + ex);
-          }
-        }
-      } catch (SQLException ex) {
-        throw new MojoExecutionException(
-            "*> Failed to initialize JDBC connection, "
-                + "url="
-                + url
-                + " user="
-                + user
-                + " password="
-                + passwd
-                + ": "
-                + ex.getLocalizedMessage());
-      } catch (IOException ex) {
-        throw new MojoExecutionException(
-            "*> Failed to write to the outfile " + outFileName + ": " + ex.getLocalizedMessage());
+  private void executeSQLStatements(
+      String url, String user, String passwd, List<String> stmts, String outFileName)
+      throws MojoExecutionException {
+    try (Connection conn = DriverManager.getConnection(url, user, passwd);
+        Statement stmt = conn.createStatement();
+        PrintWriter outfile = new PrintWriter(outFileName)) {
+      outfile.println("url=" + url + " user=" + user + " password=" + passwd);
+      for (String s : stmts) {
+        executeSQLStatement(outfile, stmt, s);
       }
+    } catch (SQLException ex) {
+      throw new MojoExecutionException(
+          "*> Failed to initialize JDBC connection, "
+              + "url="
+              + url
+              + " user="
+              + user
+              + " password="
+              + passwd
+              + ": "
+              + ex.getLocalizedMessage());
     } catch (IOException ex) {
       throw new MojoExecutionException(
-          "*> Failed to load file " + sqlFileName + " " + ex.getLocalizedMessage());
+          "*> Failed to write to the outfile " + outFileName + ": " + ex.getLocalizedMessage());
+    }
+  }
+
+  private void executeSQLStatement(PrintWriter outfile, Statement stmt, String s) {
+    try {
+      outfile.print("loading: " + s);
+      stmt.execute(s);
+      outfile.println(" success");
+    } catch (SQLException ex) {
+      outfile.println(" failure " + ex);
     }
   }
 }