PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Topshelf/Configuration/HostConfigurators/HostConfiguratorImpl.cs

http://github.com/phatboyg/Topshelf
C# | 232 lines | 171 code | 49 blank | 12 comment | 16 complexity | 3e19796902bbee24f9e5113b076094b7 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, Apache-2.0
  1. // Copyright 2007-2013 Chris Patterson, Dru Sellers, Travis Smith, et. al.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  4. // this file except in compliance with the License. You may obtain a copy of the
  5. // License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software distributed
  10. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  11. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  12. // specific language governing permissions and limitations under the License.
  13. namespace Topshelf.HostConfigurators
  14. {
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using Builders;
  19. using CommandLineParser;
  20. using Configurators;
  21. using Logging;
  22. using Options;
  23. using Runtime;
  24. using Runtime.Windows;
  25. public class HostConfiguratorImpl :
  26. HostConfigurator,
  27. Configurator
  28. {
  29. readonly IList<CommandLineConfigurator> _commandLineOptionConfigurators;
  30. readonly IList<HostBuilderConfigurator> _configurators;
  31. readonly WindowsHostSettings _settings;
  32. bool _commandLineApplied;
  33. EnvironmentBuilderFactory _environmentBuilderFactory;
  34. HostBuilderFactory _hostBuilderFactory;
  35. ServiceBuilderFactory _serviceBuilderFactory;
  36. public HostConfiguratorImpl()
  37. {
  38. _configurators = new List<HostBuilderConfigurator>();
  39. _commandLineOptionConfigurators = new List<CommandLineConfigurator>();
  40. _settings = new WindowsHostSettings();
  41. _environmentBuilderFactory = DefaultEnvironmentBuilderFactory;
  42. _hostBuilderFactory = DefaultHostBuilderFactory;
  43. }
  44. public IEnumerable<ValidateResult> Validate()
  45. {
  46. if (_hostBuilderFactory == null)
  47. yield return this.Failure("HostBuilderFactory", "must not be null");
  48. if (_serviceBuilderFactory == null)
  49. yield return this.Failure("ServiceBuilderFactory", "must not be null");
  50. if (_environmentBuilderFactory == null)
  51. yield return this.Failure("EnvironmentBuilderFactory", "must not be null");
  52. if (string.IsNullOrEmpty(_settings.DisplayName) && string.IsNullOrEmpty(_settings.Name))
  53. yield return this.Failure("DisplayName", "must be specified and not empty");
  54. if (string.IsNullOrEmpty(_settings.Name))
  55. yield return this.Failure("Name", "must be specified and not empty");
  56. else
  57. {
  58. var disallowed = new[] {'\t', '\r', '\n', '\\', '/'};
  59. if (_settings.Name.IndexOfAny(disallowed) >= 0)
  60. yield return this.Failure("Name", "must not contain whitespace, '/', or '\\' characters");
  61. }
  62. foreach (ValidateResult result in _configurators.SelectMany(x => x.Validate()))
  63. yield return result;
  64. yield return this.Success("Name", _settings.Name);
  65. if (_settings.Name != _settings.DisplayName)
  66. yield return this.Success("DisplayName", _settings.DisplayName);
  67. if (_settings.Name != _settings.Description)
  68. yield return this.Success("Description", _settings.Description);
  69. if (!string.IsNullOrEmpty(_settings.InstanceName))
  70. yield return this.Success("InstanceName", _settings.InstanceName);
  71. yield return this.Success("ServiceName", _settings.ServiceName);
  72. }
  73. public void SetDisplayName(string name)
  74. {
  75. _settings.DisplayName = name;
  76. }
  77. public void SetServiceName(string name)
  78. {
  79. _settings.Name = name;
  80. }
  81. public void SetDescription(string description)
  82. {
  83. _settings.Description = description;
  84. }
  85. public void SetInstanceName(string instanceName)
  86. {
  87. _settings.InstanceName = instanceName;
  88. }
  89. public void SetStartTimeout(TimeSpan startTimeOut)
  90. {
  91. _settings.StartTimeOut = startTimeOut;
  92. }
  93. public void SetStopTimeout(TimeSpan stopTimeOut)
  94. {
  95. _settings.StopTimeOut = stopTimeOut;
  96. }
  97. public void EnablePauseAndContinue()
  98. {
  99. _settings.CanPauseAndContinue = true;
  100. }
  101. public void EnableShutdown()
  102. {
  103. _settings.CanShutdown = true;
  104. }
  105. public void EnableSessionChanged()
  106. {
  107. _settings.CanSessionChanged = true;
  108. }
  109. public void UseHostBuilder(HostBuilderFactory hostBuilderFactory)
  110. {
  111. _hostBuilderFactory = hostBuilderFactory;
  112. }
  113. public void UseServiceBuilder(ServiceBuilderFactory serviceBuilderFactory)
  114. {
  115. _serviceBuilderFactory = serviceBuilderFactory;
  116. }
  117. public void UseEnvironmentBuilder(EnvironmentBuilderFactory environmentBuilderFactory)
  118. {
  119. _environmentBuilderFactory = environmentBuilderFactory;
  120. }
  121. public void AddConfigurator(HostBuilderConfigurator configurator)
  122. {
  123. _configurators.Add(configurator);
  124. }
  125. public void ApplyCommandLine()
  126. {
  127. if (_commandLineApplied)
  128. return;
  129. IEnumerable<Option> options = CommandLine.Parse<Option>(ConfigureCommandLineParser);
  130. ApplyCommandLineOptions(options);
  131. }
  132. public void ApplyCommandLine(string commandLine)
  133. {
  134. IEnumerable<Option> options = CommandLine.Parse<Option>(ConfigureCommandLineParser, commandLine);
  135. ApplyCommandLineOptions(options);
  136. _commandLineApplied = true;
  137. }
  138. public void AddCommandLineSwitch(string name, Action<bool> callback)
  139. {
  140. var configurator = new CommandLineSwitchConfigurator(name, callback);
  141. _commandLineOptionConfigurators.Add(configurator);
  142. }
  143. public void AddCommandLineDefinition(string name, Action<string> callback)
  144. {
  145. var configurator = new CommandLineDefinitionConfigurator(name, callback);
  146. _commandLineOptionConfigurators.Add(configurator);
  147. }
  148. public Host CreateHost()
  149. {
  150. Type type = typeof(HostFactory);
  151. HostLogger.Get<HostConfiguratorImpl>()
  152. .InfoFormat("{0} v{1}, .NET Framework v{2}", type.Namespace, type.Assembly.GetName().Version,
  153. Environment.Version);
  154. EnvironmentBuilder environmentBuilder = _environmentBuilderFactory(this);
  155. HostEnvironment environment = environmentBuilder.Build();
  156. ServiceBuilder serviceBuilder = _serviceBuilderFactory(_settings);
  157. HostBuilder builder = _hostBuilderFactory(environment, _settings);
  158. foreach (HostBuilderConfigurator configurator in _configurators)
  159. builder = configurator.Configure(builder);
  160. return builder.Build(serviceBuilder);
  161. }
  162. void ApplyCommandLineOptions(IEnumerable<Option> options)
  163. {
  164. foreach (Option option in options)
  165. option.ApplyTo(this);
  166. }
  167. void ConfigureCommandLineParser(ICommandLineElementParser<Option> parser)
  168. {
  169. CommandLineParserOptions.AddTopshelfOptions(parser);
  170. foreach (CommandLineConfigurator optionConfigurator in _commandLineOptionConfigurators)
  171. optionConfigurator.Configure(parser);
  172. CommandLineParserOptions.AddUnknownOptions(parser);
  173. }
  174. static HostBuilder DefaultHostBuilderFactory(HostEnvironment environment, HostSettings settings)
  175. {
  176. return new RunBuilder(environment, settings);
  177. }
  178. static EnvironmentBuilder DefaultEnvironmentBuilderFactory(HostConfigurator configurator)
  179. {
  180. return new WindowsHostEnvironmentBuilder(configurator);
  181. }
  182. }
  183. }