org.jboss.marshalling.Unmarshaller Java Examples

The following examples show how to use org.jboss.marshalling.Unmarshaller. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MarshallingDecoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) 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();
    }
}
 
Example #2
Source File: MarshallingDecoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) 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();
    }
}
 
Example #3
Source File: CompatibleMarshallingDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    if (discardingTooLongFrame) {
        buffer.skipBytes(actualReadableBytes());
        checkpoint();
        return;
    }

    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();
        out.add(obj);
    } catch (LimitingByteInput.TooBigObjectException ignored) {
        discardingTooLongFrame = true;
        throw new TooLongFrameException();
    } 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();
    }
}
 
Example #4
Source File: ThreadLocalUnmarshallerProvider.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception {
    Unmarshaller unmarshaller = unmarshallers.get();
    if (unmarshaller == null) {
        unmarshaller = factory.createUnmarshaller(config);
        unmarshallers.set(unmarshaller);
    }
    return unmarshaller;
}
 
Example #5
Source File: ContextBoundUnmarshallerProvider.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception {
    Attribute<Unmarshaller> attr = ctx.channel().attr(UNMARSHALLER);
    Unmarshaller unmarshaller = attr.get();
    if (unmarshaller == null) {
        unmarshaller = super.getUnmarshaller(ctx);
        attr.set(unmarshaller);
    }
    return unmarshaller;
}
 
Example #6
Source File: AbstractCompatibleMarshallingEncoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testMarshalling() throws Exception {
    @SuppressWarnings("RedundantStringConstructorCall")
    String testObject = new String("test");

    final MarshallerFactory marshallerFactory = createMarshallerFactory();
    final MarshallingConfiguration configuration = createMarshallingConfig();

    EmbeddedChannel ch = new EmbeddedChannel(createEncoder());

    ch.writeOutbound(testObject);
    assertTrue(ch.finish());

    ByteBuf buffer = ch.readOutbound();

    Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(configuration);
    unmarshaller.start(Marshalling.createByteInput(truncate(buffer).nioBuffer()));
    String read = (String) unmarshaller.readObject();
    assertEquals(testObject, read);

    assertEquals(-1, unmarshaller.read());

    assertNull(ch.readOutbound());

    unmarshaller.finish();
    unmarshaller.close();
    buffer.release();
}
 
Example #7
Source File: MarshallingCodeCFactory.java    From netty-custom-protocol with MIT License 5 votes vote down vote up
/**
   * 创建Jboss Unmarshaller解码对象
   * @return Unmarshaller
   * @throws IOException 
   */
  public static Unmarshaller buildUnMarshalling() throws IOException {
final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5);
Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(configuration);
return unmarshaller;
  }
 
Example #8
Source File: CounterIO.java    From SLP-Core with MIT License 5 votes vote down vote up
public static Counter readCounter(File file) {
	System.out.println("Reading counter from: " + file);
       try (FileInputStream is = new FileInputStream(file)) {
       	final Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(configuration);
           unmarshaller.start(Marshalling.createByteInput(is));
           Counter counter = (Counter) unmarshaller.readObject();
           unmarshaller.finish();
           is.close();
           return counter;
       } catch (IOException | ClassNotFoundException e) {
           System.err.print("Un-marshalling failed: ");
           e.printStackTrace();
       }
	return null;
}
 
Example #9
Source File: ProtocolUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static <T> T unmarshal(final Unmarshaller unmarshaller, final Class<T> expectedType) throws IOException {
    try {
        return unmarshaller.readObject(expectedType);
    } catch (ClassNotFoundException e) {
        throw ProcessLogger.ROOT_LOGGER.failedToReadObject(e);
    }
}
 
Example #10
Source File: CompatibleMarshallingDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    if (discardingTooLongFrame) {
        buffer.skipBytes(actualReadableBytes());
        checkpoint();
        return;
    }

    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();
        out.add(obj);
    } catch (LimitingByteInput.TooBigObjectException ignored) {
        discardingTooLongFrame = true;
        throw new TooLongFrameException();
    } 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();
    }
}
 
Example #11
Source File: StreamUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void safeFinish(final Unmarshaller unmarshaller) {
    if (unmarshaller != null) try {
        unmarshaller.finish();
    } catch (IOException e) {
        ProcessLogger.PROTOCOL_LOGGER.failedToFinishUnmarshaller(e, unmarshaller);
    }
}
 
Example #12
Source File: ThreadLocalUnmarshallerProvider.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception {
    Unmarshaller unmarshaller = unmarshallers.get();
    if (unmarshaller == null) {
        unmarshaller = factory.createUnmarshaller(config);
        unmarshallers.set(unmarshaller);
    }
    return unmarshaller;
}
 
Example #13
Source File: ContextBoundUnmarshallerProvider.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #14
Source File: AbstractCompatibleMarshallingEncoderTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Test
public void testMarshalling() throws IOException, ClassNotFoundException {
    @SuppressWarnings("RedundantStringConstructorCall")
    String testObject = new String("test");

    final MarshallerFactory marshallerFactory = createMarshallerFactory();
    final MarshallingConfiguration configuration = createMarshallingConfig();

    EmbeddedChannel ch = new EmbeddedChannel(createEncoder());

    ch.writeOutbound(testObject);
    assertTrue(ch.finish());

    ByteBuf buffer = (ByteBuf) ch.readOutbound();

    Unmarshaller unmarshaller = marshallerFactory.createUnmarshaller(configuration);
    unmarshaller.start(Marshalling.createByteInput(truncate(buffer).nioBuffer()));
    String read = (String) unmarshaller.readObject();
    assertEquals(testObject, read);

    assertEquals(-1, unmarshaller.read());

    assertNull(ch.readOutbound());

    unmarshaller.finish();
    unmarshaller.close();
    buffer.release();
}
 
Example #15
Source File: DefaultUnmarshallerProvider.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception {
    return factory.createUnmarshaller(config);
}
 
Example #16
Source File: ProtocolUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static Unmarshaller getUnmarshaller(final MarshallingConfiguration config) throws IOException {
    return MARSHALLER_FACTORY.createUnmarshaller(config);
}
 
Example #17
Source File: DefaultUnmarshallerProvider.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception {
    return factory.createUnmarshaller(config);
}
 
Example #18
Source File: UnmarshallerProvider.java    From netty4.0.27Learn with Apache License 2.0 2 votes vote down vote up
/**
 * Get the {@link Unmarshaller} for the given {@link ChannelHandlerContext}
 */
Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception;
 
Example #19
Source File: UnmarshallerProvider.java    From netty-4.1.22 with Apache License 2.0 2 votes vote down vote up
/**
 * Get the {@link Unmarshaller} for the given {@link ChannelHandlerContext} 为给定的ChannelHandlerContext获取Unmarshaller
 */
Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception;
 
Example #20
Source File: ProcessLogger.java    From wildfly-core with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Logs an error message indicating a failure to finish the unmarshaller.
 *
 * @param cause        the cause of the error.
 * @param unmarshaller the marshaller in error.
 */
@LogMessage(level = ERROR)
@Message(id = 38, value = "Failed to finish the unmarshaller %s")
void failedToFinishUnmarshaller(@Cause Throwable cause, Unmarshaller unmarshaller);