Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 1.80 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.*;
  2.  
  3.  
  4. public class PrevaylerJr {
  5.  
  6.         public static interface Command extends Serializable {
  7.                 Object executeOn(Object system);
  8.         }
  9.  
  10.        
  11.         private final Object _system;
  12.         private final ObjectOutputStream _journal;
  13.  
  14.  
  15.         public PrevaylerJr(Serializable initialState, File storageFile) throws Exception {
  16.                 File tempFile = new File(storageFile.getAbsolutePath() + ".tmp");
  17.                 _system = restoreState(initialState, storageFile, tempFile);
  18.                 _journal = new ObjectOutputStream(new FileOutputStream(tempFile));
  19.                 writeToJournal(_system);
  20.                
  21.                 if (storageFile.delete() && tempFile.renameTo(storageFile))
  22.                         return;
  23.                
  24.                 throw new IOException("Unable to rename " + tempFile + " to " + storageFile);
  25.         }
  26.  
  27.  
  28.         synchronized
  29.         public Object executeTransaction(Command transaction) throws Exception {
  30.                 writeToJournal(transaction);
  31.                 return transaction.executeOn(_system);
  32.         }
  33.  
  34.  
  35.         synchronized
  36.         public Object executeQuery(Command query) {
  37.                 return query.executeOn(_system);
  38.         }
  39.  
  40.  
  41.         private Object restoreState(Object initialState, File storageFile, File tempFile) {
  42.                 Object state = initialState;
  43.                 try {
  44.                         File fileToRead = storageFile.exists() ? storageFile : tempFile;
  45.                         ObjectInputStream input = new ObjectInputStream(new FileInputStream(fileToRead));
  46.                        
  47.                         state = restoreImage(input);
  48.                         restoreCommands(state, input);
  49.                        
  50.                 } catch (Exception endOfStreamReached) {}
  51.                
  52.                 return state;
  53.         }
  54.  
  55.  
  56.         private Serializable restoreImage(ObjectInputStream input) throws IOException, ClassNotFoundException {
  57.                 return (Serializable) input.readObject();
  58.         }
  59.  
  60.  
  61.         private void restoreCommands(Object state, ObjectInputStream input) throws IOException, ClassNotFoundException {
  62.                 while (true)
  63.                         ((Command) input.readObject()).executeOn(state);
  64.         }
  65.  
  66.  
  67.         private void writeToJournal(Object object) throws IOException {
  68.                 _journal.writeObject(object);
  69.                 _journal.flush();
  70.         }
  71.  
  72. }