/// <summary> /// Initializes a Manager that stores Databases in the given directory. /// </summary> /// <param name="directoryFile"><see cref="System.IO.DirectoryInfo"/> object for initializing the Manager object.</param> /// <param name="options">Option flags for initialization.</param> /// <exception cref="T:System.IO.DirectoryNotFoundException">Thrown when there is an error while accessing or creating the given directory.</exception> public Manager(DirectoryInfo directoryFile, ManagerOptions options) { if (directoryFile == null) { Log.To.Database.E(TAG, "directoryFile cannot be null in ctor, throwing..."); throw new ArgumentNullException("directoryFile"); } this.directoryFile = directoryFile; Options = options ?? DefaultOptions; this.databases = new Dictionary <string, Database>(); this.replications = new List <Replication>(); Shared = new SharedState(); //create the directory, but don't fail if it already exists if (!directoryFile.Exists) { directoryFile.Create(); directoryFile.Refresh(); if (!directoryFile.Exists) { throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.InternalServerError, TAG, "Unable to create directory {0}", directoryFile); } } UpgradeOldDatabaseFiles(directoryFile); #if __IOS__ Foundation.NSString protection; switch (options.FileProtection & Foundation.NSDataWritingOptions.FileProtectionMask) { case Foundation.NSDataWritingOptions.FileProtectionNone: protection = Foundation.NSFileManager.FileProtectionNone; break; case Foundation.NSDataWritingOptions.FileProtectionComplete: protection = Foundation.NSFileManager.FileProtectionComplete; break; case Foundation.NSDataWritingOptions.FileProtectionCompleteUntilFirstUserAuthentication: protection = Foundation.NSFileManager.FileProtectionCompleteUntilFirstUserAuthentication; break; default: protection = Foundation.NSFileManager.FileProtectionCompleteUnlessOpen; break; } var attributes = new Foundation.NSDictionary(Foundation.NSFileManager.FileProtectionKey, protection); Foundation.NSError error; Foundation.NSFileManager.DefaultManager.SetAttributes(attributes, directoryFile.FullName, out error); #endif var scheduler = options.CallbackScheduler; CapturedContext = new TaskFactory(scheduler); Log.To.TaskScheduling.I(TAG, "Callbacks will be scheduled on {0}", scheduler); workExecutor = new TaskFactory(new SingleTaskThreadpoolScheduler()); _networkReachabilityManager = new NetworkReachabilityManager(); _networkReachabilityManager.StartListening(); StorageType = "SQLite"; Log.To.Database.I(TAG, "Created {0}", this); }
static Manager() { illegalCharactersPattern = new Regex(IllegalCharacters); mapper = new ObjectWriter(); DefaultOptions = ManagerOptions.Default; // // Note: Environment.SpecialFolder.LocalApplicationData returns null on Azure (and possibly other Windows Server environments) // and this is only needed by the default constructor or when accessing the SharedInstanced // So, let's only set it only when GetFolderPath returns something and allow the directory to be // manually specified via the ctor that accepts a DirectoryInfo #if __UNITY__ string defaultDirectoryPath = Unity.UnityMainThreadScheduler.PersistentDataPath; #else var defaultDirectoryPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); #endif if (!StringEx.IsNullOrWhiteSpace(defaultDirectoryPath)) { defaultDirectory = new DirectoryInfo(defaultDirectoryPath); } var gitVersion = String.Empty; var branchName = String.Empty; ReadVersion(Assembly.GetExecutingAssembly(), out branchName, out gitVersion); var infoVersion = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute; bool unofficial = infoVersion == null || infoVersion.InformationalVersion.StartsWith("0.0.0"); #if DEBUG var versionNumber = infoVersion == null || infoVersion.InformationalVersion.StartsWith("0.0.0") ? "Unofficial Debug" : infoVersion.InformationalVersion + " Debug"; #else var versionNumber = infoVersion == null || infoVersion.InformationalVersion.StartsWith("0.0.0") ? "Unofficial" : infoVersion.InformationalVersion; #endif if (unofficial) { VersionString = String.Format(".NET {0}/{1} {2} ({3})/{4}", PLATFORM, Platform.Architecture, versionNumber, branchName.Replace('/', '\\'), gitVersion.TrimEnd()); } else { VersionString = String.Format(".NET {0}/{1} {2}/{3}", PLATFORM, Platform.Architecture, versionNumber, gitVersion.TrimEnd()); } Log.To.NoDomain.I(TAG, "Starting Manager version: {0}", VersionString); AppDomain.CurrentDomain.AssemblyLoad += (sender, args) => { if (ReadVersion(args.LoadedAssembly, out branchName, out gitVersion)) { Log.To.NoDomain.I("AssemblyLoad", "Loaded assembly {0} was built at commit {1}", args.LoadedAssembly.FullName, gitVersion); } }; foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { if (ReadVersion(assembly, out branchName, out gitVersion)) { Log.To.NoDomain.I("AssemblyLoad", "Loaded assembly {0} was built at commit {1}", assembly.FullName, gitVersion); } } }