2 * Copyright (c) 2015 Brocade Communications 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.raft.persisted;
10 import com.google.common.collect.ImmutableList;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.io.ByteArrayOutputStream;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.io.ObjectOutputStream;
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.controller.cluster.raft.messages.Payload;
22 import org.opendaylight.controller.cluster.raft.messages.PersistentPayload;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Payload data for server configuration log entries.
29 * @author Thomas Pantelis
31 public final class ServerConfigurationPayload extends Payload implements PersistentPayload {
32 private static final class Proxy implements Externalizable {
33 private static final long serialVersionUID = 1L;
35 private List<ServerInfo> serverConfig;
37 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
38 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
39 @SuppressWarnings("checkstyle:RedundantModifier")
44 Proxy(final ServerConfigurationPayload payload) {
45 serverConfig = payload.getServerConfig();
49 public void writeExternal(final ObjectOutput out) throws IOException {
50 out.writeInt(serverConfig.size());
51 for (ServerInfo i : serverConfig) {
52 out.writeObject(i.getId());
53 out.writeBoolean(i.isVoting());
58 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
59 final int size = in.readInt();
60 serverConfig = new ArrayList<>(size);
61 for (int i = 0; i < size; ++i) {
62 final String id = (String) in.readObject();
63 final boolean voting = in.readBoolean();
64 serverConfig.add(new ServerInfo(id, voting));
68 private Object readResolve() {
69 return new ServerConfigurationPayload(serverConfig);
73 private static final Logger LOG = LoggerFactory.getLogger(ServerConfigurationPayload.class);
74 private static final long serialVersionUID = 1L;
76 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
77 + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
78 + "aren't serialized. FindBugs does not recognize this.")
79 private final List<ServerInfo> serverConfig;
80 private int serializedSize = -1;
82 public ServerConfigurationPayload(final @NonNull List<ServerInfo> serverConfig) {
83 this.serverConfig = ImmutableList.copyOf(serverConfig);
86 public @NonNull List<ServerInfo> getServerConfig() {
92 if (serializedSize < 0) {
93 try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
94 try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
95 out.writeObject(writeReplace());
98 serializedSize = bos.toByteArray().length;
99 } catch (IOException e) {
101 LOG.error("Error serializing", e);
105 return serializedSize;
109 public int hashCode() {
110 return serverConfig.hashCode();
114 public boolean equals(Object obj) {
123 if (getClass() != obj.getClass()) {
127 ServerConfigurationPayload other = (ServerConfigurationPayload) obj;
128 return serverConfig.equals(other.serverConfig);
132 public String toString() {
133 return "ServerConfigurationPayload [serverConfig=" + serverConfig + "]";
137 protected Object writeReplace() {
138 return new Proxy(this);