UNOMI-150 Close http client in main migration util

Manage the HTTP client lifecycle in central migration script instead of
individual scripts.


Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/b463b11e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/b463b11e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/b463b11e

Branch: refs/heads/master
Commit: b463b11ef0d11203e72199fa569f013312cdc3a3
Parents: a865027
Author: Jarek Lipski <jlip...@jahia.com>
Authored: Thu Feb 8 13:27:21 2018 +0100
Committer: Jarek Lipski <jlip...@jahia.com>
Committed: Thu Feb 8 13:27:21 2018 +0100

----------------------------------------------------------------------
 .../unomi/shell/migration/actions/Migrate.java  | 25 +++++++++++---------
 .../shell/migration/impl/MigrationTo121.java    | 15 ++++--------
 .../shell/migration/impl/MigrationTo122.java    | 15 ++++--------
 3 files changed, 22 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b463b11e/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/actions/Migrate.java
----------------------------------------------------------------------
diff --git 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/actions/Migrate.java
 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/actions/Migrate.java
index 63142a1..dd556fb 100644
--- 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/actions/Migrate.java
+++ 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/actions/Migrate.java
@@ -52,20 +52,23 @@ public class Migrate extends OsgiCommandSupport {
             return null;
         }
 
-        CloseableHttpClient httpClient = HttpUtils.initHttpClient(session);
+        CloseableHttpClient httpClient = null;
+        try {
+            httpClient = HttpUtils.initHttpClient(session);
 
-        String esAddress = ConsoleUtils.askUserWithDefaultAnswer(session, 
"Elasticsearch address (default = http://localhost:9200): ", 
"http://localhost:9200";);
+            String esAddress = ConsoleUtils.askUserWithDefaultAnswer(session, 
"Elasticsearch address (default = http://localhost:9200): ", 
"http://localhost:9200";);
 
-        for (Migration migration : migrations) {
-            if (fromVersion.compareTo(migration.getToVersion()) < 0) {
-                System.out.println("Starting migration to version " + 
migration.getToVersion());
-                migration.execute(session, httpClient, esAddress);
-                System.out.println("Migration to version " + 
migration.getToVersion() + " done successfully");
+            for (Migration migration : migrations) {
+                if (fromVersion.compareTo(migration.getToVersion()) < 0) {
+                    System.out.println("Starting migration to version " + 
migration.getToVersion());
+                    migration.execute(session, httpClient, esAddress);
+                    System.out.println("Migration to version " + 
migration.getToVersion() + " done successfully");
+                }
+            }
+        } finally {
+            if (httpClient != null) {
+                httpClient.close();
             }
-        }
-
-        if (httpClient != null) {
-            httpClient.close();
         }
 
         return null;

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b463b11e/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo121.java
----------------------------------------------------------------------
diff --git 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo121.java
 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo121.java
index a2b1111..b2cd923 100644
--- 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo121.java
+++ 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo121.java
@@ -52,17 +52,10 @@ public class MigrationTo121 implements Migration {
 
     @Override
     public void execute(CommandSession session, CloseableHttpClient 
httpClient, String esAddress) throws IOException {
-        try {
-            this.httpClient = httpClient;
-            this.session = session;
-            this.esAddress = esAddress;
-            migrateTags();
-        } catch (IOException e) {
-            if (httpClient != null) {
-                httpClient.close();
-            }
-            throw e;
-        }
+        this.httpClient = httpClient;
+        this.session = session;
+        this.esAddress = esAddress;
+        migrateTags();
     }
 
     private void migrateTags() throws IOException {

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/b463b11e/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo122.java
----------------------------------------------------------------------
diff --git 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo122.java
 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo122.java
index dec2a6a..95c3a53 100644
--- 
a/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo122.java
+++ 
b/tools/shell-commands/src/main/java/org/apache/unomi/shell/migration/impl/MigrationTo122.java
@@ -43,17 +43,10 @@ public class MigrationTo122 implements Migration {
 
     @Override
     public void execute(CommandSession session, CloseableHttpClient 
httpClient, String esAddress) throws IOException {
-        try {
-            this.httpClient = httpClient;
-            this.session = session;
-            this.esAddress = esAddress;
-            deleteOldIndexTemplate();
-        } catch (IOException e) {
-            if (httpClient != null) {
-                httpClient.close();
-            }
-            throw e;
-        }
+        this.httpClient = httpClient;
+        this.session = session;
+        this.esAddress = esAddress;
+        deleteOldIndexTemplate();
 
     }
 

Reply via email to