diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/ChannelBufferByteInput.java b/codec/src/main/java/io/netty/handler/codec/marshalling/ChannelBufferByteInput.java new file mode 100644 index 0000000000..eed0523b3b --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/ChannelBufferByteInput.java @@ -0,0 +1,80 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.buffer.ChannelBuffer; + +import java.io.IOException; + +import org.jboss.marshalling.ByteInput; + +/** + * {@link ByteInput} implementation which reads its data from a {@link ChannelBuffer} + */ +class ChannelBufferByteInput implements ByteInput { + + private final ChannelBuffer buffer; + + public ChannelBufferByteInput(ChannelBuffer buffer) { + this.buffer = buffer; + } + + @Override + public void close() throws IOException { + // nothing to do + } + + @Override + public int available() throws IOException { + return buffer.readableBytes(); + } + + @Override + public int read() throws IOException { + if (buffer.readable()) { + return buffer.readByte() & 0xff; + } + return -1; + } + + @Override + public int read(byte[] array) throws IOException { + return read(array, 0, array.length); + } + + @Override + public int read(byte[] dst, int dstIndex, int length) throws IOException { + int available = available(); + if (available == 0) { + return -1; + } + + length = Math.min(available, length); + buffer.readBytes(dst, dstIndex, length); + return length; + } + + @Override + public long skip(long bytes) throws IOException { + int readable = buffer.readableBytes(); + if (readable < bytes) { + bytes = readable; + } + buffer.readerIndex((int) (buffer.readerIndex() + bytes)); + return bytes; + } + +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/ChannelBufferByteOutput.java b/codec/src/main/java/io/netty/handler/codec/marshalling/ChannelBufferByteOutput.java new file mode 100644 index 0000000000..d19f8851a9 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/ChannelBufferByteOutput.java @@ -0,0 +1,83 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.buffer.ChannelBuffer; +import io.netty.buffer.ChannelBufferFactory; +import io.netty.buffer.ChannelBuffers; + +import java.io.IOException; + +import org.jboss.marshalling.ByteOutput; + +/** + * {@link ByteOutput} implementation which writes the data to a {@link ChannelBuffer} + * + * + */ +class ChannelBufferByteOutput implements ByteOutput { + + private final ChannelBuffer buffer; + + + /** + * Create a new instance which use the given {@link ChannelBuffer} + */ + public ChannelBufferByteOutput(ChannelBuffer buffer) { + this.buffer = buffer; + } + + /** + * Calls {@link #ChannelBufferByteOutput(ChannelBuffer)} with a dynamic {@link ChannelBuffer} + */ + public ChannelBufferByteOutput(ChannelBufferFactory factory, int estimatedLength) { + this(ChannelBuffers.dynamicBuffer(estimatedLength, factory)); + } + + @Override + public void close() throws IOException { + // Nothing todo + } + + @Override + public void flush() throws IOException { + // nothing to do + } + + @Override + public void write(int b) throws IOException { + buffer.writeByte(b); + } + + @Override + public void write(byte[] bytes) throws IOException { + buffer.writeBytes(bytes); + } + + @Override + public void write(byte[] bytes, int srcIndex, int length) throws IOException { + buffer.writeBytes(bytes, srcIndex, length); + } + + /** + * Return the {@link ChannelBuffer} which contains the written content + * + */ + public ChannelBuffer getBuffer() { + return buffer; + } + +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/CompatibleMarshallingDecoder.java b/codec/src/main/java/io/netty/handler/codec/marshalling/CompatibleMarshallingDecoder.java new file mode 100644 index 0000000000..d98d05bc76 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/CompatibleMarshallingDecoder.java @@ -0,0 +1,100 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.buffer.ChannelBuffer; +import io.netty.channel.Channel; +import io.netty.channel.ChannelInboundHandlerContext; +import io.netty.handler.codec.ReplayingDecoder; +import io.netty.handler.codec.TooLongFrameException; +import io.netty.util.VoidEnum; + +import java.io.ObjectStreamConstants; + +import org.jboss.marshalling.ByteInput; +import org.jboss.marshalling.Unmarshaller; + +/** + * {@link ReplayingDecoder} which use an {@link Unmarshaller} to read the Object out of the {@link ChannelBuffer}. + * + * If you can you should use {@link MarshallingDecoder}. + */ +public class CompatibleMarshallingDecoder extends ReplayingDecoder { + protected final UnmarshallerProvider provider; + protected final int maxObjectSize; + + /** + * Create a new instance of {@link CompatibleMarshallingDecoder}. + * + * @param provider the {@link UnmarshallerProvider} which is used to obtain the {@link Unmarshaller} for the {@link Channel} + * @param maxObjectSize the maximal size (in bytes) of the {@link Object} to unmarshal. Once the size is exceeded + * the {@link Channel} will get closed. Use a a maxObjectSize of {@link Integer#MAX_VALUE} to disable this. + * You should only do this if you are sure that the received Objects will never be big and the + * sending side are trusted, as this opens the possibility for a DOS-Attack due an {@link OutOfMemoryError}. + * + */ + public CompatibleMarshallingDecoder(UnmarshallerProvider provider, int maxObjectSize) { + this.provider = provider; + this.maxObjectSize = maxObjectSize; + } + + @Override + public Object decode(ChannelInboundHandlerContext ctx, ChannelBuffer buffer) throws Exception { + Unmarshaller unmarshaller = provider.getUnmarshaller(ctx); + ByteInput input = new ChannelBufferByteInput(buffer); + if (maxObjectSize != Integer.MAX_VALUE) { + input = new LimitingByteInput(input, maxObjectSize); + } + try { + unmarshaller.start(input); + Object obj = unmarshaller.readObject(); + unmarshaller.finish(); + return obj; + } catch (LimitingByteInput.TooBigObjectException e) { + throw new TooLongFrameException("Object to big to unmarshal"); + } finally { + // Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are + // readable. This helps to be sure that we do not leak resource + unmarshaller.close(); + } + } + + @Override + public Object decodeLast(ChannelInboundHandlerContext ctx, ChannelBuffer buffer) throws Exception { + switch (buffer.readableBytes()) { + case 0: + return null; + case 1: + // Ignore the last TC_RESET + if (buffer.getByte(buffer.readerIndex()) == ObjectStreamConstants.TC_RESET) { + buffer.skipBytes(1); + return null; + } + } + + Object decoded = decode(ctx, buffer); + return decoded; + } + + @Override + public void exceptionCaught(ChannelInboundHandlerContext ctx, Throwable cause) throws Exception { + if (cause instanceof TooLongFrameException) { + ctx.close(); + } else { + super.exceptionCaught(ctx, cause); + } + } +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/CompatibleMarshallingEncoder.java b/codec/src/main/java/io/netty/handler/codec/marshalling/CompatibleMarshallingEncoder.java new file mode 100644 index 0000000000..a8070cc51a --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/CompatibleMarshallingEncoder.java @@ -0,0 +1,61 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.buffer.ChannelBuffer; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelOutboundHandlerContext; +import io.netty.handler.codec.MessageToStreamEncoder; + +import org.jboss.marshalling.Marshaller; + +/** + * {@link MessageToStreamEncoder} implementation which uses JBoss Marshalling to marshal + * an Object. + * + * See JBoss Marshalling website + * for more informations + * + * Use {@link MarshallingEncoder} if possible. + * + */ +@Sharable +public class CompatibleMarshallingEncoder extends MessageToStreamEncoder { + + private final MarshallerProvider provider; + + + /** + * Create a new instance of the {@link CompatibleMarshallingEncoder} + * + * @param provider the {@link MarshallerProvider} to use to get the {@link Marshaller} for a {@link Channel} + */ + public CompatibleMarshallingEncoder(MarshallerProvider provider) { + this.provider = provider; + } + + + @Override + public void encode(ChannelOutboundHandlerContext ctx, Object msg, ChannelBuffer out) throws Exception { + Marshaller marshaller = provider.getMarshaller(ctx); + marshaller.start(new ChannelBufferByteOutput(out)); + marshaller.writeObject(msg); + marshaller.finish(); + marshaller.close(); + } + +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/ContextBoundUnmarshallerProvider.java b/codec/src/main/java/io/netty/handler/codec/marshalling/ContextBoundUnmarshallerProvider.java new file mode 100644 index 0000000000..026315a827 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/ContextBoundUnmarshallerProvider.java @@ -0,0 +1,56 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.util.Attribute; +import io.netty.util.AttributeKey; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; +import org.jboss.marshalling.Unmarshaller; + +/** + * {@link UnmarshallerProvider} which store a reference to the {@link Unmarshaller} in the + * {@link ChannelHandlerContext} via the {@link ChannelHandlerContext#attr(AttributeKey)} + * method. So the same {@link Unmarshaller} will be used during the life-time of a {@link Channel} + * for the {@link ChannelHandler}'s {@link ChannelHandlerContext}. + * + * + */ +public class ContextBoundUnmarshallerProvider extends DefaultUnmarshallerProvider { + + private static final AttributeKey UNMARSHALLER = new AttributeKey( + ContextBoundUnmarshallerProvider.class.getName() + ".unmarshaller", + Unmarshaller.class); + + public ContextBoundUnmarshallerProvider(MarshallerFactory factory, MarshallingConfiguration config) { + super(factory, config); + } + + @Override + public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception { + Attribute attr = ctx.attr(UNMARSHALLER); + Unmarshaller unmarshaller = attr.get(); + if (unmarshaller == null) { + unmarshaller = super.getUnmarshaller(ctx); + attr.set(unmarshaller); + } + return unmarshaller; + } +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/DefaultMarshallerProvider.java b/codec/src/main/java/io/netty/handler/codec/marshalling/DefaultMarshallerProvider.java new file mode 100644 index 0000000000..a96e97b345 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/DefaultMarshallerProvider.java @@ -0,0 +1,49 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.channel.ChannelHandlerContext; + +import org.jboss.marshalling.Marshaller; +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; + +/** + * Default implementation of {@link MarshallerProvider} which just create a new {@link Marshaller} + * on ever {@link #getMarshaller(ChannelHandlerContext)} call. + */ +public class DefaultMarshallerProvider implements MarshallerProvider { + + private final MarshallerFactory factory; + private final MarshallingConfiguration config; + + /** + * Create a new instance + * + * @param factory the {@link MarshallerFactory} to use to create {@link Marshaller} + * @param config the {@link MarshallingConfiguration} + */ + public DefaultMarshallerProvider(MarshallerFactory factory, MarshallingConfiguration config) { + this.factory = factory; + this.config = config; + } + + @Override + public Marshaller getMarshaller(ChannelHandlerContext ctx) throws Exception { + return factory.createMarshaller(config); + } + +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/DefaultUnmarshallerProvider.java b/codec/src/main/java/io/netty/handler/codec/marshalling/DefaultUnmarshallerProvider.java new file mode 100644 index 0000000000..0fc03d7a5d --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/DefaultUnmarshallerProvider.java @@ -0,0 +1,50 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.channel.ChannelHandlerContext; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; +import org.jboss.marshalling.Unmarshaller; + +/** + * Default implementation of {@link UnmarshallerProvider} which will just create a new {@link Unmarshaller} + * on every call to {@link #getUnmarshaller(ChannelHandlerContext)} + * + */ +public class DefaultUnmarshallerProvider implements UnmarshallerProvider { + + private final MarshallerFactory factory; + private final MarshallingConfiguration config; + + /** + * Create a new instance of {@link DefaultMarshallerProvider} + * + * @param factory the {@link MarshallerFactory} to use to create {@link Unmarshaller} + * @param config the {@link MarshallingConfiguration} + */ + public DefaultUnmarshallerProvider(MarshallerFactory factory, MarshallingConfiguration config) { + this.factory = factory; + this.config = config; + } + + @Override + public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception { + return factory.createUnmarshaller(config); + } + +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/LimitingByteInput.java b/codec/src/main/java/io/netty/handler/codec/marshalling/LimitingByteInput.java new file mode 100644 index 0000000000..597238f338 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/LimitingByteInput.java @@ -0,0 +1,107 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import java.io.IOException; + +import org.jboss.marshalling.ByteInput; + +/** + * {@link ByteInput} implementation which wraps another {@link ByteInput} and throws a {@link TooBigObjectException} + * if the read limit was reached. + */ +class LimitingByteInput implements ByteInput { + + // Use a static instance here to remove the overhead of fillStacktrace + private static final TooBigObjectException EXCEPTION = new TooBigObjectException(); + + private final ByteInput input; + private final long limit; + private long read; + + public LimitingByteInput(ByteInput input, long limit) { + if (limit <= 0) { + throw new IllegalArgumentException("The limit MUST be > 0"); + } + this.input = input; + this.limit = limit; + } + + @Override + public void close() throws IOException { + // Nothing todo + } + + @Override + public int available() throws IOException { + int available = input.available(); + int readable = readable(available); + return readable; + } + + @Override + public int read() throws IOException { + int readable = readable(1); + if (readable > 0) { + int b = input.read(); + read++; + return b; + } else { + throw EXCEPTION; + } + } + + @Override + public int read(byte[] array) throws IOException { + return read(array, 0, array.length); + } + + @Override + public int read(byte[] array, int offset, int length) throws IOException { + int readable = readable(length); + if (readable > 0) { + int i = input.read(array, offset, readable); + read += i; + return i; + } else { + throw EXCEPTION; + } + } + + @Override + public long skip(long bytes) throws IOException { + int readable = readable((int) bytes); + if (readable > 0) { + long i = input.skip(readable); + read += i; + return i; + } else { + throw EXCEPTION; + } + } + + private int readable(int length) { + return (int) Math.min(length, limit - read); + } + + /** + * Exception that will get thrown if the {@link Object} is to big to unmarshall + * + */ + static final class TooBigObjectException extends IOException { + private static final long serialVersionUID = 1L; + } +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/MarshallerProvider.java b/codec/src/main/java/io/netty/handler/codec/marshalling/MarshallerProvider.java new file mode 100644 index 0000000000..3fe3248835 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/MarshallerProvider.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.channel.ChannelHandlerContext; + +import org.jboss.marshalling.Marshaller; + +/** + * This provider is responsible to get a {@link Marshaller} for the given {@link ChannelHandlerContext}. + */ +public interface MarshallerProvider { + + /** + * Get a {@link Marshaller} for the given {@link ChannelHandlerContext} + */ + Marshaller getMarshaller(ChannelHandlerContext ctx) throws Exception; +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/MarshallingDecoder.java b/codec/src/main/java/io/netty/handler/codec/marshalling/MarshallingDecoder.java new file mode 100644 index 0000000000..d05ed1d759 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/MarshallingDecoder.java @@ -0,0 +1,88 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.buffer.ChannelBuffer; +import io.netty.channel.ChannelInboundHandlerContext; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; +import io.netty.handler.codec.TooLongFrameException; + +import java.io.StreamCorruptedException; + +import org.jboss.marshalling.ByteInput; +import org.jboss.marshalling.Unmarshaller; +/** + * Decoder which MUST be used with {@link MarshallingEncoder}. + * + * A {@link LengthFieldBasedFrameDecoder} which use an {@link Unmarshaller} to read the Object out of the {@link ChannelBuffer}. + * + */ +public class MarshallingDecoder extends LengthFieldBasedFrameDecoder { + + private final UnmarshallerProvider provider; + + /** + * Creates a new decoder whose maximum object size is {@code 1048576} + * bytes. If the size of the received object is greater than + * {@code 1048576} bytes, a {@link StreamCorruptedException} will be + * raised. + * + */ + public MarshallingDecoder(UnmarshallerProvider provider) { + this(provider, 1048576); + } + + /** + * Creates a new decoder with the specified maximum object size. + * + * @param maxObjectSize the maximum byte length of the serialized object. + * if the length of the received object is greater + * than this value, {@link TooLongFrameException} + * will be raised. + */ + public MarshallingDecoder(UnmarshallerProvider provider, int maxObjectSize) { + super(maxObjectSize, 0, 4, 0, 4); + this.provider = provider; + } + + + @Override + public Object decode(ChannelInboundHandlerContext ctx, ChannelBuffer in) throws Exception { + ChannelBuffer frame = (ChannelBuffer) super.decode(ctx, in); + if (frame == null) { + return null; + } + + Unmarshaller unmarshaller = provider.getUnmarshaller(ctx); + ByteInput input = new ChannelBufferByteInput(frame); + + try { + unmarshaller.start(input); + Object obj = unmarshaller.readObject(); + unmarshaller.finish(); + return obj; + } finally { + // Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are + // readable. This helps to be sure that we do not leak resource + unmarshaller.close(); + } + } + + @Override + protected ChannelBuffer extractFrame(ChannelBuffer buffer, int index, int length) { + return buffer.slice(index, length); + } +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/MarshallingEncoder.java b/codec/src/main/java/io/netty/handler/codec/marshalling/MarshallingEncoder.java new file mode 100644 index 0000000000..e560229989 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/MarshallingEncoder.java @@ -0,0 +1,65 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.buffer.ChannelBuffer; +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelOutboundHandlerContext; +import io.netty.handler.codec.MessageToStreamEncoder; + +import org.jboss.marshalling.Marshaller; + +/** + * {@link MessageToStreamEncoder} implementation which uses JBoss Marshalling to marshal + * an Object. Be aware that this encoder is not compatible with an other client that just use + * JBoss Marshalling as it includes the size of every {@link Object} that gets serialized in + * front of the {@link Object} itself. + * + * Use this with {@link MarshallingDecoder} + * + * See JBoss Marshalling website + * for more informations + * + */ +@Sharable +public class MarshallingEncoder extends MessageToStreamEncoder { + + private static final byte[] LENGTH_PLACEHOLDER = new byte[4]; + private final MarshallerProvider provider; + + /** + * Creates a new encoder. + * + * @param provider the {@link MarshallerProvider} to use + */ + public MarshallingEncoder(MarshallerProvider provider) { + this.provider = provider; + } + + @Override + public void encode(ChannelOutboundHandlerContext ctx, Object msg, ChannelBuffer out) throws Exception { + Marshaller marshaller = provider.getMarshaller(ctx); + int lengthPos = out.writerIndex(); + out.writeBytes(LENGTH_PLACEHOLDER); + ChannelBufferByteOutput output = new ChannelBufferByteOutput(out); + marshaller.start(output); + marshaller.writeObject(msg); + marshaller.finish(); + marshaller.close(); + + out.setInt(lengthPos, out.writerIndex() - lengthPos - 4); + } +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/ThreadLocalMarshallerProvider.java b/codec/src/main/java/io/netty/handler/codec/marshalling/ThreadLocalMarshallerProvider.java new file mode 100644 index 0000000000..e1cb4aee25 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/ThreadLocalMarshallerProvider.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.channel.ChannelHandlerContext; + +import org.jboss.marshalling.Marshaller; +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; + +/** + * {@link UnmarshallerProvider} implementation which use a {@link ThreadLocal} to store references + * to {@link Marshaller} instances. This may give you some performance boost if you need to marshall + * many small {@link Object}'s and your actual Thread count is not to big + */ +public class ThreadLocalMarshallerProvider implements MarshallerProvider { + private final ThreadLocal marshallers = new ThreadLocal(); + + private final MarshallerFactory factory; + private final MarshallingConfiguration config; + + /** + * Create a new instance of the {@link ThreadLocalMarshallerProvider} + * + * @param factory the {@link MarshallerFactory} to use to create {@link Marshaller}'s if needed + * @param config the {@link MarshallingConfiguration} to use + */ + public ThreadLocalMarshallerProvider(MarshallerFactory factory, MarshallingConfiguration config) { + this.factory = factory; + this.config = config; + } + + @Override + public Marshaller getMarshaller(ChannelHandlerContext ctx) throws Exception { + Marshaller marshaller = marshallers.get(); + if (marshaller == null) { + marshaller = factory.createMarshaller(config); + marshallers.set(marshaller); + } + return marshaller; + } +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/ThreadLocalUnmarshallerProvider.java b/codec/src/main/java/io/netty/handler/codec/marshalling/ThreadLocalUnmarshallerProvider.java new file mode 100644 index 0000000000..8146fd4cf3 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/ThreadLocalUnmarshallerProvider.java @@ -0,0 +1,56 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.channel.ChannelHandlerContext; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; +import org.jboss.marshalling.Unmarshaller; + +/** + * {@link UnmarshallerProvider} implementation which use a {@link ThreadLocal} to store references + * to {@link Unmarshaller} instances. This may give you some performance boost if you need to unmarshall + * many small {@link Object}'s. + */ +public class ThreadLocalUnmarshallerProvider implements UnmarshallerProvider { + private final ThreadLocal unmarshallers = new ThreadLocal(); + + private final MarshallerFactory factory; + private final MarshallingConfiguration config; + + /** + * Create a new instance of the {@link ThreadLocalUnmarshallerProvider} + * + * @param factory the {@link MarshallerFactory} to use to create {@link Unmarshaller}'s if needed + * @param config the {@link MarshallingConfiguration} to use + */ + public ThreadLocalUnmarshallerProvider(MarshallerFactory factory, MarshallingConfiguration config) { + this.factory = factory; + this.config = config; + } + + @Override + public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception { + Unmarshaller unmarshaller = unmarshallers.get(); + if (unmarshaller == null) { + unmarshaller = factory.createUnmarshaller(config); + unmarshallers.set(unmarshaller); + } + return unmarshaller; + } + +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/UnmarshallerProvider.java b/codec/src/main/java/io/netty/handler/codec/marshalling/UnmarshallerProvider.java new file mode 100644 index 0000000000..10d8c0e6ca --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/UnmarshallerProvider.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.Unmarshaller; +import io.netty.channel.ChannelHandlerContext; + +/** + * This provider is responsible to get an {@link Unmarshaller} for a {@link ChannelHandlerContext} + * + */ +public interface UnmarshallerProvider { + + /** + * Get the {@link Unmarshaller} for the given {@link ChannelHandlerContext} + */ + Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception; +} diff --git a/codec/src/main/java/io/netty/handler/codec/marshalling/package-info.java b/codec/src/main/java/io/netty/handler/codec/marshalling/package-info.java new file mode 100644 index 0000000000..c95b607278 --- /dev/null +++ b/codec/src/main/java/io/netty/handler/codec/marshalling/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * Decoder and Encoder which uses JBoss Marshalling. + * + */ +package io.netty.handler.codec.marshalling; diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingDecoderTest.java new file mode 100644 index 0000000000..c387b96de0 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingDecoderTest.java @@ -0,0 +1,142 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import static org.junit.Assert.*; +import io.netty.buffer.ChannelBuffer; +import io.netty.buffer.ChannelBuffers; +import io.netty.channel.ChannelHandler; +import io.netty.handler.codec.CodecException; +import io.netty.handler.codec.TooLongFrameException; +import io.netty.handler.codec.embedder.DecoderEmbedder; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import junit.framework.Assert; + +import org.jboss.marshalling.Marshaller; +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.Marshalling; +import org.jboss.marshalling.MarshallingConfiguration; +import org.junit.Test; + +public abstract class AbstractCompatibleMarshallingDecoderTest { + private final String testObject = new String("test"); + + @Test + public void testSimpleUnmarshalling() throws IOException { + MarshallerFactory marshallerFactory = createMarshallerFactory(); + MarshallingConfiguration configuration = createMarshallingConfig(); + + DecoderEmbedder decoder = new DecoderEmbedder(createDecoder(Integer.MAX_VALUE)); + + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + Marshaller marshaller = marshallerFactory.createMarshaller(configuration); + marshaller.start(Marshalling.createByteOutput(bout)); + marshaller.writeObject(testObject); + marshaller.finish(); + marshaller.close(); + + byte[] testBytes = bout.toByteArray(); + + decoder.offer(input(testBytes)); + assertTrue(decoder.finish()); + + String unmarshalled = (String) decoder.poll(); + + Assert.assertEquals(testObject, unmarshalled); + + Assert.assertNull(decoder.poll()); + } + + protected ChannelBuffer input(byte[] input) { + return ChannelBuffers.wrappedBuffer(input); + } + + @Test + public void testFragmentedUnmarshalling() throws IOException { + MarshallerFactory marshallerFactory = createMarshallerFactory(); + MarshallingConfiguration configuration = createMarshallingConfig(); + + DecoderEmbedder decoder = new DecoderEmbedder(createDecoder(Integer.MAX_VALUE)); + + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + Marshaller marshaller = marshallerFactory.createMarshaller(configuration); + marshaller.start(Marshalling.createByteOutput(bout)); + marshaller.writeObject(testObject); + marshaller.finish(); + marshaller.close(); + + byte[] testBytes = bout.toByteArray(); + + ChannelBuffer buffer = input(testBytes); + ChannelBuffer slice = buffer.readSlice(2); + + decoder.offer(slice); + decoder.offer(buffer); + assertTrue(decoder.finish()); + + + String unmarshalled = (String) decoder.poll(); + + Assert.assertEquals(testObject, unmarshalled); + + Assert.assertNull(decoder.poll()); + } + + @Test + public void testTooBigObject() throws IOException { + MarshallerFactory marshallerFactory = createMarshallerFactory(); + MarshallingConfiguration configuration = createMarshallingConfig(); + + ChannelHandler mDecoder = createDecoder(4); + DecoderEmbedder decoder = new DecoderEmbedder(mDecoder); + + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + Marshaller marshaller = marshallerFactory.createMarshaller(configuration); + marshaller.start(Marshalling.createByteOutput(bout)); + marshaller.writeObject(testObject); + marshaller.finish(); + marshaller.close(); + + byte[] testBytes = bout.toByteArray(); + + decoder.offer(input(testBytes)); + try { + decoder.poll(); + fail(); + } catch (CodecException e) { + assertEquals(TooLongFrameException.class, e.getClass()); + + + } + + } + + protected ChannelHandler createDecoder(int maxObjectSize) { + return new CompatibleMarshallingDecoder(createProvider(createMarshallerFactory(), createMarshallingConfig()), maxObjectSize); + } + + protected UnmarshallerProvider createProvider(MarshallerFactory factory, MarshallingConfiguration config) { + return new DefaultUnmarshallerProvider(factory, config); + + } + + protected abstract MarshallerFactory createMarshallerFactory(); + protected abstract MarshallingConfiguration createMarshallingConfig(); + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java new file mode 100644 index 0000000000..29b6607073 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/AbstractCompatibleMarshallingEncoderTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.buffer.ChannelBuffer; +import io.netty.channel.ChannelHandler; +import io.netty.handler.codec.embedder.EncoderEmbedder; + +import java.io.IOException; + +import junit.framework.Assert; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.Marshalling; +import org.jboss.marshalling.MarshallingConfiguration; +import org.jboss.marshalling.Unmarshaller; +import org.junit.Test; + +public abstract class AbstractCompatibleMarshallingEncoderTest { + + @Test + public void testMarshalling() throws IOException, ClassNotFoundException { + String testObject = new String("test"); + + final MarshallerFactory marshallerFactory = createMarshallerFactory(); + final MarshallingConfiguration configuration = createMarshallingConfig(); + + EncoderEmbedder encoder = new EncoderEmbedder(createEncoder()); + + encoder.offer(testObject); + Assert.assertTrue(encoder.finish()); + + ChannelBuffer buffer = encoder.poll(); + + Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(configuration); + unmarshaller.start(Marshalling.createByteInput(truncate(buffer).toByteBuffer())); + String read = (String) unmarshaller.readObject(); + Assert.assertEquals(testObject, read); + + Assert.assertEquals(-1, unmarshaller.read()); + + Assert.assertNull(encoder.poll()); + + unmarshaller.finish(); + unmarshaller.close(); + } + protected ChannelBuffer truncate(ChannelBuffer buf) { + return buf; + } + + protected ChannelHandler createEncoder() { + return new CompatibleMarshallingEncoder(createProvider()); + } + + protected MarshallerProvider createProvider() { + return new DefaultMarshallerProvider(createMarshallerFactory(), createMarshallingConfig()); + } + + protected abstract MarshallerFactory createMarshallerFactory(); + + protected abstract MarshallingConfiguration createMarshallingConfig(); + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverCompatibleMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverCompatibleMarshallingDecoderTest.java new file mode 100644 index 0000000000..150257f0af --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverCompatibleMarshallingDecoderTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.Marshalling; +import org.jboss.marshalling.MarshallingConfiguration; + +public class RiverCompatibleMarshallingDecoderTest extends AbstractCompatibleMarshallingDecoderTest { + + @Override + protected MarshallerFactory createMarshallerFactory() { + return Marshalling.getProvidedMarshallerFactory("river"); + } + + @Override + protected MarshallingConfiguration createMarshallingConfig() { + // Create a configuration + final MarshallingConfiguration configuration = new MarshallingConfiguration(); + configuration.setVersion(3); + return configuration; + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverCompatibleMarshallingEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverCompatibleMarshallingEncoderTest.java new file mode 100644 index 0000000000..3061cd19ec --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverCompatibleMarshallingEncoderTest.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.Marshalling; +import org.jboss.marshalling.MarshallingConfiguration; + +public class RiverCompatibleMarshallingEncoderTest extends AbstractCompatibleMarshallingEncoderTest { + + + @Override + protected MarshallerFactory createMarshallerFactory() { + return Marshalling.getProvidedMarshallerFactory("river"); + } + + @Override + protected MarshallingConfiguration createMarshallingConfig() { + // Create a configuration + final MarshallingConfiguration configuration = new MarshallingConfiguration(); + configuration.setVersion(3); + return configuration; + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverContextBoundCompatibleMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverContextBoundCompatibleMarshallingDecoderTest.java new file mode 100644 index 0000000000..9fee0c59e1 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverContextBoundCompatibleMarshallingDecoderTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; + +public class RiverContextBoundCompatibleMarshallingDecoderTest extends RiverCompatibleMarshallingDecoderTest { + + @Override + protected UnmarshallerProvider createProvider(MarshallerFactory factory, MarshallingConfiguration config) { + return new ContextBoundUnmarshallerProvider(factory, config); + } + + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverContextBoundMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverContextBoundMarshallingDecoderTest.java new file mode 100644 index 0000000000..bfb6868ecc --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverContextBoundMarshallingDecoderTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; + +public class RiverContextBoundMarshallingDecoderTest extends RiverMarshallingDecoderTest { + + @Override + protected UnmarshallerProvider createProvider(MarshallerFactory factory, MarshallingConfiguration config) { + return new ContextBoundUnmarshallerProvider(factory, config); + } + + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingDecoderTest.java new file mode 100644 index 0000000000..57f0b1cf17 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingDecoderTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.buffer.ChannelBuffer; +import io.netty.buffer.ChannelBuffers; +import io.netty.channel.ChannelHandler; + +public class RiverMarshallingDecoderTest extends RiverCompatibleMarshallingDecoderTest { + + @Override + protected ChannelBuffer input(byte[] input) { + ChannelBuffer length = ChannelBuffers.buffer(4); + length.writeInt(input.length); + return ChannelBuffers.wrappedBuffer(length, ChannelBuffers.wrappedBuffer(input)); + } + + @Override + protected ChannelHandler createDecoder(int maxObjectSize) { + return new MarshallingDecoder(createProvider(createMarshallerFactory(), createMarshallingConfig()), maxObjectSize); + } + + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingEncoderTest.java new file mode 100644 index 0000000000..e69b54d6e6 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverMarshallingEncoderTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.buffer.ChannelBuffer; +import io.netty.channel.ChannelHandler; + +public class RiverMarshallingEncoderTest extends RiverCompatibleMarshallingEncoderTest { + + @Override + protected ChannelBuffer truncate(ChannelBuffer buf) { + buf.readInt(); + return buf; + } + + @Override + protected ChannelHandler createEncoder() { + return new MarshallingEncoder(createProvider()); + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalCompatibleMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalCompatibleMarshallingDecoderTest.java new file mode 100644 index 0000000000..19e71678a3 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalCompatibleMarshallingDecoderTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; + +public class RiverThreadLocalCompatibleMarshallingDecoderTest extends RiverCompatibleMarshallingDecoderTest { + + @Override + protected UnmarshallerProvider createProvider(MarshallerFactory factory, MarshallingConfiguration config) { + return new ThreadLocalUnmarshallerProvider(factory, config); + } + + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalCompatibleMarshallingEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalCompatibleMarshallingEncoderTest.java new file mode 100644 index 0000000000..d575218134 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalCompatibleMarshallingEncoderTest.java @@ -0,0 +1,25 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +public class RiverThreadLocalCompatibleMarshallingEncoderTest extends RiverCompatibleMarshallingEncoderTest { + + @Override + protected MarshallerProvider createProvider() { + return new ThreadLocalMarshallerProvider(createMarshallerFactory(), createMarshallingConfig()); + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalMarshallingDecoderTest.java new file mode 100644 index 0000000000..d00f7c280e --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalMarshallingDecoderTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; + +public class RiverThreadLocalMarshallingDecoderTest extends RiverMarshallingDecoderTest { + + @Override + protected UnmarshallerProvider createProvider(MarshallerFactory factory, MarshallingConfiguration config) { + return new ThreadLocalUnmarshallerProvider(factory, config); + } + + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalMarshallingEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalMarshallingEncoderTest.java new file mode 100644 index 0000000000..abe20d0ade --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/RiverThreadLocalMarshallingEncoderTest.java @@ -0,0 +1,25 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +public class RiverThreadLocalMarshallingEncoderTest extends RiverMarshallingEncoderTest { + + @Override + protected MarshallerProvider createProvider() { + return new ThreadLocalMarshallerProvider(createMarshallerFactory(), createMarshallingConfig()); + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialCompatibleMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialCompatibleMarshallingDecoderTest.java new file mode 100644 index 0000000000..ffb5a95321 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialCompatibleMarshallingDecoderTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.Marshalling; +import org.jboss.marshalling.MarshallingConfiguration; + +public class SerialCompatibleMarshallingDecoderTest extends AbstractCompatibleMarshallingDecoderTest { + + @Override + protected MarshallerFactory createMarshallerFactory() { + return Marshalling.getProvidedMarshallerFactory("serial"); + } + + @Override + protected MarshallingConfiguration createMarshallingConfig() { + // Create a configuration + final MarshallingConfiguration configuration = new MarshallingConfiguration(); + configuration.setVersion(5); + return configuration; + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialCompatibleMarshallingEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialCompatibleMarshallingEncoderTest.java new file mode 100644 index 0000000000..1308b25e96 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialCompatibleMarshallingEncoderTest.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.Marshalling; +import org.jboss.marshalling.MarshallingConfiguration; + +public class SerialCompatibleMarshallingEncoderTest extends AbstractCompatibleMarshallingEncoderTest { + + + @Override + protected MarshallerFactory createMarshallerFactory() { + return Marshalling.getProvidedMarshallerFactory("serial"); + } + + @Override + protected MarshallingConfiguration createMarshallingConfig() { + // Create a configuration + final MarshallingConfiguration configuration = new MarshallingConfiguration(); + configuration.setVersion(5); + return configuration; + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialContextBoundCompatibleMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialContextBoundCompatibleMarshallingDecoderTest.java new file mode 100644 index 0000000000..a41d14c05d --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialContextBoundCompatibleMarshallingDecoderTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; + +public class SerialContextBoundCompatibleMarshallingDecoderTest extends SerialCompatibleMarshallingDecoderTest { + + + @Override + protected UnmarshallerProvider createProvider(MarshallerFactory factory, MarshallingConfiguration config) { + return new ContextBoundUnmarshallerProvider(factory, config); + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialContextBoundMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialContextBoundMarshallingDecoderTest.java new file mode 100644 index 0000000000..79f0037b59 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialContextBoundMarshallingDecoderTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; + +public class SerialContextBoundMarshallingDecoderTest extends SerialMarshallingDecoderTest { + + + @Override + protected UnmarshallerProvider createProvider(MarshallerFactory factory, MarshallingConfiguration config) { + return new ContextBoundUnmarshallerProvider(factory, config); + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingDecoderTest.java new file mode 100644 index 0000000000..4f3c13d07e --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingDecoderTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.buffer.ChannelBuffer; +import io.netty.buffer.ChannelBuffers; +import io.netty.channel.ChannelHandler; + +public class SerialMarshallingDecoderTest extends SerialCompatibleMarshallingDecoderTest { + + @Override + protected ChannelBuffer input(byte[] input) { + ChannelBuffer length = ChannelBuffers.buffer(4); + length.writeInt(input.length); + return ChannelBuffers.wrappedBuffer(length, ChannelBuffers.wrappedBuffer(input)); + } + + @Override + protected ChannelHandler createDecoder(int maxObjectSize) { + return new MarshallingDecoder(createProvider(createMarshallerFactory(), createMarshallingConfig()), maxObjectSize); + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingEncoderTest.java new file mode 100644 index 0000000000..ab538bc38a --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialMarshallingEncoderTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import io.netty.buffer.ChannelBuffer; +import io.netty.channel.ChannelHandler; + +public class SerialMarshallingEncoderTest extends SerialCompatibleMarshallingEncoderTest{ + + @Override + protected ChannelBuffer truncate(ChannelBuffer buf) { + buf.readInt(); + return buf; + } + + @Override + protected ChannelHandler createEncoder() { + return new MarshallingEncoder(createProvider()); + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalCompatibleMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalCompatibleMarshallingDecoderTest.java new file mode 100644 index 0000000000..28b7e21741 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalCompatibleMarshallingDecoderTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; + +public class SerialThreadLocalCompatibleMarshallingDecoderTest extends SerialCompatibleMarshallingDecoderTest { + + + @Override + protected UnmarshallerProvider createProvider(MarshallerFactory factory, MarshallingConfiguration config) { + return new ThreadLocalUnmarshallerProvider(factory, config); + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalCompatibleMarshallingEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalCompatibleMarshallingEncoderTest.java new file mode 100644 index 0000000000..9908c42602 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalCompatibleMarshallingEncoderTest.java @@ -0,0 +1,25 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +public class SerialThreadLocalCompatibleMarshallingEncoderTest extends SerialCompatibleMarshallingEncoderTest { + + @Override + protected MarshallerProvider createProvider() { + return new ThreadLocalMarshallerProvider(createMarshallerFactory(), createMarshallingConfig()); + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalMarshallingDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalMarshallingDecoderTest.java new file mode 100644 index 0000000000..35af18dac1 --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalMarshallingDecoderTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; + +public class SerialThreadLocalMarshallingDecoderTest extends SerialMarshallingDecoderTest { + + + @Override + protected UnmarshallerProvider createProvider(MarshallerFactory factory, MarshallingConfiguration config) { + return new ThreadLocalUnmarshallerProvider(factory, config); + } + +} diff --git a/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalMarshallingEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalMarshallingEncoderTest.java new file mode 100644 index 0000000000..fdb7ea1eae --- /dev/null +++ b/codec/src/test/java/io/netty/handler/codec/marshalling/SerialThreadLocalMarshallingEncoderTest.java @@ -0,0 +1,25 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.marshalling; + +public class SerialThreadLocalMarshallingEncoderTest extends SerialMarshallingEncoderTest { + + @Override + protected MarshallerProvider createProvider() { + return new ThreadLocalMarshallerProvider(createMarshallerFactory(), createMarshallingConfig()); + } + +}