2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.nettyutil.handler.ssh.sshd1028;
10 import java.lang.reflect.Field;
11 import java.nio.channels.AsynchronousChannelGroup;
12 import java.security.AccessController;
13 import java.security.PrivilegedAction;
14 import org.opendaylight.netconf.shaded.sshd.common.FactoryManager;
15 import org.opendaylight.netconf.shaded.sshd.common.io.IoConnector;
16 import org.opendaylight.netconf.shaded.sshd.common.io.IoHandler;
17 import org.opendaylight.netconf.shaded.sshd.common.io.nio2.Nio2ServiceFactory;
18 import org.opendaylight.netconf.shaded.sshd.common.util.threads.CloseableExecutorService;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Custom Nio2ServiceFactory which creates instances of NetconfNio2Connector instead of Nio2Connector.
24 * Should be removed when SSHD-1028 is fixed.
26 public class NetconfNio2ServiceFactory extends Nio2ServiceFactory {
27 private static final Logger LOG = LoggerFactory.getLogger(NetconfNio2ServiceFactory.class);
28 private static final Field FIELD_GROUP;
31 final Field fieldGroup;
33 fieldGroup = NetconfNio2ServiceFactory.class.getSuperclass().getDeclaredField("group");
34 } catch (NoSuchFieldException e) {
35 LOG.error("Cannot access the ChannelGroup from the " + "Nio2ServiceFactory");
36 throw new ExceptionInInitializerError(e);
39 AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
40 fieldGroup.setAccessible(true);
44 FIELD_GROUP = fieldGroup;
47 public NetconfNio2ServiceFactory(final FactoryManager factoryManager, final CloseableExecutorService service) {
48 super(factoryManager, service);
52 public IoConnector createConnector(final IoHandler handler) {
53 if (FIELD_GROUP != null) {
55 final AsynchronousChannelGroup group = (AsynchronousChannelGroup)FIELD_GROUP.get(this);
56 return autowireCreatedService(new NetconfNio2Connector(getFactoryManager(), handler, group));
57 } catch (IllegalAccessException e) {
58 LOG.error("NetconfNio2Connector cannot be instanciated. Creating default Nio2Connector instead.");
62 return super.createConnector(handler);