Add a ContextBoundUnmarshallerProvider. See #324

This commit is contained in:
Norman Maurer 2012-05-24 16:19:09 +02:00
parent 22282cf3e2
commit be20b50512
17 changed files with 245 additions and 22 deletions

View File

@ -56,7 +56,7 @@ public class CompatibleMarshallingDecoder extends ReplayingDecoder<VoidEnum> {
@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);

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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 {

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}