diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingDecoder.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingDecoder.java index 8c49beab46..76fdae0b24 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingDecoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingDecoder.java @@ -56,7 +56,7 @@ public class CompatibleMarshallingDecoder extends ReplayingDecoder { @Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, VoidEnum state) throws Exception { - Unmarshaller unmarshaller = provider.getUnmarshaller(channel); + Unmarshaller unmarshaller = provider.getUnmarshaller(ctx); ByteInput input = new ChannelBufferByteInput(buffer); if (maxObjectSize != Integer.MAX_VALUE) { input = new LimitingByteInput(input, maxObjectSize); diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingEncoder.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingEncoder.java index 341d334075..3403eb2a59 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingEncoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/CompatibleMarshallingEncoder.java @@ -49,7 +49,7 @@ public class CompatibleMarshallingEncoder extends OneToOneEncoder { @Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { - Marshaller marshaller = provider.getMarshaller(channel); + Marshaller marshaller = provider.getMarshaller(ctx); ChannelBufferByteOutput output = new ChannelBufferByteOutput(ctx.getChannel().getConfig().getBufferFactory(), 256); marshaller.start(output); marshaller.writeObject(msg); diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/ContextBoundUnmarshallerProvider.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/ContextBoundUnmarshallerProvider.java new file mode 100644 index 0000000000..dee1ae2c83 --- /dev/null +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/ContextBoundUnmarshallerProvider.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 org.jboss.netty.handler.codec.marshalling; + +import org.jboss.marshalling.MarshallerFactory; +import org.jboss.marshalling.MarshallingConfiguration; +import org.jboss.marshalling.Unmarshaller; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandler; +import org.jboss.netty.channel.ChannelHandlerContext; + +/** + * {@link UnmarshallerProvider} which store a reference to the {@link Unmarshaller} in the + * {@link ChannelHandlerContext} via the {@link ChannelHandlerContext#setAttachment(Object)} + * 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 { + + public ContextBoundUnmarshallerProvider(MarshallerFactory factory, MarshallingConfiguration config) { + super(factory, config); + } + + @Override + public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception { + Unmarshaller unmarshaller = (Unmarshaller) ctx.getAttachment(); + if (unmarshaller == null) { + unmarshaller = super.getUnmarshaller(ctx); + ctx.setAttachment(unmarshaller); + } + return unmarshaller; + } + +} diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/DefaultMarshallerProvider.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/DefaultMarshallerProvider.java index 6ce5319ec3..dd2c953875 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/DefaultMarshallerProvider.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/DefaultMarshallerProvider.java @@ -18,11 +18,11 @@ package org.jboss.netty.handler.codec.marshalling; import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.MarshallerFactory; import org.jboss.marshalling.MarshallingConfiguration; -import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; /** * Default implementation of {@link MarshallerProvider} which just create a new {@link Marshaller} - * on ever {@link #getMarshaller(Channel)} call. + * on ever {@link #getMarshaller(ChannelHandlerContext)} call. * * */ @@ -42,7 +42,7 @@ public class DefaultMarshallerProvider implements MarshallerProvider { this.config = config; } - public Marshaller getMarshaller(Channel channel) throws Exception { + public Marshaller getMarshaller(ChannelHandlerContext ctx) throws Exception { return factory.createMarshaller(config); } diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/DefaultUnmarshallerProvider.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/DefaultUnmarshallerProvider.java index 8b33df3009..441831b90b 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/DefaultUnmarshallerProvider.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/DefaultUnmarshallerProvider.java @@ -18,11 +18,11 @@ package org.jboss.netty.handler.codec.marshalling; import org.jboss.marshalling.MarshallerFactory; import org.jboss.marshalling.MarshallingConfiguration; import org.jboss.marshalling.Unmarshaller; -import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; /** * Default implementation of {@link UnmarshallerProvider} which will just create a new {@link Unmarshaller} - * on every call to {@link #getUnmarshaller(Channel)} + * on every call to {@link #getUnmarshaller(ChannelHandlerContext)} * */ public class DefaultUnmarshallerProvider implements UnmarshallerProvider { @@ -41,7 +41,7 @@ public class DefaultUnmarshallerProvider implements UnmarshallerProvider { this.config = config; } - public Unmarshaller getUnmarshaller(Channel channel) throws Exception { + public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception { return factory.createUnmarshaller(config); } diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallerProvider.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallerProvider.java index 11d2dddd55..83b0dfed66 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallerProvider.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallerProvider.java @@ -16,17 +16,17 @@ package org.jboss.netty.handler.codec.marshalling; import org.jboss.marshalling.Marshaller; -import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; /** - * This provider is responsible to get a {@link Marshaller} for the given Channel. + * 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 Channel} + * Get a {@link Marshaller} for the given {@link ChannelHandlerContext} */ - Marshaller getMarshaller(Channel channel) throws Exception; + Marshaller getMarshaller(ChannelHandlerContext ctx) throws Exception; } diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingDecoder.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingDecoder.java index d4a1ad30ad..d14faf3343 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingDecoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingDecoder.java @@ -68,7 +68,7 @@ public class MarshallingDecoder extends LengthFieldBasedFrameDecoder { return null; } - Unmarshaller unmarshaller = provider.getUnmarshaller(channel); + Unmarshaller unmarshaller = provider.getUnmarshaller(ctx); ByteInput input = new ChannelBufferByteInput(frame); try { diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingEncoder.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingEncoder.java index ea003f1965..c82e54d8c3 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingEncoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/MarshallingEncoder.java @@ -74,7 +74,7 @@ public class MarshallingEncoder extends OneToOneEncoder { @Override protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { - Marshaller marshaller = provider.getMarshaller(channel); + Marshaller marshaller = provider.getMarshaller(ctx); ChannelBufferByteOutput output = new ChannelBufferByteOutput(ctx.getChannel().getConfig().getBufferFactory(), estimatedLength); output.getBuffer().writeBytes(LENGTH_PLACEHOLDER); marshaller.start(output); diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/ThreadLocalMarshallerProvider.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/ThreadLocalMarshallerProvider.java index bfa63a8b93..9fb3f865f6 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/ThreadLocalMarshallerProvider.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/ThreadLocalMarshallerProvider.java @@ -18,7 +18,7 @@ package org.jboss.netty.handler.codec.marshalling; import org.jboss.marshalling.Marshaller; import org.jboss.marshalling.MarshallerFactory; import org.jboss.marshalling.MarshallingConfiguration; -import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; /** * {@link UnmarshallerProvider} implementation which use a {@link ThreadLocal} to store references @@ -46,7 +46,7 @@ public class ThreadLocalMarshallerProvider implements MarshallerProvider { this.config = config; } - public Marshaller getMarshaller(Channel channel) throws Exception { + public Marshaller getMarshaller(ChannelHandlerContext ctx) throws Exception { Marshaller marshaller = marshallers.get(); if (marshaller == null) { marshaller = factory.createMarshaller(config); diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/ThreadLocalUnmarshallerProvider.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/ThreadLocalUnmarshallerProvider.java index 6fe5afcf11..c23d56c53d 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/ThreadLocalUnmarshallerProvider.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/ThreadLocalUnmarshallerProvider.java @@ -18,7 +18,7 @@ package org.jboss.netty.handler.codec.marshalling; import org.jboss.marshalling.MarshallerFactory; import org.jboss.marshalling.MarshallingConfiguration; import org.jboss.marshalling.Unmarshaller; -import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; /** * {@link UnmarshallerProvider} implementation which use a {@link ThreadLocal} to store references @@ -44,7 +44,7 @@ public class ThreadLocalUnmarshallerProvider implements UnmarshallerProvider { this.config = config; } - public Unmarshaller getUnmarshaller(Channel channel) throws Exception { + public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception { Unmarshaller unmarshaller = unmarshallers.get(); if (unmarshaller == null) { unmarshaller = factory.createUnmarshaller(config); diff --git a/src/main/java/org/jboss/netty/handler/codec/marshalling/UnmarshallerProvider.java b/src/main/java/org/jboss/netty/handler/codec/marshalling/UnmarshallerProvider.java index 42d7291545..0683553e42 100644 --- a/src/main/java/org/jboss/netty/handler/codec/marshalling/UnmarshallerProvider.java +++ b/src/main/java/org/jboss/netty/handler/codec/marshalling/UnmarshallerProvider.java @@ -16,16 +16,16 @@ package org.jboss.netty.handler.codec.marshalling; import org.jboss.marshalling.Unmarshaller; -import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; /** - * This provider is responsible to get an {@link Unmarshaller} for a {@link Channel} + * This provider is responsible to get an {@link Unmarshaller} for a {@link ChannelHandlerContext} * */ public interface UnmarshallerProvider { /** - * Get the {@link Unmarshaller} for the given {@link Channel} + * Get the {@link Unmarshaller} for the given {@link ChannelHandlerContext} */ - Unmarshaller getUnmarshaller(Channel channel) throws Exception; + Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception; } diff --git a/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverContextBoundCompatibleMarshallingDecoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverContextBoundCompatibleMarshallingDecoderTest.java new file mode 100644 index 0000000000..636ed6fe1d --- /dev/null +++ b/src/test/java/org/jboss/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 org.jboss.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/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverContextBoundMarshallingDecoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverContextBoundMarshallingDecoderTest.java new file mode 100644 index 0000000000..b695cc5604 --- /dev/null +++ b/src/test/java/org/jboss/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 org.jboss.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/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverThreadLocalMarshallingDecoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/RiverThreadLocalMarshallingDecoderTest.java new file mode 100644 index 0000000000..31cecbeffb --- /dev/null +++ b/src/test/java/org/jboss/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 org.jboss.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/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialContextBoundCompatibleMarshallingDecoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialContextBoundCompatibleMarshallingDecoderTest.java new file mode 100644 index 0000000000..b67f3cfa30 --- /dev/null +++ b/src/test/java/org/jboss/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 org.jboss.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/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialContextBoundMarshallingDecoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialContextBoundMarshallingDecoderTest.java new file mode 100644 index 0000000000..9c0c9c8a51 --- /dev/null +++ b/src/test/java/org/jboss/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 org.jboss.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/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialThreadLocalMarshallingDecoderTest.java b/src/test/java/org/jboss/netty/handler/codec/marshalling/SerialThreadLocalMarshallingDecoderTest.java new file mode 100644 index 0000000000..921bf39efa --- /dev/null +++ b/src/test/java/org/jboss/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 org.jboss.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); + } + +}