Moved SystemProperties from Lucene.Net.TestFramework to Lucene.Net so the defaulting and security exception handling can be used globally
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/1a4c3b81 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/1a4c3b81 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/1a4c3b81 Branch: refs/heads/master Commit: 1a4c3b81daca3e766229d25f85d95132371ccf9f Parents: 6c6a17b Author: Shad Storhaug <[email protected]> Authored: Mon Jul 31 06:46:54 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Mon Jul 31 06:46:54 2017 +0700 ---------------------------------------------------------------------- .../Lucene.Net.TestFramework.csproj | 6 +- .../Support/SystemProperties.cs | 173 ------------------ src/Lucene.Net/Lucene.Net.csproj | 1 + src/Lucene.Net/Support/SystemProperties.cs | 175 +++++++++++++++++++ 4 files changed, 178 insertions(+), 177 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1a4c3b81/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 533a4ec..ae3c34b 100644 --- a/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj +++ b/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="utf-8"?> +<?xml version="1.0" encoding="utf-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one @@ -19,7 +19,6 @@ under the License. --> - <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <PropertyGroup> @@ -449,7 +448,6 @@ <SubType>Code</SubType> </Compile> <Compile Include="Support\ExceptionSerializationTestBase.cs" /> - <Compile Include="Support\SystemProperties.cs" /> <Compile Include="Support\ApiScanTestBase.cs" /> <Compile Include="Util\Automaton\AutomatonTestUtil.cs"> <SubType>Code</SubType> @@ -530,4 +528,4 @@ <Target Name="AfterBuild"> </Target> --> -</Project> +</Project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1a4c3b81/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 deleted file mode 100644 index fa6fe45..0000000 --- a/src/Lucene.Net.TestFramework/Support/SystemProperties.cs +++ /dev/null @@ -1,173 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * -*/ - -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); - } - } -} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1a4c3b81/src/Lucene.Net/Lucene.Net.csproj ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Lucene.Net.csproj b/src/Lucene.Net/Lucene.Net.csproj index e29c82a..8f8fc4b 100644 --- a/src/Lucene.Net/Lucene.Net.csproj +++ b/src/Lucene.Net/Lucene.Net.csproj @@ -656,6 +656,7 @@ <Compile Include="Support\RectangularArrays.cs" /> <Compile Include="Support\Search\ReferenceContext.cs" /> <Compile Include="Support\Search\ReferenceManagerExtensions.cs" /> + <Compile Include="Support\SystemProperties.cs" /> <Compile Include="Support\Threading\ICompletionService.cs" /> <Compile Include="Support\IO\IDataInput.cs" /> <Compile Include="Support\IO\IDataOutput.cs" /> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1a4c3b81/src/Lucene.Net/Support/SystemProperties.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/SystemProperties.cs b/src/Lucene.Net/Support/SystemProperties.cs new file mode 100644 index 0000000..7661eaa --- /dev/null +++ b/src/Lucene.Net/Support/SystemProperties.cs @@ -0,0 +1,175 @@ +// LUCENENET TODO: API: Replace all references to Environment.GetEnvironmentVariable() with this (safer) class. + +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +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); + } + + internal 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); + } + } +}
