int[] sizes = new int[3];
byte[] retval = null;
int lastSize;
MarshalledValueOutputStream out;
ExposedByteArrayOutputStream baos = new ExposedByteArrayOutputStream(1024);
try {
initializeStateTransfer(baos);
lastSize = baos.size();
}
catch (Throwable t)
{
log.error("failed initialing state transfer byte[]", t);
if (!suppressErrors)
throw t;
return null;
}
try {
if(generateTransient) {
out = new MarshalledValueOutputStream(baos);
marshallTransientState(rootNode, out);
out.close();
sizes[0] = baos.size() - lastSize;
lastSize = baos.size();
if (debug) {
log.debug("generated the in-memory state (" + sizes[0] +
" bytes)");
}
// Return any state associated with the subtree but not stored in it
if (cache instanceof PojoCache) {
out = new MarshalledValueOutputStream(baos);
marshallAssociatedState(fqn, out);
out.close();
sizes[1] = baos.size() - lastSize;
lastSize = baos.size();
if (debug) {
log.debug("returning the associated state (" + sizes[1] +
" bytes)");
}
}
}
}
catch(Throwable t) {
log.error("failed getting the in-memory (transient) state", t);
if (!suppressErrors)
throw t;
// Reset the byte array and see if we can continue with persistent state
// TODO reconsider this -- why are errors suppressed at all?
sizes[0] = sizes[1] = 0;
baos.reset();
try {
initializeStateTransfer(baos);
}
catch (Throwable t1) {
log.error("failed re-initializing state transfer", t1);
return null;
}
}
if (generatePersistent) {
try {
if (debug)
log.debug("getting the persistent state");
byte[] persState = null;
if (fqn.size() == 0)
persState = cache.getCacheLoader().loadEntireState();
else
persState = ((ExtendedCacheLoader)cache.getCacheLoader()).loadState(fqn);
if (persState != null) {
sizes[2] = persState.length;
baos.write(persState);
}
if (debug) {
log.debug("generated the persistent state (" + sizes[2] +
" bytes)");
}
}
catch(Throwable t) {
log.error("failed getting the persistent state", t);
if (!suppressErrors)
throw t;
sizes[2] = 0;
}
}
// Overwrite the placeholders used for the sizes of the state transfer
// components with the correct values
try {
byte[] bytes = baos.getRawBuffer();
overwriteInt(bytes, 8, sizes[0]);
overwriteInt(bytes, 12, sizes[1]);
overwriteInt(bytes, 16, sizes[2]);
retval = bytes;