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 2015/05/18 15:44:53 UTC
mina-sshd git commit: [SSHD-466] Move common 'checkConfig' code to
AbstractFactoryManager
Repository: mina-sshd
Updated Branches:
refs/heads/master 690082009 -> d9ed7ab21
[SSHD-466] Move common 'checkConfig' code to AbstractFactoryManager
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/d9ed7ab2
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/d9ed7ab2
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/d9ed7ab2
Branch: refs/heads/master
Commit: d9ed7ab2129b7f5b84bd8315819e7601592cbe3a
Parents: 6900820
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Mon May 18 16:44:43 2015 +0300
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Mon May 18 16:44:43 2015 +0300
----------------------------------------------------------------------
.../main/java/org/apache/sshd/SshClient.java | 49 ++++--------
.../main/java/org/apache/sshd/SshServer.java | 78 ++++++++------------
.../sshd/common/AbstractFactoryManager.java | 24 ++++++
3 files changed, 71 insertions(+), 80 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d9ed7ab2/sshd-core/src/main/java/org/apache/sshd/SshClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/SshClient.java b/sshd-core/src/main/java/org/apache/sshd/SshClient.java
index 0c935d1..7ed51af 100644
--- a/sshd-core/src/main/java/org/apache/sshd/SshClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/SshClient.java
@@ -63,13 +63,13 @@ import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.SshdSocketAddress;
import org.apache.sshd.common.config.SshConfigFileReader;
import org.apache.sshd.common.future.SshFutureListener;
-import org.apache.sshd.common.io.DefaultIoServiceFactoryFactory;
import org.apache.sshd.common.io.IoConnectFuture;
import org.apache.sshd.common.io.IoConnector;
import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
import org.apache.sshd.common.session.AbstractSession;
+import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.SecurityUtils;
-import org.apache.sshd.common.util.ThreadUtils;
+import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.common.util.io.NoCloseInputStream;
import org.apache.sshd.common.util.io.NoCloseOutputStream;
import org.bouncycastle.openssl.PasswordFinder;
@@ -164,37 +164,17 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa
this.userAuthFactories = userAuthFactories;
}
+ @Override
protected void checkConfig() {
- if (getKeyExchangeFactories() == null) {
- throw new IllegalArgumentException("KeyExchangeFactories not set");
- }
- if (getScheduledExecutorService() == null) {
- setScheduledExecutorService(
- ThreadUtils.newSingleThreadScheduledExecutor(this.toString() + "-timer"),
- true);
- }
- if (getCipherFactories() == null) {
- throw new IllegalArgumentException("CipherFactories not set");
- }
- if (getCompressionFactories() == null) {
- throw new IllegalArgumentException("CompressionFactories not set");
- }
- if (getMacFactories() == null) {
- throw new IllegalArgumentException("MacFactories not set");
- }
- if (getRandomFactory() == null) {
- throw new IllegalArgumentException("RandomFactory not set");
- }
- if (getTcpipForwarderFactory() == null) {
- throw new IllegalArgumentException("TcpipForwarderFactory not set");
- }
- if (getServerKeyVerifier() == null) {
- throw new IllegalArgumentException("ServerKeyVerifier not set");
- }
+ super.checkConfig();
+
+ ValidateUtils.checkNotNull(getTcpipForwarderFactory(), "TcpipForwarderFactory not set", GenericUtils.EMPTY_OBJECT_ARRAY);
+ ValidateUtils.checkNotNull(getServerKeyVerifier(), "ServerKeyVerifier not set", GenericUtils.EMPTY_OBJECT_ARRAY);
+
// Register the additional agent forwarding channel if needed
if (getAgentFactory() != null) {
List<NamedFactory<Channel>> factories = getChannelFactories();
- if (factories == null) {
+ if (GenericUtils.isEmpty(factories)) {
factories = new ArrayList<NamedFactory<Channel>>();
} else {
factories = new ArrayList<NamedFactory<Channel>>(factories);
@@ -202,16 +182,15 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa
factories.add(getAgentFactory().getChannelForwardingFactory());
setChannelFactories(factories);
}
- if (getIoServiceFactoryFactory() == null) {
- setIoServiceFactoryFactory(new DefaultIoServiceFactoryFactory());
- }
- if (getServiceFactories() == null) {
+
+ if (GenericUtils.isEmpty(getServiceFactories())) {
setServiceFactories(Arrays.asList(
new ClientUserAuthService.Factory(),
new ClientConnectionService.Factory()
));
}
- if (getUserAuthFactories() == null) {
+
+ if (GenericUtils.isEmpty(getUserAuthFactories())) {
setUserAuthFactories(Arrays.asList(
UserAuthPublicKey.UserAuthPublicKeyFactory.INSTANCE,
UserAuthKeyboardInteractive.UserAuthKeyboardInteractiveFactory.INSTANCE,
@@ -248,6 +227,7 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa
protected Closeable getInnerCloseable() {
return builder()
.run(new Runnable() {
+ @SuppressWarnings("synthetic-access")
@Override
public void run() {
removeSessionTimeout(sessionFactory);
@@ -255,6 +235,7 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa
})
.sequential(connector, ioServiceFactory)
.run(new Runnable() {
+ @SuppressWarnings("synthetic-access")
@Override
public void run() {
connector = null;
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d9ed7ab2/sshd-core/src/main/java/org/apache/sshd/SshServer.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/SshServer.java b/sshd-core/src/main/java/org/apache/sshd/SshServer.java
index df81053..dfba133 100644
--- a/sshd-core/src/main/java/org/apache/sshd/SshServer.java
+++ b/sshd-core/src/main/java/org/apache/sshd/SshServer.java
@@ -36,16 +36,16 @@ import org.apache.sshd.common.ForwardingFilter;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.Session;
import org.apache.sshd.common.SshdSocketAddress;
-import org.apache.sshd.common.io.DefaultIoServiceFactoryFactory;
import org.apache.sshd.common.io.IoAcceptor;
import org.apache.sshd.common.io.IoServiceFactory;
import org.apache.sshd.common.io.IoSession;
import org.apache.sshd.common.io.mina.MinaServiceFactory;
import org.apache.sshd.common.io.nio2.Nio2ServiceFactory;
import org.apache.sshd.common.session.AbstractSession;
+import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.common.util.SecurityUtils;
-import org.apache.sshd.common.util.ThreadUtils;
+import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.CommandFactory;
import org.apache.sshd.server.PasswordAuthenticator;
@@ -211,14 +211,13 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
this.tcpipForwardingFilter = forwardingFilter;
}
+ @Override
protected void checkConfig() {
- if (getPort() < 0) {
- throw new IllegalArgumentException("Bad port number: " + port);
- }
- if (getKeyExchangeFactories() == null) {
- throw new IllegalArgumentException("KeyExchangeFactories not set");
- }
- if (getUserAuthFactories() == null) {
+ super.checkConfig();
+
+ ValidateUtils.checkTrue(getPort() >= 0 /* zero means not set yet */, "Bad port number: %d", Integer.valueOf(getPort()));
+
+ if (GenericUtils.isEmpty(getUserAuthFactories())) {
List<NamedFactory<UserAuth>> factories = new ArrayList<NamedFactory<UserAuth>>();
if (getPasswordAuthenticator() != null) {
factories.add(UserAuthPassword.UserAuthPasswordFactory.INSTANCE);
@@ -230,42 +229,16 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
if (getGSSAuthenticator() != null) {
factories.add(UserAuthGSS.UserAuthGSSFactory.INSTANCE);
}
- if (factories.size() > 0) {
- setUserAuthFactories(factories);
- } else {
- throw new IllegalArgumentException("UserAuthFactories not set");
- }
- }
- if (getScheduledExecutorService() == null) {
- setScheduledExecutorService(
- ThreadUtils.newSingleThreadScheduledExecutor(this.toString() + "-timer"),
- true);
- }
- if (getCipherFactories() == null) {
- throw new IllegalArgumentException("CipherFactories not set");
- }
- if (getCompressionFactories() == null) {
- throw new IllegalArgumentException("CompressionFactories not set");
- }
- if (getMacFactories() == null) {
- throw new IllegalArgumentException("MacFactories not set");
+
+ ValidateUtils.checkTrue(factories.size() > 0, "UserAuthFactories not set", GenericUtils.EMPTY_OBJECT_ARRAY);
+ setUserAuthFactories(factories);
}
- if (getChannelFactories() == null) {
- throw new IllegalArgumentException("ChannelFactories not set");
- }
- if (getRandomFactory() == null) {
- throw new IllegalArgumentException("RandomFactory not set");
- }
- if (getKeyPairProvider() == null) {
- throw new IllegalArgumentException("HostKeyProvider not set");
- }
- if (getFileSystemFactory() == null) {
- throw new IllegalArgumentException("FileSystemFactory not set");
- }
- if (getIoServiceFactoryFactory() == null) {
- setIoServiceFactoryFactory(new DefaultIoServiceFactoryFactory());
- }
- if (getServiceFactories() == null) {
+
+ ValidateUtils.checkNotNullAndNotEmpty(getChannelFactories(), "ChannelFactories not set", GenericUtils.EMPTY_OBJECT_ARRAY);
+ ValidateUtils.checkNotNull(getKeyPairProvider(), "HostKeyProvider not set", GenericUtils.EMPTY_OBJECT_ARRAY);
+ ValidateUtils.checkNotNull(getFileSystemFactory(), "FileSystemFactory not set", GenericUtils.EMPTY_OBJECT_ARRAY);
+
+ if (GenericUtils.isEmpty(getServiceFactories())) {
setServiceFactories(Arrays.asList(
new ServerUserAuthService.Factory(),
new ServerConnectionService.Factory()
@@ -288,14 +261,24 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
setupSessionTimeout(sessionFactory);
- if (host != null) {
- String[] hosts = host.split(",");
+ String hostsList=getHost();
+ if (!GenericUtils.isEmpty(hostsList)) {
+ String[] hosts = GenericUtils.split(hostsList, ',');
for (String host : hosts) {
+ if (log.isDebugEnabled()) {
+ log.debug("start() - resolve bind host={}", host);
+ }
+
InetAddress[] inetAddresses = InetAddress.getAllByName(host);
for (InetAddress inetAddress : inetAddresses) {
+ if (log.isTraceEnabled()) {
+ log.trace("start() - bind host={} / {}", host, inetAddress);
+ }
+
acceptor.bind(new InetSocketAddress(inetAddress, port));
if (port == 0) {
port = ((InetSocketAddress) acceptor.getBoundAddresses().iterator().next()).getPort();
+ log.info("start() listen on auto-allocated port=" + port);
}
}
}
@@ -303,6 +286,7 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
acceptor.bind(new InetSocketAddress(port));
if (port == 0) {
port = ((InetSocketAddress) acceptor.getBoundAddresses().iterator().next()).getPort();
+ log.info("start() listen on auto-allocated port=" + port);
}
}
}
@@ -326,6 +310,7 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
protected Closeable getInnerCloseable() {
return builder()
.run(new Runnable() {
+ @SuppressWarnings("synthetic-access")
@Override
public void run() {
removeSessionTimeout(sessionFactory);
@@ -333,6 +318,7 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
})
.sequential(acceptor, ioServiceFactory)
.run(new Runnable() {
+ @SuppressWarnings("synthetic-access")
@Override
public void run() {
acceptor = null;
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d9ed7ab2/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
index 22af67e..91ef180 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/AbstractFactoryManager.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/AbstractFactoryManager.java
@@ -30,12 +30,16 @@ import java.util.concurrent.TimeUnit;
import org.apache.sshd.agent.SshAgentFactory;
import org.apache.sshd.common.compression.Compression;
import org.apache.sshd.common.file.FileSystemFactory;
+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.session.AbstractSessionFactory;
import org.apache.sshd.common.session.ConnectionService;
import org.apache.sshd.common.session.SessionTimeoutListener;
import org.apache.sshd.common.util.CloseableUtils;
+import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.common.util.ThreadUtils;
+import org.apache.sshd.common.util.ValidateUtils;
/**
* TODO Add javadoc
@@ -304,4 +308,24 @@ public abstract class AbstractFactoryManager extends CloseableUtils.AbstractInne
}
sessionTimeoutListener = null;
}
+
+ protected void checkConfig() {
+ ValidateUtils.checkNotNullAndNotEmpty(getKeyExchangeFactories(), "KeyExchangeFactories not set", GenericUtils.EMPTY_OBJECT_ARRAY);
+
+ if (getScheduledExecutorService() == null) {
+ setScheduledExecutorService(
+ ThreadUtils.newSingleThreadScheduledExecutor(this.toString() + "-timer"),
+ true);
+ }
+
+ ValidateUtils.checkNotNullAndNotEmpty(getCipherFactories(), "CipherFactories not set", GenericUtils.EMPTY_OBJECT_ARRAY);
+ ValidateUtils.checkNotNullAndNotEmpty(getCompressionFactories(), "CompressionFactories not set", GenericUtils.EMPTY_OBJECT_ARRAY);
+ ValidateUtils.checkNotNullAndNotEmpty(getMacFactories(), "MacFactories not set", GenericUtils.EMPTY_OBJECT_ARRAY);
+
+ ValidateUtils.checkNotNull(getRandomFactory(), "RandomFactory not set", GenericUtils.EMPTY_OBJECT_ARRAY);
+
+ if (getIoServiceFactoryFactory() == null) {
+ setIoServiceFactoryFactory(new DefaultIoServiceFactoryFactory());
+ }
+ }
}