--- /dev/null
+/*
+ * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.raft.base.messages;
+
+import com.google.common.base.Preconditions;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+/**
+ * Abstract base that implements Externalizable with no-op methods that is intended for classes that use the
+ * externalizable proxy pattern but have no data to serialize and read-resolve to a static instance.
+ *
+ * @author Thomas Pantelis
+ */
+public abstract class EmptyExternalizableProxy implements Externalizable {
+ private static final long serialVersionUID = 1L;
+
+ private final Object readResolveTo;
+
+ protected EmptyExternalizableProxy(Object readResolveTo) {
+ this.readResolveTo = Preconditions.checkNotNull(readResolveTo);
+ }
+
+ @Override
+ public void writeExternal(ObjectOutput out) throws IOException {
+ }
+
+ @Override
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ }
+
+ protected Object readResolve() {
+ return readResolveTo;
+ }
+}
// Hidden on purpose
}
- private Object readResolve() {
- return INSTANCE;
+ private Object writeReplace() {
+ return new Proxy();
+ }
+
+ private static class Proxy extends EmptyExternalizableProxy {
+ private static final long serialVersionUID = 1L;
+
+ // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
+ // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
+ @SuppressWarnings("checkstyle:RedundantModifier")
+ public Proxy() {
+ super(INSTANCE);
+ }
}
}
package org.opendaylight.controller.cluster.raft.client.messages;
import java.io.Serializable;
+import org.opendaylight.controller.cluster.raft.base.messages.EmptyExternalizableProxy;
/**
* Message sent to a raft actor to shutdown gracefully. If it's the leader it will transfer leadership to a
// Hidden on purpose
}
- private Object readResolve() {
- return INSTANCE;
+ private Object writeReplace() {
+ return new Proxy();
+ }
+
+ private static class Proxy extends EmptyExternalizableProxy {
+ private static final long serialVersionUID = 1L;
+
+ // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
+ // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
+ @SuppressWarnings("checkstyle:RedundantModifier")
+ public Proxy() {
+ super(INSTANCE);
+ }
}
}
--- /dev/null
+/*
+ * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.raft.base.messages;
+
+import static org.junit.Assert.assertSame;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.junit.Test;
+
+/**
+ * Unit tests for TimeoutNow.
+ *
+ * @author Thomas Pantelis
+ */
+public class TimeoutNowTest {
+
+ @Test
+ public void test() {
+ TimeoutNow cloned = (TimeoutNow) SerializationUtils.clone(TimeoutNow.INSTANCE);
+ assertSame("Cloned instance", TimeoutNow.INSTANCE, cloned);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.raft.client.messages;
+
+import static org.junit.Assert.assertSame;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.junit.Test;
+
+/**
+ * Unit tests for Shutdown.
+ *
+ * @author Thomas Pantelis
+ */
+public class ShutdownTest {
+
+ @Test
+ public void test() {
+ Shutdown cloned = (Shutdown) SerializationUtils.clone(Shutdown.INSTANCE);
+ assertSame("Cloned instance", Shutdown.INSTANCE, cloned);
+ }
+}