@Override public void objectToStream(Object object, DataOutput stream) throws Exception { int version = this.context.getCurrentVersion(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (DataOutputStream output = new DataOutputStream(bytes)) { this.versionSerializer.writeInt(output, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(Marshalling.createByteOutput(output)); marshaller.writeObject(object); marshaller.flush(); } } byte[] buffer = bytes.toByteArray(); IndexSerializer.VARIABLE.writeInt(stream, buffer.length); stream.write(buffer); }
@Override public Object objectFromStream(DataInput stream) throws Exception { int size = IndexSerializer.VARIABLE.readInt(stream); byte[] buffer = new byte[size]; stream.readFully(buffer); if (this.factory.isUnknownForkResponse(ByteBuffer.wrap(buffer))) return NoSuchService.INSTANCE; try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(buffer))) { int version = this.versionSerializer.readInt(input); try (Unmarshaller unmarshaller = this.context.createUnmarshaller(version)) { unmarshaller.start(Marshalling.createByteInput(input)); return unmarshaller.readObject(); } } } }
byte[] getBytes() throws IOException { byte[] bytes = this.bytes; if (bytes != null) return bytes; if (this.object == null) return null; int version = this.context.getCurrentVersion(); ByteArrayOutputStream output = new ByteArrayOutputStream(); ClassLoader loader = setThreadContextClassLoader(this.context.getClassLoader()); try (SimpleDataOutput data = new SimpleDataOutput(Marshalling.createByteOutput(output))) { IndexSerializer.VARIABLE.writeInt(data, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(data); marshaller.writeObject(this.object); marshaller.finish(); return output.toByteArray(); } } finally { setThreadContextClassLoader(loader); } }
/** * {@inheritDoc} * @see org.wildfly.clustering.marshalling.spi.MarshalledValue#get(java.lang.Object) */ @SuppressWarnings("unchecked") @Override public synchronized T get(MarshallingContext context) throws IOException, ClassNotFoundException { if (this.object == null) { this.context = context; if (this.bytes != null) { ByteArrayInputStream input = new ByteArrayInputStream(this.bytes); ClassLoader loader = setThreadContextClassLoader(this.context.getClassLoader()); try (SimpleDataInput data = new SimpleDataInput(Marshalling.createByteInput(input))) { int version = IndexSerializer.VARIABLE.readInt(data); try (Unmarshaller unmarshaller = context.createUnmarshaller(version)) { unmarshaller.start(data); this.object = (T) unmarshaller.readObject(); unmarshaller.finish(); this.bytes = null; // Free up memory } } finally { setThreadContextClassLoader(loader); } } } return this.object; }
CommandResponseMarshaller(ChannelCommandDispatcherFactoryConfiguration config) { this.context = config.getMarshallingContext(); this.factory = config.getChannelFactory(); this.versionSerializer = IndexSerializer.select(this.context.getCurrentVersion()); }
/** * {@inheritDoc} * @see org.wildfly.clustering.marshalling.spi.MarshalledValue#get(java.lang.Object) */ @SuppressWarnings("unchecked") @Override public synchronized T get(MarshallingContext context) throws IOException, ClassNotFoundException { if (this.object == null) { this.context = context; if (this.bytes != null) { ByteArrayInputStream input = new ByteArrayInputStream(this.bytes); ClassLoader loader = setThreadContextClassLoader(this.context.getClassLoader()); try (SimpleDataInput data = new SimpleDataInput(Marshalling.createByteInput(input))) { int version = IndexSerializer.VARIABLE.readInt(data); try (Unmarshaller unmarshaller = context.createUnmarshaller(version)) { unmarshaller.start(data); this.object = (T) unmarshaller.readObject(); unmarshaller.finish(); this.bytes = null; // Free up memory } } finally { setThreadContextClassLoader(loader); } } } return this.object; }
CommandResponseMarshaller(ChannelCommandDispatcherFactoryConfiguration config) { this.context = config.getMarshallingContext(); this.factory = config.getChannelFactory(); this.versionSerializer = IndexSerializer.select(this.context.getCurrentVersion()); }
@Override public <R> byte[] marshal(Command<R, ? super C> command) throws IOException { int version = this.context.getCurrentVersion(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (DataOutputStream output = new DataOutputStream(bytes)) { IndexSerializer.VARIABLE.writeInt(output, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(Marshalling.createByteOutput(output)); marshaller.writeObject(this.id); marshaller.writeObject(command); marshaller.flush(); } return bytes.toByteArray(); } } }
byte[] getBytes() throws IOException { byte[] bytes = this.bytes; if (bytes != null) return bytes; if (this.object == null) return null; int version = this.context.getCurrentVersion(); ByteArrayOutputStream output = new ByteArrayOutputStream(); ClassLoader loader = setThreadContextClassLoader(this.context.getClassLoader()); try (SimpleDataOutput data = new SimpleDataOutput(Marshalling.createByteOutput(output))) { IndexSerializer.VARIABLE.writeInt(data, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(data); marshaller.writeObject(this.object); marshaller.finish(); return output.toByteArray(); } } finally { setThreadContextClassLoader(loader); } }
private Callable<Object> read(Message message) throws Exception { try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(message.getRawBuffer(), message.getOffset(), message.getLength()))) { int version = IndexSerializer.VARIABLE.readInt(input); try (Unmarshaller unmarshaller = this.marshallingContext.createUnmarshaller(version)) { unmarshaller.start(Marshalling.createByteInput(input)); Object clientId = unmarshaller.readObject(); Optional<Object> context = this.contexts.get(clientId); if (context == null) return () -> NoSuchService.INSTANCE; @SuppressWarnings("unchecked") Command<Object, Object> command = (Command<Object, Object>) unmarshaller.readObject(); // Wrap execution result in an Optional, since command execution might return null ExceptionSupplier<Optional<Object>, Exception> task = () -> Optional.ofNullable(command.execute(context.orElse(null))); return () -> this.executor.execute(task).orElse(Optional.of(NoSuchService.INSTANCE)).orElse(null); } } }
@Override public void objectToStream(Object object, DataOutput stream) throws Exception { int version = this.context.getCurrentVersion(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (DataOutputStream output = new DataOutputStream(bytes)) { this.versionSerializer.writeInt(output, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(Marshalling.createByteOutput(output)); marshaller.writeObject(object); marshaller.flush(); } } byte[] buffer = bytes.toByteArray(); IndexSerializer.VARIABLE.writeInt(stream, buffer.length); stream.write(buffer); }
@Override public Object objectFromStream(DataInput stream) throws Exception { int size = IndexSerializer.VARIABLE.readInt(stream); byte[] buffer = new byte[size]; stream.readFully(buffer); if (this.factory.isUnknownForkResponse(ByteBuffer.wrap(buffer))) return NoSuchService.INSTANCE; try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(buffer))) { int version = this.versionSerializer.readInt(input); try (Unmarshaller unmarshaller = this.context.createUnmarshaller(version)) { unmarshaller.start(Marshalling.createByteInput(input)); return unmarshaller.readObject(); } } } }
@Override public <R> byte[] marshal(Command<R, ? super C> command) throws IOException { int version = this.context.getCurrentVersion(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (DataOutputStream output = new DataOutputStream(bytes)) { IndexSerializer.VARIABLE.writeInt(output, version); try (Marshaller marshaller = this.context.createMarshaller(version)) { marshaller.start(Marshalling.createByteOutput(output)); marshaller.writeObject(this.id); marshaller.writeObject(command); marshaller.flush(); } return bytes.toByteArray(); } } }
private Callable<Object> read(Message message) throws Exception { try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(message.getRawBuffer(), message.getOffset(), message.getLength()))) { int version = IndexSerializer.VARIABLE.readInt(input); try (Unmarshaller unmarshaller = this.marshallingContext.createUnmarshaller(version)) { unmarshaller.start(Marshalling.createByteInput(input)); Object clientId = unmarshaller.readObject(); Optional<Object> context = this.contexts.get(clientId); if (context == null) return () -> NoSuchService.INSTANCE; @SuppressWarnings("unchecked") Command<Object, Object> command = (Command<Object, Object>) unmarshaller.readObject(); // Wrap execution result in an Optional, since command execution might return null ExceptionSupplier<Optional<Object>, Exception> task = () -> Optional.ofNullable(command.execute(context.orElse(null))); return () -> this.executor.execute(task).orElse(Optional.of(NoSuchService.INSTANCE)).orElse(null); } } }