Skip to content

Commit

Permalink
KARAF-7174: Implement a SSH channel resource clearner whiteboard
Browse files Browse the repository at this point in the history
  • Loading branch information
jbonofre committed Nov 12, 2024
1 parent a8d9901 commit 40a0e02
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
13 changes: 12 additions & 1 deletion log/src/main/java/org/apache/karaf/log/command/LogTail.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.apache.karaf.shell.api.console.ChannelResourceCleaner;
import org.apache.karaf.shell.api.console.Session;
import org.ops4j.pax.logging.spi.PaxAppender;
import org.osgi.framework.BundleContext;
Expand All @@ -31,7 +32,8 @@

@Command(scope = "log", name = "tail", description = "Continuously display log entries. Use ctrl-c to quit this command")
@Service
public class LogTail extends DisplayLog {
public class LogTail extends DisplayLog implements ChannelResourceCleaner {

@Reference
Session session;

Expand All @@ -52,6 +54,9 @@ public Object execute() throws Exception {
PaxAppender appender = event -> printEvent(out, event, minLevel);
ServiceTracker<LogService, LogService> tracker = new LogServiceTracker(context, LogService.class, null, appender);
tracker.open();

context.registerService(ChannelResourceCleaner.class, this, null);

try {
synchronized (this) {
wait();
Expand All @@ -65,6 +70,12 @@ public Object execute() throws Exception {
out.println();
return null;
}

@Override
public void close() {
System.out.println("STOP TAIL");
stopTail();
}

private synchronized void stopTail() {
notifyAll();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.karaf.shell.api.console;

public interface ChannelResourceCleaner {

void close();

}
19 changes: 19 additions & 0 deletions shell/ssh/src/main/java/org/apache/karaf/shell/ssh/Activator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;

import org.apache.karaf.shell.api.action.lifecycle.Manager;
import org.apache.karaf.shell.api.console.ChannelResourceCleaner;
import org.apache.karaf.shell.api.console.CommandLoggingFilter;
import org.apache.karaf.shell.api.console.Session;
import org.apache.karaf.shell.api.console.SessionFactory;
Expand All @@ -34,8 +36,11 @@
import org.apache.karaf.util.tracker.annotation.Managed;
import org.apache.karaf.util.tracker.annotation.RequireService;
import org.apache.karaf.util.tracker.annotation.Services;
import org.apache.sshd.common.channel.Channel;
import org.apache.sshd.common.channel.ChannelListener;
import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.common.keyprovider.KeyPairProvider;
import org.apache.sshd.common.session.SessionListener;
import org.apache.sshd.core.CoreModuleProperties;
import org.apache.sshd.scp.server.ScpCommandFactory;
import org.apache.sshd.server.SshServer;
Expand Down Expand Up @@ -189,6 +194,20 @@ protected SshServer createSshServer(SessionFactory sessionFactory) {
server.setKeyExchangeFactories(SshUtils.buildKexAlgorithms(kexAlgorithms));
server.setSignatureFactories(SshUtils.buildSigAlgorithms(sigAlgorithms));
server.setShellFactory(new ShellFactoryImpl(sessionFactory));
server.addSessionListener(new SessionListener() {
@Override
public void sessionDisconnect(org.apache.sshd.common.session.Session session, int reason, String msg, String language, boolean initiator) {
try {
Collection<ServiceReference<ChannelResourceCleaner>> references = bundleContext.getServiceReferences(ChannelResourceCleaner.class, null);
for (ServiceReference<ChannelResourceCleaner> reference : references) {
ChannelResourceCleaner cleaner = bundleContext.getService(reference);
cleaner.close();
}
} catch (Exception e) {
// no-op
}
}
});

if (sftpEnabled) {
server.setCommandFactory(new ScpCommandFactory.Builder().withDelegate((channel, cmd) -> new ShellCommand(sessionFactory, cmd)).build());
Expand Down

0 comments on commit 40a0e02

Please sign in to comment.