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;
21  
22  import org.apache.mina.core.buffer.IoBuffer;
23  import org.apache.mina.core.session.IoSession;
24  
25  /**
26   * A {@link ProtocolDecoder} implementation which decorates an existing decoder
27   * to be thread-safe.  Please be careful if you're going to use this decorator
28   * because it can be a root of performance degradation in a multi-thread
29   * environment.  Also, by default, appropriate synchronization is done
30   * on a per-session basis by {@link ProtocolCodecFilter}.  Please use this
31   * decorator only when you need to synchronize on a per-decoder basis, which
32   * is not common.
33   *
34   * @author The Apache MINA Project (dev@mina.apache.org)
35   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
36   */
37  public class SynchronizedProtocolDecoder implements ProtocolDecoder {
38      private final ProtocolDecoder decoder;
39  
40      /**
41       * Creates a new instance which decorates the specified <tt>decoder</tt>.
42       */
43      public SynchronizedProtocolDecoder(ProtocolDecoder decoder) {
44          if (decoder == null) {
45              throw new NullPointerException("decoder");
46          }
47          this.decoder = decoder;
48      }
49  
50      /**
51       * Returns the decoder this decoder is decorating.
52       */
53      public ProtocolDecoder getDecoder() {
54          return decoder;
55      }
56  
57      public void decode(IoSession session, IoBuffer in,
58              ProtocolDecoderOutput out) throws Exception {
59          synchronized (decoder) {
60              decoder.decode(session, in, out);
61          }
62      }
63  
64      public void finishDecode(IoSession session, ProtocolDecoderOutput out)
65              throws Exception {
66          synchronized (decoder) {
67              decoder.finishDecode(session, out);
68          }
69      }
70  
71      public void dispose(IoSession session) throws Exception {
72          synchronized (decoder) {
73              decoder.dispose(session);
74          }
75      }
76  }