Changeset 380

Show
Ignore:
Timestamp:
07/25/10 12:06:34 (2 years ago)
Author:
ke
Message:

First steps towards loading saved sessions.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • kahina/trunk/src/org/kahina/core/KahinaInstance.java

    r379 r380  
    55import java.io.FileOutputStream; 
    66import java.io.IOException; 
     7import java.io.ObjectInputStream; 
    78import java.io.ObjectOutputStream; 
    89import java.util.Set; 
     10import java.util.zip.ZipException; 
     11import java.util.zip.ZipFile; 
    912 
    1013import javax.swing.JOptionPane; 
     
    131134        private void loadSession(File file) 
    132135        { 
    133                 // TODO Auto-generated method stub 
    134                  
     136                ZipFile zipFile = null; 
     137                try 
     138                { 
     139                        zipFile = new ZipFile(file); 
     140                        ObjectInputStream in = new ObjectInputStream(zipFile.getInputStream(zipFile.getEntry("state"))); 
     141                        state = castToStateType(in.readObject()); 
     142                        in.close(); 
     143                        File directory = FileUtilities.createTemporaryDirectory(); 
     144                        FileUtilities.unzipToDirectory(zipFile, directory, "steps/"); 
     145                        // TODO load steps into magazine 
     146                } catch (Exception e) 
     147                { 
     148                        gui.showMessageDialog(SwingUtilities.visualError("Session could not be loaded due to the following problem: ", e), "Error", JOptionPane.ERROR_MESSAGE); 
     149                        return; 
     150                } finally 
     151                { 
     152                        if (zipFile != null) 
     153                        { 
     154                                try 
     155                                { 
     156                                        zipFile.close(); 
     157                                } catch (IOException e) 
     158                                { 
     159                                        gui.showMessageDialog(SwingUtilities.visualError("Session could not be loaded due to the following problem: ", e), "Error", JOptionPane.ERROR_MESSAGE); 
     160                                } 
     161                        } 
     162                } 
     163        } 
     164 
     165        @SuppressWarnings("unchecked") 
     166        private S castToStateType(Object object) 
     167        { 
     168                return (S) object; 
    135169        } 
    136170 
     
    143177                } catch (IOException e) 
    144178                { 
    145                         gui.showMessageDialog(SwingUtilities.visualError("State could not be saved due to the following problem:", e), "Error", JOptionPane.ERROR_MESSAGE); 
     179                        gui.showMessageDialog(SwingUtilities.visualError("Session could not be saved due to the following problem:", e), "Error", JOptionPane.ERROR_MESSAGE); 
    146180                        return; 
    147181                } 
     
    157191                if (!stepFolder.mkdir()) 
    158192                { 
    159                         gui.showMessageDialog("Failed to create directory " + directory + ". State not saved.", "Error", JOptionPane.ERROR_MESSAGE); 
     193                        gui.showMessageDialog("Failed to create directory " + directory + ". Session not saved.", "Error", JOptionPane.ERROR_MESSAGE); 
    160194                        return; 
    161195                } 
    162196                DataManager dm = KahinaRunner.getDataManager(); 
    163                 ProgressMonitorWrapper monitor = gui.createProgressMonitorWrapper("Saving state", null, 0, dm.persistSteps() * 2 + 2); 
     197                ProgressMonitorWrapper monitor = gui.createProgressMonitorWrapper("Saving session", null, 0, dm.persistSteps() * 2 + 2); 
    164198                ObjectOutputStream out = null; 
    165199                try 
     
    181215                { 
    182216                        monitor.close(); 
    183                         gui.showMessageDialog(SwingUtilities.visualError("State could not be saved due to the following problem: ", e), "Error", JOptionPane.ERROR_MESSAGE); 
     217                        gui.showMessageDialog(SwingUtilities.visualError("Session could not be saved due to the following problem: ", e), "Error", JOptionPane.ERROR_MESSAGE); 
    184218                } finally 
    185219                { 
     
    191225                                } catch (IOException e) 
    192226                                { 
    193                                         gui.showMessageDialog(SwingUtilities.visualError("State could not be saved due to the following problem: ", e), "Error", JOptionPane.ERROR_MESSAGE); 
     227                                        gui.showMessageDialog(SwingUtilities.visualError("Session could not be saved due to the following problem: ", e), "Error", JOptionPane.ERROR_MESSAGE); 
    194228                                } 
    195229                        } 
  • kahina/trunk/src/org/kahina/core/data/DataManager.java

    r375 r380  
    159159                // do nothing 
    160160        } 
     161 
     162        public void load(File directory) 
     163        { 
     164                throw new UnsupportedOperationException("This data manager does not support persistence."); 
     165        } 
    161166} 
  • kahina/trunk/src/org/kahina/core/util/FileUtilities.java

    r376 r380  
    99import java.io.InputStream; 
    1010import java.io.OutputStream; 
     11import java.util.Enumeration; 
    1112import java.util.zip.ZipEntry; 
     13import java.util.zip.ZipFile; 
    1214import java.util.zip.ZipOutputStream; 
    1315 
     
    3335                                out.close(); 
    3436                        } 
    35                 } // Java is absurd. 
     37                } 
    3638        } 
    3739 
    38         private static void copy(File file, OutputStream out) throws IOException 
     40        private static void copy(File sourcee, OutputStream out) throws IOException 
    3941        { 
    40                 InputStream in = new BufferedInputStream(new FileInputStream(file)); 
    41                 int length; 
    42                 byte[] buffer = new byte[4096]; 
     42                InputStream in = new BufferedInputStream(new FileInputStream(sourcee)); 
    4343                try 
    4444                { 
    45                         while ((length = in.read(buffer)) > 0) 
    46                         { 
    47                                 out.write(buffer, 0, length); 
    48                         } 
     45                        copy(in, out); 
    4946                } catch (IOException e) 
    5047                { 
     
    5350                { 
    5451                        in.close(); 
     52                } 
     53        } 
     54 
     55        private static void copy(InputStream in, File destination) throws IOException 
     56        { 
     57                OutputStream out = new BufferedOutputStream(new FileOutputStream(destination)); 
     58                try 
     59                { 
     60                        copy(in, out); 
     61                } catch (IOException e) 
     62                { 
     63                        throw e; 
     64                } finally 
     65                { 
     66                        out.close(); 
     67                } 
     68        } 
     69 
     70        private static void copy(InputStream in, OutputStream out) throws IOException 
     71        { 
     72                int length; 
     73                byte[] buffer = new byte[4096]; 
     74                while ((length = in.read(buffer)) > 0) 
     75                { 
     76                        out.write(buffer, 0, length); 
    5577                } 
    5678        } 
     
    116138        } 
    117139 
     140        /** 
     141         * Unzips zip entries whose names start with the given prefix to a given 
     142         * directory. At the moment, this supports only flat structures without 
     143         * further subdirectories. 
     144         *  
     145         * @param zipFile 
     146         * @param directory 
     147         * @param prefix 
     148         * @throws IOException 
     149         */ 
     150        public static void unzipToDirectory(ZipFile zipFile, File directory, String prefix) throws IOException 
     151        { 
     152                int length = prefix.length(); 
     153                Enumeration<? extends ZipEntry> entries = zipFile.entries(); 
     154                while (entries.hasMoreElements()) 
     155                { 
     156                        ZipEntry entry = entries.nextElement(); 
     157                        String name = entry.getName(); 
     158                        if (name.startsWith(prefix)) 
     159                        { 
     160                                File file = new File(directory, name.substring(length)); 
     161                                InputStream in = null; 
     162                                try 
     163                                { 
     164                                        in = zipFile.getInputStream(entry); 
     165                                        copy(in, file); 
     166                                } catch (IOException e) 
     167                                { 
     168                                        throw e; 
     169                                } finally 
     170                                { 
     171                                        if (in != null) 
     172                                        { 
     173                                                in.close(); 
     174                                        } 
     175                                } 
     176                        } 
     177                } 
     178        } 
     179 
    118180} 
  • kahina/trunk/src/org/kahina/tralesld/bridge/TraleSLDBridge.java

    r370 r380  
    5050        private TraleSLDFSPacker packer; 
    5151 
    52         private int lastStepIDWithFreshPacker = 0; 
    53  
    5452        private Sharer<TraleSLDVariableBinding> bindingSharer; 
    55  
    56         private int lastStepIDWithFreshSharer = 0; 
    5753 
    5854        public TraleSLDBridge(TraleSLDState state) 
     
    332328        private TraleSLDFS createFSObject(String grisuMessage, int stepID) 
    333329        { 
    334                 if (KahinaRunner.getDataHandlingMethod() == KahinaDataHandlingMethod.MAGAZINE) 
     330                /*if (KahinaRunner.getDataHandlingMethod() == KahinaDataHandlingMethod.MAGAZINE) 
    335331                { 
    336332                        // YUCK! 
     
    340336                                lastStepIDWithFreshPacker = stepID; 
    341337                        } 
    342                 } 
     338                }*/ 
    343339                return packer.pack(grisuMessage); 
    344340        } 
     
    346342        private TraleSLDVariableBinding createBindingObject(TraleSLDVariableBinding traleSLDVariableBinding, int stepID) 
    347343        { 
    348                 if (KahinaRunner.getDataHandlingMethod() == KahinaDataHandlingMethod.MAGAZINE) 
     344                /*if (KahinaRunner.getDataHandlingMethod() == KahinaDataHandlingMethod.MAGAZINE) 
    349345                { 
    350346                        if (stepID - lastStepIDWithFreshSharer >= 1000) 
     
    353349                                lastStepIDWithFreshSharer = stepID; 
    354350                        } 
    355                 } 
     351                }*/ 
    356352                return bindingSharer.share(traleSLDVariableBinding); 
    357353        }