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.serialization;
21  
22  import org.apache.mina.core.buffer.BufferDataException;
23  import org.apache.mina.core.session.IoSession;
24  import org.apache.mina.filter.codec.ProtocolCodecFactory;
25  import org.apache.mina.filter.codec.ProtocolDecoder;
26  import org.apache.mina.filter.codec.ProtocolEncoder;
27  
28  /**
29   * A {@link ProtocolCodecFactory} that serializes and deserializes Java objects.
30   * This codec is very useful when you have to prototype your application rapidly
31   * without any specific codec.
32   *
33   * @author The Apache MINA Project (dev@mina.apache.org)
34   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
35   */
36  public class ObjectSerializationCodecFactory implements ProtocolCodecFactory {
37      private final ObjectSerializationEncoder encoder;
38  
39      private final ObjectSerializationDecoder decoder;
40  
41      /**
42       * Creates a new instance with the {@link ClassLoader} of
43       * the current thread.
44       */
45      public ObjectSerializationCodecFactory() {
46          this(Thread.currentThread().getContextClassLoader());
47      }
48  
49      /**
50       * Creates a new instance with the specified {@link ClassLoader}.
51       */
52      public ObjectSerializationCodecFactory(ClassLoader classLoader) {
53          encoder = new ObjectSerializationEncoder();
54          decoder = new ObjectSerializationDecoder(classLoader);
55      }
56  
57      public ProtocolEncoder getEncoder(IoSession session) {
58          return encoder;
59      }
60  
61      public ProtocolDecoder getDecoder(IoSession session) {
62          return decoder;
63      }
64  
65      /**
66       * Returns the allowed maximum size of the encoded object.
67       * If the size of the encoded object exceeds this value, the encoder
68       * will throw a {@link IllegalArgumentException}.  The default value
69       * is {@link Integer#MAX_VALUE}.
70       * <p>
71       * This method does the same job with {@link ObjectSerializationEncoder#getMaxObjectSize()}.
72       */
73      public int getEncoderMaxObjectSize() {
74          return encoder.getMaxObjectSize();
75      }
76  
77      /**
78       * Sets the allowed maximum size of the encoded object.
79       * If the size of the encoded object exceeds this value, the encoder
80       * will throw a {@link IllegalArgumentException}.  The default value
81       * is {@link Integer#MAX_VALUE}.
82       * <p>
83       * This method does the same job with {@link ObjectSerializationEncoder#setMaxObjectSize(int)}.
84       */
85      public void setEncoderMaxObjectSize(int maxObjectSize) {
86          encoder.setMaxObjectSize(maxObjectSize);
87      }
88  
89      /**
90       * Returns the allowed maximum size of the object to be decoded.
91       * If the size of the object to be decoded exceeds this value, the
92       * decoder will throw a {@link BufferDataException}.  The default
93       * value is <tt>1048576</tt> (1MB).
94       * <p>
95       * This method does the same job with {@link ObjectSerializationDecoder#getMaxObjectSize()}.
96       */
97      public int getDecoderMaxObjectSize() {
98          return decoder.getMaxObjectSize();
99      }
100 
101     /**
102      * Sets the allowed maximum size of the object to be decoded.
103      * If the size of the object to be decoded exceeds this value, the
104      * decoder will throw a {@link BufferDataException}.  The default
105      * value is <tt>1048576</tt> (1MB).
106      * <p>
107      * This method does the same job with {@link ObjectSerializationDecoder#setMaxObjectSize(int)}.
108      */
109     public void setDecoderMaxObjectSize(int maxObjectSize) {
110         decoder.setMaxObjectSize(maxObjectSize);
111     }
112 }