Table of Contents Previous Section

Fault Tolerant Application .h and .m Files

FaultTolerantApplication.h
#import <WebObjects/WOWebScriptApplication.h>
@interface FaultTolerantApplication:WOWebScriptApplication

@end
FaultTolerantApplication.m
#import "FaultTolerantApplication.h"
@implementation FaultTolerantApplication
- (NSString *)storeIDFromStateID:(NSString *)aStateID 
{
  NSArray *stateIDComponents = [aStateID componentsSeparatedByString:@"."];
  NSString *storeID = nil;
    
  if (!stateIDComponents)
    storeID = nil;
   else if ( ([stateIDComponents count] != 3) ) 
    [NSException raise:NSInvalidArgumentException 
      format:@"Invalid state ID: %@.", aStateID];
  else 
    storeID = [NSString stringWithFormat:@"%@.%@", 
      [stateIDComponents objectAtIndex:0], 
      [stateIDComponents objectAtIndex:1]];
  return storeID;
}
- (NSString *)stateFilePathForStateID:(NSString *)aStateID 
{
  NSString *storeID = [self storeIDFromStateID:aStateID]; 
  NSString *stateDirectory = [self pathForResource:@"State" ofType:@""];
  NSString *stateFilePath = [NSString stringWithFormat:@"%@/%@", 
    stateDirectory,  storeID];
  return stateFilePath;
}
- (void)restoreToStateWithID:(NSString *)aStateID 
{ 
  NSString *stateFilePath;
  NSData *stateData;
    
  // Get the path for the state archive file and read the state NSData
  stateFilePath = [self stateFilePathForStateID:aStateID];
  stateData = [[[NSData alloc] initWithContentsOfFile:stateFilePath] autorelease];
    
  // Restore the stateData as the state for the current session
  [super restoreToStateWithID:aStateID data:stateData];

}
- (NSString *)stateID 
{
  NSString *newStateID;
  NSData *stateData;
  NSString *stateFilePath;
  // Ask the application to capture the state for the session (snapshot all 
  // the persistent and session keys for all the active pages in the session), 
  // and to return a new stateID (stateID for the current interaction in the
  // session).
  newStateID = [super stateID];
    
  // Now that the session state has been prepared, ask the application for it 
  // (in an NSData form).
  stateData = [self stateDataForID:newStateID];
    
  // Write the session state to the appropriate file
  stateFilePath = [self stateFilePathForStateID:newStateID];
  [stateData writeToFile:stateFilePath atomically:NO];
    
  // Make sure that the application does not keep its own copy of the state 
   [self terminateSession]; 
  return newStateID;
}
@end

Table of Contents Next Section