Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2021 for trunk/sources


Ignore:
Timestamp:
06/05/09 13:29:18 (15 years ago)
Author:
kgrading
Message:

added functionality to uptime manager (#669)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Hive.Client.Core/3.2/ConfigurationManager/UptimeManager.cs

    r2011 r2021  
    66using System.Xml.Serialization;
    77using System.IO;
     8using HeuristicLab.Hive.Client.Common;
    89
    910namespace HeuristicLab.Hive.Client.Core.ConfigurationManager {
    1011  public class UptimeManager {
    1112
    12     public List<Appointment> Appointments { get; set; }
     13    private List<Appointment> appointments = null;
     14    public List<Appointment> Appointments {
     15      get {
     16        if (appointments == null)
     17          RestoreFromHDD();
     18        return appointments;
     19      }
     20      set {
     21        appointments = value;
     22        PersistToHDD();
     23      }
     24    }
    1325    private static String path = System.IO.Directory.GetCurrentDirectory()+"\\plugins\\Hive.Client.Jobs\\";
    1426
     
    2537    private void PersistToHDD() {
    2638      XmlSerializer s = new XmlSerializer(typeof(List<Appointment>));
    27       using (TextWriter w = new StreamWriter(path + "calendar.xml")) {
     39      if (!Directory.Exists(path))
     40        Directory.CreateDirectory(path);
     41      TextWriter w = null;
     42      try {
     43        w = new StreamWriter(path + "calendar.xml");
    2844        s.Serialize(w, Appointments); 
     45      } catch(Exception e) {
     46        Logging.Instance.Error(this.ToString(), "Persistance of the Calendar failed!", e);
     47      } finally {
     48        if(w!=null)
     49          w.Close();
    2950      }
    3051    }
     
    3253    private void RestoreFromHDD() {
    3354      XmlSerializer s = new XmlSerializer(typeof(List<Appointment>));
    34       using (TextReader r = new StreamReader(path + "calendar.xml")) {
    35         Appointments = (List<Appointment>)s.Deserialize(r);       
    36       }           
     55      if(File.Exists(Path.Combine(path, "calendar.xml"))) {
     56        TextReader r = null;
     57       
     58        try {
     59          r = new StreamReader(path + "calendar.xml");
     60          Appointments = (List<Appointment>)s.Deserialize(r);       
     61        } catch (Exception e) {
     62          Logging.Instance.Error(this.ToString(), "Deserialization of Calendar failed", e);
     63          Logging.Instance.Info(this.ToString(), "Starting with a new one");
     64          appointments = new List<Appointment>();
     65        } finally {
     66          if(r!=null)
     67            r.Close();         
     68        }
     69      } else {
     70        Appointments = new List<Appointment>();
     71      }
    3772    }
    3873
Note: See TracChangeset for help on using the changeset viewer.