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 */ 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 public ObjectSerializationCodecFactory(ClassLoader classLoader) { 52 encoder = new ObjectSerializationEncoder(); 53 decoder = new ObjectSerializationDecoder(classLoader); 54 } 55 56 public ProtocolEncoder getEncoder(IoSession session) { 57 return encoder; 58 } 59 60 public ProtocolDecoder getDecoder(IoSession session) { 61 return decoder; 62 } 63 64 /** 65 * Returns the allowed maximum size of the encoded object. 66 * If the size of the encoded object exceeds this value, the encoder 67 * will throw a {@link IllegalArgumentException}. The default value 68 * is {@link Integer#MAX_VALUE}. 69 * <p> 70 * This method does the same job with {@link ObjectSerializationEncoder#getMaxObjectSize()}. 71 */ 72 public int getEncoderMaxObjectSize() { 73 return encoder.getMaxObjectSize(); 74 } 75 76 /** 77 * Sets the allowed maximum size of the encoded object. 78 * If the size of the encoded object exceeds this value, the encoder 79 * will throw a {@link IllegalArgumentException}. The default value 80 * is {@link Integer#MAX_VALUE}. 81 * <p> 82 * This method does the same job with {@link ObjectSerializationEncoder#setMaxObjectSize(int)}. 83 */ 84 public void setEncoderMaxObjectSize(int maxObjectSize) { 85 encoder.setMaxObjectSize(maxObjectSize); 86 } 87 88 /** 89 * Returns the allowed maximum size of the object to be decoded. 90 * If the size of the object to be decoded exceeds this value, the 91 * decoder will throw a {@link BufferDataException}. The default 92 * value is <tt>1048576</tt> (1MB). 93 * <p> 94 * This method does the same job with {@link ObjectSerializationDecoder#getMaxObjectSize()}. 95 */ 96 public int getDecoderMaxObjectSize() { 97 return decoder.getMaxObjectSize(); 98 } 99 100 /** 101 * Sets 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#setMaxObjectSize(int)}. 107 */ 108 public void setDecoderMaxObjectSize(int maxObjectSize) { 109 decoder.setMaxObjectSize(maxObjectSize); 110 } 111 }