Moved SystemProperties back from Lucene.Net.Core.Support to Lucene.Net.TestFramework. For any application settings, we want the host application to provide them, not hide them in environment variables.
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/9d0c0185 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/9d0c0185 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/9d0c0185 Branch: refs/heads/api-work Commit: 9d0c0185fcc883ff84f13658f923656cef5b50c3 Parents: 11f987c Author: Shad Storhaug <[email protected]> Authored: Sat Mar 18 04:01:33 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Sat Mar 18 04:37:17 2017 +0700 ---------------------------------------------------------------------- src/Lucene.Net.Core/Lucene.Net.csproj | 1 - src/Lucene.Net.Core/Support/SystemProperties.cs | 152 ------------------- .../Lucene.Net.TestFramework.csproj | 3 +- .../Support/SystemProperties.cs | 152 +++++++++++++++++++ 4 files changed, 154 insertions(+), 154 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/9d0c0185/src/Lucene.Net.Core/Lucene.Net.csproj ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Lucene.Net.csproj b/src/Lucene.Net.Core/Lucene.Net.csproj index 673a129..dca7a21 100644 --- a/src/Lucene.Net.Core/Lucene.Net.csproj +++ b/src/Lucene.Net.Core/Lucene.Net.csproj @@ -678,7 +678,6 @@ <Compile Include="Support\StringCharSequenceWrapper.cs" /> <Compile Include="Support\StringExtensions.cs" /> <Compile Include="Support\StringTokenizer.cs" /> - <Compile Include="Support\SystemProperties.cs" /> <Compile Include="Support\TaskSchedulerCompletionService.cs" /> <Compile Include="Support\ThreadFactory.cs" /> <Compile Include="Support\TimeHelper.cs" /> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/9d0c0185/src/Lucene.Net.Core/Support/SystemProperties.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Support/SystemProperties.cs b/src/Lucene.Net.Core/Support/SystemProperties.cs deleted file mode 100644 index 5746e27..0000000 --- a/src/Lucene.Net.Core/Support/SystemProperties.cs +++ /dev/null @@ -1,152 +0,0 @@ -using System; -using System.Security; - -namespace Lucene.Net.Support -{ - /// <summary> - /// Helper for environment variables. This class helps to convert the environment - /// variables to int or bool data types and also silently handles read permission - /// errors. - /// <para/> - /// For instructions how to set environment variables for your OS, see - /// <a href="https://www.schrodinger.com/kb/1842">https://www.schrodinger.com/kb/1842</a>. - /// <para/> - /// Note that if you want to load any of these settings for your application from a - /// configuration file, it is recommended your application load them at startup and - /// call <see cref="SystemProperties.SetProperty(string, string)"/> to set them. - /// <para/> - /// Set the environment variable <c>lucene.ignoreSecurityExceptions</c> to <c>false</c> - /// to change the read behavior of these methods to throw the underlying exception - /// instead of returning the default value. - /// </summary> - public static class SystemProperties - { - /// <summary> - /// Retrieves the value of an environment variable from the current process. - /// </summary> - /// <param name="key">The name of the environment variable.</param> - /// <returns>The environment variable value.</returns> - public static string GetProperty(string key) - { - return GetProperty(key, null); - } - - /// <summary> - /// Retrieves the value of an environment variable from the current process, - /// with a default value if it doens't exist or the caller doesn't have - /// permission to read the value. - /// </summary> - /// <param name="key">The name of the environment variable.</param> - /// <param name="defaultValue">The value to use if the environment variable does not exist - /// or the caller doesn't have permission to read the value.</param> - /// <returns>The environment variable value.</returns> - public static string GetProperty(string key, string defaultValue) - { - return GetProperty<string>(key, defaultValue, - (str) => - { - return str; - } - ); - } - - /// <summary> - /// Retrieves the value of an environment variable from the current process - /// as <see cref="bool"/>. If the value cannot be cast to <see cref="bool"/>, returns <c>false</c>. - /// </summary> - /// <param name="key">The name of the environment variable.</param> - /// <returns>The environment variable value.</returns> - public static bool GetPropertyAsBoolean(string key) - { - return GetPropertyAsBoolean(key, false); - } - - /// <summary> - /// Retrieves the value of an environment variable from the current process as <see cref="bool"/>, - /// with a default value if it doens't exist, the caller doesn't have permission to read the value, - /// or the value cannot be cast to a <see cref="bool"/>. - /// </summary> - /// <param name="key">The name of the environment variable.</param> - /// <param name="defaultValue">The value to use if the environment variable does not exist, - /// the caller doesn't have permission to read the value, or the value cannot be cast to <see cref="bool"/>.</param> - /// <returns>The environment variable value.</returns> - public static bool GetPropertyAsBoolean(string key, bool defaultValue) - { - return GetProperty<bool>(key, defaultValue, - (str) => - { - bool value; - return bool.TryParse(str, out value) ? value : defaultValue; - } - ); - } - - /// <summary> - /// Retrieves the value of an environment variable from the current process - /// as <see cref="int"/>. If the value cannot be cast to <see cref="int"/>, returns <c>0</c>. - /// </summary> - /// <param name="key">The name of the environment variable.</param> - /// <returns>The environment variable value.</returns> - public static int GetPropertyAsInt32(string key) - { - return GetPropertyAsInt32(key, 0); - } - - /// <summary> - /// Retrieves the value of an environment variable from the current process as <see cref="int"/>, - /// with a default value if it doens't exist, the caller doesn't have permission to read the value, - /// or the value cannot be cast to a <see cref="int"/>. - /// </summary> - /// <param name="key">The name of the environment variable.</param> - /// <param name="defaultValue">The value to use if the environment variable does not exist, - /// the caller doesn't have permission to read the value, or the value cannot be cast to <see cref="int"/>.</param> - /// <returns>The environment variable value.</returns> - public static int GetPropertyAsInt32(string key, int defaultValue) - { - return GetProperty<int>(key, defaultValue, - (str) => - { - int value; - return int.TryParse(str, out value) ? value : defaultValue; - } - ); - } - - private static T GetProperty<T>(string key, T defaultValue, Func<string, T> conversionFunction) - { - string setting; - if (ignoreSecurityExceptions) - { - try - { - setting = Environment.GetEnvironmentVariable(key); - } - catch (SecurityException) - { - setting = null; - } - } - else - { - setting = Environment.GetEnvironmentVariable(key); - } - - return string.IsNullOrEmpty(setting) - ? defaultValue - : conversionFunction(setting); - } - - private static bool ignoreSecurityExceptions = GetPropertyAsBoolean("lucene.ignoreSecurityExceptions", true); - - /// <summary> - /// Creates, modifies, or deletes an environment variable stored in the current process. - /// </summary> - /// <param name="key">The name of the environment variable.</param> - /// <param name="value">The new environment variable value.</param> - /// <exception cref="SecurityException">The caller does not have the required permission to perform this operation.</exception> - public static void SetProperty(string key, string value) - { - Environment.SetEnvironmentVariable(key, value); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/9d0c0185/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj b/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj index 6d46436..0073b98 100644 --- a/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj +++ b/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj @@ -21,7 +21,7 @@ <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> - <DefineConstants>TRACE;FEATURE_SERIALIZABLE</DefineConstants> + <DefineConstants>TRACE;DEBUG</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <Prefer32Bit>false</Prefer32Bit> @@ -429,6 +429,7 @@ <Compile Include="Support\RandomizedTest.cs"> <SubType>Code</SubType> </Compile> + <Compile Include="Support\SystemProperties.cs" /> <Compile Include="Util\ApiScanTestBase.cs" /> <Compile Include="Util\Automaton\AutomatonTestUtil.cs"> <SubType>Code</SubType> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/9d0c0185/src/Lucene.Net.TestFramework/Support/SystemProperties.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Support/SystemProperties.cs b/src/Lucene.Net.TestFramework/Support/SystemProperties.cs new file mode 100644 index 0000000..d843a69 --- /dev/null +++ b/src/Lucene.Net.TestFramework/Support/SystemProperties.cs @@ -0,0 +1,152 @@ +using System; +using System.Security; + +namespace Lucene.Net.Support +{ + /// <summary> + /// Helper for environment variables. This class helps to convert the environment + /// variables to int or bool data types and also silently handles read permission + /// errors. + /// <para/> + /// For instructions how to set environment variables for your OS, see + /// <a href="https://www.schrodinger.com/kb/1842">https://www.schrodinger.com/kb/1842</a>. + /// <para/> + /// Note that if you want to load any of these settings for your application from a + /// configuration file, it is recommended your application load them at startup and + /// call <see cref="SystemProperties.SetProperty(string, string)"/> to set them. + /// <para/> + /// Set the environment variable <c>lucene.ignoreSecurityExceptions</c> to <c>false</c> + /// to change the read behavior of these methods to throw the underlying exception + /// instead of returning the default value. + /// </summary> + public static class SystemProperties + { + /// <summary> + /// Retrieves the value of an environment variable from the current process. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <returns>The environment variable value.</returns> + public static string GetProperty(string key) + { + return GetProperty(key, null); + } + + /// <summary> + /// Retrieves the value of an environment variable from the current process, + /// with a default value if it doens't exist or the caller doesn't have + /// permission to read the value. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <param name="defaultValue">The value to use if the environment variable does not exist + /// or the caller doesn't have permission to read the value.</param> + /// <returns>The environment variable value.</returns> + public static string GetProperty(string key, string defaultValue) + { + return GetProperty<string>(key, defaultValue, + (str) => + { + return str; + } + ); + } + + /// <summary> + /// Retrieves the value of an environment variable from the current process + /// as <see cref="bool"/>. If the value cannot be cast to <see cref="bool"/>, returns <c>false</c>. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <returns>The environment variable value.</returns> + public static bool GetPropertyAsBoolean(string key) + { + return GetPropertyAsBoolean(key, false); + } + + /// <summary> + /// Retrieves the value of an environment variable from the current process as <see cref="bool"/>, + /// with a default value if it doens't exist, the caller doesn't have permission to read the value, + /// or the value cannot be cast to a <see cref="bool"/>. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <param name="defaultValue">The value to use if the environment variable does not exist, + /// the caller doesn't have permission to read the value, or the value cannot be cast to <see cref="bool"/>.</param> + /// <returns>The environment variable value.</returns> + public static bool GetPropertyAsBoolean(string key, bool defaultValue) + { + return GetProperty<bool>(key, defaultValue, + (str) => + { + bool value; + return bool.TryParse(str, out value) ? value : defaultValue; + } + ); + } + + /// <summary> + /// Retrieves the value of an environment variable from the current process + /// as <see cref="int"/>. If the value cannot be cast to <see cref="int"/>, returns <c>0</c>. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <returns>The environment variable value.</returns> + public static int GetPropertyAsInt32(string key) + { + return GetPropertyAsInt32(key, 0); + } + + /// <summary> + /// Retrieves the value of an environment variable from the current process as <see cref="int"/>, + /// with a default value if it doens't exist, the caller doesn't have permission to read the value, + /// or the value cannot be cast to a <see cref="int"/>. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <param name="defaultValue">The value to use if the environment variable does not exist, + /// the caller doesn't have permission to read the value, or the value cannot be cast to <see cref="int"/>.</param> + /// <returns>The environment variable value.</returns> + public static int GetPropertyAsInt32(string key, int defaultValue) + { + return GetProperty<int>(key, defaultValue, + (str) => + { + int value; + return int.TryParse(str, out value) ? value : defaultValue; + } + ); + } + + private static T GetProperty<T>(string key, T defaultValue, Func<string, T> conversionFunction) + { + string setting; + if (ignoreSecurityExceptions) + { + try + { + setting = Environment.GetEnvironmentVariable(key); + } + catch (SecurityException) + { + setting = null; + } + } + else + { + setting = Environment.GetEnvironmentVariable(key); + } + + return string.IsNullOrEmpty(setting) + ? defaultValue + : conversionFunction(setting); + } + + private static bool ignoreSecurityExceptions = GetPropertyAsBoolean("lucene.ignoreSecurityExceptions", false); + + /// <summary> + /// Creates, modifies, or deletes an environment variable stored in the current process. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <param name="value">The new environment variable value.</param> + /// <exception cref="SecurityException">The caller does not have the required permission to perform this operation.</exception> + public static void SetProperty(string key, string value) + { + Environment.SetEnvironmentVariable(key, value); + } + } +} \ No newline at end of file
