Example usage for org.apache.commons.lang3 SerializationUtils serialize

List of usage examples for org.apache.commons.lang3 SerializationUtils serialize

Introduction

In this page you can find the example usage for org.apache.commons.lang3 SerializationUtils serialize.

Prototype

public static byte[] serialize(final Serializable obj) 

Source Link

Document

Serializes an Object to a byte array for storage/serialization.

Usage

From source file:org.hillview.dataset.RemoteDataSet.java

@Override
public Observable<PartialResult<ControlMessage.StatusList>> manage(ControlMessage message) {
    final ManageOperation manageOp = new ManageOperation(message);
    final byte[] serializedOp = SerializationUtils.serialize(manageOp);
    final UUID operationId = UUID.randomUUID();
    final Command command = Command.newBuilder().setIdsIndex(this.remoteHandle)
            .setSerializedOp(ByteString.copyFrom(serializedOp)).setHighId(operationId.getMostSignificantBits())
            .setLowId(operationId.getLeastSignificantBits()).build();
    final SerializedSubject<PartialResult<ControlMessage.StatusList>, PartialResult<ControlMessage.StatusList>> subj = createSerializedSubject();
    final StreamObserver<PartialResponse> responseObserver = new ManageObserver(subj, message, this);
    return subj.unsubscribeOn(ExecutorUtils.getUnsubscribeScheduler()).doOnSubscribe(
            () -> this.stub.withDeadlineAfter(TIMEOUT, TimeUnit.MILLISECONDS).manage(command, responseObserver))
            .doOnUnsubscribe(() -> this.unsubscribe(operationId));
}

From source file:org.hillview.dataset.RemoteDataSet.java

/**
 * Unsubscribes an operation. This method is safe to invoke multiple times because the
 * logic on the remote end is idempotent.
 *//*w w  w .  j  av  a2 s.  c o  m*/
private void unsubscribe(final UUID id) {
    HillviewLogger.instance.info("Unsubscribe called", "{0}", id);
    final UnsubscribeOperation op = new UnsubscribeOperation(id);
    final byte[] serializedOp = SerializationUtils.serialize(op);
    final Command command = Command.newBuilder().setIdsIndex(this.remoteHandle)
            .setSerializedOp(ByteString.copyFrom(serializedOp)).setHighId(id.getMostSignificantBits())
            .setLowId(id.getLeastSignificantBits()).build();
    this.stub.withDeadlineAfter(TIMEOUT, TimeUnit.MILLISECONDS).unsubscribe(command, new StreamObserver<Ack>() {
        @Override
        public void onNext(final Ack ack) {
        }

        @Override
        public void onError(final Throwable throwable) {
        }

        @Override
        public void onCompleted() {
        }
    });
}

From source file:org.hillview.dataset.remoting.HillviewServer.java

/**
 * Subscriber that handles map, flatMap and zip.
 */// w  w  w.j a v a2s.  c  om
private Subscriber<PartialResult<IDataSet>> createSubscriber(final Command command, final UUID id,
        final String operation, final StreamObserver<PartialResponse> responseObserver) {
    return new Subscriber<PartialResult<IDataSet>>() {
        @Nullable
        private PartialResponse memoizedResult = null;
        @Nullable
        private Integer memoizedDatasetIndex = null;
        private CompletableFuture queue = CompletableFuture.completedFuture(null);

        @Override
        public void onCompleted() {
            queue = queue.thenRunAsync(() -> {
                if (MEMOIZE && this.memoizedResult != null) {
                    HillviewServer.this.memoizedCommands.insert(command, this.memoizedResult,
                            Converters.checkNull(this.memoizedDatasetIndex));
                }
                responseObserver.onCompleted();
                HillviewServer.this.removeSubscription(id, operation + " completed");
            }, executorService);
        }

        @Override
        public void onError(final Throwable e) {
            queue = queue.thenRunAsync(() -> {
                HillviewLogger.instance.error("Error when creating subscriber", e);
                e.printStackTrace();
                responseObserver.onError(asStatusRuntimeException(e));
                HillviewServer.this.removeSubscription(id, operation + " on error");
            }, executorService);
        }

        @Override
        public void onNext(final PartialResult<IDataSet> pr) {
            queue = queue.thenRunAsync(() -> {
                Integer idsIndex = null;
                if (pr.deltaValue != null) {
                    idsIndex = HillviewServer.this.save(pr.deltaValue);
                }
                final OperationResponse<PartialResult<Integer>> res = new OperationResponse<PartialResult<Integer>>(
                        new PartialResult<Integer>(pr.deltaDone, idsIndex));
                final byte[] bytes = SerializationUtils.serialize(res);
                final PartialResponse result = PartialResponse.newBuilder()
                        .setSerializedOp(ByteString.copyFrom(bytes)).build();
                if (MEMOIZE) {
                    this.memoizedResult = result;
                    this.memoizedDatasetIndex = idsIndex;
                }
                responseObserver.onNext(result);
            }, executorService);
        }
    };
}

From source file:org.hillview.dataset.remoting.HillviewServer.java

/**
 * Implementation of sketch() service in hillview.proto.
 *//*from  ww  w .  jav  a 2 s.c  o  m*/
@Override
@SuppressWarnings("unchecked")
public void sketch(final Command command, final StreamObserver<PartialResponse> responseObserver) {
    try {
        final UUID commandId = this.getId(command);
        boolean memoize = MEMOIZE; // The value may change while we execute
        final IDataSet dataset = this.getIfValid(command.getIdsIndex(), responseObserver);
        if (dataset == null)
            return;
        if (this.respondIfReplyIsMemoized(command, responseObserver, false)) {
            HillviewLogger.instance.info("Found memoized sketch", "on IDataSet#{0}", command.getIdsIndex());
            return;
        }
        final byte[] bytes = command.getSerializedOp().toByteArray();
        final SketchOperation sketchOp = SerializationUtils.deserialize(bytes);
        final Observable<PartialResult> observable = dataset.sketch(sketchOp.sketch);
        Subscriber subscriber = new Subscriber<PartialResult>() {
            @Nullable
            private Object sketchResultAccumulator = memoize ? sketchOp.sketch.getZero() : null;
            private CompletableFuture queue = CompletableFuture.completedFuture(null);

            @Override
            public void onCompleted() {
                queue = queue.thenRunAsync(() -> {
                    responseObserver.onCompleted();
                    HillviewServer.this.removeSubscription(commandId, "sketch completed");

                    if (memoize && this.sketchResultAccumulator != null) {
                        final OperationResponse<PartialResult> res = new OperationResponse<PartialResult>(
                                new PartialResult(1.0, this.sketchResultAccumulator));
                        final byte[] bytes = SerializationUtils.serialize(res);
                        final PartialResponse memoizedResult = PartialResponse.newBuilder()
                                .setSerializedOp(ByteString.copyFrom(bytes)).build();
                        HillviewServer.this.memoizedCommands.insert(command, memoizedResult, 0);
                    }
                }, executorService);
            }

            @Override
            public void onError(final Throwable e) {
                queue = queue.thenRunAsync(() -> {
                    HillviewLogger.instance.error("Exception in sketch", e);
                    e.printStackTrace();
                    responseObserver.onError(asStatusRuntimeException(e));
                    HillviewServer.this.removeSubscription(commandId, "sketch onError");
                }, executorService);
            }

            @Override
            public void onNext(final PartialResult pr) {
                queue = queue.thenRunAsync(() -> {
                    if (memoize && this.sketchResultAccumulator != null)
                        this.sketchResultAccumulator = sketchOp.sketch.add(this.sketchResultAccumulator,
                                pr.deltaValue);
                    final OperationResponse<PartialResult> res = new OperationResponse<PartialResult>(pr);
                    final byte[] bytes = SerializationUtils.serialize(res);
                    responseObserver.onNext(
                            PartialResponse.newBuilder().setSerializedOp(ByteString.copyFrom(bytes)).build());
                }, executorService);
            }
        };
        final Subscription sub = observable.unsubscribeOn(ExecutorUtils.getUnsubscribeScheduler())
                .subscribe(subscriber);
        boolean unsub = this.saveSubscription(commandId, sub, "sketch");
        if (unsub)
            sub.unsubscribe();
    } catch (final Exception e) {
        HillviewLogger.instance.error("Exception in sketch", e);
        e.printStackTrace();
        responseObserver.onError(asStatusRuntimeException(e));
    }
}

From source file:org.hillview.dataset.remoting.HillviewServer.java

/**
 * Implementation of manage() service in hillview.proto.
 *//*ww w .  ja v a  2  s  .  c  om*/
@Override
@SuppressWarnings("unchecked")
public void manage(Command command, StreamObserver<PartialResponse> responseObserver) {
    try {
        final UUID commandId = this.getId(command);
        // TODO: handle errors in a better way in manage commands
        final IDataSet dataset = this.getIfValid(command.getIdsIndex(), responseObserver);
        if (dataset == null)
            return;
        final byte[] bytes = command.getSerializedOp().toByteArray();
        final ManageOperation manage = SerializationUtils.deserialize(bytes);
        Observable<PartialResult<ControlMessage.StatusList>> observable = dataset.manage(manage.message);
        final Callable<ControlMessage.StatusList> callable = () -> {
            HillviewLogger.instance.info("Starting manage", "{0}", manage.message.toString());
            ControlMessage.Status status;
            try {
                status = manage.message.remoteServerAction(this);
            } catch (final Throwable t) {
                status = new ControlMessage.Status("Exception", t);
            }
            ControlMessage.StatusList result = new ControlMessage.StatusList(status);
            HillviewLogger.instance.info("Completed manage", "{0}", manage.message.toString());
            return result;
        };
        Observable<JsonList<ControlMessage.Status>> executed = Observable.fromCallable(callable);
        observable = observable.mergeWith(executed.map(l -> new PartialResult(0, l)));

        Subscriber subscriber = new Subscriber<PartialResult<ControlMessage.StatusList>>() {
            @Override
            public void onCompleted() {
                responseObserver.onCompleted();
                HillviewServer.this.removeSubscription(commandId, "manage completed");
            }

            @Override
            public void onError(final Throwable e) {
                HillviewLogger.instance.error("Exception in manage operation", e);
                e.printStackTrace();
                responseObserver.onError(e);
                HillviewServer.this.removeSubscription(commandId, "manage onError");
            }

            @Override
            public void onNext(final PartialResult pr) {
                final OperationResponse<PartialResult> res = new OperationResponse<PartialResult>(pr);
                final byte[] bytes = SerializationUtils.serialize(res);
                responseObserver.onNext(
                        PartialResponse.newBuilder().setSerializedOp(ByteString.copyFrom(bytes)).build());
            }
        };
        // Results of management commands are never memoized.
        final Subscription sub = observable.unsubscribeOn(ExecutorUtils.getUnsubscribeScheduler())
                .subscribe(subscriber);
        boolean unsub = this.saveSubscription(commandId, sub, "manage");
        if (unsub)
            sub.unsubscribe();
    } catch (final Exception e) {
        HillviewLogger.instance.error("Exception in manage", e);
        e.printStackTrace();
    }
}

From source file:org.janusgraph.graphdb.database.serialize.attribute.SerializableSerializer.java

@Override
public void write(WriteBuffer buffer, T attribute) {
    DataOutput out = (DataOutput) buffer;
    out.writeObjectNotNull(SerializationUtils.serialize(attribute));
}

From source file:org.jasig.cas.services.AttributeReleasePolicyTests.java

@Test
public void verifyAttributeFilterMappedAttributes() {
    final ReturnMappedAttributeReleasePolicy policy = new ReturnMappedAttributeReleasePolicy();
    final Map<String, String> mappedAttr = new HashMap<>();
    mappedAttr.put("attr1", "newAttr1");

    policy.setAllowedAttributes(mappedAttr);

    final Principal p = mock(Principal.class);

    final Map<String, Object> map = new HashMap<>();
    map.put("attr1", "value1");
    map.put("attr2", "value2");
    map.put("attr3", Arrays.asList("v3", "v4"));

    when(p.getAttributes()).thenReturn(map);
    when(p.getId()).thenReturn("principalId");

    final Map<String, Object> attr = policy.getAttributes(p);
    assertEquals(attr.size(), 1);//from   www.j  ava2s . c  o  m
    assertTrue(attr.containsKey("newAttr1"));

    final byte[] data = SerializationUtils.serialize(policy);
    final ReturnMappedAttributeReleasePolicy p2 = SerializationUtils.deserialize(data);
    assertNotNull(p2);
    assertEquals(p2.getAllowedAttributes(), policy.getAllowedAttributes());
}

From source file:org.jasig.cas.services.AttributeReleasePolicyTests.java

@Test
public void verifyServiceAttributeFilterAllowedAttributes() {
    final ReturnAllowedAttributeReleasePolicy policy = new ReturnAllowedAttributeReleasePolicy();
    policy.setAllowedAttributes(Arrays.asList("attr1", "attr3"));
    final Principal p = mock(Principal.class);

    final Map<String, Object> map = new HashMap<>();
    map.put("attr1", "value1");
    map.put("attr2", "value2");
    map.put("attr3", Arrays.asList("v3", "v4"));

    when(p.getAttributes()).thenReturn(map);
    when(p.getId()).thenReturn("principalId");

    final Map<String, Object> attr = policy.getAttributes(p);
    assertEquals(attr.size(), 2);/* w ww  . j a v  a2s . c  om*/
    assertTrue(attr.containsKey("attr1"));
    assertTrue(attr.containsKey("attr3"));

    final byte[] data = SerializationUtils.serialize(policy);
    final ReturnAllowedAttributeReleasePolicy p2 = SerializationUtils.deserialize(data);
    assertNotNull(p2);
    assertEquals(p2.getAllowedAttributes(), policy.getAllowedAttributes());
}

From source file:org.jasig.cas.services.AttributeReleasePolicyTests.java

@Test
public void verifyServiceAttributeFilterAllowedAttributesWithARegexFilter() {
    final ReturnAllowedAttributeReleasePolicy policy = new ReturnAllowedAttributeReleasePolicy();
    policy.setAllowedAttributes(Arrays.asList("attr1", "attr3", "another"));
    policy.setAttributeFilter(new RegisteredServiceRegexAttributeFilter("v3"));
    final Principal p = mock(Principal.class);

    final Map<String, Object> map = new HashMap<>();
    map.put("attr1", "value1");
    map.put("attr2", "value2");
    map.put("attr3", Arrays.asList("v3", "v4"));

    when(p.getAttributes()).thenReturn(map);
    when(p.getId()).thenReturn("principalId");

    final Map<String, Object> attr = policy.getAttributes(p);
    assertEquals(attr.size(), 1);//from   ww  w.  j  a  v a  2 s .  c o m
    assertTrue(attr.containsKey("attr3"));

    final byte[] data = SerializationUtils.serialize(policy);
    final ReturnAllowedAttributeReleasePolicy p2 = SerializationUtils.deserialize(data);
    assertNotNull(p2);
    assertEquals(p2.getAllowedAttributes(), policy.getAllowedAttributes());
    assertEquals(p2.getAttributeFilter(), policy.getAttributeFilter());
}

From source file:org.jasig.cas.services.AttributeReleasePolicyTests.java

@Test
public void verifyServiceAttributeFilterAllAttributes() {
    final ReturnAllAttributeReleasePolicy policy = new ReturnAllAttributeReleasePolicy();
    final Principal p = mock(Principal.class);

    final Map<String, Object> map = new HashMap<>();
    map.put("attr1", "value1");
    map.put("attr2", "value2");
    map.put("attr3", Arrays.asList("v3", "v4"));

    when(p.getAttributes()).thenReturn(map);
    when(p.getId()).thenReturn("principalId");

    final Map<String, Object> attr = policy.getAttributes(p);
    assertEquals(attr.size(), map.size());

    final byte[] data = SerializationUtils.serialize(policy);
    final ReturnAllAttributeReleasePolicy p2 = SerializationUtils.deserialize(data);
    assertNotNull(p2);/* w  ww .j ava  2 s  .  com*/
}