org.wildfly.clustering.ejb.cache.bean.DefaultBeanMetaDataEntryMarshaller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wildfly-clustering-ejb-cache Show documentation
Show all versions of wildfly-clustering-ejb-cache Show documentation
Contains code common to the wildfly-clustering-ejb-hotrod and wildfly-clustering-ejb-infinispan modules.
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.clustering.ejb.cache.bean;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import org.infinispan.protostream.descriptors.WireType;
import org.jboss.ejb.client.SessionID;
import org.wildfly.clustering.server.offset.Offset;
import org.wildfly.clustering.server.offset.OffsetValue;
import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller;
import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader;
import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter;
/**
* ProtoStream marshaller for a {@link DefaultBeanMetaDataEntry}.
* @author Paul Ferraro
*/
public class DefaultBeanMetaDataEntryMarshaller implements ProtoStreamMarshaller> {
private static final int NAME_INDEX = 1;
private static final int GROUP_IDENTIFIER_INDEX = 2;
private static final int CREATION_TIME_INDEX = 3;
private static final int LAST_ACCESS_OFFSET_INDEX = 4;
private static final Instant DEFAULT_CREATION_TIME = Instant.EPOCH;
@SuppressWarnings("unchecked")
@Override
public Class extends DefaultBeanMetaDataEntry> getJavaClass() {
return (Class>) (Class>) DefaultBeanMetaDataEntry.class;
}
@Override
public DefaultBeanMetaDataEntry readFrom(ProtoStreamReader reader) throws IOException {
String name = null;
SessionID groupId = null;
Instant creationTime = Instant.EPOCH;
Offset lastAccessOffset = Offset.forInstant(Duration.ZERO);
while (!reader.isAtEnd()) {
int tag = reader.readTag();
switch (WireType.getTagFieldNumber(tag)) {
case NAME_INDEX:
name = reader.readString();
break;
case GROUP_IDENTIFIER_INDEX:
groupId = reader.readObject(SessionID.class);
break;
case CREATION_TIME_INDEX:
creationTime = reader.readObject(Instant.class);
break;
case LAST_ACCESS_OFFSET_INDEX:
lastAccessOffset = reader.readObject(lastAccessOffset.getClass());
break;
default:
reader.skipField(tag);
}
}
DefaultBeanMetaDataEntry entry = new DefaultBeanMetaDataEntry<>(name, groupId, creationTime);
entry.getLastAccess().setOffset(lastAccessOffset);
return entry;
}
@Override
public void writeTo(ProtoStreamWriter writer, DefaultBeanMetaDataEntry metaData) throws IOException {
String name = metaData.getName();
if (name != null) {
writer.writeString(NAME_INDEX, name);
}
SessionID groupId = metaData.getGroupId();
if (groupId != null) {
writer.writeObject(GROUP_IDENTIFIER_INDEX, groupId);
}
OffsetValue lastAccess = metaData.getLastAccess();
Instant creationTime = lastAccess.getBasis();
if (!DEFAULT_CREATION_TIME.equals(creationTime)) {
writer.writeObject(CREATION_TIME_INDEX, creationTime);
}
Offset lastAccessOffset = lastAccess.getOffset();
if (!lastAccessOffset.isZero()) {
writer.writeObject(LAST_ACCESS_OFFSET_INDEX, lastAccessOffset);
}
}
}