2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.messages;
10 import akka.actor.ExtendedActorSystem;
11 import akka.serialization.JSerializer;
12 import akka.util.ClassLoaderObjectInputStream;
13 import com.google.common.base.Preconditions;
14 import java.io.ByteArrayInputStream;
15 import java.io.IOException;
16 import org.apache.commons.lang3.SerializationUtils;
17 import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor;
20 * Specialized message transformer, which transforms a {@link ReadyLocalTransaction}
21 * into a {@link BatchedModifications} message. This serializer needs to be plugged
22 * into akka serialization to allow forwarding of ReadyLocalTransaction to remote
25 public final class ReadyLocalTransactionSerializer extends JSerializer {
27 private final ExtendedActorSystem system;
29 public ReadyLocalTransactionSerializer(final ExtendedActorSystem system) {
30 this.system = Preconditions.checkNotNull(system);
34 public int identifier() {
39 public boolean includeManifest() {
44 public byte[] toBinary(final Object obj) {
45 Preconditions.checkArgument(obj instanceof ReadyLocalTransaction, "Unsupported object type %s", obj.getClass());
46 final ReadyLocalTransaction readyLocal = (ReadyLocalTransaction) obj;
47 final BatchedModifications batched = new BatchedModifications(readyLocal.getTransactionId(),
48 readyLocal.getRemoteVersion());
49 batched.setDoCommitOnReady(readyLocal.isDoCommitOnReady());
50 batched.setTotalMessagesSent(1);
51 batched.setReady(true);
53 readyLocal.getModification().applyToCursor(new BatchedCursor(batched));
55 return SerializationUtils.serialize(batched);
59 public Object fromBinaryJava(final byte[] bytes, final Class<?> clazz) {
60 try (ClassLoaderObjectInputStream is = new ClassLoaderObjectInputStream(system.dynamicAccess().classLoader(),
61 new ByteArrayInputStream(bytes))) {
62 return is.readObject();
63 } catch (IOException | ClassNotFoundException e) {
64 throw new IllegalStateException("Failed to deserialize object", e);
68 private static final class BatchedCursor extends AbstractBatchedModificationsCursor {
69 private final BatchedModifications message;
71 BatchedCursor(final BatchedModifications message) {
72 this.message = Preconditions.checkNotNull(message);
76 protected BatchedModifications getModifications() {