/*------------------------------------------------------------ */
@Override
protected Object refresh(final NoSqlSession session, Object version)
{
log.debug("refresh " + session);
ISerializableSession data = null;
try
{
data = getKey(session.getClusterId());
}
catch (TranscoderException error)
{
throw (new IllegalStateException("unable to deserialize session: id=" + session.getClusterId(), error));
}
// check if our in memory version is the same as what is on KVS
if (version != null)
{
long saved = 0;
if (data != null)
{
saved = data.getVersion();
if (saved == (Long) version)
{
log.debug("refresh not needed");
return version;
}
version = Long.valueOf(saved);
}
}
// If it doesn't exist, invalidate
if (data == null)
{
log.debug("refresh:marking invalid, no object");
session.invalidate();
return null;
}
// If it has been flagged invalid, invalidate
boolean valid = data.isValid();
if (!valid)
{
log.debug("refresh:marking invalid, valid flag " + valid);
session.invalidate();
return null;
}
// We need to update the attributes. We will model this as a passivate,
// followed by bindings and then activation.
session.willPassivate();
try
{
for (Enumeration<String> e = data.getAttributeNames(); e.hasMoreElements();)
{
String name = e.nextElement();
Object value = data.getAttribute(name);
// only bind value if it didn't exist in session
if (!session.getNames().contains(name))
{
session.doPutOrRemove(name, value);
session.bindValue(name, value);
}
else
{
session.doPutOrRemove(name, value);
}
}
// cleanup, remove values from session, that don't exist in data anymore:
for (String name : session.getNames())
{
if (!data.getAttributeMap().containsKey(name))
{
session.doPutOrRemove(name, null);
session.unbindValue(name, session.getAttribute(name));
}
}