Repository: karaf Updated Branches: refs/heads/karaf-3.0.x 805a3db98 -> 6d5b4bfe8
[KARAF-3254] Very high memory consumption with the watch command Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/6d5b4bfe Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/6d5b4bfe Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/6d5b4bfe Branch: refs/heads/karaf-3.0.x Commit: 6d5b4bfe882f0595f00d1c650e9d57a21d770846 Parents: 805a3db Author: Guillaume Nodet <[email protected]> Authored: Mon Oct 13 13:33:39 2014 +0200 Committer: Guillaume Nodet <[email protected]> Committed: Mon Oct 13 13:59:33 2014 +0200 ---------------------------------------------------------------------- .../karaf/shell/commands/impl/WatchAction.java | 18 +++++++------- .../impl/SecuredCommandProcessorImpl.java | 26 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/6d5b4bfe/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/WatchAction.java ---------------------------------------------------------------------- diff --git a/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/WatchAction.java b/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/WatchAction.java index 7edcb9b..86bb562 100644 --- a/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/WatchAction.java +++ b/shell/commands/src/main/java/org/apache/karaf/shell/commands/impl/WatchAction.java @@ -86,13 +86,15 @@ public class WatchAction extends AbstractAction { public void run() { try { - byteArrayOutputStream = new ByteArrayOutputStream(); - printStream = new PrintStream(byteArrayOutputStream); - session = commandProcessor.createSession(null, printStream, printStream); - session.put("SCOPE", "shell:bundle:*"); - String output = ""; + if (session == null) { + byteArrayOutputStream = new ByteArrayOutputStream(); + printStream = new PrintStream(byteArrayOutputStream); + session = commandProcessor.createSession(null, printStream, printStream); + session.put("SCOPE", "shell:bundle:*"); + } + byteArrayOutputStream.reset(); session.execute(command); - output = byteArrayOutputStream.toString(); + String output = byteArrayOutputStream.toString(); if (doDisplay) { if (!append) { System.out.print("\33[2J"); @@ -101,10 +103,8 @@ public class WatchAction extends AbstractAction { System.out.print(output); System.out.flush(); } - byteArrayOutputStream.close(); - session.close(); } catch (Exception e) { - //Ingore + //Ignore } } http://git-wip-us.apache.org/repos/asf/karaf/blob/6d5b4bfe/shell/console/src/main/java/org/apache/karaf/shell/security/impl/SecuredCommandProcessorImpl.java ---------------------------------------------------------------------- diff --git a/shell/console/src/main/java/org/apache/karaf/shell/security/impl/SecuredCommandProcessorImpl.java b/shell/console/src/main/java/org/apache/karaf/shell/security/impl/SecuredCommandProcessorImpl.java index 0e16a56..e6aba9b 100644 --- a/shell/console/src/main/java/org/apache/karaf/shell/security/impl/SecuredCommandProcessorImpl.java +++ b/shell/console/src/main/java/org/apache/karaf/shell/security/impl/SecuredCommandProcessorImpl.java @@ -19,8 +19,10 @@ package org.apache.karaf.shell.security.impl; import org.apache.felix.gogo.api.CommandSessionListener; import org.apache.felix.gogo.runtime.CommandProcessorImpl; import org.apache.felix.gogo.runtime.CommandProxy; +import org.apache.felix.gogo.runtime.CommandSessionImpl; import org.apache.felix.gogo.runtime.activator.Activator; import org.apache.felix.service.command.CommandProcessor; +import org.apache.felix.service.command.CommandSession; import org.apache.felix.service.command.Converter; import org.apache.felix.service.threadio.ThreadIO; import org.apache.karaf.jaas.boot.principal.RolePrincipal; @@ -32,6 +34,9 @@ import org.osgi.framework.ServiceReference; import org.osgi.util.tracker.ServiceTracker; import javax.security.auth.Subject; + +import java.io.InputStream; +import java.io.PrintStream; import java.security.AccessControlContext; import java.security.AccessController; import java.util.ArrayList; @@ -42,6 +47,10 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +/** + * Note: this CommandProcessor can only be used to create a single session, as closing the + * session will also close the command processor. + */ public class SecuredCommandProcessorImpl extends CommandProcessorImpl { private final BundleContext bundleContext; @@ -102,6 +111,23 @@ public class SecuredCommandProcessorImpl extends CommandProcessorImpl { listenerTracker.close(); } + public CommandSession createSession(InputStream in, PrintStream out, PrintStream err) { + synchronized (sessions) { + if (stopped) { + throw new IllegalStateException("CommandProcessor has been stopped"); + } + CommandSessionImpl session = new CommandSessionImpl(this, in, out, err) { + @Override + public void close() { + super.close(); + SecuredCommandProcessorImpl.this.close(); + } + }; + sessions.put(session, null); + return session; + } + } + private ServiceTracker<Object, Object> trackCommands(final BundleContext context, String roleClause) throws InvalidSyntaxException { Filter filter = context.createFilter(String.format("(&(%s=*)(%s=*)%s)", CommandProcessor.COMMAND_SCOPE, CommandProcessor.COMMAND_FUNCTION, roleClause));
