summaryrefslogtreecommitdiff
path: root/Gestor.Infrastructure/Gestor.Infrastructure.Configuration/ConfigurationFileCache.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Gestor.Infrastructure/Gestor.Infrastructure.Configuration/ConfigurationFileCache.cs')
-rw-r--r--Gestor.Infrastructure/Gestor.Infrastructure.Configuration/ConfigurationFileCache.cs133
1 files changed, 133 insertions, 0 deletions
diff --git a/Gestor.Infrastructure/Gestor.Infrastructure.Configuration/ConfigurationFileCache.cs b/Gestor.Infrastructure/Gestor.Infrastructure.Configuration/ConfigurationFileCache.cs
new file mode 100644
index 0000000..4864edc
--- /dev/null
+++ b/Gestor.Infrastructure/Gestor.Infrastructure.Configuration/ConfigurationFileCache.cs
@@ -0,0 +1,133 @@
+using Gestor.Common.Helpers;
+using NHibernate.Cfg;
+using System;
+using System.ComponentModel;
+using System.Data.SqlClient;
+using System.IO;
+using System.Reflection;
+using System.Runtime.Serialization.Formatters.Binary;
+
+namespace Gestor.Infrastructure.Configuration
+{
+ public class ConfigurationFileCache
+ {
+ private readonly string _cacheFile;
+
+ private readonly Assembly _definitionsAssembly;
+
+ private bool IsConfigurationFileValid
+ {
+ get
+ {
+ string location;
+ if (!File.Exists(this._cacheFile))
+ {
+ return false;
+ }
+ FileInfo fileInfo = new FileInfo(this._cacheFile);
+ Assembly assembly = this._definitionsAssembly;
+ if (assembly != null)
+ {
+ location = assembly.Location;
+ }
+ else
+ {
+ location = null;
+ }
+ string str = location;
+ if (str == null)
+ {
+ return false;
+ }
+ FileInfo fileInfo1 = new FileInfo(str);
+ if (fileInfo.Length < (long)5120)
+ {
+ return false;
+ }
+ return fileInfo.LastWriteTime >= fileInfo1.LastWriteTime;
+ }
+ }
+
+ public ConfigurationFileCache(string connectionString)
+ {
+ if (connectionString == null)
+ {
+ return;
+ }
+ this._definitionsAssembly = Assembly.GetExecutingAssembly();
+ SqlConnection sqlConnection = new SqlConnection(connectionString);
+ string str = string.Concat("Data_", sqlConnection.Database, ".cfg");
+ sqlConnection.Dispose();
+ this._cacheFile = string.Concat(AppDomain.CurrentDomain.BaseDirectory, str);
+ }
+
+ public void DeleteCacheFile()
+ {
+ try
+ {
+ if (File.Exists(this._cacheFile))
+ {
+ File.Delete(this._cacheFile);
+ }
+ }
+ catch (Exception exception)
+ {
+ }
+ }
+
+ public NHibernate.Cfg.Configuration LoadConfigurationFromFile()
+ {
+ NHibernate.Cfg.Configuration configuration;
+ if (!this.IsConfigurationFileValid)
+ {
+ return null;
+ }
+ try
+ {
+ using (FileStream fileStream = File.Open(this._cacheFile, FileMode.Open, FileAccess.Read))
+ {
+ using (MemoryStream memoryStream = new MemoryStream(ConfigurationFileCache.ReadFully(fileStream).DecryptBytes()))
+ {
+ configuration = (new BinaryFormatter()).Deserialize(memoryStream) as NHibernate.Cfg.Configuration;
+ }
+ }
+ }
+ catch (Exception exception)
+ {
+ this.DeleteCacheFile();
+ configuration = null;
+ }
+ return configuration;
+ }
+
+ private static byte[] ReadFully(Stream inputStream)
+ {
+ byte[] array;
+ using (MemoryStream memoryStream = new MemoryStream())
+ {
+ inputStream.CopyTo(memoryStream);
+ array = memoryStream.ToArray();
+ }
+ return array;
+ }
+
+ public void SaveConfigurationToFile(NHibernate.Cfg.Configuration configuration)
+ {
+ try
+ {
+ using (FileStream fileStream = File.Open(this._cacheFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
+ {
+ using (MemoryStream memoryStream = new MemoryStream())
+ {
+ (new BinaryFormatter()).Serialize(memoryStream, configuration);
+ byte[] numArray = memoryStream.ToArray().EncryptBytes();
+ fileStream.Write(numArray, 0, (int)numArray.Length);
+ }
+ }
+ }
+ catch (Exception exception)
+ {
+ }
+ }
+ }
+} \ No newline at end of file