Skip to content

Commit a062e98

Browse files
author
Denis Bazhenov
committed
Merge branch 'ssh-2.7'
2 parents 6726133 + 9fb27da commit a062e98

File tree

5 files changed

+42
-77
lines changed

5 files changed

+42
-77
lines changed

groovy-shell-server/src/main/java/me/bazhenov/groovysh/GroovyShellCommand.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static java.util.Collections.singletonList;
44
import static me.bazhenov.groovysh.GroovyShellService.SHELL_KEY;
5+
import static org.codehaus.groovy.tools.shell.IO.Verbosity.DEBUG;
56

67
import groovy.lang.Binding;
78
import groovy.lang.Closure;
@@ -14,46 +15,37 @@
1415
import java.util.List;
1516
import java.util.Map;
1617
import java.util.concurrent.atomic.AtomicBoolean;
17-
import me.bazhenov.groovysh.thread.ServerSessionAwareThreadFactory;
1818
import org.apache.groovy.groovysh.Groovysh;
1919
import org.apache.sshd.common.SshException;
2020
import org.apache.sshd.common.session.helpers.AbstractSession;
2121
import org.apache.sshd.server.Environment;
2222
import org.apache.sshd.server.ExitCallback;
23-
import org.apache.sshd.server.SessionAware;
2423
import org.apache.sshd.server.SshServer;
24+
import org.apache.sshd.server.channel.ChannelSession;
2525
import org.apache.sshd.server.command.Command;
26-
import org.apache.sshd.server.session.ServerSession;
2726
import org.codehaus.groovy.tools.shell.IO;
2827

29-
class GroovyShellCommand implements Command, SessionAware {
28+
class GroovyShellCommand implements Command {
3029

3130
private final SshServer sshd;
3231
private final Map<String, Object> bindings;
3332
private final List<String> defaultScripts;
34-
private final ServerSessionAwareThreadFactory threadFactory;
3533
private InputStream in;
3634
private OutputStream out;
3735
private OutputStream err;
3836
private ExitCallback callback;
3937
private Thread wrapper;
40-
private ServerSession session;
38+
private ChannelSession session;
4139
private final AtomicBoolean isChannelAlive;
4240

4341
GroovyShellCommand(SshServer sshd, Map<String, Object> bindings, List<String> defaultScripts,
44-
ServerSessionAwareThreadFactory threadFactory, AtomicBoolean isChannelAlive) {
42+
AtomicBoolean isChannelAlive) {
4543
this.sshd = sshd;
4644
this.bindings = bindings;
4745
this.defaultScripts = defaultScripts;
48-
this.threadFactory = threadFactory;
4946
this.isChannelAlive = isChannelAlive;
5047
}
5148

52-
@Override
53-
public void setSession(ServerSession session) {
54-
this.session = session;
55-
}
56-
5749
@Override
5850
public void setInputStream(InputStream in) {
5951
this.in = in;
@@ -75,12 +67,13 @@ public void setExitCallback(ExitCallback callback) {
7567
}
7668

7769
@Override
78-
public void start(final Environment env) throws IOException {
70+
public void start(ChannelSession session, Environment env) throws IOException {
71+
this.session = session;
7972
TtyFilterOutputStream out = new TtyFilterOutputStream(this.out, isChannelAlive);
8073
TtyFilterOutputStream err = new TtyFilterOutputStream(this.err, isChannelAlive);
8174

8275
IO io = new IO(in, out, err);
83-
io.setVerbosity(IO.Verbosity.DEBUG);
76+
io.setVerbosity(DEBUG);
8477
Groovysh shell = new Groovysh(createBinding(bindings, out, err), io);
8578
shell.setErrorHook(new Closure<Object>(this) {
8679
@Override
@@ -100,29 +93,36 @@ public Object call(Object... args) {
10093
+ e.getClass().getName() + ": " + e.getMessage());
10194
}
10295

103-
session.setAttribute(SHELL_KEY, shell);
96+
this.session.setAttribute(SHELL_KEY, shell);
10497

105-
wrapper = threadFactory.newThread(() -> {
98+
Runnable runnable = () -> {
10699
try {
107100
SshTerminal.registerEnvironment(env);
108101
shell.run("");
109102
callback.onExit(0);
110103
} catch (RuntimeException | Error e) {
111104
callback.onExit(-1, e.getMessage());
112105
}
113-
}, session);
106+
};
107+
wrapper = newThread(runnable, this.session);
114108
wrapper.start();
115109
}
116110

111+
private static Thread newThread(Runnable r, ChannelSession session) {
112+
String address = session.getSession().getIoSession().getRemoteAddress().toString();
113+
String threadName = "GroovySh Client Thread: " + address;
114+
return new Thread(r, threadName);
115+
}
116+
117117
private Binding createBinding(Map<String, Object> objects, OutputStream out, OutputStream err)
118118
throws UnsupportedEncodingException {
119119
Binding binding = new Binding();
120120

121-
if (objects != null) {
122-
for (Map.Entry<String, Object> row : objects.entrySet()) {
123-
binding.setVariable(row.getKey(), row.getValue());
124-
}
125-
}
121+
if (objects != null) {
122+
for (Map.Entry<String, Object> row : objects.entrySet()) {
123+
binding.setVariable(row.getKey(), row.getValue());
124+
}
125+
}
126126

127127
binding.setVariable("out", createPrintStream(out));
128128
binding.setVariable("err", createPrintStream(err));
@@ -167,7 +167,7 @@ public Groovysh call(Object... args) {
167167
}
168168

169169
@Override
170-
public void destroy() {
170+
public void destroy(ChannelSession channel) {
171171
wrapper.interrupt();
172172
}
173173
}

groovy-shell-server/src/main/java/me/bazhenov/groovysh/GroovyShellService.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
import static jline.TerminalFactory.Flavor.UNIX;
1919
import static jline.TerminalFactory.registerFlavor;
2020
import static org.apache.groovy.groovysh.util.PackageHelper.IMPORT_COMPLETION_PREFERENCE_KEY;
21-
import static org.apache.sshd.common.FactoryManager.IDLE_TIMEOUT;
22-
import static org.apache.sshd.common.FactoryManager.NIO2_READ_TIMEOUT;
21+
import static org.apache.sshd.common.PropertyResolverUtils.updateProperty;
22+
import static org.apache.sshd.core.CoreModuleProperties.IDLE_TIMEOUT;
23+
import static org.apache.sshd.core.CoreModuleProperties.NIO2_READ_TIMEOUT;
2324
import static org.apache.sshd.server.SshServer.setUpDefaultServer;
2425

2526
import java.io.File;
@@ -28,21 +29,18 @@
2829
import java.util.List;
2930
import java.util.Map;
3031
import java.util.concurrent.atomic.AtomicBoolean;
31-
import me.bazhenov.groovysh.thread.DefaultGroovyshThreadFactory;
32-
import me.bazhenov.groovysh.thread.ServerSessionAwareThreadFactory;
3332
import org.apache.groovy.groovysh.Groovysh;
34-
import org.apache.sshd.common.Factory;
35-
import org.apache.sshd.common.NamedFactory;
36-
import org.apache.sshd.common.PropertyResolverUtils;
3733
import org.apache.sshd.common.session.Session;
3834
import org.apache.sshd.common.session.SessionListener;
3935
import org.apache.sshd.server.SshServer;
40-
import org.apache.sshd.server.auth.UserAuth;
36+
import org.apache.sshd.server.auth.UserAuthFactory;
4137
import org.apache.sshd.server.auth.UserAuthNoneFactory;
4238
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
4339
import org.apache.sshd.server.auth.password.UserAuthPasswordFactory;
40+
import org.apache.sshd.server.channel.ChannelSession;
4441
import org.apache.sshd.server.command.Command;
4542
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
43+
import org.apache.sshd.server.shell.ShellFactory;
4644
import org.codehaus.groovy.tools.shell.util.Preferences;
4745

4846
/**
@@ -57,7 +55,6 @@ public class GroovyShellService {
5755
private String host;
5856
private Map<String, Object> bindings;
5957
private PasswordAuthenticator passwordAuthenticator;
60-
private ServerSessionAwareThreadFactory threadFactory = new DefaultGroovyshThreadFactory();
6158

6259
static final Session.AttributeKey<Groovysh> SHELL_KEY = new Session.AttributeKey<>();
6360
private List<String> defaultScripts = new ArrayList<>();
@@ -136,10 +133,6 @@ public void setPasswordAuthenticator(PasswordAuthenticator passwordAuthenticator
136133
this.passwordAuthenticator = passwordAuthenticator;
137134
}
138135

139-
public void setThreadFactory(ServerSessionAwareThreadFactory threadFactory) {
140-
this.threadFactory = threadFactory;
141-
}
142-
143136
public void setDefaultScripts(List<String> defaultScriptNames) {
144137
this.defaultScripts = defaultScriptNames;
145138
}
@@ -152,9 +145,9 @@ public void setDefaultScripts(List<String> defaultScriptNames) {
152145
public synchronized void start() throws IOException {
153146
sshd = buildSshServer();
154147
sshd.start();
155-
if (disableImportCompletions) {
156-
Preferences.put(IMPORT_COMPLETION_PREFERENCE_KEY, "true");
157-
}
148+
if (disableImportCompletions) {
149+
Preferences.put(IMPORT_COMPLETION_PREFERENCE_KEY, "true");
150+
}
158151
}
159152

160153
private SshServer buildSshServer() {
@@ -165,9 +158,8 @@ private SshServer buildSshServer() {
165158
}
166159

167160
long idleTimeOut = HOURS.toMillis(1);
168-
PropertyResolverUtils.updateProperty(sshd, IDLE_TIMEOUT, idleTimeOut);
169-
PropertyResolverUtils
170-
.updateProperty(sshd, NIO2_READ_TIMEOUT, idleTimeOut + SECONDS.toMillis(15L));
161+
updateProperty(sshd, IDLE_TIMEOUT.getName(), idleTimeOut);
162+
updateProperty(sshd, NIO2_READ_TIMEOUT.getName(), idleTimeOut + SECONDS.toMillis(15L));
171163

172164
sshd.addSessionListener(new SessionListener() {
173165
@Override
@@ -185,9 +177,9 @@ public void sessionException(Session session, Throwable t) {
185177
@Override
186178
public void sessionClosed(Session session) {
187179
Groovysh shell = session.getAttribute(SHELL_KEY);
188-
if (shell != null) {
189-
shell.getRunner().setRunning(false);
190-
}
180+
if (shell != null) {
181+
shell.getRunner().setRunning(false);
182+
}
191183
}
192184
});
193185

@@ -198,7 +190,7 @@ public void sessionClosed(Session session) {
198190
}
199191

200192
private void configureAuthentication(SshServer sshd) {
201-
NamedFactory<UserAuth> auth;
193+
UserAuthFactory auth;
202194
if (this.passwordAuthenticator != null) {
203195
sshd.setPasswordAuthenticator(this.passwordAuthenticator);
204196
auth = new UserAuthPasswordFactory();
@@ -213,11 +205,11 @@ public synchronized void destroy() throws IOException {
213205
sshd.stop(true);
214206
}
215207

216-
class GroovyShellFactory implements Factory<Command> {
208+
class GroovyShellFactory implements ShellFactory {
217209

218210
@Override
219-
public Command create() {
220-
return new GroovyShellCommand(sshd, bindings, defaultScripts, threadFactory, isServiceAlive);
211+
public Command createShell(ChannelSession channel) {
212+
return new GroovyShellCommand(sshd, bindings, defaultScripts, isServiceAlive);
221213
}
222214
}
223215
}

groovy-shell-server/src/main/java/me/bazhenov/groovysh/spring/GroovyShellServiceBean.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.HashMap;
66
import java.util.Map;
77
import me.bazhenov.groovysh.GroovyShellService;
8-
import me.bazhenov.groovysh.thread.ServerSessionAwareThreadFactory;
98
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
109
import org.springframework.beans.BeansException;
1110
import org.springframework.beans.factory.BeanIsAbstractException;
@@ -69,10 +68,6 @@ public void setPasswordAuthenticator(PasswordAuthenticator passwordAuthenticator
6968
service.setPasswordAuthenticator(passwordAuthenticator);
7069
}
7170

72-
public void setThreadFactory(ServerSessionAwareThreadFactory threadFactory) {
73-
service.setThreadFactory(threadFactory);
74-
}
75-
7671
/**
7772
* Set the comma delimited list of default scripts
7873
*

groovy-shell-server/src/main/java/me/bazhenov/groovysh/thread/DefaultGroovyshThreadFactory.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

groovy-shell-server/src/main/java/me/bazhenov/groovysh/thread/ServerSessionAwareThreadFactory.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)