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      public DemuxingProtocolCodecFactory() {
44          // Do nothing
45      }
46  
47      /**
48       * {@inheritDoc}
49       */
50      public ProtocolEncoder getEncoder(IoSession session) throws Exception {
51          return encoder;
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      public ProtocolDecoder getDecoder(IoSession session) throws Exception {
58          return decoder;
59      }
60  
61      @SuppressWarnings("unchecked")
62      public void addMessageEncoder(Class<?> messageType, Class<? extends MessageEncoder> encoderClass) {
63          this.encoder.addMessageEncoder(messageType, encoderClass);
64      }
65  
66      public <T> void addMessageEncoder(Class<T> messageType, MessageEncoder<? super T> encoder) {
67          this.encoder.addMessageEncoder(messageType, encoder);
68      }
69  
70      public <T> void addMessageEncoder(Class<T> messageType, MessageEncoderFactory<? super T> factory) {
71          this.encoder.addMessageEncoder(messageType, factory);
72      }
73  
74      @SuppressWarnings("unchecked")
75      public void addMessageEncoder(Iterable<Class<?>> messageTypes, Class<? extends MessageEncoder> encoderClass) {
76          for (Class<?> messageType : messageTypes) {
77              addMessageEncoder(messageType, encoderClass);
78          }
79      }
80  
81      public <T> void addMessageEncoder(Iterable<Class<? extends T>> messageTypes, MessageEncoder<? super T> encoder) {
82          for (Class<? extends T> messageType : messageTypes) {
83              addMessageEncoder(messageType, encoder);
84          }
85      }
86  
87      public <T> void addMessageEncoder(Iterable<Class<? extends T>> messageTypes,
88              MessageEncoderFactory<? super T> factory) {
89          for (Class<? extends T> messageType : messageTypes) {
90              addMessageEncoder(messageType, factory);
91          }
92      }
93  
94      public void addMessageDecoder(Class<? extends MessageDecoder> decoderClass) {
95          this.decoder.addMessageDecoder(decoderClass);
96      }
97  
98      public void addMessageDecoder(MessageDecoder decoder) {
99          this.decoder.addMessageDecoder(decoder);
100     }
101 
102     public void addMessageDecoder(MessageDecoderFactory factory) {
103         this.decoder.addMessageDecoder(factory);
104     }
105 }