View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.ftp.core.control;
22  
23  import goldengate.common.command.ReplyCode;
24  import goldengate.ftp.core.config.FtpConfiguration;
25  import goldengate.ftp.core.session.FtpSession;
26  
27  import org.jboss.netty.buffer.ChannelBuffer;
28  import org.jboss.netty.buffer.ChannelBuffers;
29  import org.jboss.netty.channel.ChannelPipeline;
30  import org.jboss.netty.channel.ChannelPipelineFactory;
31  import org.jboss.netty.channel.Channels;
32  import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
33  import org.jboss.netty.handler.execution.ExecutionHandler;
34  
35  /**
36   * Pipeline factory for Control command connection
37   *
38   * @author Frederic Bregier
39   *
40   */
41  public class FtpPipelineFactory implements ChannelPipelineFactory {
42      /**
43       * CRLF, CRNUL, LF delimiters
44       */
45      private static final ChannelBuffer[] delimiter = new ChannelBuffer[] {
46              ChannelBuffers.wrappedBuffer(ReplyCode.CRLF.getBytes()),
47              ChannelBuffers.wrappedBuffer(ReplyCode.CRNUL.getBytes()),
48              ChannelBuffers.wrappedBuffer(ReplyCode.LF.getBytes()) };
49  
50      private static final FtpControlStringDecoder ftpControlStringDecoder = new FtpControlStringDecoder();
51  
52      private static final FtpControlStringEncoder ftpControlStringEncoder = new FtpControlStringEncoder();
53  
54      /**
55       * Business Handler Class if any (Target Mode only)
56       */
57      private final Class<? extends BusinessHandler> businessHandler;
58  
59      /**
60       * Configuration
61       */
62      private final FtpConfiguration configuration;
63  
64      /**
65       * Constructor which Initializes some data for Server only
66       *
67       * @param businessHandler
68       * @param configuration
69       */
70      public FtpPipelineFactory(Class<? extends BusinessHandler> businessHandler,
71              FtpConfiguration configuration) {
72          this.businessHandler = businessHandler;
73          this.configuration = configuration;
74      }
75  
76      /**
77       * Create the pipeline with Handler, ObjectDecoder, ObjectEncoder.
78       *
79       * @see org.jboss.netty.channel.ChannelPipelineFactory#getPipeline()
80       */
81      public ChannelPipeline getPipeline() throws Exception {
82          ChannelPipeline pipeline = Channels.pipeline();
83          // Add the text line codec combination first,
84          pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192,
85                  delimiter));
86          pipeline.addLast("decoder", ftpControlStringDecoder);
87          pipeline.addLast("encoder", ftpControlStringEncoder);
88          // Threaded execution for business logic
89          pipeline.addLast("pipelineExecutor", new ExecutionHandler(configuration
90                  .getFtpInternalConfiguration().getPipelineExecutor()));
91          // and then business logic. New one on every connection
92          BusinessHandler newbusiness = businessHandler.newInstance();
93          NetworkHandler newNetworkHandler = new NetworkHandler(new FtpSession(
94                  configuration, newbusiness));
95          pipeline.addLast("handler", newNetworkHandler);
96          return pipeline;
97      }
98  }