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.codec.delimited.serialization;
    21
    22
    23  import org.apache.mina.codec.IoBuffer;
    24  import org.apache.mina.codec.ProtocolDecoderException;
    25  import org.apache.mina.codec.delimited.IoBufferDecoder;
    26  import org.apache.thrift.TBase;
    27  import org.apache.thrift.TDeserializer;
    28  import org.apache.thrift.TException;
    29  import org.apache.thrift.protocol.TBinaryProtocol;
    30
    31  /**
    32   * Decode {@link IoBuffer} into Thrift messages.
    33   *
    34   * @param <OUTPUT> the base type for decoded messages.
    35   *
    36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    37   */
    38  public class ThriftMessageDecoder<OUTPUT extends TBase<?, ?>> extends IoBufferDecoder<OUTPUT> {
    39      private final TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
    40
    41      private final Class<OUTPUT> clazz;
    42
    43      /**
    44       * Create thrift message decoder
    45       *
    46       * @param clazz the base class for decoded messages
    47       */
    48      public ThriftMessageDecoder(Class<OUTPUT> clazz) {
    49          super();
    50          this.clazz = clazz;
    51      }
    52
    53      public static <L extends TBase<?, ?>> ThriftMessageDecoder<L> newInstance(Class<L> clazz) {
    54          return new ThriftMessageDecoder<L>(clazz);
    55      }
    56
    57      /**
    58       * {@inheritDoc}
    59       */
    60      @Override
    61      public OUTPUT decode(IoBuffer input) {
    62          OUTPUT object;
    63          try {
    64              byte array[] = new byte[input.remaining()];
    65              input.get(array);
    66              object = clazz.newInstance();
    67              deserializer.deserialize(object, array);
    68              return object;
    69          } catch (TException e) {
    70              throw new ProtocolDecoderException(e);
    71          } catch (InstantiationException e) {
    72              throw new ProtocolDecoderException(e);
    73          } catch (IllegalAccessException e) {
    74              throw new ProtocolDecoderException(e);
    75          }
    76      }
    77
    78  }