Lens-549 : Fix Lens client instantiation (Pranav Kumar Agarwal via sharad)
Project: http://git-wip-us.apache.org/repos/asf/incubator-lens/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-lens/commit/1254a1e2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-lens/tree/1254a1e2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-lens/diff/1254a1e2 Branch: refs/heads/current-release-line Commit: 1254a1e2e774752ecb672a0100fffdc7d3779a5c Parents: 4498c44 Author: Pranav Kumar Agarwal <[email protected]> Authored: Sat May 30 16:36:28 2015 +0530 Committer: Sharad Agarwal <[email protected]> Committed: Sat May 30 16:36:28 2015 +0530 ---------------------------------------------------------------------- .../lens/cli/commands/BaseLensCommand.java | 17 +++++--- .../lens/client/LensClientSingletonWrapper.java | 43 +++++++++++--------- 2 files changed, 34 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/1254a1e2/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java ---------------------------------------------------------------------- diff --git a/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java b/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java index 3deddfa..74b4e91 100644 --- a/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java +++ b/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java @@ -22,6 +22,7 @@ import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; + import java.util.Date; import org.apache.lens.client.LensClient; @@ -57,6 +58,8 @@ public class BaseLensCommand implements ExecutionProcessor { protected static boolean isConnectionActive; public static final String DATE_FMT = "yyyy-MM-dd'T'HH:mm:ss:SSS"; + private LensClient lensClient = null; + public static final ThreadLocal<DateFormat> DATE_PARSER = new ThreadLocal<DateFormat>() { @Override @@ -83,7 +86,7 @@ public class BaseLensCommand implements ExecutionProcessor { protected static synchronized void closeClientConnection() { if (isConnectionActive) { log.debug("Request for stopping lens cli received"); - getClient().closeConnection(); + getClientWrapper().getClient().closeConnection(); isConnectionActive = false; } } @@ -92,7 +95,6 @@ public class BaseLensCommand implements ExecutionProcessor { * Instantiates a new base lens command. */ public BaseLensCommand() { - getClient(); mapper = new ObjectMapper(); mapper.setSerializationInclusion(Inclusion.NON_NULL); mapper.setSerializationInclusion(Inclusion.NON_DEFAULT); @@ -112,15 +114,18 @@ public class BaseLensCommand implements ExecutionProcessor { return false; } }); - isConnectionActive = true; } public void setClient(LensClient client) { - getClientWrapper().setClient(client); + lensClient = client; } - public static LensClient getClient() { - return getClientWrapper().getClient(); + public LensClient getClient() { + if (lensClient == null) { + setClient(getClientWrapper().getClient()); + isConnectionActive = true; + } + return lensClient; } public static LensClientSingletonWrapper getClientWrapper() { http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/1254a1e2/lens-client/src/main/java/org/apache/lens/client/LensClientSingletonWrapper.java ---------------------------------------------------------------------- diff --git a/lens-client/src/main/java/org/apache/lens/client/LensClientSingletonWrapper.java b/lens-client/src/main/java/org/apache/lens/client/LensClientSingletonWrapper.java index 165d41d..d9ab5a1 100644 --- a/lens-client/src/main/java/org/apache/lens/client/LensClientSingletonWrapper.java +++ b/lens-client/src/main/java/org/apache/lens/client/LensClientSingletonWrapper.java @@ -50,26 +50,6 @@ public class LensClientSingletonWrapper { * Instantiates a new lens client singleton wrapper. */ LensClientSingletonWrapper() { - try { - client = new LensClient(); - } catch (LensClientServerConnectionException e) { - if (e.getErrorCode() != 401) { - explainFailedAttempt(e); - throw e; - } - // Connecting without password prompt failed. - for (int i = 0; i < MAX_RETRIES; i++) { - try { - client = new LensClient(Credentials.prompt()); - break; - } catch (LensClientServerConnectionException lensClientServerConnectionException) { - explainFailedAttempt(lensClientServerConnectionException); - if (i == MAX_RETRIES - 1) { - throw lensClientServerConnectionException; - } - } - } - } } /** @@ -92,6 +72,29 @@ public class LensClientSingletonWrapper { } public LensClient getClient() { + if (client != null) { + return client; + } + try { + client = new LensClient(); + } catch (LensClientServerConnectionException e) { + if (e.getErrorCode() != 401) { + explainFailedAttempt(e); + throw e; + } + // Connecting without password prompt failed. + for (int i = 0; i < MAX_RETRIES; i++) { + try { + client = new LensClient(Credentials.prompt()); + break; + } catch (LensClientServerConnectionException lensClientServerConnectionException) { + explainFailedAttempt(lensClientServerConnectionException); + if (i == MAX_RETRIES - 1) { + throw lensClientServerConnectionException; + } + } + } + } return client; }
