Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1797


Ignore:
Timestamp:
05/14/09 13:49:36 (16 years ago)
Author:
epitzer
Message:

Optionally include necessary assemblies in serial data. (#621)

Location:
trunk/sources/HeuristicLab.Persistence/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs

    r1795 r1797  
    88using HeuristicLab.Persistence.Default.Decomposers.Storable;
    99using System.Text;
     10using System.Reflection;
     11using System.IO;
    1012
    1113namespace HeuristicLab.Persistence.Core {
     
    3537    public List<TypeMapping> TypeCache {
    3638      get {
    37         List<TypeMapping> result = new List<TypeMapping>();
    38         foreach (var pair in typeCache) {
    39           string serializer = null;
    40           IFormatter f = configuration.GetFormatter(pair.Key);
    41           if (f != null) {
    42             serializer = f.GetType().AssemblyQualifiedName;
    43           } else {
    44             IDecomposer d = configuration.GetDecomposer(pair.Key);
    45             serializer = d.GetType().AssemblyQualifiedName;
    46           }
    47           result.Add(new TypeMapping(pair.Value, pair.Key.AssemblyQualifiedName, serializer));
     39        BuildTypeCache();
     40        return externalTypeCache;
     41      }
     42    }
     43
     44    public List<string> RequiredFiles {
     45      get {
     46        BuildTypeCache();
     47        return requiredFiles;
     48      }
     49    }
     50
     51    private List<TypeMapping> externalTypeCache;
     52    private List<string> requiredFiles;
     53    private void BuildTypeCache() {     
     54      externalTypeCache = new List<TypeMapping>();
     55      Dictionary<Assembly, bool> assemblies = new Dictionary<Assembly, bool>();
     56      foreach (var pair in typeCache) {
     57        string serializer = null;
     58        IFormatter f = configuration.GetFormatter(pair.Key);
     59        if (f != null) {
     60          serializer = f.GetType().AssemblyQualifiedName;
     61          assemblies[f.GetType().Assembly] = true;
     62        } else {
     63          IDecomposer d = configuration.GetDecomposer(pair.Key);
     64          serializer = d.GetType().AssemblyQualifiedName;
     65          assemblies[d.GetType().Assembly] = true;
    4866        }
    49         return result;
     67        externalTypeCache.Add(new TypeMapping(pair.Value, pair.Key.AssemblyQualifiedName, serializer));
     68        assemblies[pair.Key.Assembly] = true;
    5069      }
     70      Dictionary<string, bool> files = new Dictionary<string, bool>();
     71      foreach (Assembly a in assemblies.Keys) {
     72        files[a.CodeBase] = true;
     73      }
     74      requiredFiles = new List<string>(files.Keys);
    5175    }
    5276
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs

    r1733 r1797  
    161161
    162162    public static void Serialize(object obj, string filename, Configuration config) {
     163      Serialize(obj, filename, config, false);
     164    }
     165
     166    public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies) {
    163167      string tempfile = Path.GetTempFileName();
    164168      try {
    165         Serialize(obj, File.Create(tempfile), config);
     169        Serialize(obj, File.Create(tempfile), config, includeAssemblies);
    166170        File.Copy(tempfile, filename, true);
    167171        File.Delete(tempfile);
     
    172176    }
    173177
     178
    174179    public static void Serialize(object obj, Stream stream, Configuration config) {
     180      Serialize(obj, stream, config, false);
     181    }
     182
     183    public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies) {
    175184      try {
    176185        Serializer serializer = new Serializer(obj, config);
     
    193202          }
    194203          writer.Flush();
     204          if (includeAssemblies) {
     205            foreach (string name in serializer.RequiredFiles) {
     206              Uri uri = new Uri(name);
     207              if (!uri.IsFile) {
     208                Logger.Warn("cannot read non-local files");
     209                continue;
     210              }
     211              zipStream.PutNextEntry(new ZipEntry(Path.GetFileName(uri.PathAndQuery)));
     212              FileStream reader = File.OpenRead(uri.PathAndQuery);
     213              byte[] buffer = new byte[1024 * 1024];
     214              while (true) {
     215                int bytesRead = reader.Read(buffer, 0, 1024 * 1024);
     216                if (bytesRead == 0)
     217                  break;
     218                zipStream.Write(buffer, 0, bytesRead);
     219              }
     220              writer.Flush();
     221            }
     222          }
    195223        }
    196224      } catch (PersistenceException e) {
Note: See TracChangeset for help on using the changeset viewer.