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      @Override
62      public ProtocolEncoder getEncoder(IoSession session) {
63          return encoder;
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public ProtocolDecoder getDecoder(IoSession session) {
71          return decoder;
72      }
73  
74      /**
75       * @return the allowed maximum size of the encoded object.
76       * If the size of the encoded object exceeds this value, the encoder
77       * will throw a {@link IllegalArgumentException}.  The default value
78       * is {@link Integer#MAX_VALUE}.
79       * <p>
80       * This method does the same job with {@link ObjectSerializationEncoder#getMaxObjectSize()}.
81       */
82      public int getEncoderMaxObjectSize() {
83          return encoder.getMaxObjectSize();
84      }
85  
86      /**
87       * Sets the allowed maximum size of the encoded object.
88       * If the size of the encoded object exceeds this value, the encoder
89       * will throw a {@link IllegalArgumentException}.  The default value
90       * is {@link Integer#MAX_VALUE}.
91       * <p>
92       * This method does the same job with {@link ObjectSerializationEncoder#setMaxObjectSize(int)}.
93       * 
94       * @param maxObjectSize The maximum size of the encoded object
95       */
96      public void setEncoderMaxObjectSize(int maxObjectSize) {
97          encoder.setMaxObjectSize(maxObjectSize);
98      }
99  
100     /**
101      * @return the allowed maximum size of the object to be decoded.
102      * If the size of the object to be decoded exceeds this value, the
103      * decoder will throw a {@link BufferDataException}.  The default
104      * value is <tt>1048576</tt> (1MB).
105      * <p>
106      * This method does the same job with {@link ObjectSerializationDecoder#getMaxObjectSize()}.
107      */
108     public int getDecoderMaxObjectSize() {
109         return decoder.getMaxObjectSize();
110     }
111 
112     /**
113      * Sets the allowed maximum size of the object to be decoded.
114      * If the size of the object to be decoded exceeds this value, the
115      * decoder will throw a {@link BufferDataException}.  The default
116      * value is <tt>1048576</tt> (1MB).
117      * <p>
118      * This method does the same job with {@link ObjectSerializationDecoder#setMaxObjectSize(int)}.
119      * 
120      * @param maxObjectSize The maximum size of the decoded object
121      */
122     public void setDecoderMaxObjectSize(int maxObjectSize) {
123         decoder.setMaxObjectSize(maxObjectSize);
124     }
125 }