[SSHD-806] Split mina io support in its own module
Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/d07089ce Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/d07089ce Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/d07089ce Branch: refs/heads/master Commit: d07089ceafac5c07604f90fdde92c70a8046f54f Parents: d458c57 Author: Guillaume Nodet <gno...@apache.org> Authored: Tue Mar 6 11:19:32 2018 +0100 Committer: Guillaume Nodet <gno...@apache.org> Committed: Tue Mar 6 13:38:43 2018 +0100 ---------------------------------------------------------------------- pom.xml | 1 + sshd-core/pom.xml | 40 +--- ...pache.sshd.common.io.IoServiceFactoryFactory | 1 - .../io/BuiltinIoServiceFactoryFactories.java | 46 ++++- .../sshd/common/io/mina/MinaAcceptor.java | 118 ----------- .../sshd/common/io/mina/MinaConnector.java | 128 ------------ .../apache/sshd/common/io/mina/MinaService.java | 197 ------------------- .../sshd/common/io/mina/MinaServiceFactory.java | 56 ------ .../io/mina/MinaServiceFactoryFactory.java | 51 ----- .../apache/sshd/common/io/mina/MinaSession.java | 183 ----------------- .../apache/sshd/common/io/mina/MinaSupport.java | 48 ----- .../java/org/apache/sshd/server/SshServer.java | 7 +- .../java/org/apache/sshd/client/ClientTest.java | 10 +- .../client/config/keys/ClientIdentityTest.java | 2 +- .../BuiltinIoServiceFactoryFactoriesTest.java | 3 + .../io/DefaultIoServiceFactoryFactoryTest.java | 6 + .../sshd/common/util/SecurityUtilsTest.java | 2 +- .../server/config/keys/ServerIdentityTest.java | 2 +- .../server/subsystem/sftp/SshFsMounter.java | 7 +- .../apache/sshd/util/test/BaseTestSupport.java | 12 ++ sshd-mina/pom.xml | 150 ++++++++++++++ ...pache.sshd.common.io.IoServiceFactoryFactory | 20 ++ .../sshd/common/io/mina/MinaAcceptor.java | 118 +++++++++++ .../sshd/common/io/mina/MinaConnector.java | 128 ++++++++++++ .../apache/sshd/common/io/mina/MinaService.java | 197 +++++++++++++++++++ .../sshd/common/io/mina/MinaServiceFactory.java | 56 ++++++ .../io/mina/MinaServiceFactoryFactory.java | 51 +++++ .../apache/sshd/common/io/mina/MinaSession.java | 183 +++++++++++++++++ .../apache/sshd/common/io/mina/MinaSupport.java | 48 +++++ 29 files changed, 1033 insertions(+), 838 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index e5ac9b9..574d178 100644 --- a/pom.xml +++ b/pom.xml @@ -1004,6 +1004,7 @@ <modules> <module>sshd-core</module> + <module>sshd-mina</module> <module>sshd-ldap</module> <module>sshd-git</module> <module>sshd-contrib</module> http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/pom.xml ---------------------------------------------------------------------- diff --git a/sshd-core/pom.xml b/sshd-core/pom.xml index 7eac70c..60883ab 100644 --- a/sshd-core/pom.xml +++ b/sshd-core/pom.xml @@ -43,11 +43,6 @@ <artifactId>slf4j-api</artifactId> </dependency> <dependency> - <groupId>org.apache.mina</groupId> - <artifactId>mina-core</artifactId> - <optional>true</optional> - </dependency> - <dependency> <groupId>tomcat</groupId> <artifactId>tomcat-apr</artifactId> <optional>true</optional> @@ -73,6 +68,11 @@ <!-- test dependencies --> <dependency> + <groupId>org.apache.mina</groupId> + <artifactId>mina-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <scope>test</scope> @@ -205,36 +205,6 @@ </plugins> </build> </profile> - <!-- - <profile> - <id>test-mina</id> - <activation> - <activeByDefault>true</activeByDefault> - </activation> - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-surefire-plugin</artifactId> - <executions> - <execution> - <id>mina</id> - <goals> - <goal>test</goal> - </goals> - <configuration> - <reportsDirectory>${project.build.directory}/surefire-reports-mina</reportsDirectory> - <systemProperties> - <org.apache.sshd.common.io.IoServiceFactoryFactory>org.apache.sshd.common.io.mina.MinaServiceFactoryFactory</org.apache.sshd.common.io.IoServiceFactoryFactory> - </systemProperties> - </configuration> - </execution> - </executions> - </plugin> - </plugins> - </build> - </profile> - --> <profile> <id>load-test</id> <build> http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/main/filtered-resources/META-INF/services/org.apache.sshd.common.io.IoServiceFactoryFactory ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/filtered-resources/META-INF/services/org.apache.sshd.common.io.IoServiceFactoryFactory b/sshd-core/src/main/filtered-resources/META-INF/services/org.apache.sshd.common.io.IoServiceFactoryFactory index b59ba00..4190aeb 100644 --- a/sshd-core/src/main/filtered-resources/META-INF/services/org.apache.sshd.common.io.IoServiceFactoryFactory +++ b/sshd-core/src/main/filtered-resources/META-INF/services/org.apache.sshd.common.io.IoServiceFactoryFactory @@ -18,4 +18,3 @@ ## org.apache.sshd.common.io.nio2.Nio2ServiceFactoryFactory -org.apache.sshd.common.io.mina.MinaServiceFactoryFactory http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java b/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java index 1310735..9013223 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java @@ -25,27 +25,55 @@ import java.util.Set; import org.apache.sshd.common.NamedFactory; import org.apache.sshd.common.NamedResource; -import org.apache.sshd.common.io.mina.MinaServiceFactoryFactory; +import org.apache.sshd.common.OptionalFeature; import org.apache.sshd.common.io.nio2.Nio2ServiceFactoryFactory; /** * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> */ -public enum BuiltinIoServiceFactoryFactories implements NamedFactory<IoServiceFactoryFactory> { +public enum BuiltinIoServiceFactoryFactories implements NamedFactory<IoServiceFactoryFactory>, OptionalFeature { NIO2(Nio2ServiceFactoryFactory.class), - NMINA(MinaServiceFactoryFactory.class); + MINA("org.apache.sshd.common.io.mina.MinaServiceFactoryFactory"); public static final Set<BuiltinIoServiceFactoryFactories> VALUES = Collections.unmodifiableSet(EnumSet.allOf(BuiltinIoServiceFactoryFactories.class)); private final Class<? extends IoServiceFactoryFactory> factoryClass; + private final String factoryClassName; BuiltinIoServiceFactoryFactories(Class<? extends IoServiceFactoryFactory> clazz) { factoryClass = clazz; + factoryClassName = null; } + BuiltinIoServiceFactoryFactories(String clazz) { + factoryClass = null; + factoryClassName = clazz; + } + + public final String getFactoryClassName() { + if (factoryClass != null) { + return factoryClass.getName(); + } else { + return factoryClassName; + } + } + + @SuppressWarnings("unchecked") public final Class<? extends IoServiceFactoryFactory> getFactoryClass() { - return factoryClass; + if (factoryClass != null) { + return factoryClass; + } + try { + return (Class) Class.forName(factoryClassName, true, BuiltinIoServiceFactoryFactories.class.getClassLoader()); + } catch (ClassNotFoundException e) { + try { + return (Class) Class.forName(factoryClassName, true, Thread.currentThread().getContextClassLoader()); + } catch (ClassNotFoundException e1) { + throw new RuntimeException(e); + + } + } } @Override @@ -67,6 +95,15 @@ public enum BuiltinIoServiceFactoryFactories implements NamedFactory<IoServiceFa } } + @Override + public boolean isSupported() { + try { + return getFactoryClass() != null; + } catch (RuntimeException e) { + return false; + } + } + public static BuiltinIoServiceFactoryFactories fromFactoryName(String name) { return NamedResource.findByName(name, String.CASE_INSENSITIVE_ORDER, VALUES); } @@ -84,4 +121,5 @@ public enum BuiltinIoServiceFactoryFactories implements NamedFactory<IoServiceFa return null; } + } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java deleted file mode 100644 index 5f540e6..0000000 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java +++ /dev/null @@ -1,118 +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.io.mina; - -import java.io.IOException; -import java.net.SocketAddress; -import java.util.Collection; -import java.util.Set; -import java.util.concurrent.atomic.AtomicReference; - -import org.apache.mina.core.service.IoAcceptor; -import org.apache.mina.core.service.IoHandler; -import org.apache.mina.core.service.IoProcessor; -import org.apache.mina.core.service.IoService; -import org.apache.mina.transport.socket.nio.NioSession; -import org.apache.mina.transport.socket.nio.NioSocketAcceptor; -import org.apache.sshd.common.FactoryManager; - -/** - * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> - */ -public class MinaAcceptor extends MinaService implements org.apache.sshd.common.io.IoAcceptor, IoHandler { - protected final AtomicReference<IoAcceptor> acceptorHolder = new AtomicReference<>(null); - - // Acceptor - protected int backlog = DEFAULT_BACKLOG; - protected boolean reuseAddress = DEFAULT_REUSE_ADDRESS; - - public MinaAcceptor(FactoryManager manager, org.apache.sshd.common.io.IoHandler handler, IoProcessor<NioSession> ioProcessor) { - super(manager, handler, ioProcessor); - - backlog = manager.getIntProperty(FactoryManager.SOCKET_BACKLOG, DEFAULT_BACKLOG); - reuseAddress = manager.getBooleanProperty(FactoryManager.SOCKET_REUSEADDR, DEFAULT_REUSE_ADDRESS); - } - - protected IoAcceptor createAcceptor() { - NioSocketAcceptor acceptor = new NioSocketAcceptor(ioProcessor); - acceptor.setCloseOnDeactivation(false); - acceptor.setReuseAddress(reuseAddress); - acceptor.setBacklog(backlog); - configure(acceptor.getSessionConfig()); - return acceptor; - } - - protected IoAcceptor getAcceptor() { - IoAcceptor acceptor; - synchronized (acceptorHolder) { - acceptor = acceptorHolder.get(); - if (acceptor != null) { - return acceptor; - } - - acceptor = createAcceptor(); - acceptor.setHandler(this); - acceptorHolder.set(acceptor); - } - - log.debug("Created IoAcceptor"); - return acceptor; - } - - @Override - protected IoService getIoService() { - return getAcceptor(); - } - - @Override - public void bind(Collection<? extends SocketAddress> addresses) throws IOException { - IoAcceptor acceptor = getAcceptor(); - acceptor.bind(addresses); - } - - @Override - public void bind(SocketAddress address) throws IOException { - IoAcceptor acceptor = getAcceptor(); - acceptor.bind(address); - } - - @Override - public void unbind() { - IoAcceptor acceptor = getAcceptor(); - acceptor.unbind(); - } - - @Override - public void unbind(Collection<? extends SocketAddress> addresses) { - IoAcceptor acceptor = getAcceptor(); - acceptor.unbind(addresses); - } - - @Override - public void unbind(SocketAddress address) { - IoAcceptor acceptor = getAcceptor(); - acceptor.unbind(address); - } - - @Override - public Set<SocketAddress> getBoundAddresses() { - IoAcceptor acceptor = getAcceptor(); - return acceptor.getLocalAddresses(); - } -} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaConnector.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaConnector.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaConnector.java deleted file mode 100644 index e7da612..0000000 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaConnector.java +++ /dev/null @@ -1,128 +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.io.mina; - -import java.net.SocketAddress; -import java.util.concurrent.atomic.AtomicReference; - -import org.apache.mina.core.future.ConnectFuture; -import org.apache.mina.core.future.IoFutureListener; -import org.apache.mina.core.service.IoConnector; -import org.apache.mina.core.service.IoHandler; -import org.apache.mina.core.service.IoProcessor; -import org.apache.mina.core.session.IoSession; -import org.apache.mina.transport.socket.nio.NioSession; -import org.apache.mina.transport.socket.nio.NioSocketConnector; -import org.apache.sshd.common.FactoryManager; -import org.apache.sshd.common.future.DefaultSshFuture; -import org.apache.sshd.common.io.IoConnectFuture; - -/** - * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> - */ -public class MinaConnector extends MinaService implements org.apache.sshd.common.io.IoConnector, IoHandler { - protected final AtomicReference<IoConnector> connectorHolder = new AtomicReference<>(null); - - public MinaConnector(FactoryManager manager, org.apache.sshd.common.io.IoHandler handler, IoProcessor<NioSession> ioProcessor) { - super(manager, handler, ioProcessor); - } - - protected IoConnector createConnector() { - NioSocketConnector connector = new NioSocketConnector(ioProcessor); - configure(connector.getSessionConfig()); - return connector; - } - - protected IoConnector getConnector() { - IoConnector connector; - synchronized (connectorHolder) { - connector = connectorHolder.get(); - if (connector != null) { - return connector; - } - - connector = createConnector(); - connector.setHandler(this); - connectorHolder.set(connector); - } - - if (log.isDebugEnabled()) { - log.debug("Created IoConnector"); - } - return connector; - } - - @Override - protected org.apache.mina.core.service.IoService getIoService() { - return getConnector(); - } - - @Override - public IoConnectFuture connect(SocketAddress address) { - class Future extends DefaultSshFuture<IoConnectFuture> implements IoConnectFuture { - Future(Object lock) { - super(address, lock); - } - - @Override - public org.apache.sshd.common.io.IoSession getSession() { - Object v = getValue(); - return v instanceof org.apache.sshd.common.io.IoSession ? (org.apache.sshd.common.io.IoSession) v : null; - } - - @Override - public Throwable getException() { - Object v = getValue(); - return v instanceof Throwable ? (Throwable) v : null; - } - - @Override - public boolean isConnected() { - return getValue() instanceof org.apache.sshd.common.io.IoSession; - } - - @Override - public void setSession(org.apache.sshd.common.io.IoSession session) { - setValue(session); - } - - @Override - public void setException(Throwable exception) { - setValue(exception); - } - } - - IoConnectFuture future = new Future(null); - IoConnector connector = getConnector(); - ConnectFuture connectFuture = connector.connect(address); - connectFuture.addListener((IoFutureListener<ConnectFuture>) cf -> { - Throwable t = cf.getException(); - if (t != null) { - future.setException(t); - } else if (cf.isCanceled()) { - future.cancel(); - } else { - IoSession ioSession = cf.getSession(); - org.apache.sshd.common.io.IoSession sshSession = getSession(ioSession); - future.setSession(sshSession); - } - }); - return future; - } -} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaService.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaService.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaService.java deleted file mode 100644 index 15c1a7f..0000000 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaService.java +++ /dev/null @@ -1,197 +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.io.mina; - -import java.util.Comparator; -import java.util.Map; -import java.util.TreeMap; - -import org.apache.mina.core.RuntimeIoException; -import org.apache.mina.core.buffer.IoBuffer; -import org.apache.mina.core.service.IoHandler; -import org.apache.mina.core.service.IoProcessor; -import org.apache.mina.core.service.IoService; -import org.apache.mina.core.session.IdleStatus; -import org.apache.mina.core.session.IoSession; -import org.apache.mina.core.session.IoSessionConfig; -import org.apache.mina.transport.socket.SocketSessionConfig; -import org.apache.mina.transport.socket.nio.NioSession; -import org.apache.sshd.common.Closeable; -import org.apache.sshd.common.FactoryManager; -import org.apache.sshd.common.util.GenericUtils; -import org.apache.sshd.common.util.closeable.AbstractCloseable; - -/** - * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> - */ -public abstract class MinaService extends AbstractCloseable implements org.apache.sshd.common.io.IoService, IoHandler, Closeable { - protected final FactoryManager manager; - protected final org.apache.sshd.common.io.IoHandler handler; - protected final IoProcessor<NioSession> ioProcessor; - protected IoSessionConfig sessionConfig; - - public MinaService(FactoryManager manager, org.apache.sshd.common.io.IoHandler handler, IoProcessor<NioSession> ioProcessor) { - this.manager = manager; - this.handler = handler; - this.ioProcessor = ioProcessor; - } - - protected abstract IoService getIoService(); - - public void dispose() { - IoService ioService = getIoService(); - ioService.dispose(); - } - - @Override - protected void doCloseImmediately() { - IoService ioService = getIoService(); - ioService.dispose(); - super.doCloseImmediately(); - } - - @Override - public Map<Long, org.apache.sshd.common.io.IoSession> getManagedSessions() { - IoService ioService = getIoService(); - Map<Long, IoSession> managedMap = ioService.getManagedSessions(); - Map<Long, IoSession> mina = new TreeMap<>(managedMap); - Map<Long, org.apache.sshd.common.io.IoSession> sessions = new TreeMap<>(Comparator.naturalOrder()); - for (Long id : mina.keySet()) { - // Avoid possible NPE if the MinaSession hasn't been created yet - IoSession minaSession = mina.get(id); - org.apache.sshd.common.io.IoSession session = getSession(minaSession); - if (session != null) { - sessions.put(id, session); - } - } - return sessions; - } - - @Override - public void sessionOpened(IoSession session) throws Exception { - // Empty handler - } - - @Override - public void sessionIdle(IoSession session, IdleStatus status) throws Exception { - // Empty handler - } - - @Override - public void messageSent(IoSession session, Object message) throws Exception { - // Empty handler - } - - @Override - public void inputClosed(IoSession session) throws Exception { - session.closeNow(); - } - - @Override - public void sessionCreated(IoSession session) throws Exception { - org.apache.sshd.common.io.IoSession ioSession = new MinaSession(this, session); - session.setAttribute(org.apache.sshd.common.io.IoSession.class, ioSession); - handler.sessionCreated(ioSession); - } - - @Override - public void sessionClosed(IoSession session) throws Exception { - handler.sessionClosed(getSession(session)); - } - - @Override - public void exceptionCaught(IoSession session, Throwable cause) throws Exception { - handler.exceptionCaught(getSession(session), cause); - } - - @Override - public void messageReceived(IoSession session, Object message) throws Exception { - handler.messageReceived(getSession(session), MinaSupport.asReadable((IoBuffer) message)); - } - - protected org.apache.sshd.common.io.IoSession getSession(IoSession session) { - return (org.apache.sshd.common.io.IoSession) - session.getAttribute(org.apache.sshd.common.io.IoSession.class); - } - - protected void configure(SocketSessionConfig config) { - Boolean boolVal = getBoolean(FactoryManager.SOCKET_KEEPALIVE); - if (boolVal != null) { - try { - config.setKeepAlive(boolVal); - } catch (RuntimeIoException t) { - handleConfigurationError(config, FactoryManager.SOCKET_KEEPALIVE, boolVal, t); - } - } - - Integer intVal = getInteger(FactoryManager.SOCKET_SNDBUF); - if (intVal != null) { - try { - config.setSendBufferSize(intVal); - } catch (RuntimeIoException t) { - handleConfigurationError(config, FactoryManager.SOCKET_SNDBUF, intVal, t); - } - } - - intVal = getInteger(FactoryManager.SOCKET_RCVBUF); - if (intVal != null) { - try { - config.setReceiveBufferSize(intVal); - } catch (RuntimeIoException t) { - handleConfigurationError(config, FactoryManager.SOCKET_RCVBUF, intVal, t); - } - } - - intVal = getInteger(FactoryManager.SOCKET_LINGER); - if (intVal != null) { - try { - config.setSoLinger(intVal); - } catch (RuntimeIoException t) { - handleConfigurationError(config, FactoryManager.SOCKET_LINGER, intVal, t); - } - } - - boolVal = getBoolean(FactoryManager.TCP_NODELAY); - if (boolVal != null) { - try { - config.setTcpNoDelay(boolVal); - } catch (RuntimeIoException t) { - handleConfigurationError(config, FactoryManager.TCP_NODELAY, boolVal, t); - } - } - - if (sessionConfig != null) { - config.setAll(sessionConfig); - } - } - - protected void handleConfigurationError(SocketSessionConfig config, String propName, Object propValue, RuntimeIoException t) { - Throwable e = GenericUtils.resolveExceptionCause(t); - log.warn("handleConfigurationError({}={}) failed ({}) to configure: {}", - propName, propValue, e.getClass().getSimpleName(), e.getMessage()); - } - - protected Integer getInteger(String property) { - return manager.getInteger(property); - } - - protected Boolean getBoolean(String property) { - return manager.getBoolean(property); - } -} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java deleted file mode 100644 index dd16ba4..0000000 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java +++ /dev/null @@ -1,56 +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.io.mina; - -import java.util.concurrent.ExecutorService; - -import org.apache.mina.core.service.IoProcessor; -import org.apache.mina.core.service.SimpleIoProcessorPool; -import org.apache.mina.transport.socket.nio.NioProcessor; -import org.apache.mina.transport.socket.nio.NioSession; -import org.apache.sshd.common.FactoryManager; -import org.apache.sshd.common.io.AbstractIoServiceFactory; -import org.apache.sshd.common.io.IoAcceptor; -import org.apache.sshd.common.io.IoConnector; -import org.apache.sshd.common.io.IoHandler; -import org.apache.sshd.common.util.threads.ThreadUtils; - -/** - */ -public class MinaServiceFactory extends AbstractIoServiceFactory { - - private final IoProcessor<NioSession> ioProcessor; - - public MinaServiceFactory(FactoryManager factoryManager, ExecutorService service, boolean shutdownOnExit) { - super(factoryManager, - service == null ? ThreadUtils.newCachedThreadPool(factoryManager.toString() + "-mina") : service, - service == null || shutdownOnExit); - ioProcessor = new SimpleIoProcessorPool<>(NioProcessor.class, getExecutorService(), getNioWorkers(factoryManager), null); - } - - @Override - public IoConnector createConnector(IoHandler handler) { - return new MinaConnector(getFactoryManager(), handler, ioProcessor); - } - - @Override - public IoAcceptor createAcceptor(IoHandler handler) { - return new MinaAcceptor(getFactoryManager(), handler, ioProcessor); - } -} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactoryFactory.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactoryFactory.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactoryFactory.java deleted file mode 100644 index 7057d5a..0000000 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactoryFactory.java +++ /dev/null @@ -1,51 +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.io.mina; - -import java.util.concurrent.ExecutorService; - -import org.apache.sshd.common.FactoryManager; -import org.apache.sshd.common.io.AbstractIoServiceFactoryFactory; -import org.apache.sshd.common.io.IoServiceFactory; - -/** - */ -public class MinaServiceFactoryFactory extends AbstractIoServiceFactoryFactory { - public MinaServiceFactoryFactory() { - this(null, true); - } - - /** - * @param executors The {@link ExecutorService} to use for spawning threads. - * If {@code null} then an internal service is allocated - in which case it - * is automatically shutdown regardless of the value of the <tt>shutdownOnExit</tt> - * parameter value - * @param shutdownOnExit If {@code true} then the {@link ExecutorService#shutdownNow()} - * will be called (unless it is an internally allocated service which is always - * closed) - */ - public MinaServiceFactoryFactory(ExecutorService executors, boolean shutdownOnExit) { - super(executors, shutdownOnExit); - } - - @Override - public IoServiceFactory create(FactoryManager manager) { - return new MinaServiceFactory(manager, getExecutorService(), isShutdownOnExit()); - } -} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java deleted file mode 100644 index 259f044..0000000 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java +++ /dev/null @@ -1,183 +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.io.mina; - -import java.net.SocketAddress; -import java.util.Objects; - -import org.apache.mina.core.buffer.IoBuffer; -import org.apache.mina.core.future.IoFutureListener; -import org.apache.mina.core.future.WriteFuture; -import org.apache.sshd.common.Closeable; -import org.apache.sshd.common.future.CloseFuture; -import org.apache.sshd.common.future.DefaultCloseFuture; -import org.apache.sshd.common.future.SshFutureListener; -import org.apache.sshd.common.io.AbstractIoWriteFuture; -import org.apache.sshd.common.io.IoService; -import org.apache.sshd.common.io.IoSession; -import org.apache.sshd.common.io.IoWriteFuture; -import org.apache.sshd.common.util.NumberUtils; -import org.apache.sshd.common.util.buffer.Buffer; -import org.apache.sshd.common.util.closeable.AbstractInnerCloseable; -import org.apache.sshd.common.util.closeable.IoBaseCloseable; - -/** - * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> - */ -public class MinaSession extends AbstractInnerCloseable implements IoSession { - private final MinaService service; - private final org.apache.mina.core.session.IoSession session; - private final Object sessionWriteId; - - public MinaSession(MinaService service, org.apache.mina.core.session.IoSession session) { - this.service = service; - this.session = session; - this.sessionWriteId = Objects.toString(session); - } - - public org.apache.mina.core.session.IoSession getSession() { - return session; - } - - public void suspend() { - session.suspendRead(); - session.suspendWrite(); - } - - @Override - public Object getAttribute(Object key) { - return session.getAttribute(key); - } - - @Override - public Object setAttribute(Object key, Object value) { - return session.setAttribute(key, value); - } - - @Override - public Object removeAttribute(Object key) { - return session.removeAttribute(key); - } - - @Override - public SocketAddress getRemoteAddress() { - return session.getRemoteAddress(); - } - - @Override - public SocketAddress getLocalAddress() { - return session.getLocalAddress(); - } - - @Override - public long getId() { - return session.getId(); - } - - @Override - protected Closeable getInnerCloseable() { - return new IoBaseCloseable() { - @SuppressWarnings("synthetic-access") - private final DefaultCloseFuture future = new DefaultCloseFuture(MinaSession.this.toString(), lock); - - @SuppressWarnings("synthetic-access") - @Override - public boolean isClosing() { - return session.isClosing(); - } - - @SuppressWarnings("synthetic-access") - @Override - public boolean isClosed() { - return !session.isConnected(); - } - - @Override - public void addCloseFutureListener(SshFutureListener<CloseFuture> listener) { - future.addListener(listener); - } - - @Override - public void removeCloseFutureListener(SshFutureListener<CloseFuture> listener) { - future.removeListener(listener); - } - - @SuppressWarnings("synthetic-access") - @Override - public org.apache.sshd.common.future.CloseFuture close(boolean immediately) { - org.apache.mina.core.future.CloseFuture cf = immediately ? session.closeNow() : session.closeOnFlush(); - cf.addListener(f -> future.setValue(Boolean.TRUE)); - return future; - } - }; - } - - // NOTE !!! data buffer may NOT be re-used when method returns - at least until IoWriteFuture is signalled - public IoWriteFuture write(byte[] data) { - return write(data, 0, NumberUtils.length(data)); - } - - // NOTE !!! data buffer may NOT be re-used when method returns - at least until IoWriteFuture is signalled - public IoWriteFuture write(byte[] data, int offset, int len) { - return write(IoBuffer.wrap(data, offset, len)); - } - - @Override // NOTE !!! data buffer may NOT be re-used when method returns - at least until IoWriteFuture is signalled - public IoWriteFuture writePacket(Buffer buffer) { - return write(MinaSupport.asIoBuffer(buffer)); - } - - // NOTE !!! data buffer may NOT be re-used when method returns - at least until IoWriteFuture is signalled - public IoWriteFuture write(IoBuffer buffer) { - Future future = new Future(sessionWriteId, null); - session.write(buffer).addListener((IoFutureListener<WriteFuture>) cf -> { - Throwable t = cf.getException(); - if (t != null) { - future.setException(t); - } else { - future.setWritten(); - } - }); - return future; - } - - public static class Future extends AbstractIoWriteFuture { - public Future(Object id, Object lock) { - super(id, lock); - } - - public void setWritten() { - setValue(Boolean.TRUE); - } - - public void setException(Throwable exception) { - setValue(Objects.requireNonNull(exception, "No exception specified")); - } - } - - @Override - public IoService getService() { - return service; - } - - @Override - public String toString() { - return getClass().getSimpleName() + "[local=" + session.getLocalAddress() + ", remote=" + session.getRemoteAddress() + "]"; - } -} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSupport.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSupport.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSupport.java deleted file mode 100644 index cb943ba..0000000 --- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSupport.java +++ /dev/null @@ -1,48 +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.io.mina; - -import org.apache.mina.core.buffer.IoBuffer; -import org.apache.sshd.common.util.Readable; -import org.apache.sshd.common.util.buffer.Buffer; - -public final class MinaSupport { - - private MinaSupport() { - throw new UnsupportedOperationException("No instance allowed"); - } - - public static Readable asReadable(final IoBuffer buffer) { - return new Readable() { - @Override - public int available() { - return buffer.remaining(); - } - - @Override - public void getRawBytes(byte[] data, int offset, int len) { - buffer.get(data, offset, len); - } - }; - } - - public static IoBuffer asIoBuffer(Buffer buffer) { - return IoBuffer.wrap(buffer.array(), buffer.rpos(), buffer.available()); - } -} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java b/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java index 5725152..839c0ef 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/SshServer.java @@ -52,11 +52,10 @@ import org.apache.sshd.common.config.SshConfigFileReader; import org.apache.sshd.common.config.keys.BuiltinIdentities; import org.apache.sshd.common.config.keys.KeyUtils; import org.apache.sshd.common.helpers.AbstractFactoryManager; +import org.apache.sshd.common.io.BuiltinIoServiceFactoryFactories; 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.keyprovider.KeyPairProvider; import org.apache.sshd.common.keyprovider.MappedKeyPairProvider; import org.apache.sshd.common.session.helpers.AbstractSession; @@ -553,9 +552,9 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa } provider = args[++i]; if ("mina".equals(provider)) { - System.setProperty(IoServiceFactory.class.getName(), MinaServiceFactory.class.getName()); + System.setProperty(IoServiceFactory.class.getName(), BuiltinIoServiceFactoryFactories.MINA.getFactoryClassName()); } else if ("nio2".endsWith(provider)) { - System.setProperty(IoServiceFactory.class.getName(), Nio2ServiceFactory.class.getName()); + System.setProperty(IoServiceFactory.class.getName(), BuiltinIoServiceFactoryFactories.NIO2.getFactoryClassName()); } else { System.err.println("provider should be mina or nio2: " + argName); error = true; http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java b/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java index 8d41661..e67df5f 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java @@ -86,8 +86,6 @@ import org.apache.sshd.common.io.IoOutputStream; import org.apache.sshd.common.io.IoReadFuture; import org.apache.sshd.common.io.IoSession; import org.apache.sshd.common.io.IoWriteFuture; -import org.apache.sshd.common.io.mina.MinaSession; -import org.apache.sshd.common.io.nio2.Nio2Session; import org.apache.sshd.common.keyprovider.KeyPairProvider; import org.apache.sshd.common.session.ConnectionService; import org.apache.sshd.common.session.Session; @@ -1534,10 +1532,10 @@ public class ClientTest extends BaseTestSupport { } private void suspend(IoSession ioSession) { - if (ioSession instanceof MinaSession) { - ((MinaSession) ioSession).suspend(); - } else { - ((Nio2Session) ioSession).suspend(); + try { + ioSession.getClass().getMethod("suspend").invoke(ioSession); + } catch (Exception e) { + throw new RuntimeException(e); } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/test/java/org/apache/sshd/client/config/keys/ClientIdentityTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/client/config/keys/ClientIdentityTest.java b/sshd-core/src/test/java/org/apache/sshd/client/config/keys/ClientIdentityTest.java index 580d051..d6c692a 100644 --- a/sshd-core/src/test/java/org/apache/sshd/client/config/keys/ClientIdentityTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/client/config/keys/ClientIdentityTest.java @@ -49,7 +49,7 @@ public class ClientIdentityTest extends BaseTestSupport { @Test public void testLoadClientIdentities() throws Exception { - Path resFolder = getClassResourcesFolder(TEST_SUBFOLDER, getClass()); + Path resFolder = getTestResourcesFolder(); LinkOption[] options = IoUtils.getLinkOptions(true); Collection<BuiltinIdentities> expected = EnumSet.noneOf(BuiltinIdentities.class); for (BuiltinIdentities type : BuiltinIdentities.VALUES) { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/test/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactoriesTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactoriesTest.java b/sshd-core/src/test/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactoriesTest.java index 1eda0b4..9f5fbf9 100644 --- a/sshd-core/src/test/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactoriesTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactoriesTest.java @@ -48,6 +48,9 @@ public class BuiltinIoServiceFactoryFactoriesTest extends BaseTestSupport { @Test public void testFromFactoryClass() { for (BuiltinIoServiceFactoryFactories expected : BuiltinIoServiceFactoryFactories.VALUES) { + if (!expected.isSupported()) { + continue; + } Class<?> clazz = expected.getFactoryClass(); assertSame(clazz.getSimpleName(), expected, BuiltinIoServiceFactoryFactories.fromFactoryClass(clazz)); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/test/java/org/apache/sshd/common/io/DefaultIoServiceFactoryFactoryTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/common/io/DefaultIoServiceFactoryFactoryTest.java b/sshd-core/src/test/java/org/apache/sshd/common/io/DefaultIoServiceFactoryFactoryTest.java index 965c7d4..0b664d4 100644 --- a/sshd-core/src/test/java/org/apache/sshd/common/io/DefaultIoServiceFactoryFactoryTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/common/io/DefaultIoServiceFactoryFactoryTest.java @@ -43,6 +43,9 @@ public class DefaultIoServiceFactoryFactoryTest extends BaseTestSupport { @Test public void testBuiltinIoServiceFactoryFactories() { for (BuiltinIoServiceFactoryFactories f : BuiltinIoServiceFactoryFactories.VALUES) { + if (!f.isSupported()) { + continue; + } String name = f.getName(); IoServiceFactoryFactory factoryInstance = DefaultIoServiceFactoryFactory.newInstance(IoServiceFactoryFactory.class, name); @@ -65,6 +68,9 @@ public class DefaultIoServiceFactoryFactoryTest extends BaseTestSupport { String propName = IoServiceFactoryFactory.class.getName(); for (BuiltinIoServiceFactoryFactories f : BuiltinIoServiceFactoryFactories.VALUES) { + if (!f.isSupported()) { + continue; + } String name = f.getName(); try { System.setProperty(propName, name); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/test/java/org/apache/sshd/common/util/SecurityUtilsTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/SecurityUtilsTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/SecurityUtilsTest.java index 8bd2129..a9b8181 100644 --- a/sshd-core/src/test/java/org/apache/sshd/common/util/SecurityUtilsTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/common/util/SecurityUtilsTest.java @@ -145,7 +145,7 @@ public class SecurityUtilsTest extends BaseTestSupport { private KeyPair testLoadPrivateKey(String name, Class<? extends PublicKey> pubType, Class<? extends PrivateKey> prvType) throws IOException, GeneralSecurityException { - Path folder = getClassResourcesFolder(TEST_SUBFOLDER); + Path folder = getTestResourcesFolder(); Path file = folder.resolve(name); KeyPair kpFile = testLoadPrivateKeyFile(file, pubType, prvType); if (SecurityUtils.isBouncyCastleRegistered()) { http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/test/java/org/apache/sshd/server/config/keys/ServerIdentityTest.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/server/config/keys/ServerIdentityTest.java b/sshd-core/src/test/java/org/apache/sshd/server/config/keys/ServerIdentityTest.java index c6c379d..49fef0b 100644 --- a/sshd-core/src/test/java/org/apache/sshd/server/config/keys/ServerIdentityTest.java +++ b/sshd-core/src/test/java/org/apache/sshd/server/config/keys/ServerIdentityTest.java @@ -50,7 +50,7 @@ public class ServerIdentityTest extends BaseTestSupport { @Test public void testLoadServerIdentities() throws Exception { - Path resFolder = getClassResourcesFolder(TEST_SUBFOLDER, getClass()); + Path resFolder = getTestResourcesFolder(); Collection<Path> paths = new ArrayList<>(BuiltinIdentities.VALUES.size()); LinkOption[] options = IoUtils.getLinkOptions(true); Collection<BuiltinIdentities> expected = EnumSet.noneOf(BuiltinIdentities.class); http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/test/java/org/apache/sshd/server/subsystem/sftp/SshFsMounter.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/server/subsystem/sftp/SshFsMounter.java b/sshd-core/src/test/java/org/apache/sshd/server/subsystem/sftp/SshFsMounter.java index 6b57926..e6b10e0 100644 --- a/sshd-core/src/test/java/org/apache/sshd/server/subsystem/sftp/SshFsMounter.java +++ b/sshd-core/src/test/java/org/apache/sshd/server/subsystem/sftp/SshFsMounter.java @@ -36,9 +36,8 @@ import java.util.concurrent.Future; import org.apache.sshd.common.PropertyResolver; import org.apache.sshd.common.PropertyResolverUtils; import org.apache.sshd.common.config.SshConfigFileReader; +import org.apache.sshd.common.io.BuiltinIoServiceFactoryFactories; import org.apache.sshd.common.io.IoServiceFactory; -import org.apache.sshd.common.io.mina.MinaServiceFactory; -import org.apache.sshd.common.io.nio2.Nio2ServiceFactory; import org.apache.sshd.common.util.GenericUtils; import org.apache.sshd.common.util.ValidateUtils; import org.apache.sshd.common.util.logging.AbstractLoggingBean; @@ -264,9 +263,9 @@ public final class SshFsMounter { String provider = args[++i]; if ("mina".equals(provider)) { - System.setProperty(IoServiceFactory.class.getName(), MinaServiceFactory.class.getName()); + System.setProperty(IoServiceFactory.class.getName(), BuiltinIoServiceFactoryFactories.MINA.getFactoryClassName()); } else if ("nio2".endsWith(provider)) { - System.setProperty(IoServiceFactory.class.getName(), Nio2ServiceFactory.class.getName()); + System.setProperty(IoServiceFactory.class.getName(), BuiltinIoServiceFactoryFactories.NIO2.getFactoryClassName()); } else { System.err.println("provider should be mina or nio2: " + argName); error = true; http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-core/src/test/java/org/apache/sshd/util/test/BaseTestSupport.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/test/java/org/apache/sshd/util/test/BaseTestSupport.java b/sshd-core/src/test/java/org/apache/sshd/util/test/BaseTestSupport.java index c8227ed..2263217 100644 --- a/sshd-core/src/test/java/org/apache/sshd/util/test/BaseTestSupport.java +++ b/sshd-core/src/test/java/org/apache/sshd/util/test/BaseTestSupport.java @@ -21,9 +21,12 @@ package org.apache.sshd.util.test; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URISyntaxException; +import java.net.URL; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; +import java.nio.file.Paths; import java.security.Key; import java.security.KeyPair; import java.security.interfaces.DSAParams; @@ -217,6 +220,15 @@ public abstract class BaseTestSupport extends Assert { return parent.resolve("src"); } + protected Path getTestResourcesFolder() { + try { + URL url = getClass().getResource(getClass().getSimpleName() + ".class"); + return Paths.get(url.toURI()).getParent(); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } + protected Path getClassResourcesFolder(String resType /* test or main */) { return getClassResourcesFolder(resType, getClass()); } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-mina/pom.xml ---------------------------------------------------------------------- diff --git a/sshd-mina/pom.xml b/sshd-mina/pom.xml new file mode 100644 index 0000000..aaba57d --- /dev/null +++ b/sshd-mina/pom.xml @@ -0,0 +1,150 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + + <!-- + + 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. + --> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.sshd</groupId> + <artifactId>sshd</artifactId> + <version>1.7.1-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + + <artifactId>sshd-mina</artifactId> + <name>Apache Mina SSHD :: Mina</name> + <packaging>jar</packaging> + <inceptionYear>2008</inceptionYear> + + <properties> + <projectRoot>${basedir}/..</projectRoot> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.sshd</groupId> + <artifactId>sshd-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.mina</groupId> + <artifactId>mina-core</artifactId> + </dependency> + + <!-- test dependencies --> + <dependency> + <groupId>net.i2p.crypto</groupId> + <artifactId>eddsa</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>jcl-over-slf4j</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.jcraft</groupId> + <artifactId>jsch</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.jcraft</groupId> + <artifactId>jzlib</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>commons-httpclient</groupId> + <artifactId>commons-httpclient</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>ch.ethz.ganymed</groupId> + <artifactId>ganymed-ssh2</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.servicemix.bundles</groupId> + <artifactId>org.apache.servicemix.bundles.not-yet-commons-ssl</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <!-- + <testSourceDirectory>${basedir}/../sshd-core/src/test/java</testSourceDirectory> + --> + <resources> + <resource> + <directory>src/main/filtered-resources</directory> + <filtering>true</filtering> + </resource> + <!-- + <resource> + <directory>${basedir}/../sshd-core/src/test/resources</directory> + <targetPath>${basedir}/target/test-classes</targetPath> + </resource> + --> + </resources> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <redirectTestOutputToFile>true</redirectTestOutputToFile> + <reportsDirectory>${project.build.directory}/surefire-reports-mina</reportsDirectory> + <systemProperties> + <org.apache.sshd.common.io.IoServiceFactory>org.apache.sshd.common.io.mina.MinaServiceFactory</org.apache.sshd.common.io.IoServiceFactory> + </systemProperties> + <excludes> + <exclude>**/*LoadTest.java</exclude> + </excludes> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <configuration> + <additionalparam>-Xdoclint:none</additionalparam> + </configuration> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-mina/src/main/filtered-resources/META-INF/services/org.apache.sshd.common.io.IoServiceFactoryFactory ---------------------------------------------------------------------- diff --git a/sshd-mina/src/main/filtered-resources/META-INF/services/org.apache.sshd.common.io.IoServiceFactoryFactory b/sshd-mina/src/main/filtered-resources/META-INF/services/org.apache.sshd.common.io.IoServiceFactoryFactory new file mode 100644 index 0000000..b842d2f --- /dev/null +++ b/sshd-mina/src/main/filtered-resources/META-INF/services/org.apache.sshd.common.io.IoServiceFactoryFactory @@ -0,0 +1,20 @@ +## +## 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. +## + +org.apache.sshd.common.io.mina.MinaServiceFactoryFactory http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java ---------------------------------------------------------------------- diff --git a/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java b/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java new file mode 100644 index 0000000..5f540e6 --- /dev/null +++ b/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaAcceptor.java @@ -0,0 +1,118 @@ +/* + * 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.io.mina; + +import java.io.IOException; +import java.net.SocketAddress; +import java.util.Collection; +import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.mina.core.service.IoAcceptor; +import org.apache.mina.core.service.IoHandler; +import org.apache.mina.core.service.IoProcessor; +import org.apache.mina.core.service.IoService; +import org.apache.mina.transport.socket.nio.NioSession; +import org.apache.mina.transport.socket.nio.NioSocketAcceptor; +import org.apache.sshd.common.FactoryManager; + +/** + * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> + */ +public class MinaAcceptor extends MinaService implements org.apache.sshd.common.io.IoAcceptor, IoHandler { + protected final AtomicReference<IoAcceptor> acceptorHolder = new AtomicReference<>(null); + + // Acceptor + protected int backlog = DEFAULT_BACKLOG; + protected boolean reuseAddress = DEFAULT_REUSE_ADDRESS; + + public MinaAcceptor(FactoryManager manager, org.apache.sshd.common.io.IoHandler handler, IoProcessor<NioSession> ioProcessor) { + super(manager, handler, ioProcessor); + + backlog = manager.getIntProperty(FactoryManager.SOCKET_BACKLOG, DEFAULT_BACKLOG); + reuseAddress = manager.getBooleanProperty(FactoryManager.SOCKET_REUSEADDR, DEFAULT_REUSE_ADDRESS); + } + + protected IoAcceptor createAcceptor() { + NioSocketAcceptor acceptor = new NioSocketAcceptor(ioProcessor); + acceptor.setCloseOnDeactivation(false); + acceptor.setReuseAddress(reuseAddress); + acceptor.setBacklog(backlog); + configure(acceptor.getSessionConfig()); + return acceptor; + } + + protected IoAcceptor getAcceptor() { + IoAcceptor acceptor; + synchronized (acceptorHolder) { + acceptor = acceptorHolder.get(); + if (acceptor != null) { + return acceptor; + } + + acceptor = createAcceptor(); + acceptor.setHandler(this); + acceptorHolder.set(acceptor); + } + + log.debug("Created IoAcceptor"); + return acceptor; + } + + @Override + protected IoService getIoService() { + return getAcceptor(); + } + + @Override + public void bind(Collection<? extends SocketAddress> addresses) throws IOException { + IoAcceptor acceptor = getAcceptor(); + acceptor.bind(addresses); + } + + @Override + public void bind(SocketAddress address) throws IOException { + IoAcceptor acceptor = getAcceptor(); + acceptor.bind(address); + } + + @Override + public void unbind() { + IoAcceptor acceptor = getAcceptor(); + acceptor.unbind(); + } + + @Override + public void unbind(Collection<? extends SocketAddress> addresses) { + IoAcceptor acceptor = getAcceptor(); + acceptor.unbind(addresses); + } + + @Override + public void unbind(SocketAddress address) { + IoAcceptor acceptor = getAcceptor(); + acceptor.unbind(address); + } + + @Override + public Set<SocketAddress> getBoundAddresses() { + IoAcceptor acceptor = getAcceptor(); + return acceptor.getLocalAddresses(); + } +} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaConnector.java ---------------------------------------------------------------------- diff --git a/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaConnector.java b/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaConnector.java new file mode 100644 index 0000000..e7da612 --- /dev/null +++ b/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaConnector.java @@ -0,0 +1,128 @@ +/* + * 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.io.mina; + +import java.net.SocketAddress; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.mina.core.future.ConnectFuture; +import org.apache.mina.core.future.IoFutureListener; +import org.apache.mina.core.service.IoConnector; +import org.apache.mina.core.service.IoHandler; +import org.apache.mina.core.service.IoProcessor; +import org.apache.mina.core.session.IoSession; +import org.apache.mina.transport.socket.nio.NioSession; +import org.apache.mina.transport.socket.nio.NioSocketConnector; +import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.future.DefaultSshFuture; +import org.apache.sshd.common.io.IoConnectFuture; + +/** + * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> + */ +public class MinaConnector extends MinaService implements org.apache.sshd.common.io.IoConnector, IoHandler { + protected final AtomicReference<IoConnector> connectorHolder = new AtomicReference<>(null); + + public MinaConnector(FactoryManager manager, org.apache.sshd.common.io.IoHandler handler, IoProcessor<NioSession> ioProcessor) { + super(manager, handler, ioProcessor); + } + + protected IoConnector createConnector() { + NioSocketConnector connector = new NioSocketConnector(ioProcessor); + configure(connector.getSessionConfig()); + return connector; + } + + protected IoConnector getConnector() { + IoConnector connector; + synchronized (connectorHolder) { + connector = connectorHolder.get(); + if (connector != null) { + return connector; + } + + connector = createConnector(); + connector.setHandler(this); + connectorHolder.set(connector); + } + + if (log.isDebugEnabled()) { + log.debug("Created IoConnector"); + } + return connector; + } + + @Override + protected org.apache.mina.core.service.IoService getIoService() { + return getConnector(); + } + + @Override + public IoConnectFuture connect(SocketAddress address) { + class Future extends DefaultSshFuture<IoConnectFuture> implements IoConnectFuture { + Future(Object lock) { + super(address, lock); + } + + @Override + public org.apache.sshd.common.io.IoSession getSession() { + Object v = getValue(); + return v instanceof org.apache.sshd.common.io.IoSession ? (org.apache.sshd.common.io.IoSession) v : null; + } + + @Override + public Throwable getException() { + Object v = getValue(); + return v instanceof Throwable ? (Throwable) v : null; + } + + @Override + public boolean isConnected() { + return getValue() instanceof org.apache.sshd.common.io.IoSession; + } + + @Override + public void setSession(org.apache.sshd.common.io.IoSession session) { + setValue(session); + } + + @Override + public void setException(Throwable exception) { + setValue(exception); + } + } + + IoConnectFuture future = new Future(null); + IoConnector connector = getConnector(); + ConnectFuture connectFuture = connector.connect(address); + connectFuture.addListener((IoFutureListener<ConnectFuture>) cf -> { + Throwable t = cf.getException(); + if (t != null) { + future.setException(t); + } else if (cf.isCanceled()) { + future.cancel(); + } else { + IoSession ioSession = cf.getSession(); + org.apache.sshd.common.io.IoSession sshSession = getSession(ioSession); + future.setSession(sshSession); + } + }); + return future; + } +} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaService.java ---------------------------------------------------------------------- diff --git a/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaService.java b/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaService.java new file mode 100644 index 0000000..15c1a7f --- /dev/null +++ b/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaService.java @@ -0,0 +1,197 @@ +/* + * 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.io.mina; + +import java.util.Comparator; +import java.util.Map; +import java.util.TreeMap; + +import org.apache.mina.core.RuntimeIoException; +import org.apache.mina.core.buffer.IoBuffer; +import org.apache.mina.core.service.IoHandler; +import org.apache.mina.core.service.IoProcessor; +import org.apache.mina.core.service.IoService; +import org.apache.mina.core.session.IdleStatus; +import org.apache.mina.core.session.IoSession; +import org.apache.mina.core.session.IoSessionConfig; +import org.apache.mina.transport.socket.SocketSessionConfig; +import org.apache.mina.transport.socket.nio.NioSession; +import org.apache.sshd.common.Closeable; +import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.util.GenericUtils; +import org.apache.sshd.common.util.closeable.AbstractCloseable; + +/** + * @author <a href="mailto:d...@mina.apache.org">Apache MINA SSHD Project</a> + */ +public abstract class MinaService extends AbstractCloseable implements org.apache.sshd.common.io.IoService, IoHandler, Closeable { + protected final FactoryManager manager; + protected final org.apache.sshd.common.io.IoHandler handler; + protected final IoProcessor<NioSession> ioProcessor; + protected IoSessionConfig sessionConfig; + + public MinaService(FactoryManager manager, org.apache.sshd.common.io.IoHandler handler, IoProcessor<NioSession> ioProcessor) { + this.manager = manager; + this.handler = handler; + this.ioProcessor = ioProcessor; + } + + protected abstract IoService getIoService(); + + public void dispose() { + IoService ioService = getIoService(); + ioService.dispose(); + } + + @Override + protected void doCloseImmediately() { + IoService ioService = getIoService(); + ioService.dispose(); + super.doCloseImmediately(); + } + + @Override + public Map<Long, org.apache.sshd.common.io.IoSession> getManagedSessions() { + IoService ioService = getIoService(); + Map<Long, IoSession> managedMap = ioService.getManagedSessions(); + Map<Long, IoSession> mina = new TreeMap<>(managedMap); + Map<Long, org.apache.sshd.common.io.IoSession> sessions = new TreeMap<>(Comparator.naturalOrder()); + for (Long id : mina.keySet()) { + // Avoid possible NPE if the MinaSession hasn't been created yet + IoSession minaSession = mina.get(id); + org.apache.sshd.common.io.IoSession session = getSession(minaSession); + if (session != null) { + sessions.put(id, session); + } + } + return sessions; + } + + @Override + public void sessionOpened(IoSession session) throws Exception { + // Empty handler + } + + @Override + public void sessionIdle(IoSession session, IdleStatus status) throws Exception { + // Empty handler + } + + @Override + public void messageSent(IoSession session, Object message) throws Exception { + // Empty handler + } + + @Override + public void inputClosed(IoSession session) throws Exception { + session.closeNow(); + } + + @Override + public void sessionCreated(IoSession session) throws Exception { + org.apache.sshd.common.io.IoSession ioSession = new MinaSession(this, session); + session.setAttribute(org.apache.sshd.common.io.IoSession.class, ioSession); + handler.sessionCreated(ioSession); + } + + @Override + public void sessionClosed(IoSession session) throws Exception { + handler.sessionClosed(getSession(session)); + } + + @Override + public void exceptionCaught(IoSession session, Throwable cause) throws Exception { + handler.exceptionCaught(getSession(session), cause); + } + + @Override + public void messageReceived(IoSession session, Object message) throws Exception { + handler.messageReceived(getSession(session), MinaSupport.asReadable((IoBuffer) message)); + } + + protected org.apache.sshd.common.io.IoSession getSession(IoSession session) { + return (org.apache.sshd.common.io.IoSession) + session.getAttribute(org.apache.sshd.common.io.IoSession.class); + } + + protected void configure(SocketSessionConfig config) { + Boolean boolVal = getBoolean(FactoryManager.SOCKET_KEEPALIVE); + if (boolVal != null) { + try { + config.setKeepAlive(boolVal); + } catch (RuntimeIoException t) { + handleConfigurationError(config, FactoryManager.SOCKET_KEEPALIVE, boolVal, t); + } + } + + Integer intVal = getInteger(FactoryManager.SOCKET_SNDBUF); + if (intVal != null) { + try { + config.setSendBufferSize(intVal); + } catch (RuntimeIoException t) { + handleConfigurationError(config, FactoryManager.SOCKET_SNDBUF, intVal, t); + } + } + + intVal = getInteger(FactoryManager.SOCKET_RCVBUF); + if (intVal != null) { + try { + config.setReceiveBufferSize(intVal); + } catch (RuntimeIoException t) { + handleConfigurationError(config, FactoryManager.SOCKET_RCVBUF, intVal, t); + } + } + + intVal = getInteger(FactoryManager.SOCKET_LINGER); + if (intVal != null) { + try { + config.setSoLinger(intVal); + } catch (RuntimeIoException t) { + handleConfigurationError(config, FactoryManager.SOCKET_LINGER, intVal, t); + } + } + + boolVal = getBoolean(FactoryManager.TCP_NODELAY); + if (boolVal != null) { + try { + config.setTcpNoDelay(boolVal); + } catch (RuntimeIoException t) { + handleConfigurationError(config, FactoryManager.TCP_NODELAY, boolVal, t); + } + } + + if (sessionConfig != null) { + config.setAll(sessionConfig); + } + } + + protected void handleConfigurationError(SocketSessionConfig config, String propName, Object propValue, RuntimeIoException t) { + Throwable e = GenericUtils.resolveExceptionCause(t); + log.warn("handleConfigurationError({}={}) failed ({}) to configure: {}", + propName, propValue, e.getClass().getSimpleName(), e.getMessage()); + } + + protected Integer getInteger(String property) { + return manager.getInteger(property); + } + + protected Boolean getBoolean(String property) { + return manager.getBoolean(property); + } +} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java ---------------------------------------------------------------------- diff --git a/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java b/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java new file mode 100644 index 0000000..dd16ba4 --- /dev/null +++ b/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactory.java @@ -0,0 +1,56 @@ +/* + * 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.io.mina; + +import java.util.concurrent.ExecutorService; + +import org.apache.mina.core.service.IoProcessor; +import org.apache.mina.core.service.SimpleIoProcessorPool; +import org.apache.mina.transport.socket.nio.NioProcessor; +import org.apache.mina.transport.socket.nio.NioSession; +import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.io.AbstractIoServiceFactory; +import org.apache.sshd.common.io.IoAcceptor; +import org.apache.sshd.common.io.IoConnector; +import org.apache.sshd.common.io.IoHandler; +import org.apache.sshd.common.util.threads.ThreadUtils; + +/** + */ +public class MinaServiceFactory extends AbstractIoServiceFactory { + + private final IoProcessor<NioSession> ioProcessor; + + public MinaServiceFactory(FactoryManager factoryManager, ExecutorService service, boolean shutdownOnExit) { + super(factoryManager, + service == null ? ThreadUtils.newCachedThreadPool(factoryManager.toString() + "-mina") : service, + service == null || shutdownOnExit); + ioProcessor = new SimpleIoProcessorPool<>(NioProcessor.class, getExecutorService(), getNioWorkers(factoryManager), null); + } + + @Override + public IoConnector createConnector(IoHandler handler) { + return new MinaConnector(getFactoryManager(), handler, ioProcessor); + } + + @Override + public IoAcceptor createAcceptor(IoHandler handler) { + return new MinaAcceptor(getFactoryManager(), handler, ioProcessor); + } +} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/d07089ce/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactoryFactory.java ---------------------------------------------------------------------- diff --git a/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactoryFactory.java b/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactoryFactory.java new file mode 100644 index 0000000..7057d5a --- /dev/null +++ b/sshd-mina/src/main/java/org/apache/sshd/common/io/mina/MinaServiceFactoryFactory.java @@ -0,0 +1,51 @@ +/* + * 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.io.mina; + +import java.util.concurrent.ExecutorService; + +import org.apache.sshd.common.FactoryManager; +import org.apache.sshd.common.io.AbstractIoServiceFactoryFactory; +import org.apache.sshd.common.io.IoServiceFactory; + +/** + */ +public class MinaServiceFactoryFactory extends AbstractIoServiceFactoryFactory { + public MinaServiceFactoryFactory() { + this(null, true); + } + + /** + * @param executors The {@link ExecutorService} to use for spawning threads. + * If {@code null} then an internal service is allocated - in which case it + * is automatically shutdown regardless of the value of the <tt>shutdownOnExit</tt> + * parameter value + * @param shutdownOnExit If {@code true} then the {@link ExecutorService#shutdownNow()} + * will be called (unless it is an internally allocated service which is always + * closed) + */ + public MinaServiceFactoryFactory(ExecutorService executors, boolean shutdownOnExit) { + super(executors, shutdownOnExit); + } + + @Override + public IoServiceFactory create(FactoryManager manager) { + return new MinaServiceFactory(manager, getExecutorService(), isShutdownOnExit()); + } +}