View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.filter.codec.demux;
21  
22  import org.apache.mina.core.session.IoSession;
23  import org.apache.mina.filter.codec.ProtocolCodecFactory;
24  import org.apache.mina.filter.codec.ProtocolDecoder;
25  import org.apache.mina.filter.codec.ProtocolEncoder;
26  
27  /**
28   * A convenience {@link ProtocolCodecFactory} that provides {@link DemuxingProtocolEncoder}
29   * and {@link DemuxingProtocolDecoder} as a pair.
30   * <p>
31   * {@link DemuxingProtocolEncoder} and {@link DemuxingProtocolDecoder} demultiplex
32   * incoming messages and buffers to appropriate {@link MessageEncoder}s and 
33   * {@link MessageDecoder}s.
34   *
35   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
36   */
37  public class DemuxingProtocolCodecFactory implements ProtocolCodecFactory {
38  
39      private final DemuxingProtocolEncoder encoder = new DemuxingProtocolEncoder();
40  
41      private final DemuxingProtocolDecoder decoder = new DemuxingProtocolDecoder();
42  
43      /**
44       * {@inheritDoc}
45       */
46      @Override
47      public ProtocolEncoder getEncoder(IoSession session) throws Exception {
48          return encoder;
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public ProtocolDecoder getDecoder(IoSession session) throws Exception {
56          return decoder;
57      }
58  
59      /**
60       * Adds a new message encoder for a given message type
61       * 
62       * @param messageType The message type
63       * @param encoderClass The associated encoder class
64       */
65      public void addMessageEncoder(Class<?> messageType, Class<? extends MessageEncoder> encoderClass) {
66          this.encoder.addMessageEncoder(messageType, encoderClass);
67      }
68  
69      /**
70       * Adds a new message encoder for a given message type
71       * 
72       * @param <T> The message type
73       * @param messageType The message type
74       * @param encoder The associated encoder instance
75       */
76      public <T> void addMessageEncoder(Class<T> messageType, MessageEncoder<? super T> encoder) {
77          this.encoder.addMessageEncoder(messageType, encoder);
78      }
79  
80      /**
81       * Adds a new message encoder for a given message type
82       * 
83       * @param <T> The message type
84       * @param messageType The message type
85       * @param factory The associated encoder factory
86       */
87      public <T> void addMessageEncoder(Class<T> messageType, MessageEncoderFactory<? super T> factory) {
88          this.encoder.addMessageEncoder(messageType, factory);
89      }
90  
91      /**
92       * Adds a new message encoder for a list of message types
93       * 
94       * @param messageTypes The message types
95       * @param encoderClass The associated encoder class
96       */
97      public void addMessageEncoder(Iterable<Class<?>> messageTypes, Class<? extends MessageEncoder> encoderClass) {
98          for (Class<?> messageType : messageTypes) {
99              addMessageEncoder(messageType, encoderClass);
100         }
101     }
102 
103     /**
104      * Adds a new message encoder for a list of message types
105      * 
106      * @param <T> The message type
107      * @param messageTypes The messages types
108      * @param encoder The associated encoder instance
109      */
110     public <T> void addMessageEncoder(Iterable<Class<? extends T>> messageTypes, MessageEncoder<? super T> encoder) {
111         for (Class<? extends T> messageType : messageTypes) {
112             addMessageEncoder(messageType, encoder);
113         }
114     }
115 
116     /**
117      * Adds a new message encoder for a list of message types
118      * 
119      * @param <T> The message type
120      * @param messageTypes The messages types
121      * @param factory The associated encoder factory
122      */
123     public <T> void addMessageEncoder(Iterable<Class<? extends T>> messageTypes,
124             MessageEncoderFactory<? super T> factory) {
125         for (Class<? extends T> messageType : messageTypes) {
126             addMessageEncoder(messageType, factory);
127         }
128     }
129 
130     /**
131      * Adds a new message decoder
132      * 
133      * @param decoderClass The associated decoder class
134      */
135     public void addMessageDecoder(Class<? extends MessageDecoder> decoderClass) {
136         this.decoder.addMessageDecoder(decoderClass);
137     }
138 
139     /**
140      * Adds a new message decoder
141      * 
142      * @param decoder The associated decoder instance
143      */
144     public void addMessageDecoder(MessageDecoder decoder) {
145         this.decoder.addMessageDecoder(decoder);
146     }
147 
148     /**
149      * Adds a new message decoder
150      * 
151      * @param factory The associated decoder factory
152      */
153     public void addMessageDecoder(MessageDecoderFactory factory) {
154         this.decoder.addMessageDecoder(factory);
155     }
156 }