Forward-port the JBoss Marshalling codec

This commit is contained in:
Trustin Lee 2012-05-30 23:36:03 -07:00
parent d7a198a60f
commit 34f697a06c
37 changed files with 1774 additions and 0 deletions

View File

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

View File

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

View File

@ -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<Object, VoidEnum> {
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<Byte> 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<Byte> 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<Byte> ctx, Throwable cause) throws Exception {
if (cause instanceof TooLongFrameException) {
ctx.close();
} else {
super.exceptionCaught(ctx, cause);
}
}
}

View File

@ -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 <a href="http://www.jboss.org/jbossmarshalling">JBoss Marshalling website</a>
* for more informations
*
* Use {@link MarshallingEncoder} if possible.
*
*/
@Sharable
public class CompatibleMarshallingEncoder extends MessageToStreamEncoder<Object> {
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<Object> ctx, Object msg, ChannelBuffer out) throws Exception {
Marshaller marshaller = provider.getMarshaller(ctx);
marshaller.start(new ChannelBufferByteOutput(out));
marshaller.writeObject(msg);
marshaller.finish();
marshaller.close();
}
}

View File

@ -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> UNMARSHALLER = new AttributeKey<Unmarshaller>(
ContextBoundUnmarshallerProvider.class.getName() + ".unmarshaller",
Unmarshaller.class);
public ContextBoundUnmarshallerProvider(MarshallerFactory factory, MarshallingConfiguration config) {
super(factory, config);
}
@Override
public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception {
Attribute<Unmarshaller> attr = ctx.attr(UNMARSHALLER);
Unmarshaller unmarshaller = attr.get();
if (unmarshaller == null) {
unmarshaller = super.getUnmarshaller(ctx);
attr.set(unmarshaller);
}
return unmarshaller;
}
}

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 <a href="http://www.jboss.org/jbossmarshalling">JBoss Marshalling website</a>
* for more informations
*
*/
@Sharable
public class MarshallingEncoder extends MessageToStreamEncoder<Object> {
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<Object> 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);
}
}

View File

@ -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<Marshaller> marshallers = new ThreadLocal<Marshaller>();
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;
}
}

View File

@ -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<Unmarshaller> unmarshallers = new ThreadLocal<Unmarshaller>();
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;
}
}

View File

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

View File

@ -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 <a href="http://www.jboss.org/jbossmarshalling">JBoss Marshalling</a>.
*
*/
package io.netty.handler.codec.marshalling;

View File

@ -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<Object> decoder = new DecoderEmbedder<Object>(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<Object> decoder = new DecoderEmbedder<Object>(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<Object> decoder = new DecoderEmbedder<Object>(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();
}

View File

@ -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<ChannelBuffer> encoder = new EncoderEmbedder<ChannelBuffer>(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();
}

View File

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

View File

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

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

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

View File

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

View File

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

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

View File

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

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

View File

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

View File

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

View File

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

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

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

View File

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

View File

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

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

View File

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

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

View File

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