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 <a href="http://mina.apache.org">Apache MINA Project</a>
34   */
35  public class ObjectSerializationCodecFactory implements ProtocolCodecFactory {
36      private final ObjectSerializationEncoder encoder;
37  
38      private final ObjectSerializationDecoder decoder;
39  
40      /**
41       * Creates a new instance with the {@link ClassLoader} of
42       * the current thread.
43       */
44      public ObjectSerializationCodecFactory() {
45          this(Thread.currentThread().getContextClassLoader());
46      }
47  
48      /**
49       * Creates a new instance with the specified {@link ClassLoader}.
50       * 
51       * @param classLoader The class loader to use
52       */
53      public ObjectSerializationCodecFactory(ClassLoader classLoader) {
54          encoder = new ObjectSerializationEncoder();
55          decoder = new ObjectSerializationDecoder(classLoader);
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      public ProtocolEncoder getEncoder(IoSession session) {
62          return encoder;
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      public ProtocolDecoder getDecoder(IoSession session) {
69          return decoder;
70      }
71  
72      /**
73       * @return the allowed maximum size of the encoded object.
74       * If the size of the encoded object exceeds this value, the encoder
75       * will throw a {@link IllegalArgumentException}.  The default value
76       * is {@link Integer#MAX_VALUE}.
77       * <p>
78       * This method does the same job with {@link ObjectSerializationEncoder#getMaxObjectSize()}.
79       */
80      public int getEncoderMaxObjectSize() {
81          return encoder.getMaxObjectSize();
82      }
83  
84      /**
85       * Sets the allowed maximum size of the encoded object.
86       * If the size of the encoded object exceeds this value, the encoder
87       * will throw a {@link IllegalArgumentException}.  The default value
88       * is {@link Integer#MAX_VALUE}.
89       * <p>
90       * This method does the same job with {@link ObjectSerializationEncoder#setMaxObjectSize(int)}.
91       * 
92       * @param maxObjectSize The maximum size of the encoded object
93       */
94      public void setEncoderMaxObjectSize(int maxObjectSize) {
95          encoder.setMaxObjectSize(maxObjectSize);
96      }
97  
98      /**
99       * @return the allowed maximum size of the object to be decoded.
100      * If the size of the object to be decoded exceeds this value, the
101      * decoder will throw a {@link BufferDataException}.  The default
102      * value is <tt>1048576</tt> (1MB).
103      * <p>
104      * This method does the same job with {@link ObjectSerializationDecoder#getMaxObjectSize()}.
105      */
106     public int getDecoderMaxObjectSize() {
107         return decoder.getMaxObjectSize();
108     }
109 
110     /**
111      * Sets the allowed maximum size of the object to be decoded.
112      * If the size of the object to be decoded exceeds this value, the
113      * decoder will throw a {@link BufferDataException}.  The default
114      * value is <tt>1048576</tt> (1MB).
115      * <p>
116      * This method does the same job with {@link ObjectSerializationDecoder#setMaxObjectSize(int)}.
117      * 
118      * @param maxObjectSize The maximum size of the decoded object
119      */
120     public void setDecoderMaxObjectSize(int maxObjectSize) {
121         decoder.setMaxObjectSize(maxObjectSize);
122     }
123 }