/// <summary> /// Returns a stream to the requested file. Don't forget to close the file once you're finished! /// See documentation for further information. /// </summary> /// <remarks>If the file does not exist in the local cache, it is fetched and cached. /// If the file does exist, its checksum is compared with the one from the server-side file /// and may be cached again and returned.</remarks> /// <param name="path">The path to the file to return. Must be a relative path and not be null or empty.</param> /// <returns>A <see cref="Stream"/> containing the file contents.</returns> /// <exception cref="System.IO.IOException">The file was not found. /// -or- An error occurred retrieving the file from the local cache directory. /// -or- An error occurred retrieving the file from the server.</exception> public Stream GetFileFromPath(string path) { AssertNotDisposed(); Assertions.AssertNotEmpty(path, "path"); try { using (var service = ServiceFactory.GetServiceWrapper <IFileTransferService>()) { if (!_fileCache.IsCached(path)) { GetFileAndStoreInCache(path, service.Instance); } else { string sourceChecksum = service.Instance.GetFileChecksum(path); string cacheFileChecksum = _fileCache.GetChecksum(path); if (!string.Equals(sourceChecksum, cacheFileChecksum)) { GetFileAndStoreInCache(path, service.Instance); } } return(_fileCache.OpenFile(path)); } } catch (Exception ex) { Logger.Instance.LogException(this, ex); throw new IOException(string.Format(Properties.Resources.GetFileFromPathRetrievalError, path), ex); } }
void ISettingsServiceInternal.SetSetting(string identifier, string name, SettingItem value) { try { Assertions.AssertNotEmpty(identifier, "identifier"); Assertions.AssertNotEmpty(name, "name"); IEnumerable <SettingKey> savedSettings = null; lock (SyncRoot) { SettingKey key = SettingKey.Create(identifier, name); List <KeyValuePair <SettingKey, SettingItem> > settings = new List <KeyValuePair <SettingKey, SettingItem> >(); settings.Add(new KeyValuePair <SettingKey, SettingItem>(key, value)); savedSettings = SaveSettings(settings); } if (savedSettings != null && savedSettings.Any()) { OnSettingChanged(new SettingChangedEventArgs(savedSettings)); } } catch (Exception ex) { throw AlarmWorkflowFaultDetails.CreateFault(ex); } }
public AdvancedInfoEntry(string key, object value) { Assertions.AssertNotEmpty(key, "key"); Key = key; Value = value; }
/// <summary> /// Creates the database context for a connection to the specified server, /// by using a custom EDMX path and retrieving the other options from the backend configuration. /// </summary> /// <typeparam name="T">The ObjectContext type to create.</typeparam> /// <param name="edmxPath">The path to the .edmx-file. This path must be relative to the project. /// See <see cref="ContextCreationOptions.EdmxPath"/> for further information.</param> /// <returns>The created database context (derived from <see cref="ObjectContext"/>).</returns> public static T CreateContext <T>(string edmxPath) where T : ObjectContext { Assertions.AssertNotEmpty(edmxPath, "edmxPath"); ContextCreationOptions options = ContextCreationOptions.CreateFromSettings(); options.EdmxPath = edmxPath; return(CreateContext <T>(options)); }
/// <summary> /// Creates a new <see cref="SettingKey"/> instance. /// </summary> /// <param name="identifier">The identifier of the setting.</param> /// <param name="name">The name of the setting.</param> /// <returns>A new <see cref="SettingKey"/> instance.</returns> public static SettingKey Create(string identifier, string name) { Assertions.AssertNotEmpty(identifier, "identifier"); Assertions.AssertNotEmpty(name, "name"); return(new SettingKey() { Identifier = identifier, Name = name }); }
/// <summary> /// Traverses the property graph of an object and returns the value of a certain property. /// </summary> /// <param name="graph">The object graph to use. Must not be null.</param> /// <param name="expression">The expression of the property to set. Must not be empty.</param> /// <returns>The value of the property.</returns> /// <exception cref="T:MissingFieldException">A certain property in the expression was not found.</exception> /// <exception cref="T:ExpressionNotSupportedException">The expression was not supported.</exception> public static object GetValueFromExpression(object graph, string expression) { Assertions.AssertNotNull(graph, "graph"); Assertions.AssertNotEmpty(expression, "expression"); PropertyInfo property = null; object target = null; GetPropertyFromExpression(graph, expression, true, out property, out target); return(property.GetValue(target, null)); }
/// <summary> /// Initializes a new instance of the <see cref="SettingItem"/> class. /// </summary> /// <param name="identifier">The identifier that this setting is associated with.</param> /// <param name="name">The name of this setting.</param> /// <param name="defaultValue">The default value of this setting.</param> /// <param name="type">The type of the setting.</param> internal SettingItem(string identifier, string name, string defaultValue, Type type) : this() { Assertions.AssertNotEmpty(identifier, "identifier"); Assertions.AssertNotEmpty(name, "name"); Assertions.AssertNotNull(type, "type"); this.Identifier = identifier; this.Name = name; this.SettingType = type; _defaultValueSerialized = defaultValue; }
/// <summary> /// Traverses the property graph of an object and sets a certain property to a given value. /// </summary> /// <param name="graph">The object graph to use. Must not be null.</param> /// <param name="expression">The expression of the property to set. Must not be empty.</param> /// <param name="value">The value to set.</param> /// <exception cref="T:MissingFieldException">A certain property in the expression was not found.</exception> /// <exception cref="T:ExpressionNotSupportedException">The expression was not supported.</exception> public static void SetValueFromExpression(object graph, string expression, object value) { Assertions.AssertNotNull(graph, "graph"); Assertions.AssertNotEmpty(expression, "expression"); PropertyInfo property = null; object target = null; GetPropertyFromExpression(graph, expression, true, out property, out target); property.SetValue(target, value, null); }
/// <summary> /// Initializes the logger. /// </summary> /// <param name="logName">The name of the log. This will be used as the folder name of this log.</param> /// <exception cref="System.InvalidOperationException">This instance is already initialized.</exception> public void Initialize(string logName) { Assertions.AssertNotEmpty(logName, "logName"); if (_log != null) { throw new InvalidOperationException("This instance is already initialized!"); } Log4netConfigurator.Configure(logName); _log = LogManager.GetLogger(logName); }
void IFileCache.StoreFile(string path, Stream content) { Assertions.AssertNotEmpty(path, "path"); Assertions.AssertNotNull(content, "content"); string absPath = GetAbsolutePath(GetStringHash(path)); EnsureCacheDirectoryExists(); using (FileStream destination = new FileStream(absPath, FileMode.Create, FileAccess.Write)) { content.CopyTo(destination); } }
/// <summary> /// Attaches an event handler to the <see cref="E:AppDomain.CurrentDomain.UnhandledException"/> event, /// which automatically creates error reports for each escalated exception. /// </summary> /// <param name="componentName">The default component name to use for each new error report. Must not be null or empty.</param> public static void RegisterAppDomainUnhandledExceptionListener(string componentName) { Assertions.AssertNotEmpty(componentName, "componentName"); AppDomain.CurrentDomain.UnhandledException += (o, e) => { Exception exception = (Exception)e.ExceptionObject; ErrorReport report = new ErrorReport(exception); report.SourceComponentName = componentName; report.IsTerminating = e.IsTerminating; CreateErrorReportInternal(report); }; }
SettingItem ISettingsServiceInternal.GetSetting(string identifier, string name) { Assertions.AssertNotEmpty(identifier, "identifier"); Assertions.AssertNotEmpty(name, "name"); SettingItem item = _settings.GetSetting(identifier, name); lock (SyncRoot) { ApplySettingValue(identifier, name, item); } return(item); }
private static void AssertPathIsNotRootedAndExists(string path) { Assertions.AssertNotEmpty(path, "path"); if (Path.IsPathRooted(path)) { throw new IOException(Properties.Resources.FilePathIsRootError); } string localPath = GetLocalPathForRelative(path); if (!File.Exists(localPath)) { throw new FileNotFoundException(string.Format(Properties.Resources.FilePathNotFoundError, path)); } }
/// <summary> /// Initializes a new instance of the <see cref="ConfigurableServiceLocator"/> class with a custom configuration file path. /// </summary> /// <param name="configurationFile">The name of the configuration file. If this file path is relative, it is assumed relative to the working directory.</param> /// <exception cref="AlarmWorkflow.Shared.Core.AssertionFailedException">The parameters were invalid.</exception> /// <exception cref="System.IO.FileNotFoundException">The configuration file was not found.</exception> /// <exception cref="System.Xml.Schema.XmlSchemaValidationException">The configuration file was malformed.</exception> public ConfigurableServiceLocator(string configurationFile) { Assertions.AssertNotEmpty(configurationFile, "configurationFile"); if (!Path.IsPathRooted(configurationFile)) { configurationFile = Path.Combine(Utilities.GetWorkingDirectory(), configurationFile); } if (!File.Exists(configurationFile)) { throw new FileNotFoundException(Properties.Resources.ServiceLocationFileNotPresent, configurationFile); } _serviceLocationCache = new ReadOnlyCollection <IBackendServiceLocation>(BuildServiceLocationCache(configurationFile).ToList()); }
/// <summary> /// Traverses the property graph of an object and sets a certain property to a given value. /// </summary> /// <param name="graph">The object graph to use. Must not be null.</param> /// <param name="expression">The expression of the property to set. Must not be empty.</param> /// <param name="value">The value to set.</param> /// <returns>Whether or not the value could be set. If this returns <c>false</c>, then the expression led to a nonexistent property.</returns> public static bool TrySetValueFromExpression(object graph, string expression, object value) { Assertions.AssertNotNull(graph, "graph"); Assertions.AssertNotEmpty(expression, "expression"); PropertyInfo property = null; object target = null; bool success = GetPropertyFromExpression(graph, expression, false, out property, out target); if (!success) { value = null; return(false); } property.SetValue(target, value, null); return(true); }
SettingItem ISettingsServiceInternal.GetSetting(string identifier, string name) { try { Assertions.AssertNotEmpty(identifier, "identifier"); Assertions.AssertNotEmpty(name, "name"); SettingItem item = _settings.GetSetting(identifier, name); lock (SyncRoot) { ApplySettingValue(identifier, name, item); } return(item); } catch (Exception ex) { throw AlarmWorkflowFaultDetails.CreateFault(ex); } }
void ISettingsServiceInternal.SetSetting(string identifier, string name, SettingItem value) { Assertions.AssertNotEmpty(identifier, "identifier"); Assertions.AssertNotEmpty(name, "name"); IEnumerable <SettingKey> savedSettings = null; lock (SyncRoot) { SettingKey key = SettingKey.Create(identifier, name); Dictionary <SettingKey, SettingItem> settings = new Dictionary <SettingKey, SettingItem>(); settings.Add(key, value); savedSettings = SaveSettings(settings); } if (savedSettings != null && savedSettings.Any()) { OnSettingChanged(new SettingChangedEventArgs(savedSettings)); } }
/// <summary> /// Parses a string that tells how to format an object using macros within curly braces. /// </summary> /// <param name="graph">The object graph to use. Must not be null.</param> /// <param name="format">The format string, using the property values in curly braces (expressions), like {Property}. Must not be empty.</param> /// <returns>The formatted string.</returns> public virtual string ToString(TInput graph, string format) { Assertions.AssertNotNull(graph, "graph"); Assertions.AssertNotEmpty(format, "format"); StringBuilder sb = new StringBuilder(format); if (Options.HasFlag(ObjectFormatterOptions.RemoveNewlines)) { sb.Replace("\n", " "); sb.Replace(Environment.NewLine, " "); } foreach (string macro in GetMacros(format)) { string expression = macro.Substring(1, macro.Length - 2); string value = ProcessMacro(graph, macro, expression); sb.Replace(macro, value); } return(sb.ToString()); }
/// <summary> /// Returns the full path to a file that lies within the deployed "TestData" directory. /// </summary> /// <param name="relativePath">The relative file path of the file within the deployed "TestData" directory. Must not be null or empty.</param> /// <returns>The full path to a file that lies within the deployed "TestData" directory.</returns> protected string GetTestDataFilePath(string relativePath) { Assertions.AssertNotEmpty(relativePath, "relativePath"); return(Path.Combine(TestContext.DeploymentDirectory, relativePath)); }
private string GetStringHash(string input) { Assertions.AssertNotEmpty(input, "input"); return(Utilities.ComputeSHA1(Encoding.UTF8.GetBytes(input))); }
private string GetAbsolutePath(string path) { Assertions.AssertNotEmpty(path, "path"); return(Path.Combine(_absoluteCacheDirectory.FullName, path)); }