001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.filter.codec.serialization;
021
022import org.apache.mina.core.buffer.BufferDataException;
023import org.apache.mina.core.session.IoSession;
024import org.apache.mina.filter.codec.ProtocolCodecFactory;
025import org.apache.mina.filter.codec.ProtocolDecoder;
026import org.apache.mina.filter.codec.ProtocolEncoder;
027
028/**
029 * A {@link ProtocolCodecFactory} that serializes and deserializes Java objects.
030 * This codec is very useful when you have to prototype your application rapidly
031 * without any specific codec.
032 *
033 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
034 */
035public class ObjectSerializationCodecFactory implements ProtocolCodecFactory {
036    private final ObjectSerializationEncoder encoder;
037
038    private final ObjectSerializationDecoder decoder;
039
040    /**
041     * Creates a new instance with the {@link ClassLoader} of
042     * the current thread.
043     */
044    public ObjectSerializationCodecFactory() {
045        this(Thread.currentThread().getContextClassLoader());
046    }
047
048    /**
049     * Creates a new instance with the specified {@link ClassLoader}.
050     * 
051     * @param classLoader The class loader to use
052     */
053    public ObjectSerializationCodecFactory(ClassLoader classLoader) {
054        encoder = new ObjectSerializationEncoder();
055        decoder = new ObjectSerializationDecoder(classLoader);
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    public ProtocolEncoder getEncoder(IoSession session) {
062        return encoder;
063    }
064
065    /**
066     * {@inheritDoc}
067     */
068    public ProtocolDecoder getDecoder(IoSession session) {
069        return decoder;
070    }
071
072    /**
073     * @return the allowed maximum size of the encoded object.
074     * If the size of the encoded object exceeds this value, the encoder
075     * will throw a {@link IllegalArgumentException}.  The default value
076     * is {@link Integer#MAX_VALUE}.
077     * <p>
078     * This method does the same job with {@link ObjectSerializationEncoder#getMaxObjectSize()}.
079     */
080    public int getEncoderMaxObjectSize() {
081        return encoder.getMaxObjectSize();
082    }
083
084    /**
085     * Sets the allowed maximum size of the encoded object.
086     * If the size of the encoded object exceeds this value, the encoder
087     * will throw a {@link IllegalArgumentException}.  The default value
088     * is {@link Integer#MAX_VALUE}.
089     * <p>
090     * This method does the same job with {@link ObjectSerializationEncoder#setMaxObjectSize(int)}.
091     * 
092     * @param maxObjectSize The maximum size of the encoded object
093     */
094    public void setEncoderMaxObjectSize(int maxObjectSize) {
095        encoder.setMaxObjectSize(maxObjectSize);
096    }
097
098    /**
099     * @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}