001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.mina;
018    
019    import java.nio.charset.Charset;
020    
021    import org.apache.mina.filter.codec.ProtocolCodecFactory;
022    import org.apache.mina.filter.codec.ProtocolDecoder;
023    import org.apache.mina.filter.codec.ProtocolEncoder;
024    import org.apache.mina.filter.codec.textline.LineDelimiter;
025    import org.apache.mina.filter.codec.textline.TextLineDecoder;
026    import org.apache.mina.filter.codec.textline.TextLineEncoder;
027    
028    /**
029     * Text line codec that supports setting charset and delimiter.
030     * <p/>
031     * Uses Mina's default TextLineEncoder and TextLineDncoder. 
032     */
033    public class TextLineCodecFactory implements ProtocolCodecFactory {
034    
035        private TextLineEncoder encoder;
036        private TextLineDecoder decoder;
037    
038        public TextLineCodecFactory(Charset charset, LineDelimiter delimiter) {
039            if (delimiter.equals(LineDelimiter.AUTO)) {
040                // AUTO not supported by encoder
041                encoder = new TextLineEncoder(charset);
042            } else {
043                encoder = new TextLineEncoder(charset, delimiter);
044            }
045            decoder = new TextLineDecoder(charset, delimiter);
046        }
047    
048        public ProtocolEncoder getEncoder() throws Exception {
049            return encoder;
050        }
051    
052        public ProtocolDecoder getDecoder() throws Exception {
053            return decoder;
054        }
055    
056        public void setEncoderMaxLineLength(int encoderMaxLineLength) {
057            encoder.setMaxLineLength(encoderMaxLineLength);
058        }
059    
060        public int getEncoderMaxLineLength() {
061            return encoder.getMaxLineLength();
062        }
063    
064        public void setDecoderMaxLineLength(int decoderMaxLineLength) {
065            decoder.setMaxLineLength(decoderMaxLineLength);
066        }
067    
068        public int getDecoderMaxLineLength() {
069            return decoder.getMaxLineLength();
070        }
071    
072    }