You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2016/02/21 14:12:21 UTC
[5/5] mina-sshd git commit: [SSHD-654] Expose mechanism to read/write
SSH debug messages via session object
[SSHD-654] Expose mechanism to read/write SSH debug messages via session object
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/2114c101
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/2114c101
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/2114c101
Branch: refs/heads/master
Commit: 2114c10122a015fb6067f038419199b70b22c7c0
Parents: 751ea6a
Author: Lyor Goldstein <ly...@gmail.com>
Authored: Sun Feb 21 15:12:59 2016 +0200
Committer: Lyor Goldstein <ly...@gmail.com>
Committed: Sun Feb 21 15:12:59 2016 +0200
----------------------------------------------------------------------
.../java/org/apache/sshd/client/SshClient.java | 4 +-
.../client/session/AbstractClientSession.java | 2 +-
.../client/session/ClientConnectionService.java | 2 +-
.../sshd/client/session/ClientSessionImpl.java | 2 +-
.../sshd/client/session/SessionFactory.java | 2 +-
.../sshd/common/AbstractFactoryManager.java | 369 ---
.../org/apache/sshd/common/BaseBuilder.java | 1 +
.../org/apache/sshd/common/FactoryManager.java | 2 +
.../sshd/common/config/SshConfigFileReader.java | 2 +-
.../global/AbstractOpenSshHostKeysHandler.java | 2 +-
.../common/impl/AbstractFactoryManager.java | 389 +++
.../common/kex/dh/AbstractDHKeyExchange.java | 2 +-
.../session/AbstractConnectionService.java | 662 -----
...AbstractConnectionServiceRequestHandler.java | 44 -
.../sshd/common/session/AbstractSession.java | 2236 -----------------
.../common/session/AbstractSessionFactory.java | 53 -
.../session/AbstractSessionIoHandler.java | 82 -
.../sshd/common/session/PendingWriteFuture.java | 61 -
.../session/ReservedSessionMessagesHandler.java | 50 +
.../session/ReservedSessionMessagesManager.java | 35 +
.../org/apache/sshd/common/session/Session.java | 26 +-
.../common/session/SessionTimeoutListener.java | 84 -
.../session/impl/AbstractConnectionService.java | 664 +++++
...AbstractConnectionServiceRequestHandler.java | 46 +
.../common/session/impl/AbstractSession.java | 2264 ++++++++++++++++++
.../session/impl/AbstractSessionFactory.java | 53 +
.../session/impl/AbstractSessionIoHandler.java | 82 +
.../common/session/impl/PendingWriteFuture.java | 61 +
.../ReservedSessionMessagesHandlerAdapter.java | 69 +
.../session/impl/SessionTimeoutListener.java | 86 +
.../java/org/apache/sshd/server/SshServer.java | 4 +-
.../global/CancelTcpipForwardHandler.java | 2 +-
.../sshd/server/global/KeepAliveHandler.java | 2 +-
.../server/global/NoMoreSessionsHandler.java | 2 +-
.../sshd/server/global/TcpipForwardHandler.java | 2 +-
.../server/session/AbstractServerSession.java | 2 +-
.../server/session/ServerConnectionService.java | 2 +-
.../sshd/server/session/SessionFactory.java | 2 +-
.../java/org/apache/sshd/client/ClientTest.java | 2 +-
.../common/config/SshConfigFileReaderTest.java | 2 +-
.../common/session/AbstractSessionTest.java | 344 ---
.../ReservedSessionMessagesHandlerTest.java | 248 ++
.../session/impl/AbstractSessionTest.java | 345 +++
.../java/org/apache/sshd/server/ServerTest.java | 4 +-
44 files changed, 4442 insertions(+), 3958 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java b/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java
index b3565c5..2f9bd8f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/SshClient.java
@@ -87,7 +87,6 @@ import org.apache.sshd.client.session.ClientUserAuthServiceFactory;
import org.apache.sshd.client.session.SessionFactory;
import org.apache.sshd.client.simple.AbstractSimpleClientSessionCreator;
import org.apache.sshd.client.simple.SimpleClient;
-import org.apache.sshd.common.AbstractFactoryManager;
import org.apache.sshd.common.Closeable;
import org.apache.sshd.common.Factory;
import org.apache.sshd.common.FactoryManager;
@@ -99,12 +98,13 @@ import org.apache.sshd.common.config.SshConfigFileReader;
import org.apache.sshd.common.config.keys.FilePasswordProvider;
import org.apache.sshd.common.config.keys.KeyUtils;
import org.apache.sshd.common.future.SshFutureListener;
+import org.apache.sshd.common.impl.AbstractFactoryManager;
import org.apache.sshd.common.io.IoConnectFuture;
import org.apache.sshd.common.io.IoConnector;
import org.apache.sshd.common.io.IoSession;
import org.apache.sshd.common.keyprovider.AbstractFileKeyPairProvider;
import org.apache.sshd.common.keyprovider.KeyPairProvider;
-import org.apache.sshd.common.session.AbstractSession;
+import org.apache.sshd.common.session.impl.AbstractSession;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.common.util.SecurityUtils;
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/client/session/AbstractClientSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/AbstractClientSession.java b/sshd-core/src/main/java/org/apache/sshd/client/session/AbstractClientSession.java
index 25e441c..2da9d05 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/AbstractClientSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/AbstractClientSession.java
@@ -52,8 +52,8 @@ import org.apache.sshd.common.forward.TcpipForwarder;
import org.apache.sshd.common.io.IoSession;
import org.apache.sshd.common.scp.ScpFileOpener;
import org.apache.sshd.common.scp.ScpTransferEventListener;
-import org.apache.sshd.common.session.AbstractSession;
import org.apache.sshd.common.session.ConnectionService;
+import org.apache.sshd.common.session.impl.AbstractSession;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.common.util.net.SshdSocketAddress;
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
index 5e2677e..4ddcae1 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
@@ -30,7 +30,7 @@ import org.apache.sshd.common.SshConstants;
import org.apache.sshd.common.SshException;
import org.apache.sshd.common.io.AbstractIoWriteFuture;
import org.apache.sshd.common.io.IoWriteFuture;
-import org.apache.sshd.common.session.AbstractConnectionService;
+import org.apache.sshd.common.session.impl.AbstractConnectionService;
import org.apache.sshd.common.util.buffer.Buffer;
import org.apache.sshd.server.x11.X11ForwardSupport;
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
index 14b5681..8479370 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
@@ -48,9 +48,9 @@ import org.apache.sshd.common.future.KeyExchangeFuture;
import org.apache.sshd.common.io.IoSession;
import org.apache.sshd.common.kex.KexProposalOption;
import org.apache.sshd.common.kex.KexState;
-import org.apache.sshd.common.session.AbstractConnectionService;
import org.apache.sshd.common.session.ConnectionService;
import org.apache.sshd.common.session.SessionListener;
+import org.apache.sshd.common.session.impl.AbstractConnectionService;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.common.util.buffer.Buffer;
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/client/session/SessionFactory.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/SessionFactory.java b/sshd-core/src/main/java/org/apache/sshd/client/session/SessionFactory.java
index 9e43bed..b90e92b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/SessionFactory.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/SessionFactory.java
@@ -20,7 +20,7 @@ package org.apache.sshd.client.session;
import org.apache.sshd.client.ClientFactoryManager;
import org.apache.sshd.common.io.IoSession;
-import org.apache.sshd.common.session.AbstractSessionFactory;
+import org.apache.sshd.common.session.impl.AbstractSessionFactory;
/**
* A factory of client sessions.
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/common/AbstractFactoryManager.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/AbstractFactoryManager.java b/sshd-core/src/main/java/org/apache/sshd/common/AbstractFactoryManager.java
deleted file mode 100644
index 4e4b379..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/AbstractFactoryManager.java
+++ /dev/null
@@ -1,369 +0,0 @@
-/*
- * 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.sshd.common;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.CopyOnWriteArraySet;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.sshd.agent.SshAgentFactory;
-import org.apache.sshd.common.channel.Channel;
-import org.apache.sshd.common.channel.ChannelListener;
-import org.apache.sshd.common.channel.RequestHandler;
-import org.apache.sshd.common.config.VersionProperties;
-import org.apache.sshd.common.file.FileSystemFactory;
-import org.apache.sshd.common.forward.TcpipForwarderFactory;
-import org.apache.sshd.common.io.DefaultIoServiceFactoryFactory;
-import org.apache.sshd.common.io.IoServiceFactory;
-import org.apache.sshd.common.io.IoServiceFactoryFactory;
-import org.apache.sshd.common.kex.AbstractKexFactoryManager;
-import org.apache.sshd.common.random.Random;
-import org.apache.sshd.common.session.AbstractSessionFactory;
-import org.apache.sshd.common.session.ConnectionService;
-import org.apache.sshd.common.session.SessionListener;
-import org.apache.sshd.common.session.SessionTimeoutListener;
-import org.apache.sshd.common.util.EventListenerUtils;
-import org.apache.sshd.common.util.ValidateUtils;
-import org.apache.sshd.common.util.threads.ThreadUtils;
-import org.apache.sshd.server.forward.ForwardingFilter;
-
-/**
- * TODO Add javadoc
- *
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public abstract class AbstractFactoryManager extends AbstractKexFactoryManager implements FactoryManager {
-
- protected IoServiceFactoryFactory ioServiceFactoryFactory;
- protected IoServiceFactory ioServiceFactory;
- protected Factory<Random> randomFactory;
- protected List<NamedFactory<Channel>> channelFactories;
- protected SshAgentFactory agentFactory;
- protected ScheduledExecutorService executor;
- protected boolean shutdownExecutor;
- protected TcpipForwarderFactory tcpipForwarderFactory;
- protected ForwardingFilter tcpipForwardingFilter;
- protected FileSystemFactory fileSystemFactory;
- protected List<ServiceFactory> serviceFactories;
- protected List<RequestHandler<ConnectionService>> globalRequestHandlers;
- protected SessionTimeoutListener sessionTimeoutListener;
- protected ScheduledFuture<?> timeoutListenerFuture;
- protected final Collection<SessionListener> sessionListeners = new CopyOnWriteArraySet<>();
- protected final SessionListener sessionListenerProxy;
- protected final Collection<ChannelListener> channelListeners = new CopyOnWriteArraySet<>();
- protected final ChannelListener channelListenerProxy;
-
- private final Map<String, Object> properties = new ConcurrentHashMap<>();
- private final Map<AttributeKey<?>, Object> attributes = new ConcurrentHashMap<>();
- private PropertyResolver parentResolver = SyspropsMapWrapper.SYSPROPS_RESOLVER;
-
- protected AbstractFactoryManager() {
- ClassLoader loader = getClass().getClassLoader();
- sessionListenerProxy = EventListenerUtils.proxyWrapper(SessionListener.class, loader, sessionListeners);
- channelListenerProxy = EventListenerUtils.proxyWrapper(ChannelListener.class, loader, channelListeners);
- }
-
- @Override
- public IoServiceFactory getIoServiceFactory() {
- synchronized (ioServiceFactoryFactory) {
- if (ioServiceFactory == null) {
- ioServiceFactory = ioServiceFactoryFactory.create(this);
- }
- }
- return ioServiceFactory;
- }
-
- public IoServiceFactoryFactory getIoServiceFactoryFactory() {
- return ioServiceFactoryFactory;
- }
-
- public void setIoServiceFactoryFactory(IoServiceFactoryFactory ioServiceFactory) {
- this.ioServiceFactoryFactory = ioServiceFactory;
- }
-
- @Override
- public Factory<Random> getRandomFactory() {
- return randomFactory;
- }
-
- public void setRandomFactory(Factory<Random> randomFactory) {
- this.randomFactory = randomFactory;
- }
-
- @Override
- public Map<String, Object> getProperties() {
- return properties;
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public <T> T getAttribute(AttributeKey<T> key) {
- return (T) attributes.get(ValidateUtils.checkNotNull(key, "No key"));
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public <T> T setAttribute(AttributeKey<T> key, T value) {
- return (T) attributes.put(
- ValidateUtils.checkNotNull(key, "No key"),
- ValidateUtils.checkNotNull(value, "No value"));
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public <T> T removeAttribute(AttributeKey<T> key) {
- return (T) attributes.remove(ValidateUtils.checkNotNull(key, "No key"));
- }
-
- @Override
- public <T> T resolveAttribute(AttributeKey<T> key) {
- return AttributeStore.Utils.resolveAttribute(this, key);
- }
-
- @Override
- public PropertyResolver getParentPropertyResolver() {
- return parentResolver;
- }
-
- public void setParentPropertyResolver(PropertyResolver parent) {
- parentResolver = parent;
- }
-
- @Override
- public String getVersion() {
- return PropertyResolverUtils.getStringProperty(VersionProperties.getVersionProperties(), "sshd-version", DEFAULT_VERSION).toUpperCase();
- }
-
- @Override
- public List<NamedFactory<Channel>> getChannelFactories() {
- return channelFactories;
- }
-
- public void setChannelFactories(List<NamedFactory<Channel>> channelFactories) {
- this.channelFactories = channelFactories;
- }
-
- public int getNioWorkers() {
- int nb = PropertyResolverUtils.getIntProperty(this, NIO_WORKERS, DEFAULT_NIO_WORKERS);
- if (nb > 0) {
- return nb;
- } else { // it may have been configured to a negative value
- return DEFAULT_NIO_WORKERS;
- }
- }
-
- public void setNioWorkers(int nioWorkers) {
- if (nioWorkers > 0) {
- PropertyResolverUtils.updateProperty(this, NIO_WORKERS, nioWorkers);
- } else {
- PropertyResolverUtils.updateProperty(this, NIO_WORKERS, null);
- }
- }
-
- @Override
- public SshAgentFactory getAgentFactory() {
- return agentFactory;
- }
-
- public void setAgentFactory(SshAgentFactory agentFactory) {
- this.agentFactory = agentFactory;
- }
-
- @Override
- public ScheduledExecutorService getScheduledExecutorService() {
- return executor;
- }
-
- public void setScheduledExecutorService(ScheduledExecutorService executor) {
- setScheduledExecutorService(executor, false);
- }
-
- public void setScheduledExecutorService(ScheduledExecutorService executor, boolean shutdownExecutor) {
- this.executor = executor;
- this.shutdownExecutor = shutdownExecutor;
- }
-
- @Override
- public TcpipForwarderFactory getTcpipForwarderFactory() {
- return tcpipForwarderFactory;
- }
-
- public void setTcpipForwarderFactory(TcpipForwarderFactory tcpipForwarderFactory) {
- this.tcpipForwarderFactory = tcpipForwarderFactory;
- }
-
- @Override
- public ForwardingFilter getTcpipForwardingFilter() {
- return tcpipForwardingFilter;
- }
-
- public void setTcpipForwardingFilter(ForwardingFilter tcpipForwardingFilter) {
- this.tcpipForwardingFilter = tcpipForwardingFilter;
- }
-
- @Override
- public FileSystemFactory getFileSystemFactory() {
- return fileSystemFactory;
- }
-
- public void setFileSystemFactory(FileSystemFactory fileSystemFactory) {
- this.fileSystemFactory = fileSystemFactory;
- }
-
- @Override
- public List<ServiceFactory> getServiceFactories() {
- return serviceFactories;
- }
-
- public void setServiceFactories(List<ServiceFactory> serviceFactories) {
- this.serviceFactories = serviceFactories;
- }
-
- @Override
- public List<RequestHandler<ConnectionService>> getGlobalRequestHandlers() {
- return globalRequestHandlers;
- }
-
- public void setGlobalRequestHandlers(List<RequestHandler<ConnectionService>> globalRequestHandlers) {
- this.globalRequestHandlers = globalRequestHandlers;
- }
-
- @Override
- public void addSessionListener(SessionListener listener) {
- ValidateUtils.checkNotNull(listener, "addSessionListener(%s) null instance", this);
- // avoid race conditions on notifications while manager is being closed
- if (!isOpen()) {
- log.warn("addSessionListener({})[{}] ignore registration while manager is closing", this, listener);
- return;
- }
-
- if (this.sessionListeners.add(listener)) {
- log.trace("addSessionListener({})[{}] registered", this, listener);
- } else {
- log.trace("addSessionListener({})[{}] ignored duplicate", this, listener);
- }
- }
-
- @Override
- public void removeSessionListener(SessionListener listener) {
- if (this.sessionListeners.remove(listener)) {
- log.trace("removeSessionListener({})[{}] removed", this, listener);
- } else {
- log.trace("removeSessionListener({})[{}] not registered", this, listener);
- }
- }
-
- @Override
- public SessionListener getSessionListenerProxy() {
- return sessionListenerProxy;
- }
-
- @Override
- public void addChannelListener(ChannelListener listener) {
- ValidateUtils.checkNotNull(listener, "addChannelListener(%s) null instance", this);
- // avoid race conditions on notifications while manager is being closed
- if (!isOpen()) {
- log.warn("addChannelListener({})[{}] ignore registration while session is closing", this, listener);
- return;
- }
-
- if (this.channelListeners.add(listener)) {
- log.trace("addChannelListener({})[{}] registered", this, listener);
- } else {
- log.trace("addChannelListener({})[{}] ignored duplicate", this, listener);
- }
- }
-
- @Override
- public void removeChannelListener(ChannelListener listener) {
- if (this.channelListeners.remove(listener)) {
- log.trace("removeChannelListener({})[{}] removed", this, listener);
- } else {
- log.trace("removeChannelListener({})[{}] not registered", this, listener);
- }
- }
-
- @Override
- public ChannelListener getChannelListenerProxy() {
- return channelListenerProxy;
- }
-
- protected void setupSessionTimeout(final AbstractSessionFactory<?, ?> sessionFactory) {
- // set up the the session timeout listener and schedule it
- sessionTimeoutListener = createSessionTimeoutListener();
- addSessionListener(sessionTimeoutListener);
-
- timeoutListenerFuture = getScheduledExecutorService()
- .scheduleAtFixedRate(sessionTimeoutListener, 1, 1, TimeUnit.SECONDS);
- }
-
- protected void removeSessionTimeout(final AbstractSessionFactory<?, ?> sessionFactory) {
- stopSessionTimeoutListener(sessionFactory);
- }
-
- protected SessionTimeoutListener createSessionTimeoutListener() {
- return new SessionTimeoutListener();
- }
-
- protected void stopSessionTimeoutListener(final AbstractSessionFactory<?, ?> sessionFactory) {
- // cancel the timeout monitoring task
- if (timeoutListenerFuture != null) {
- try {
- timeoutListenerFuture.cancel(true);
- } finally {
- timeoutListenerFuture = null;
- }
- }
-
- // remove the sessionTimeoutListener completely; should the SSH server/client be restarted, a new one
- // will be created.
- if (sessionTimeoutListener != null) {
- try {
- removeSessionListener(sessionTimeoutListener);
- } finally {
- sessionTimeoutListener = null;
- }
- }
- }
-
- protected void checkConfig() {
- ValidateUtils.checkNotNullAndNotEmpty(getKeyExchangeFactories(), "KeyExchangeFactories not set");
-
- if (getScheduledExecutorService() == null) {
- setScheduledExecutorService(
- ThreadUtils.newSingleThreadScheduledExecutor(this.toString() + "-timer"),
- true);
- }
-
- ValidateUtils.checkNotNullAndNotEmpty(getCipherFactories(), "CipherFactories not set");
- ValidateUtils.checkNotNullAndNotEmpty(getCompressionFactories(), "CompressionFactories not set");
- ValidateUtils.checkNotNullAndNotEmpty(getMacFactories(), "MacFactories not set");
-
- ValidateUtils.checkNotNull(getRandomFactory(), "RandomFactory not set");
-
- if (getIoServiceFactoryFactory() == null) {
- setIoServiceFactoryFactory(new DefaultIoServiceFactoryFactory());
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/common/BaseBuilder.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/BaseBuilder.java b/sshd-core/src/main/java/org/apache/sshd/common/BaseBuilder.java
index a47f6e5..ca8802f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/BaseBuilder.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/BaseBuilder.java
@@ -33,6 +33,7 @@ import org.apache.sshd.common.file.FileSystemFactory;
import org.apache.sshd.common.file.nativefs.NativeFileSystemFactory;
import org.apache.sshd.common.forward.DefaultTcpipForwarderFactory;
import org.apache.sshd.common.forward.TcpipForwarderFactory;
+import org.apache.sshd.common.impl.AbstractFactoryManager;
import org.apache.sshd.common.kex.BuiltinDHFactories;
import org.apache.sshd.common.kex.KeyExchange;
import org.apache.sshd.common.mac.BuiltinMacs;
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/common/FactoryManager.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/FactoryManager.java b/sshd-core/src/main/java/org/apache/sshd/common/FactoryManager.java
index 9ab1f7e..14c093d 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/FactoryManager.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/FactoryManager.java
@@ -32,6 +32,7 @@ import org.apache.sshd.common.io.IoServiceFactory;
import org.apache.sshd.common.kex.KexFactoryManager;
import org.apache.sshd.common.random.Random;
import org.apache.sshd.common.session.ConnectionService;
+import org.apache.sshd.common.session.ReservedSessionMessagesManager;
import org.apache.sshd.common.session.SessionListenerManager;
import org.apache.sshd.server.forward.ForwardingFilter;
@@ -44,6 +45,7 @@ import org.apache.sshd.server.forward.ForwardingFilter;
public interface FactoryManager
extends KexFactoryManager,
SessionListenerManager,
+ ReservedSessionMessagesManager,
ChannelListenerManager,
AttributeStore,
PropertyResolver {
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/common/config/SshConfigFileReader.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/config/SshConfigFileReader.java b/sshd-core/src/main/java/org/apache/sshd/common/config/SshConfigFileReader.java
index b89a4ec..63b3711 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/config/SshConfigFileReader.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/config/SshConfigFileReader.java
@@ -40,7 +40,6 @@ import java.util.concurrent.TimeUnit;
import org.apache.sshd.client.ClientBuilder;
import org.apache.sshd.client.SshClient;
-import org.apache.sshd.common.AbstractFactoryManager;
import org.apache.sshd.common.BuiltinFactory;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.cipher.BuiltinCiphers;
@@ -48,6 +47,7 @@ import org.apache.sshd.common.cipher.Cipher;
import org.apache.sshd.common.compression.BuiltinCompressions;
import org.apache.sshd.common.compression.Compression;
import org.apache.sshd.common.compression.CompressionFactory;
+import org.apache.sshd.common.impl.AbstractFactoryManager;
import org.apache.sshd.common.kex.BuiltinDHFactories;
import org.apache.sshd.common.kex.DHFactory;
import org.apache.sshd.common.kex.KeyExchange;
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/common/global/AbstractOpenSshHostKeysHandler.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/global/AbstractOpenSshHostKeysHandler.java b/sshd-core/src/main/java/org/apache/sshd/common/global/AbstractOpenSshHostKeysHandler.java
index 28cb4d5..d4ca673 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/global/AbstractOpenSshHostKeysHandler.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/global/AbstractOpenSshHostKeysHandler.java
@@ -25,9 +25,9 @@ import java.util.Collection;
import java.util.LinkedList;
import org.apache.sshd.common.config.keys.KeyUtils;
-import org.apache.sshd.common.session.AbstractConnectionServiceRequestHandler;
import org.apache.sshd.common.session.ConnectionService;
import org.apache.sshd.common.session.Session;
+import org.apache.sshd.common.session.impl.AbstractConnectionServiceRequestHandler;
import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.common.util.buffer.Buffer;
import org.apache.sshd.common.util.buffer.keys.BufferPublicKeyParser;
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/common/impl/AbstractFactoryManager.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/impl/AbstractFactoryManager.java b/sshd-core/src/main/java/org/apache/sshd/common/impl/AbstractFactoryManager.java
new file mode 100644
index 0000000..9e35391
--- /dev/null
+++ b/sshd-core/src/main/java/org/apache/sshd/common/impl/AbstractFactoryManager.java
@@ -0,0 +1,389 @@
+/*
+ * 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.sshd.common.impl;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.sshd.agent.SshAgentFactory;
+import org.apache.sshd.common.AttributeStore;
+import org.apache.sshd.common.Factory;
+import org.apache.sshd.common.FactoryManager;
+import org.apache.sshd.common.NamedFactory;
+import org.apache.sshd.common.PropertyResolver;
+import org.apache.sshd.common.PropertyResolverUtils;
+import org.apache.sshd.common.ServiceFactory;
+import org.apache.sshd.common.SyspropsMapWrapper;
+import org.apache.sshd.common.channel.Channel;
+import org.apache.sshd.common.channel.ChannelListener;
+import org.apache.sshd.common.channel.RequestHandler;
+import org.apache.sshd.common.config.VersionProperties;
+import org.apache.sshd.common.file.FileSystemFactory;
+import org.apache.sshd.common.forward.TcpipForwarderFactory;
+import org.apache.sshd.common.io.DefaultIoServiceFactoryFactory;
+import org.apache.sshd.common.io.IoServiceFactory;
+import org.apache.sshd.common.io.IoServiceFactoryFactory;
+import org.apache.sshd.common.kex.AbstractKexFactoryManager;
+import org.apache.sshd.common.random.Random;
+import org.apache.sshd.common.session.ConnectionService;
+import org.apache.sshd.common.session.ReservedSessionMessagesHandler;
+import org.apache.sshd.common.session.SessionListener;
+import org.apache.sshd.common.session.impl.AbstractSessionFactory;
+import org.apache.sshd.common.session.impl.SessionTimeoutListener;
+import org.apache.sshd.common.util.EventListenerUtils;
+import org.apache.sshd.common.util.ValidateUtils;
+import org.apache.sshd.common.util.threads.ThreadUtils;
+import org.apache.sshd.server.forward.ForwardingFilter;
+
+/**
+ * TODO Add javadoc
+ *
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public abstract class AbstractFactoryManager extends AbstractKexFactoryManager implements FactoryManager {
+
+ protected IoServiceFactoryFactory ioServiceFactoryFactory;
+ protected IoServiceFactory ioServiceFactory;
+ protected Factory<Random> randomFactory;
+ protected List<NamedFactory<Channel>> channelFactories;
+ protected SshAgentFactory agentFactory;
+ protected ScheduledExecutorService executor;
+ protected boolean shutdownExecutor;
+ protected TcpipForwarderFactory tcpipForwarderFactory;
+ protected ForwardingFilter tcpipForwardingFilter;
+ protected FileSystemFactory fileSystemFactory;
+ protected List<ServiceFactory> serviceFactories;
+ protected List<RequestHandler<ConnectionService>> globalRequestHandlers;
+ protected SessionTimeoutListener sessionTimeoutListener;
+ protected ScheduledFuture<?> timeoutListenerFuture;
+ protected final Collection<SessionListener> sessionListeners = new CopyOnWriteArraySet<>();
+ protected final SessionListener sessionListenerProxy;
+ protected final Collection<ChannelListener> channelListeners = new CopyOnWriteArraySet<>();
+ protected final ChannelListener channelListenerProxy;
+
+ private final Map<String, Object> properties = new ConcurrentHashMap<>();
+ private final Map<AttributeKey<?>, Object> attributes = new ConcurrentHashMap<>();
+ private PropertyResolver parentResolver = SyspropsMapWrapper.SYSPROPS_RESOLVER;
+ private ReservedSessionMessagesHandler reservedSessionMessagesHandler;
+
+ protected AbstractFactoryManager() {
+ ClassLoader loader = getClass().getClassLoader();
+ sessionListenerProxy = EventListenerUtils.proxyWrapper(SessionListener.class, loader, sessionListeners);
+ channelListenerProxy = EventListenerUtils.proxyWrapper(ChannelListener.class, loader, channelListeners);
+ }
+
+ @Override
+ public IoServiceFactory getIoServiceFactory() {
+ synchronized (ioServiceFactoryFactory) {
+ if (ioServiceFactory == null) {
+ ioServiceFactory = ioServiceFactoryFactory.create(this);
+ }
+ }
+ return ioServiceFactory;
+ }
+
+ public IoServiceFactoryFactory getIoServiceFactoryFactory() {
+ return ioServiceFactoryFactory;
+ }
+
+ public void setIoServiceFactoryFactory(IoServiceFactoryFactory ioServiceFactory) {
+ this.ioServiceFactoryFactory = ioServiceFactory;
+ }
+
+ @Override
+ public Factory<Random> getRandomFactory() {
+ return randomFactory;
+ }
+
+ public void setRandomFactory(Factory<Random> randomFactory) {
+ this.randomFactory = randomFactory;
+ }
+
+ @Override
+ public Map<String, Object> getProperties() {
+ return properties;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> T getAttribute(AttributeKey<T> key) {
+ return (T) attributes.get(ValidateUtils.checkNotNull(key, "No key"));
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> T setAttribute(AttributeKey<T> key, T value) {
+ return (T) attributes.put(
+ ValidateUtils.checkNotNull(key, "No key"),
+ ValidateUtils.checkNotNull(value, "No value"));
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> T removeAttribute(AttributeKey<T> key) {
+ return (T) attributes.remove(ValidateUtils.checkNotNull(key, "No key"));
+ }
+
+ @Override
+ public <T> T resolveAttribute(AttributeKey<T> key) {
+ return AttributeStore.Utils.resolveAttribute(this, key);
+ }
+
+ @Override
+ public PropertyResolver getParentPropertyResolver() {
+ return parentResolver;
+ }
+
+ public void setParentPropertyResolver(PropertyResolver parent) {
+ parentResolver = parent;
+ }
+
+ @Override
+ public String getVersion() {
+ return PropertyResolverUtils.getStringProperty(VersionProperties.getVersionProperties(), "sshd-version", DEFAULT_VERSION).toUpperCase();
+ }
+
+ @Override
+ public List<NamedFactory<Channel>> getChannelFactories() {
+ return channelFactories;
+ }
+
+ public void setChannelFactories(List<NamedFactory<Channel>> channelFactories) {
+ this.channelFactories = channelFactories;
+ }
+
+ public int getNioWorkers() {
+ int nb = PropertyResolverUtils.getIntProperty(this, NIO_WORKERS, DEFAULT_NIO_WORKERS);
+ if (nb > 0) {
+ return nb;
+ } else { // it may have been configured to a negative value
+ return DEFAULT_NIO_WORKERS;
+ }
+ }
+
+ public void setNioWorkers(int nioWorkers) {
+ if (nioWorkers > 0) {
+ PropertyResolverUtils.updateProperty(this, NIO_WORKERS, nioWorkers);
+ } else {
+ PropertyResolverUtils.updateProperty(this, NIO_WORKERS, null);
+ }
+ }
+
+ @Override
+ public SshAgentFactory getAgentFactory() {
+ return agentFactory;
+ }
+
+ public void setAgentFactory(SshAgentFactory agentFactory) {
+ this.agentFactory = agentFactory;
+ }
+
+ @Override
+ public ScheduledExecutorService getScheduledExecutorService() {
+ return executor;
+ }
+
+ public void setScheduledExecutorService(ScheduledExecutorService executor) {
+ setScheduledExecutorService(executor, false);
+ }
+
+ public void setScheduledExecutorService(ScheduledExecutorService executor, boolean shutdownExecutor) {
+ this.executor = executor;
+ this.shutdownExecutor = shutdownExecutor;
+ }
+
+ @Override
+ public TcpipForwarderFactory getTcpipForwarderFactory() {
+ return tcpipForwarderFactory;
+ }
+
+ public void setTcpipForwarderFactory(TcpipForwarderFactory tcpipForwarderFactory) {
+ this.tcpipForwarderFactory = tcpipForwarderFactory;
+ }
+
+ @Override
+ public ForwardingFilter getTcpipForwardingFilter() {
+ return tcpipForwardingFilter;
+ }
+
+ public void setTcpipForwardingFilter(ForwardingFilter tcpipForwardingFilter) {
+ this.tcpipForwardingFilter = tcpipForwardingFilter;
+ }
+
+ @Override
+ public FileSystemFactory getFileSystemFactory() {
+ return fileSystemFactory;
+ }
+
+ public void setFileSystemFactory(FileSystemFactory fileSystemFactory) {
+ this.fileSystemFactory = fileSystemFactory;
+ }
+
+ @Override
+ public List<ServiceFactory> getServiceFactories() {
+ return serviceFactories;
+ }
+
+ public void setServiceFactories(List<ServiceFactory> serviceFactories) {
+ this.serviceFactories = serviceFactories;
+ }
+
+ @Override
+ public List<RequestHandler<ConnectionService>> getGlobalRequestHandlers() {
+ return globalRequestHandlers;
+ }
+
+ public void setGlobalRequestHandlers(List<RequestHandler<ConnectionService>> globalRequestHandlers) {
+ this.globalRequestHandlers = globalRequestHandlers;
+ }
+
+ @Override
+ public ReservedSessionMessagesHandler getReservedSessionMessagesHandler() {
+ return reservedSessionMessagesHandler;
+ }
+
+ @Override
+ public void setReservedSessionMessagesHandler(ReservedSessionMessagesHandler handler) {
+ reservedSessionMessagesHandler = handler;
+ }
+
+ @Override
+ public void addSessionListener(SessionListener listener) {
+ ValidateUtils.checkNotNull(listener, "addSessionListener(%s) null instance", this);
+ // avoid race conditions on notifications while manager is being closed
+ if (!isOpen()) {
+ log.warn("addSessionListener({})[{}] ignore registration while manager is closing", this, listener);
+ return;
+ }
+
+ if (this.sessionListeners.add(listener)) {
+ log.trace("addSessionListener({})[{}] registered", this, listener);
+ } else {
+ log.trace("addSessionListener({})[{}] ignored duplicate", this, listener);
+ }
+ }
+
+ @Override
+ public void removeSessionListener(SessionListener listener) {
+ if (this.sessionListeners.remove(listener)) {
+ log.trace("removeSessionListener({})[{}] removed", this, listener);
+ } else {
+ log.trace("removeSessionListener({})[{}] not registered", this, listener);
+ }
+ }
+
+ @Override
+ public SessionListener getSessionListenerProxy() {
+ return sessionListenerProxy;
+ }
+
+ @Override
+ public void addChannelListener(ChannelListener listener) {
+ ValidateUtils.checkNotNull(listener, "addChannelListener(%s) null instance", this);
+ // avoid race conditions on notifications while manager is being closed
+ if (!isOpen()) {
+ log.warn("addChannelListener({})[{}] ignore registration while session is closing", this, listener);
+ return;
+ }
+
+ if (this.channelListeners.add(listener)) {
+ log.trace("addChannelListener({})[{}] registered", this, listener);
+ } else {
+ log.trace("addChannelListener({})[{}] ignored duplicate", this, listener);
+ }
+ }
+
+ @Override
+ public void removeChannelListener(ChannelListener listener) {
+ if (this.channelListeners.remove(listener)) {
+ log.trace("removeChannelListener({})[{}] removed", this, listener);
+ } else {
+ log.trace("removeChannelListener({})[{}] not registered", this, listener);
+ }
+ }
+
+ @Override
+ public ChannelListener getChannelListenerProxy() {
+ return channelListenerProxy;
+ }
+
+ protected void setupSessionTimeout(final AbstractSessionFactory<?, ?> sessionFactory) {
+ // set up the the session timeout listener and schedule it
+ sessionTimeoutListener = createSessionTimeoutListener();
+ addSessionListener(sessionTimeoutListener);
+
+ timeoutListenerFuture = getScheduledExecutorService()
+ .scheduleAtFixedRate(sessionTimeoutListener, 1, 1, TimeUnit.SECONDS);
+ }
+
+ protected void removeSessionTimeout(final AbstractSessionFactory<?, ?> sessionFactory) {
+ stopSessionTimeoutListener(sessionFactory);
+ }
+
+ protected SessionTimeoutListener createSessionTimeoutListener() {
+ return new SessionTimeoutListener();
+ }
+
+ protected void stopSessionTimeoutListener(final AbstractSessionFactory<?, ?> sessionFactory) {
+ // cancel the timeout monitoring task
+ if (timeoutListenerFuture != null) {
+ try {
+ timeoutListenerFuture.cancel(true);
+ } finally {
+ timeoutListenerFuture = null;
+ }
+ }
+
+ // remove the sessionTimeoutListener completely; should the SSH server/client be restarted, a new one
+ // will be created.
+ if (sessionTimeoutListener != null) {
+ try {
+ removeSessionListener(sessionTimeoutListener);
+ } finally {
+ sessionTimeoutListener = null;
+ }
+ }
+ }
+
+ protected void checkConfig() {
+ ValidateUtils.checkNotNullAndNotEmpty(getKeyExchangeFactories(), "KeyExchangeFactories not set");
+
+ if (getScheduledExecutorService() == null) {
+ setScheduledExecutorService(
+ ThreadUtils.newSingleThreadScheduledExecutor(this.toString() + "-timer"),
+ true);
+ }
+
+ ValidateUtils.checkNotNullAndNotEmpty(getCipherFactories(), "CipherFactories not set");
+ ValidateUtils.checkNotNullAndNotEmpty(getCompressionFactories(), "CompressionFactories not set");
+ ValidateUtils.checkNotNullAndNotEmpty(getMacFactories(), "MacFactories not set");
+
+ ValidateUtils.checkNotNull(getRandomFactory(), "RandomFactory not set");
+
+ if (getIoServiceFactoryFactory() == null) {
+ setIoServiceFactoryFactory(new DefaultIoServiceFactoryFactory());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/common/kex/dh/AbstractDHKeyExchange.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/kex/dh/AbstractDHKeyExchange.java b/sshd-core/src/main/java/org/apache/sshd/common/kex/dh/AbstractDHKeyExchange.java
index a92a5bc..3da8692 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/kex/dh/AbstractDHKeyExchange.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/kex/dh/AbstractDHKeyExchange.java
@@ -21,9 +21,9 @@ package org.apache.sshd.common.kex.dh;
import org.apache.sshd.common.digest.Digest;
import org.apache.sshd.common.kex.KeyExchange;
-import org.apache.sshd.common.session.AbstractSession;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.session.SessionHolder;
+import org.apache.sshd.common.session.impl.AbstractSession;
import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.common.util.logging.AbstractLoggingBean;
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
deleted file mode 100644
index 7f26424..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
+++ /dev/null
@@ -1,662 +0,0 @@
-/*
- * 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.sshd.common.session;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicReference;
-
-import org.apache.sshd.agent.common.AgentForwardSupport;
-import org.apache.sshd.agent.common.DefaultAgentForwardSupport;
-import org.apache.sshd.client.channel.AbstractClientChannel;
-import org.apache.sshd.client.future.OpenFuture;
-import org.apache.sshd.common.Closeable;
-import org.apache.sshd.common.FactoryManager;
-import org.apache.sshd.common.NamedFactory;
-import org.apache.sshd.common.PropertyResolverUtils;
-import org.apache.sshd.common.SshConstants;
-import org.apache.sshd.common.SshException;
-import org.apache.sshd.common.channel.Channel;
-import org.apache.sshd.common.channel.ChannelListener;
-import org.apache.sshd.common.channel.OpenChannelException;
-import org.apache.sshd.common.channel.RequestHandler;
-import org.apache.sshd.common.channel.Window;
-import org.apache.sshd.common.forward.TcpipForwarder;
-import org.apache.sshd.common.forward.TcpipForwarderFactory;
-import org.apache.sshd.common.future.SshFutureListener;
-import org.apache.sshd.common.io.AbstractIoWriteFuture;
-import org.apache.sshd.common.io.IoWriteFuture;
-import org.apache.sshd.common.util.GenericUtils;
-import org.apache.sshd.common.util.Int2IntFunction;
-import org.apache.sshd.common.util.ValidateUtils;
-import org.apache.sshd.common.util.buffer.Buffer;
-import org.apache.sshd.common.util.closeable.AbstractInnerCloseable;
-import org.apache.sshd.server.x11.DefaultX11ForwardSupport;
-import org.apache.sshd.server.x11.X11ForwardSupport;
-
-/**
- * Base implementation of ConnectionService.
- *
- * @param <S> Type of {@link AbstractSession} being used
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public abstract class AbstractConnectionService<S extends AbstractSession> extends AbstractInnerCloseable implements ConnectionService {
- /**
- * Property that can be used to configure max. allowed concurrent active channels
- *
- * @see #registerChannel(Channel)
- */
- public static final String MAX_CONCURRENT_CHANNELS_PROP = "max-sshd-channels";
-
- /**
- * Default value for {@link #MAX_CONCURRENT_CHANNELS_PROP} is none specified
- */
- public static final int DEFAULT_MAX_CHANNELS = Integer.MAX_VALUE;
-
- /**
- * Default growth factor function used to resize response buffers
- */
- public static final Int2IntFunction RESPONSE_BUFFER_GROWTH_FACTOR = Int2IntFunction.Utils.add(Byte.SIZE);
-
- /**
- * Map of channels keyed by the identifier
- */
- protected final Map<Integer, Channel> channels = new ConcurrentHashMap<>();
- /**
- * Next channel identifier
- */
- protected final AtomicInteger nextChannelId = new AtomicInteger(0);
-
- private final AtomicReference<AgentForwardSupport> agentForwardHolder = new AtomicReference<>();
- private final AtomicReference<X11ForwardSupport> x11ForwardHolder = new AtomicReference<>();
- private final AtomicReference<TcpipForwarder> tcpipForwarderHolder = new AtomicReference<>();
- private final AtomicBoolean allowMoreSessions = new AtomicBoolean(true);
-
- private final S sessionInstance;
-
- protected AbstractConnectionService(S session) {
- sessionInstance = ValidateUtils.checkNotNull(session, "No session");
- }
-
- public Collection<Channel> getChannels() {
- return channels.values();
- }
-
- @Override
- public S getSession() {
- return sessionInstance;
- }
-
- @Override
- public void start() {
- // do nothing
- }
-
- @Override
- public TcpipForwarder getTcpipForwarder() {
- TcpipForwarder forwarder;
- S session = getSession();
- synchronized (tcpipForwarderHolder) {
- forwarder = tcpipForwarderHolder.get();
- if (forwarder != null) {
- return forwarder;
- }
-
- forwarder = ValidateUtils.checkNotNull(createTcpipForwarder(session), "No forwarder created for %s", session);
- tcpipForwarderHolder.set(forwarder);
- }
-
- if (log.isDebugEnabled()) {
- log.debug("getTcpipForwarder({}) created instance", session);
- }
- return forwarder;
- }
-
- protected TcpipForwarder createTcpipForwarder(S session) {
- FactoryManager manager =
- ValidateUtils.checkNotNull(session.getFactoryManager(), "No factory manager");
- TcpipForwarderFactory factory =
- ValidateUtils.checkNotNull(manager.getTcpipForwarderFactory(), "No forwarder factory");
- return factory.create(this);
- }
-
- @Override
- public X11ForwardSupport getX11ForwardSupport() {
- X11ForwardSupport x11Support;
- S session = getSession();
- synchronized (x11ForwardHolder) {
- x11Support = x11ForwardHolder.get();
- if (x11Support != null) {
- return x11Support;
- }
-
- x11Support = ValidateUtils.checkNotNull(createX11ForwardSupport(session), "No X11 forwarder created for %s", session);
- x11ForwardHolder.set(x11Support);
- }
-
- if (log.isDebugEnabled()) {
- log.debug("getX11ForwardSupport({}) created instance", session);
- }
- return x11Support;
- }
-
- protected X11ForwardSupport createX11ForwardSupport(S session) {
- return new DefaultX11ForwardSupport(this);
- }
-
- @Override
- public AgentForwardSupport getAgentForwardSupport() {
- AgentForwardSupport agentForward;
- S session = getSession();
- synchronized (agentForwardHolder) {
- agentForward = agentForwardHolder.get();
- if (agentForward != null) {
- return agentForward;
- }
-
- agentForward = ValidateUtils.checkNotNull(createAgentForwardSupport(session), "No agent forward created for %s", session);
- agentForwardHolder.set(agentForward);
- }
-
- if (log.isDebugEnabled()) {
- log.debug("getAgentForwardSupport({}) created instance", session);
- }
- return agentForward;
- }
-
- protected AgentForwardSupport createAgentForwardSupport(S session) {
- return new DefaultAgentForwardSupport(this);
- }
-
- @Override
- protected Closeable getInnerCloseable() {
- return builder()
- .sequential(tcpipForwarderHolder.get(), agentForwardHolder.get(), x11ForwardHolder.get())
- .parallel(channels.values())
- .build();
- }
-
- protected int getNextChannelId() {
- return nextChannelId.getAndIncrement();
- }
-
- @Override
- public int registerChannel(Channel channel) throws IOException {
- Session session = getSession();
- int maxChannels = PropertyResolverUtils.getIntProperty(session, MAX_CONCURRENT_CHANNELS_PROP, DEFAULT_MAX_CHANNELS);
- int curSize = channels.size();
- if (curSize > maxChannels) {
- throw new IllegalStateException("Currently active channels (" + curSize + ") at max.: " + maxChannels);
- }
-
- int channelId = getNextChannelId();
- channel.init(this, session, channelId);
-
- boolean registered = false;
- synchronized (lock) {
- if (!isClosing()) {
- channels.put(channelId, channel);
- registered = true;
- }
- }
-
- if (!registered) {
- handleChannelRegistrationFailure(channel, channelId);
- }
-
- if (log.isDebugEnabled()) {
- log.debug("registerChannel({})[id={}] {}", this, channelId, channel);
- }
- return channelId;
- }
-
- protected void handleChannelRegistrationFailure(Channel channel, int channelId) throws IOException {
- RuntimeException reason = new IllegalStateException("Channel id=" + channelId + " not registered because session is being closed: " + this);
- ChannelListener listener = channel.getChannelListenerProxy();
- try {
- listener.channelClosed(channel, reason);
- } catch (Throwable err) {
- Throwable ignored = GenericUtils.peelException(err);
- log.warn("registerChannel({})[{}] failed ({}) to inform of channel closure: {}",
- this, channel, ignored.getClass().getSimpleName(), ignored.getMessage());
- if (log.isDebugEnabled()) {
- log.debug("registerChannel(" + this + ")[" + channel + "] inform closure failure details", ignored);
- }
- if (log.isTraceEnabled()) {
- Throwable[] suppressed = ignored.getSuppressed();
- if (GenericUtils.length(suppressed) > 0) {
- for (Throwable s : suppressed) {
- log.trace("registerChannel(" + this + ")[" + channel + "] suppressed channel closed signalling", s);
- }
- }
- }
- }
-
- throw reason;
- }
-
- /**
- * Remove this channel from the list of managed channels
- *
- * @param channel the channel
- */
- @Override
- public void unregisterChannel(Channel channel) {
- Channel result = channels.remove(channel.getId());
- if (log.isDebugEnabled()) {
- log.debug("unregisterChannel({}) result={}", channel, result);
- }
- }
-
- @Override
- public void process(int cmd, Buffer buffer) throws Exception {
- switch (cmd) {
- case SshConstants.SSH_MSG_CHANNEL_OPEN:
- channelOpen(buffer);
- break;
- case SshConstants.SSH_MSG_CHANNEL_OPEN_CONFIRMATION:
- channelOpenConfirmation(buffer);
- break;
- case SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE:
- channelOpenFailure(buffer);
- break;
- case SshConstants.SSH_MSG_CHANNEL_REQUEST:
- channelRequest(buffer);
- break;
- case SshConstants.SSH_MSG_CHANNEL_DATA:
- channelData(buffer);
- break;
- case SshConstants.SSH_MSG_CHANNEL_EXTENDED_DATA:
- channelExtendedData(buffer);
- break;
- case SshConstants.SSH_MSG_CHANNEL_FAILURE:
- channelFailure(buffer);
- break;
- case SshConstants.SSH_MSG_CHANNEL_SUCCESS:
- channelSuccess(buffer);
- break;
- case SshConstants.SSH_MSG_CHANNEL_WINDOW_ADJUST:
- channelWindowAdjust(buffer);
- break;
- case SshConstants.SSH_MSG_CHANNEL_EOF:
- channelEof(buffer);
- break;
- case SshConstants.SSH_MSG_CHANNEL_CLOSE:
- channelClose(buffer);
- break;
- case SshConstants.SSH_MSG_GLOBAL_REQUEST:
- globalRequest(buffer);
- break;
- case SshConstants.SSH_MSG_REQUEST_SUCCESS:
- requestSuccess(buffer);
- break;
- case SshConstants.SSH_MSG_REQUEST_FAILURE:
- requestFailure(buffer);
- break;
- default:
- throw new IllegalStateException("Unsupported command: " + SshConstants.getCommandMessageName(cmd));
- }
- }
-
- @Override
- public boolean isAllowMoreSessions() {
- return allowMoreSessions.get();
- }
-
- @Override
- public void setAllowMoreSessions(boolean allow) {
- if (log.isDebugEnabled()) {
- log.debug("setAllowMoreSessions({}): {}", this, allow);
- }
- allowMoreSessions.set(allow);
- }
-
- public void channelOpenConfirmation(Buffer buffer) throws IOException {
- Channel channel = getChannel(buffer);
- int sender = buffer.getInt();
- int rwsize = buffer.getInt();
- int rmpsize = buffer.getInt();
- if (log.isDebugEnabled()) {
- log.debug("channelOpenConfirmation({}) SSH_MSG_CHANNEL_OPEN_CONFIRMATION sender={}, window-size={}, packet-size={}",
- channel, sender, rwsize, rmpsize);
- }
- /*
- * NOTE: the 'sender' of the SSH_MSG_CHANNEL_OPEN_CONFIRMATION is the
- * recipient on the client side - see rfc4254 section 5.1:
- *
- * 'sender channel' is the channel number allocated by the other side
- *
- * in our case, the server
- */
- channel.handleOpenSuccess(sender, rwsize, rmpsize, buffer);
- }
-
- public void channelOpenFailure(Buffer buffer) throws IOException {
- AbstractClientChannel channel = (AbstractClientChannel) getChannel(buffer);
- int id = channel.getId();
- if (log.isDebugEnabled()) {
- log.debug("channelOpenFailure({}) Received SSH_MSG_CHANNEL_OPEN_FAILURE", channel);
- }
- channels.remove(id);
- channel.handleOpenFailure(buffer);
- }
-
- /**
- * Process incoming data on a channel
- *
- * @param buffer the buffer containing the data
- * @throws IOException if an error occurs
- */
- public void channelData(Buffer buffer) throws IOException {
- Channel channel = getChannel(buffer);
- channel.handleData(buffer);
- }
-
- /**
- * Process incoming extended data on a channel
- *
- * @param buffer the buffer containing the data
- * @throws IOException if an error occurs
- */
- public void channelExtendedData(Buffer buffer) throws IOException {
- Channel channel = getChannel(buffer);
- channel.handleExtendedData(buffer);
- }
-
- /**
- * Process a window adjust packet on a channel
- *
- * @param buffer the buffer containing the window adjustment parameters
- * @throws IOException if an error occurs
- */
- public void channelWindowAdjust(Buffer buffer) throws IOException {
- try {
- Channel channel = getChannel(buffer);
- channel.handleWindowAdjust(buffer);
- } catch (SshException e) {
- if (log.isDebugEnabled()) {
- log.debug("channelWindowAdjust {} error: {}", e.getClass().getSimpleName(), e.getMessage());
- }
- }
- }
-
- /**
- * Process end of file on a channel
- *
- * @param buffer the buffer containing the packet
- * @throws IOException if an error occurs
- */
- public void channelEof(Buffer buffer) throws IOException {
- Channel channel = getChannel(buffer);
- channel.handleEof();
- }
-
- /**
- * Close a channel due to a close packet received
- *
- * @param buffer the buffer containing the packet
- * @throws IOException if an error occurs
- */
- public void channelClose(Buffer buffer) throws IOException {
- Channel channel = getChannel(buffer);
- channel.handleClose();
- }
-
- /**
- * Service a request on a channel
- *
- * @param buffer the buffer containing the request
- * @throws IOException if an error occurs
- */
- public void channelRequest(Buffer buffer) throws IOException {
- Channel channel = getChannel(buffer);
- channel.handleRequest(buffer);
- }
-
- /**
- * Process a failure on a channel
- *
- * @param buffer the buffer containing the packet
- * @throws IOException if an error occurs
- */
- public void channelFailure(Buffer buffer) throws IOException {
- Channel channel = getChannel(buffer);
- channel.handleFailure();
- }
-
- /**
- * Process a success on a channel
- *
- * @param buffer the buffer containing the packet
- * @throws IOException if an error occurs
- */
- public void channelSuccess(Buffer buffer) throws IOException {
- Channel channel = getChannel(buffer);
- channel.handleSuccess();
- }
-
- /**
- * Retrieve the channel designated by the given packet
- *
- * @param buffer the incoming packet
- * @return the target channel
- * @throws IOException if the channel does not exists
- */
- protected Channel getChannel(Buffer buffer) throws IOException {
- return getChannel(buffer.getInt(), buffer);
- }
-
- protected Channel getChannel(int recipient, Buffer buffer) throws IOException {
- Channel channel = channels.get(recipient);
- if (channel == null) {
- byte[] data = buffer.array();
- int curPos = buffer.rpos();
- int cmd = (curPos >= 5) ? (data[curPos - 5] & 0xFF) : -1;
- throw new SshException("Received " + SshConstants.getCommandMessageName(cmd) + " on unknown channel " + recipient);
- }
-
- return channel;
- }
-
- protected void channelOpen(Buffer buffer) throws Exception {
- String type = buffer.getString();
- final int sender = buffer.getInt();
- final int rwsize = buffer.getInt();
- final int rmpsize = buffer.getInt();
- /*
- * NOTE: the 'sender' is the identifier assigned by the remote side - the server in this case
- */
- if (log.isDebugEnabled()) {
- log.debug("channelOpen({}) SSH_MSG_CHANNEL_OPEN sender={}, type={}, window-size={}, packet-size={}",
- this, sender, type, rwsize, rmpsize);
- }
-
- if (isClosing()) {
- // TODO add language tag
- sendChannelOpenFailure(buffer, sender, SshConstants.SSH_OPEN_CONNECT_FAILED, "Server is shutting down while attempting to open channel type=" + type, "");
- return;
- }
-
- if (!isAllowMoreSessions()) {
- // TODO add language tag
- sendChannelOpenFailure(buffer, sender, SshConstants.SSH_OPEN_CONNECT_FAILED, "additional sessions disabled", "");
- return;
- }
-
- final Session session = getSession();
- FactoryManager manager = ValidateUtils.checkNotNull(session.getFactoryManager(), "No factory manager");
- final Channel channel = NamedFactory.Utils.create(manager.getChannelFactories(), type);
- if (channel == null) {
- // TODO add language tag
- sendChannelOpenFailure(buffer, sender, SshConstants.SSH_OPEN_UNKNOWN_CHANNEL_TYPE, "Unsupported channel type: " + type, "");
- return;
- }
-
- final int channelId = registerChannel(channel);
- channel.open(sender, rwsize, rmpsize, buffer).addListener(new SshFutureListener<OpenFuture>() {
- @Override
- @SuppressWarnings("synthetic-access")
- public void operationComplete(OpenFuture future) {
- try {
- if (future.isOpened()) {
- Window window = channel.getLocalWindow();
- if (log.isDebugEnabled()) {
- log.debug("operationComplete({}) send SSH_MSG_CHANNEL_OPEN_CONFIRMATION recipient={}, sender={}, window-size={}, packet-size={}",
- channel, sender, channelId, window.getSize(), window.getPacketSize());
- }
- Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_CONFIRMATION, Integer.SIZE);
- buf.putInt(sender); // remote (server side) identifier
- buf.putInt(channelId); // local (client side) identifier
- buf.putInt(window.getSize());
- buf.putInt(window.getPacketSize());
- session.writePacket(buf);
- } else {
- Throwable exception = future.getException();
- if (exception != null) {
- String message = exception.getMessage();
- int reasonCode = 0;
- if (exception instanceof OpenChannelException) {
- reasonCode = ((OpenChannelException) exception).getReasonCode();
- } else {
- message = exception.getClass().getSimpleName() + " while opening channel: " + message;
- }
-
- Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE, message.length() + Long.SIZE);
- sendChannelOpenFailure(buf, sender, reasonCode, message, "");
- }
- }
- } catch (IOException e) {
- if (log.isDebugEnabled()) {
- log.debug("operationComplete({}) {}: {}",
- AbstractConnectionService.this, e.getClass().getSimpleName(), e.getMessage());
- }
- session.exceptionCaught(e);
- }
- }
- });
- }
-
- protected IoWriteFuture sendChannelOpenFailure(Buffer buffer, int sender, int reasonCode, String message, String lang) throws IOException {
- if (log.isDebugEnabled()) {
- log.debug("sendChannelOpenFailure({}) sender={}, reason={}, lang={}, message='{}'",
- this, sender, SshConstants.getOpenErrorCodeName(reasonCode), lang, message);
- }
-
- Session session = getSession();
- Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE,
- Long.SIZE + GenericUtils.length(message) + GenericUtils.length(lang));
- buf.putInt(sender);
- buf.putInt(reasonCode);
- buf.putString(message);
- buf.putString(lang);
- return session.writePacket(buf);
- }
-
- /**
- * Process global requests
- *
- * @param buffer The request {@link Buffer}
- * @throws Exception If failed to process the request
- */
- protected void globalRequest(Buffer buffer) throws Exception {
- String req = buffer.getString();
- boolean wantReply = buffer.getBoolean();
- if (log.isDebugEnabled()) {
- log.debug("globalRequest({}) received SSH_MSG_GLOBAL_REQUEST {} want-reply={}",
- this, req, Boolean.valueOf(wantReply));
- }
-
- Session session = getSession();
- FactoryManager manager =
- ValidateUtils.checkNotNull(session.getFactoryManager(), "No factory manager");
- List<RequestHandler<ConnectionService>> handlers = manager.getGlobalRequestHandlers();
- if (GenericUtils.size(handlers) > 0) {
- for (RequestHandler<ConnectionService> handler : handlers) {
- RequestHandler.Result result;
- try {
- result = handler.process(this, req, wantReply, buffer);
- } catch (Throwable e) {
- log.warn("globalRequest({})[{}, want-reply={}] failed ({}) to process: {}",
- this, req, wantReply, e.getClass().getSimpleName(), e.getMessage());
- if (log.isDebugEnabled()) {
- log.debug("globalRequest(" + this + ")[" + req + ", want-reply=" + wantReply + "] failure details", e);
- }
- result = RequestHandler.Result.ReplyFailure;
- }
-
- // if Unsupported then check the next handler in line
- if (RequestHandler.Result.Unsupported.equals(result)) {
- if (log.isTraceEnabled()) {
- log.trace("globalRequest({}) {}#process({})[want-reply={}] : {}",
- this, handler.getClass().getSimpleName(), req, wantReply, result);
- }
- } else {
- sendGlobalResponse(buffer, req, result, wantReply);
- return;
- }
- }
- }
-
- handleUnknownRequest(buffer, req, wantReply);
- }
-
- protected void handleUnknownRequest(Buffer buffer, String req, boolean wantReply) throws IOException {
- log.warn("handleUnknownRequest({}) unknown global request: {}", this, req);
- sendGlobalResponse(buffer, req, RequestHandler.Result.Unsupported, wantReply);
- }
-
- protected IoWriteFuture sendGlobalResponse(Buffer buffer, String req, RequestHandler.Result result, boolean wantReply) throws IOException {
- if (log.isDebugEnabled()) {
- log.debug("sendGlobalResponse({})[{}] result={}, want-reply={}", this, req, result, wantReply);
- }
-
- if (RequestHandler.Result.Replied.equals(result) || (!wantReply)) {
- return new AbstractIoWriteFuture(null) {
- {
- setValue(Boolean.TRUE);
- }
- };
- }
-
- byte cmd = RequestHandler.Result.ReplySuccess.equals(result)
- ? SshConstants.SSH_MSG_REQUEST_SUCCESS
- : SshConstants.SSH_MSG_REQUEST_FAILURE;
- Session session = getSession();
- Buffer rsp = session.createBuffer(cmd, 2);
- return session.writePacket(rsp);
- }
-
- protected void requestSuccess(Buffer buffer) throws Exception {
- getSession().requestSuccess(buffer);
- }
-
- protected void requestFailure(Buffer buffer) throws Exception {
- getSession().requestFailure(buffer);
- }
-
- @Override
- public String toString() {
- return getClass().getSimpleName() + "[" + getSession() + "]";
- }
-}
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/2114c101/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionServiceRequestHandler.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionServiceRequestHandler.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionServiceRequestHandler.java
deleted file mode 100644
index d10786f..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionServiceRequestHandler.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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.sshd.common.session;
-
-import org.apache.sshd.common.util.buffer.Buffer;
-import org.apache.sshd.common.util.logging.AbstractLoggingBean;
-
-/**
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public abstract class AbstractConnectionServiceRequestHandler
- extends AbstractLoggingBean
- implements ConnectionServiceRequestHandler {
-
- protected AbstractConnectionServiceRequestHandler() {
- super();
- }
-
- @Override
- public Result process(ConnectionService connectionService, String request, boolean wantReply, Buffer buffer) throws Exception {
- if (log.isDebugEnabled()) {
- log.debug("process({}) request={}, want-reply={}", connectionService, request, wantReply);
- }
-
- return Result.Unsupported;
- }
-}