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.data.handler;
22  
23  import goldengate.ftp.core.command.FtpArgumentCode.TransferMode;
24  import goldengate.ftp.core.command.FtpArgumentCode.TransferStructure;
25  import goldengate.ftp.core.command.FtpArgumentCode.TransferSubType;
26  import goldengate.ftp.core.command.FtpArgumentCode.TransferType;
27  import goldengate.ftp.core.config.FtpConfiguration;
28  
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.execution.ExecutionHandler;
33  import org.jboss.netty.handler.traffic.ChannelTrafficShapingHandler;
34  
35  /**
36   * Pipeline Factory for Data Network.
37   *
38   * @author Frederic Bregier
39   *
40   */
41  public class FtpDataPipelineFactory implements ChannelPipelineFactory {
42      /**
43       * Mode Codec
44       */
45      public static final String CODEC_MODE = "MODE";
46  
47      /**
48       * Limit Codec
49       */
50      public static final String CODEC_LIMIT = "LIMITATION";
51  
52      /**
53       * Type Codec
54       */
55      public static final String CODEC_TYPE = "TYPE";
56  
57      /**
58       * Structure Codec
59       */
60      public static final String CODEC_STRUCTURE = "STRUCTURE";
61  
62      /**
63       * Pipeline Executor Codec
64       */
65      public static final String PIPELINE_EXECUTOR = "pipelineExecutor";
66  
67      /**
68       * Handler Codec
69       */
70      public static final String HANDLER = "handler";
71  
72      private static final FtpDataTypeCodec ftpDataTypeCodec = new FtpDataTypeCodec(
73              TransferType.ASCII, TransferSubType.NONPRINT);
74  
75      private static final FtpDataStructureCodec ftpDataStructureCodec = new FtpDataStructureCodec(
76              TransferStructure.FILE);
77  
78      /**
79       * Business Handler Class
80       */
81      private final Class<? extends DataBusinessHandler> dataBusinessHandler;
82  
83      /**
84       * Configuration
85       */
86      private final FtpConfiguration configuration;
87  
88      /**
89       * Is this factory for Active mode
90       */
91      private final boolean isActive;
92  
93      /**
94       * Constructor which Initializes some data
95       *
96       * @param dataBusinessHandler
97       * @param configuration
98       * @param active
99       */
100     public FtpDataPipelineFactory(
101             Class<? extends DataBusinessHandler> dataBusinessHandler,
102             FtpConfiguration configuration, boolean active) {
103         this.dataBusinessHandler = dataBusinessHandler;
104         this.configuration = configuration;
105         isActive = active;
106     }
107 
108     /**
109      * Create the pipeline with Handler, ObjectDecoder, ObjectEncoder.
110      *
111      * @see org.jboss.netty.channel.ChannelPipelineFactory#getPipeline()
112      */
113     public ChannelPipeline getPipeline() throws Exception {
114         ChannelPipeline pipeline = Channels.pipeline();
115         // Add default codec but they will change by the channelConnected
116         pipeline.addFirst(CODEC_MODE, new FtpDataModeCodec(TransferMode.STREAM,
117                 TransferStructure.FILE));
118         pipeline
119                 .addLast(CODEC_LIMIT, configuration
120                         .getFtpInternalConfiguration()
121                         .getGlobalTrafficShapingHandler());
122         ChannelTrafficShapingHandler limitChannel =
123             configuration
124             .getFtpInternalConfiguration()
125             .newChannelTrafficShapingHandler();
126         if (limitChannel != null) {
127             pipeline.addLast(CODEC_LIMIT + "CHANNEL", limitChannel);
128         }
129         pipeline.addLast(CODEC_TYPE, ftpDataTypeCodec);
130         pipeline.addLast(CODEC_STRUCTURE, ftpDataStructureCodec);
131         // Threaded execution for business logic
132         pipeline.addLast(PIPELINE_EXECUTOR, new ExecutionHandler(configuration
133                 .getFtpInternalConfiguration().getDataPipelineExecutor()));
134         // and then business logic. New one on every connection
135         DataBusinessHandler newbusiness = dataBusinessHandler.newInstance();
136         DataNetworkHandler newNetworkHandler = new DataNetworkHandler(
137                 configuration, newbusiness, isActive);
138         pipeline.addLast(HANDLER, newNetworkHandler);
139         return pipeline;
140     }
141 }