一、前言
关于通过apache的ftpserver-core包(1.0.6)中org.apache.ftpserver.listener.Listener监听器的接口实现,主要代码类Listener、AbstractListener、ListenerFactory、NioListener等,具体代码如下
二、源码说明
1.Listener接口
package org.apache.ftpserver.listener;@b@@b@import java.net.InetAddress;@b@import java.util.List;@b@import java.util.Set;@b@import org.apache.ftpserver.DataConnectionConfiguration;@b@import org.apache.ftpserver.impl.FtpIoSession;@b@import org.apache.ftpserver.impl.FtpServerContext;@b@import org.apache.ftpserver.ipfilter.IpFilter;@b@import org.apache.ftpserver.ssl.SslConfiguration;@b@import org.apache.mina.filter.firewall.Subnet;@b@@b@public abstract interface Listener@b@{@b@ public abstract void start(FtpServerContext paramFtpServerContext);@b@@b@ public abstract void stop();@b@@b@ public abstract boolean isStopped();@b@@b@ public abstract void suspend();@b@@b@ public abstract void resume();@b@@b@ public abstract boolean isSuspended();@b@@b@ public abstract Set<FtpIoSession> getActiveSessions();@b@@b@ public abstract boolean isImplicitSsl();@b@@b@ public abstract SslConfiguration getSslConfiguration();@b@@b@ public abstract int getPort();@b@@b@ public abstract String getServerAddress();@b@@b@ public abstract DataConnectionConfiguration getDataConnectionConfiguration();@b@@b@ public abstract int getIdleTimeout();@b@@b@ @Deprecated@b@ public abstract List<InetAddress> getBlockedAddresses();@b@@b@ /**@b@ * @deprecated@b@ */@b@ public abstract List<Subnet> getBlockedSubnets();@b@@b@ public abstract IpFilter getIpFilter();@b@}
2.AbstractListener抽象类
package org.apache.ftpserver.listener.nio;@b@@b@import java.net.InetAddress;@b@import java.util.List;@b@import org.apache.ftpserver.DataConnectionConfiguration;@b@import org.apache.ftpserver.ipfilter.DefaultIpFilter;@b@import org.apache.ftpserver.ipfilter.IpFilter;@b@import org.apache.ftpserver.ipfilter.IpFilterType;@b@import org.apache.ftpserver.listener.Listener;@b@import org.apache.ftpserver.ssl.SslConfiguration;@b@import org.apache.mina.filter.firewall.Subnet;@b@@b@public abstract class AbstractListener@b@ implements Listener@b@{@b@ private String serverAddress;@b@ private int port;@b@ private SslConfiguration ssl;@b@ private boolean implicitSsl;@b@ private int idleTimeout;@b@ private List<InetAddress> blockedAddresses;@b@ private List<Subnet> blockedSubnets;@b@ private IpFilter ipFilter;@b@ private DataConnectionConfiguration dataConnectionConfig;@b@@b@ @Deprecated@b@ public AbstractListener(String serverAddress, int port, boolean implicitSsl, SslConfiguration sslConfiguration, DataConnectionConfiguration dataConnectionConfig, int idleTimeout, List<InetAddress> blockedAddresses, List<Subnet> blockedSubnets)@b@ {@b@ this(serverAddress, port, implicitSsl, sslConfiguration, dataConnectionConfig, idleTimeout, createBlackListFilter(blockedAddresses, blockedSubnets));@b@@b@ this.blockedAddresses = blockedAddresses;@b@ this.blockedSubnets = blockedSubnets;@b@ }@b@@b@ public AbstractListener(String serverAddress, int port, boolean implicitSsl, SslConfiguration sslConfiguration, DataConnectionConfiguration dataConnectionConfig, int idleTimeout, IpFilter ipFilter)@b@ {@b@ this.port = 21;@b@@b@ this.implicitSsl = false;@b@@b@ this.ipFilter = null;@b@@b@ this.serverAddress = serverAddress;@b@ this.port = port;@b@ this.implicitSsl = implicitSsl;@b@ this.dataConnectionConfig = dataConnectionConfig;@b@ this.ssl = sslConfiguration;@b@ this.idleTimeout = idleTimeout;@b@ this.ipFilter = ipFilter;@b@ }@b@@b@ private static IpFilter createBlackListFilter(List<InetAddress> blockedAddresses, List<Subnet> blockedSubnets)@b@ {@b@ if ((blockedAddresses == null) && (blockedSubnets == null)) {@b@ return null;@b@ }@b@@b@ DefaultIpFilter ipFilter = new DefaultIpFilter(IpFilterType.DENY);@b@ if (blockedSubnets != null)@b@ ipFilter.addAll(blockedSubnets);@b@@b@ if (blockedAddresses != null)@b@ for (InetAddress address : blockedAddresses)@b@ ipFilter.add(new Subnet(address, 32));@b@@b@@b@ return ipFilter;@b@ }@b@@b@ public boolean isImplicitSsl()@b@ {@b@ return this.implicitSsl;@b@ }@b@@b@ public int getPort()@b@ {@b@ return this.port;@b@ }@b@@b@ protected void setPort(int port)@b@ {@b@ this.port = port;@b@ }@b@@b@ public String getServerAddress()@b@ {@b@ return this.serverAddress;@b@ }@b@@b@ public SslConfiguration getSslConfiguration()@b@ {@b@ return this.ssl;@b@ }@b@@b@ public DataConnectionConfiguration getDataConnectionConfiguration()@b@ {@b@ return this.dataConnectionConfig;@b@ }@b@@b@ public int getIdleTimeout()@b@ {@b@ return this.idleTimeout;@b@ }@b@@b@ public List<InetAddress> getBlockedAddresses()@b@ {@b@ return this.blockedAddresses;@b@ }@b@@b@ public List<Subnet> getBlockedSubnets()@b@ {@b@ return this.blockedSubnets;@b@ }@b@@b@ public IpFilter getIpFilter() {@b@ return this.ipFilter;@b@ }@b@}
3.NioListener抽象类实现
package org.apache.ftpserver.listener.nio;@b@@b@import java.io.IOException;@b@import java.net.InetAddress;@b@import java.net.InetSocketAddress;@b@import java.security.GeneralSecurityException;@b@import java.util.HashSet;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.Set;@b@import org.apache.ftpserver.DataConnectionConfiguration;@b@import org.apache.ftpserver.FtpServerConfigurationException;@b@import org.apache.ftpserver.impl.DefaultFtpHandler;@b@import org.apache.ftpserver.impl.FtpHandler;@b@import org.apache.ftpserver.impl.FtpIoSession;@b@import org.apache.ftpserver.impl.FtpServerContext;@b@import org.apache.ftpserver.ipfilter.IpFilter;@b@import org.apache.ftpserver.ipfilter.MinaIpFilter;@b@import org.apache.ftpserver.ssl.ClientAuth;@b@import org.apache.ftpserver.ssl.SslConfiguration;@b@import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;@b@import org.apache.mina.core.session.IdleStatus;@b@import org.apache.mina.core.session.IoSession;@b@import org.apache.mina.filter.codec.ProtocolCodecFilter;@b@import org.apache.mina.filter.executor.ExecutorFilter;@b@import org.apache.mina.filter.firewall.Subnet;@b@import org.apache.mina.filter.logging.MdcInjectionFilter;@b@import org.apache.mina.filter.ssl.SslFilter;@b@import org.apache.mina.transport.socket.SocketAcceptor;@b@import org.apache.mina.transport.socket.SocketSessionConfig;@b@import org.apache.mina.transport.socket.nio.NioSocketAcceptor;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class NioListener extends AbstractListener@b@{@b@ private final Logger LOG = LoggerFactory.getLogger(NioListener.class);@b@ private SocketAcceptor acceptor;@b@ private InetSocketAddress address;@b@ boolean suspended = false;@b@ private FtpHandler handler = new DefaultFtpHandler();@b@ private FtpServerContext context;@b@@b@ @Deprecated@b@ public NioListener(String serverAddress, int port, boolean implicitSsl, SslConfiguration sslConfiguration, DataConnectionConfiguration dataConnectionConfig, int idleTimeout, List<InetAddress> blockedAddresses, List<Subnet> blockedSubnets)@b@ {@b@ super(serverAddress, port, implicitSsl, sslConfiguration, dataConnectionConfig, idleTimeout, blockedAddresses, blockedSubnets);@b@ }@b@@b@ public NioListener(String serverAddress, int port, boolean implicitSsl, SslConfiguration sslConfiguration, DataConnectionConfiguration dataConnectionConfig, int idleTimeout, IpFilter ipFilter)@b@ {@b@ super(serverAddress, port, implicitSsl, sslConfiguration, dataConnectionConfig, idleTimeout, ipFilter);@b@ }@b@@b@ public synchronized void start(FtpServerContext context)@b@ {@b@ if (!(isStopped()))@b@ {@b@ throw new IllegalStateException("Listener already started");@b@ }@b@@b@ try@b@ {@b@ this.context = context;@b@@b@ this.acceptor = new NioSocketAcceptor(Runtime.getRuntime().availableProcessors());@b@@b@ if (getServerAddress() != null)@b@ this.address = new InetSocketAddress(getServerAddress(), getPort());@b@ else {@b@ this.address = new InetSocketAddress(getPort());@b@ }@b@@b@ this.acceptor.setReuseAddress(true);@b@ this.acceptor.getSessionConfig().setReadBufferSize(2048);@b@ this.acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, getIdleTimeout());@b@@b@ this.acceptor.getSessionConfig().setReceiveBufferSize(512);@b@@b@ MdcInjectionFilter mdcFilter = new MdcInjectionFilter();@b@@b@ this.acceptor.getFilterChain().addLast("mdcFilter", mdcFilter);@b@@b@ IpFilter ipFilter = getIpFilter();@b@ if (ipFilter != null)@b@ {@b@ this.acceptor.getFilterChain().addLast("ipFilter", new MinaIpFilter(ipFilter));@b@ }@b@@b@ this.acceptor.getFilterChain().addLast("threadPool", new ExecutorFilter(context.getThreadPoolExecutor()));@b@@b@ this.acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new FtpServerProtocolCodecFactory()));@b@@b@ this.acceptor.getFilterChain().addLast("mdcFilter2", mdcFilter);@b@ this.acceptor.getFilterChain().addLast("logger", new FtpLoggingFilter());@b@@b@ if (isImplicitSsl()) {@b@ SslFilter sslFilter;@b@ SslConfiguration ssl = getSslConfiguration();@b@ try@b@ {@b@ sslFilter = new SslFilter(ssl.getSSLContext());@b@ } catch (GeneralSecurityException e) {@b@ throw new FtpServerConfigurationException("SSL could not be initialized, check configuration");@b@ }@b@@b@ if (ssl.getClientAuth() == ClientAuth.NEED)@b@ sslFilter.setNeedClientAuth(true);@b@ else if (ssl.getClientAuth() == ClientAuth.WANT) {@b@ sslFilter.setWantClientAuth(true);@b@ }@b@@b@ if (ssl.getEnabledCipherSuites() != null) {@b@ sslFilter.setEnabledCipherSuites(ssl.getEnabledCipherSuites());@b@ }@b@@b@ this.acceptor.getFilterChain().addFirst("sslFilter", sslFilter);@b@ }@b@@b@ this.handler.init(context, this);@b@ this.acceptor.setHandler(new FtpHandlerAdapter(context, this.handler));@b@ try@b@ {@b@ this.acceptor.bind(this.address);@b@ } catch (IOException e) {@b@ throw new FtpServerConfigurationException("Failed to bind to address " + this.address + ", check configuration", e);@b@ }@b@@b@ updatePort();@b@ }@b@ catch (RuntimeException e)@b@ {@b@ stop();@b@@b@ throw e;@b@ }@b@ }@b@@b@ private void updatePort()@b@ {@b@ setPort(this.acceptor.getLocalAddress().getPort());@b@ }@b@@b@ public synchronized void stop()@b@ {@b@ if (this.acceptor != null) {@b@ this.acceptor.unbind();@b@ this.acceptor.dispose();@b@ this.acceptor = null;@b@ }@b@ this.context = null;@b@ }@b@@b@ public boolean isStopped()@b@ {@b@ return (this.acceptor == null);@b@ }@b@@b@ public boolean isSuspended()@b@ {@b@ return this.suspended;@b@ }@b@@b@ public synchronized void resume()@b@ {@b@ if ((this.acceptor != null) && (this.suspended))@b@ try {@b@ this.LOG.debug("Resuming listener");@b@ this.acceptor.bind(this.address);@b@ this.LOG.debug("Listener resumed");@b@@b@ updatePort();@b@@b@ this.suspended = false;@b@ } catch (IOException e) {@b@ this.LOG.error("Failed to resume listener", e);@b@ }@b@ }@b@@b@ public synchronized void suspend()@b@ {@b@ if ((this.acceptor != null) && (!(this.suspended))) {@b@ this.LOG.debug("Suspending listener");@b@ this.acceptor.unbind();@b@@b@ this.suspended = true;@b@ this.LOG.debug("Listener suspended");@b@ }@b@ }@b@@b@ public synchronized Set<FtpIoSession> getActiveSessions()@b@ {@b@ Map sessions = this.acceptor.getManagedSessions();@b@@b@ Set ftpSessions = new HashSet();@b@ for (IoSession session : sessions.values())@b@ ftpSessions.add(new FtpIoSession(session, this.context));@b@@b@ return ftpSessions;@b@ }@b@}
4.ListenerFactory工厂类
package org.apache.ftpserver.listener;@b@@b@import java.net.InetAddress;@b@import java.net.UnknownHostException;@b@import java.util.List;@b@import org.apache.ftpserver.DataConnectionConfiguration;@b@import org.apache.ftpserver.DataConnectionConfigurationFactory;@b@import org.apache.ftpserver.FtpServerConfigurationException;@b@import org.apache.ftpserver.ipfilter.IpFilter;@b@import org.apache.ftpserver.listener.nio.NioListener;@b@import org.apache.ftpserver.ssl.SslConfiguration;@b@import org.apache.mina.filter.firewall.Subnet;@b@@b@public class ListenerFactory@b@{@b@ private String serverAddress;@b@ private int port = 21;@b@ private SslConfiguration ssl;@b@ private boolean implicitSsl = false;@b@ private DataConnectionConfiguration dataConnectionConfig = new DataConnectionConfigurationFactory().createDataConnectionConfiguration();@b@ private int idleTimeout = 300;@b@ private List<InetAddress> blockedAddresses;@b@ private List<Subnet> blockedSubnets;@b@ private IpFilter ipFilter = null;@b@@b@ public ListenerFactory()@b@ {@b@ }@b@@b@ public ListenerFactory(Listener listener)@b@ {@b@ this.serverAddress = listener.getServerAddress();@b@ this.port = listener.getPort();@b@ this.ssl = listener.getSslConfiguration();@b@ this.implicitSsl = listener.isImplicitSsl();@b@ this.dataConnectionConfig = listener.getDataConnectionConfiguration();@b@ this.idleTimeout = listener.getIdleTimeout();@b@@b@ this.blockedAddresses = listener.getBlockedAddresses();@b@ this.blockedSubnets = listener.getBlockedSubnets();@b@ this.ipFilter = listener.getIpFilter();@b@ }@b@@b@ public Listener createListener()@b@ {@b@ try@b@ {@b@ InetAddress.getByName(this.serverAddress);@b@ } catch (UnknownHostException e) {@b@ throw new FtpServerConfigurationException("Unknown host", e);@b@ }@b@@b@ if ((this.ipFilter != null) && ((@b@ (this.blockedAddresses != null) || (this.blockedSubnets != null)))) {@b@ throw new IllegalStateException("Usage of IPFilter in combination with blockedAddesses/subnets is not supported. ");@b@ }@b@@b@ if ((this.blockedAddresses != null) || (this.blockedSubnets != null)) {@b@ return new NioListener(this.serverAddress, this.port, this.implicitSsl, this.ssl, this.dataConnectionConfig, this.idleTimeout, this.blockedAddresses, this.blockedSubnets);@b@ }@b@@b@ return new NioListener(this.serverAddress, this.port, this.implicitSsl, this.ssl, this.dataConnectionConfig, this.idleTimeout, this.ipFilter);@b@ }@b@@b@ public boolean isImplicitSsl()@b@ {@b@ return this.implicitSsl;@b@ }@b@@b@ public void setImplicitSsl(boolean implicitSsl)@b@ {@b@ this.implicitSsl = implicitSsl;@b@ }@b@@b@ public int getPort()@b@ {@b@ return this.port;@b@ }@b@@b@ public void setPort(int port)@b@ {@b@ this.port = port;@b@ }@b@@b@ public String getServerAddress()@b@ {@b@ return this.serverAddress;@b@ }@b@@b@ public void setServerAddress(String serverAddress)@b@ {@b@ this.serverAddress = serverAddress;@b@ }@b@@b@ public SslConfiguration getSslConfiguration()@b@ {@b@ return this.ssl;@b@ }@b@@b@ public void setSslConfiguration(SslConfiguration ssl)@b@ {@b@ this.ssl = ssl;@b@ }@b@@b@ public DataConnectionConfiguration getDataConnectionConfiguration()@b@ {@b@ return this.dataConnectionConfig;@b@ }@b@@b@ public void setDataConnectionConfiguration(DataConnectionConfiguration dataConnectionConfig)@b@ {@b@ this.dataConnectionConfig = dataConnectionConfig;@b@ }@b@@b@ public int getIdleTimeout()@b@ {@b@ return this.idleTimeout;@b@ }@b@@b@ public void setIdleTimeout(int idleTimeout)@b@ {@b@ this.idleTimeout = idleTimeout;@b@ }@b@@b@ @Deprecated@b@ public List<InetAddress> getBlockedAddresses()@b@ {@b@ return this.blockedAddresses;@b@ }@b@@b@ @Deprecated@b@ public void setBlockedAddresses(List<InetAddress> blockedAddresses)@b@ {@b@ this.blockedAddresses = blockedAddresses;@b@ }@b@@b@ @Deprecated@b@ public List<Subnet> getBlockedSubnets()@b@ {@b@ return this.blockedSubnets;@b@ }@b@@b@ @Deprecated@b@ public void setBlockedSubnets(List<Subnet> blockedSubnets)@b@ {@b@ this.blockedSubnets = blockedSubnets;@b@ }@b@@b@ public IpFilter getIpFilter()@b@ {@b@ return this.ipFilter;@b@ }@b@@b@ public void setIpFilter(IpFilter ipFilter)@b@ {@b@ this.ipFilter = ipFilter;@b@ }@b@}