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      public void addMessageEncoder(Class<?> messageType, Class<? extends MessageEncoder> encoderClass) {
62          this.encoder.addMessageEncoder(messageType, encoderClass);
63      }
64  
65      public <T> void addMessageEncoder(Class<T> messageType, MessageEncoder<? super T> encoder) {
66          this.encoder.addMessageEncoder(messageType, encoder);
67      }
68  
69      public <T> void addMessageEncoder(Class<T> messageType, MessageEncoderFactory<? super T> factory) {
70          this.encoder.addMessageEncoder(messageType, factory);
71      }
72  
73      public void addMessageEncoder(Iterable<Class<?>> messageTypes, Class<? extends MessageEncoder> encoderClass) {
74          for (Class<?> messageType : messageTypes) {
75              addMessageEncoder(messageType, encoderClass);
76          }
77      }
78  
79      public <T> void addMessageEncoder(Iterable<Class<? extends T>> messageTypes, MessageEncoder<? super T> encoder) {
80          for (Class<? extends T> messageType : messageTypes) {
81              addMessageEncoder(messageType, encoder);
82          }
83      }
84  
85      public <T> void addMessageEncoder(Iterable<Class<? extends T>> messageTypes,
86              MessageEncoderFactory<? super T> factory) {
87          for (Class<? extends T> messageType : messageTypes) {
88              addMessageEncoder(messageType, factory);
89          }
90      }
91  
92      public void addMessageDecoder(Class<? extends MessageDecoder> decoderClass) {
93          this.decoder.addMessageDecoder(decoderClass);
94      }
95  
96      public void addMessageDecoder(MessageDecoder decoder) {
97          this.decoder.addMessageDecoder(decoder);
98      }
99  
100     public void addMessageDecoder(MessageDecoderFactory factory) {
101         this.decoder.addMessageDecoder(factory);
102     }
103 }