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: */
020: package org.apache.mina.filter.codec.serialization;
021:
022: import org.apache.mina.common.BufferDataException;
023: import org.apache.mina.common.IoSession;
024: import org.apache.mina.filter.codec.ProtocolCodecFactory;
025: import org.apache.mina.filter.codec.ProtocolDecoder;
026: import 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 The Apache MINA Project (dev@mina.apache.org)
034: * @version $Rev: 596187 $, $Date: 2007-11-18 20:41:14 -0700 (Sun, 18 Nov 2007) $
035: */
036: public class ObjectSerializationCodecFactory implements
037: ProtocolCodecFactory {
038: private final ObjectSerializationEncoder encoder;
039:
040: private final ObjectSerializationDecoder decoder;
041:
042: /**
043: * Creates a new instance with the {@link ClassLoader} of
044: * the current thread.
045: */
046: public ObjectSerializationCodecFactory() {
047: this (Thread.currentThread().getContextClassLoader());
048: }
049:
050: /**
051: * Creates a new instance with the specified {@link ClassLoader}.
052: */
053: public ObjectSerializationCodecFactory(ClassLoader classLoader) {
054: encoder = new ObjectSerializationEncoder();
055: decoder = new ObjectSerializationDecoder(classLoader);
056: }
057:
058: public ProtocolEncoder getEncoder(IoSession session) {
059: return encoder;
060: }
061:
062: public ProtocolDecoder getDecoder(IoSession session) {
063: return decoder;
064: }
065:
066: /**
067: * Returns the allowed maximum size of the encoded object.
068: * If the size of the encoded object exceeds this value, the encoder
069: * will throw a {@link IllegalArgumentException}. The default value
070: * is {@link Integer#MAX_VALUE}.
071: * <p>
072: * This method does the same job with {@link ObjectSerializationEncoder#getMaxObjectSize()}.
073: */
074: public int getEncoderMaxObjectSize() {
075: return encoder.getMaxObjectSize();
076: }
077:
078: /**
079: * Sets the allowed maximum size of the encoded object.
080: * If the size of the encoded object exceeds this value, the encoder
081: * will throw a {@link IllegalArgumentException}. The default value
082: * is {@link Integer#MAX_VALUE}.
083: * <p>
084: * This method does the same job with {@link ObjectSerializationEncoder#setMaxObjectSize(int)}.
085: */
086: public void setEncoderMaxObjectSize(int maxObjectSize) {
087: encoder.setMaxObjectSize(maxObjectSize);
088: }
089:
090: /**
091: * Returns the allowed maximum size of the object to be decoded.
092: * If the size of the object to be decoded exceeds this value, the
093: * decoder will throw a {@link BufferDataException}. The default
094: * value is <tt>1048576</tt> (1MB).
095: * <p>
096: * This method does the same job with {@link ObjectSerializationDecoder#getMaxObjectSize()}.
097: */
098: public int getDecoderMaxObjectSize() {
099: return decoder.getMaxObjectSize();
100: }
101:
102: /**
103: * Sets the allowed maximum size of the object to be decoded.
104: * If the size of the object to be decoded exceeds this value, the
105: * decoder will throw a {@link BufferDataException}. The default
106: * value is <tt>1048576</tt> (1MB).
107: * <p>
108: * This method does the same job with {@link ObjectSerializationDecoder#setMaxObjectSize(int)}.
109: */
110: public void setDecoderMaxObjectSize(int maxObjectSize) {
111: decoder.setMaxObjectSize(maxObjectSize);
112: }
113: }
|