brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [1/4] brooklyn-server git commit: Terminate winrm client context on Brooklyn exit
Date Tue, 07 Mar 2017 21:12:23 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master dc3f44615 -> b55e234b3


Terminate winrm client context on Brooklyn exit

Update to new Winrm4j API, sharing a single context for all clients.


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/db79f2c4
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/db79f2c4
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/db79f2c4

Branch: refs/heads/master
Commit: db79f2c4e0c7c1b2a91156409f23a560df9d9291
Parents: dc3f446
Author: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Authored: Fri Feb 24 22:36:20 2017 +0200
Committer: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Committed: Tue Mar 7 23:10:36 2017 +0200

----------------------------------------------------------------------
 .../location/winrm/WinRmMachineLocation.java    |  4 ++
 .../internal/winrm/winrm4j/Winrm4jTool.java     | 50 +++++++++++++++++---
 2 files changed, 47 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/db79f2c4/software/winrm/src/main/java/org/apache/brooklyn/location/winrm/WinRmMachineLocation.java
----------------------------------------------------------------------
diff --git a/software/winrm/src/main/java/org/apache/brooklyn/location/winrm/WinRmMachineLocation.java
b/software/winrm/src/main/java/org/apache/brooklyn/location/winrm/WinRmMachineLocation.java
index 8d537ac..d6a1f7f 100644
--- a/software/winrm/src/main/java/org/apache/brooklyn/location/winrm/WinRmMachineLocation.java
+++ b/software/winrm/src/main/java/org/apache/brooklyn/location/winrm/WinRmMachineLocation.java
@@ -44,6 +44,7 @@ import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
 import org.apache.brooklyn.core.location.AbstractLocation;
 import org.apache.brooklyn.core.location.access.PortForwardManager;
 import org.apache.brooklyn.core.location.access.PortForwardManagerLocationResolver;
+import org.apache.brooklyn.core.mgmt.ManagementContextInjectable;
 import org.apache.brooklyn.util.core.ClassLoaderUtils;
 import org.apache.brooklyn.util.core.config.ConfigBag;
 import org.apache.brooklyn.util.core.internal.ssh.SshTool;
@@ -355,6 +356,9 @@ public class WinRmMachineLocation extends AbstractLocation implements
MachineLoc
             String toolClass = args.get(WINRM_TOOL_CLASS);
             if (toolClass == null) toolClass = Winrm4jTool.class.getName();
             WinRmTool tool = (WinRmTool) new ClassLoaderUtils(this, getManagementContext()).loadClass(toolClass).getConstructor(Map.class).newInstance(args.getAllConfig());
+            if (tool instanceof ManagementContextInjectable) {
+                ((ManagementContextInjectable)tool).setManagementContext(getManagementContext());
+            }
 
             if (LOG.isTraceEnabled()) LOG.trace("using ssh-tool {} (of type {}); props ",
tool, toolClass);
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/db79f2c4/software/winrm/src/main/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/Winrm4jTool.java
----------------------------------------------------------------------
diff --git a/software/winrm/src/main/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/Winrm4jTool.java
b/software/winrm/src/main/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/Winrm4jTool.java
index ea1fa99..970ae3d 100644
--- a/software/winrm/src/main/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/Winrm4jTool.java
+++ b/software/winrm/src/main/java/org/apache/brooklyn/util/core/internal/winrm/winrm4j/Winrm4jTool.java
@@ -26,12 +26,17 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.config.Sanitizer;
+import org.apache.brooklyn.core.internal.BrooklynProperties;
+import org.apache.brooklyn.core.mgmt.ManagementContextInjectable;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
 import org.apache.brooklyn.util.core.config.ConfigBag;
 import org.apache.brooklyn.util.core.internal.winrm.WinRmException;
 import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.javalang.Threads;
 import org.apache.brooklyn.util.text.Strings;
 import org.apache.brooklyn.util.time.Duration;
 import org.apache.brooklyn.util.time.Time;
@@ -46,13 +51,16 @@ import com.google.common.base.Stopwatch;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 
+import io.cloudsoft.winrm4j.client.WinRmClientContext;
 import io.cloudsoft.winrm4j.winrm.WinRmTool;
 import io.cloudsoft.winrm4j.winrm.WinRmToolResponse;
 
 @Beta
-public class Winrm4jTool implements org.apache.brooklyn.util.core.internal.winrm.WinRmTool
{
+public class Winrm4jTool implements org.apache.brooklyn.util.core.internal.winrm.WinRmTool,
ManagementContextInjectable {
 
     private static final Logger LOG = LoggerFactory.getLogger(Winrm4jTool.class);
+    
+    private static final ConfigKey<WinRmClientContext> CONTEXT = ConfigKeys.newConfigKey(WinRmClientContext.class,
"winrm.context");
 
     // TODO Should we move this up to the interface?
     @Beta
@@ -76,6 +84,8 @@ public class Winrm4jTool implements org.apache.brooklyn.util.core.internal.winrm
     private final Integer retriesOfNetworkFailures;
     private final Map<String, String> environment;
 
+    private ManagementContext mgmt;
+
     public Winrm4jTool(Map<String,?> config) {
         this(ConfigBag.newInstance(config));
     }
@@ -96,7 +106,12 @@ public class Winrm4jTool implements org.apache.brooklyn.util.core.internal.winrm
         retriesOfNetworkFailures = config.get(RETRIES_OF_NETWORK_FAILURES);
         environment = config.get(ENVIRONMENT);
     }
-    
+
+    @Override
+    public void setManagementContext(ManagementContext managementContext) {
+        this.mgmt = managementContext;
+    }
+
     @Override
     public org.apache.brooklyn.util.core.internal.winrm.WinRmToolResponse executeCommand(final
List<String> commands) {
         return exec(new Function<io.cloudsoft.winrm4j.winrm.WinRmTool, io.cloudsoft.winrm4j.winrm.WinRmToolResponse>()
{
@@ -200,10 +215,12 @@ public class Winrm4jTool implements org.apache.brooklyn.util.core.internal.winrm
     }
 
     private io.cloudsoft.winrm4j.winrm.WinRmTool connect() {
-        WinRmTool.Builder builder = WinRmTool.Builder.builder(host, computerName, user, password);
-        builder.setAuthenticationScheme(authenticationScheme);
-        builder.useHttps(useSecureWinrm);
-        builder.port(port);
+        WinRmClientContext context = createWinrmContext(mgmt);
+        WinRmTool.Builder builder = WinRmTool.Builder.builder(host, computerName, user, password)
+                .context(context)
+                .setAuthenticationScheme(authenticationScheme)
+                .useHttps(useSecureWinrm)
+                .port(port);
         if (environment != null) {
             builder.environment(environment);
         }
@@ -215,7 +232,25 @@ public class Winrm4jTool implements org.apache.brooklyn.util.core.internal.winrm
         }
         return builder.build();
     }
-    
+
+    private static synchronized WinRmClientContext createWinrmContext(ManagementContext mgmt)
{
+        // TODO Use getScratchpad()
+        BrooklynProperties props = ((ManagementContextInternal)mgmt).getBrooklynProperties();
+        WinRmClientContext instance = props.getConfig(CONTEXT);
+        if (instance == null) {
+            final WinRmClientContext newContext = WinRmClientContext.newInstance();
+            instance = newContext;
+            props.put(CONTEXT, instance);
+            Threads.addShutdownHook(new Runnable() {
+                @Override
+                public void run() {
+                    newContext.shutdown();
+                }
+            });
+        }
+        return instance;
+    }
+
     private <T> T getRequiredConfig(ConfigBag bag, ConfigKey<T> key) {
         T result = bag.get(key);
         if (result == null) {
@@ -241,4 +276,5 @@ public class Winrm4jTool implements org.apache.brooklyn.util.core.internal.winrm
         Exceptions.propagateIfFatal(e);
         throw new WinRmException("(" + toString() + ") " + message + ": " + e.getMessage(),
e);
     }
+
 }


Mime
View raw message