Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 0DF8E200C50 for ; Sat, 8 Apr 2017 17:48:23 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 0AE48160B88; Sat, 8 Apr 2017 15:48:23 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 1160E160B96 for ; Sat, 8 Apr 2017 17:48:21 +0200 (CEST) Received: (qmail 20356 invoked by uid 500); 8 Apr 2017 15:48:21 -0000 Mailing-List: contact commits-help@lucenenet.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: lucene-net-dev@lucenenet.apache.org Delivered-To: mailing list commits@lucenenet.apache.org Received: (qmail 20298 invoked by uid 99); 8 Apr 2017 15:48:21 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 08 Apr 2017 15:48:21 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 130DCE0287; Sat, 8 Apr 2017 15:48:21 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: nightowl888@apache.org To: commits@lucenenet.apache.org Date: Sat, 08 Apr 2017 15:48:24 -0000 Message-Id: <6d3cc31232434f839408d8fc268607f3@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [4/8] lucenenet git commit: SWEEP: Removed FEATURE_EMBEDDED_RESOURCE and changed all code that accesses embedded resources to use a new set of extension methods that aggressively search the available resources for a string match or partial match. Since t archived-at: Sat, 08 Apr 2017 15:48:23 -0000 http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net.Tests.Suggest/Suggest/LookupBenchmarkTest.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests.Suggest/Suggest/LookupBenchmarkTest.cs b/src/Lucene.Net.Tests.Suggest/Suggest/LookupBenchmarkTest.cs index 58cf223..f3d84d1 100644 --- a/src/Lucene.Net.Tests.Suggest/Suggest/LookupBenchmarkTest.cs +++ b/src/Lucene.Net.Tests.Suggest/Suggest/LookupBenchmarkTest.cs @@ -90,7 +90,8 @@ namespace Lucene.Net.Search.Suggest List input = new List(); //URL resource = LookupBenchmarkTest.class.getResource("Top50KWiki.utf8"); - var resource = typeof(LookupBenchmarkTest).GetTypeInfo().Assembly.GetManifestResourceStream("Lucene.Net.Tests.Suggest.Suggest.Top50KWiki.utf8"); + //var resource = typeof(LookupBenchmarkTest).GetTypeInfo().Assembly.GetManifestResourceStream("Lucene.Net.Tests.Suggest.Suggest.Top50KWiki.utf8"); + var resource = typeof(LookupBenchmarkTest).GetTypeInfo().Assembly.FindAndGetManifestResourceStream(typeof(LookupBenchmarkTest), "Top50KWiki.utf8"); Debug.Assert(resource != null, "Resource missing: Top50KWiki.utf8"); string line = null; http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs index d4602e0..77a6401 100644 --- a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs +++ b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility.cs @@ -158,9 +158,6 @@ namespace Lucene.Net.Index } */ - // LUCENENET specific to load resources for this type - internal const string CURRENT_RESOURCE_DIRECTORY = "Lucene.Net.Tests.Index."; - internal static readonly string[] OldNames = new string[] { "40.cfs", "40.nocfs", "41.cfs", "41.nocfs", "42.cfs", "42.nocfs", "45.cfs", "45.nocfs", "461.cfs", "461.nocfs" @@ -213,7 +210,7 @@ namespace Lucene.Net.Index foreach (string name in names) { DirectoryInfo dir = CreateTempDir(name); - using (Stream zipFileStream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + "index." + name + ".zip")) + using (Stream zipFileStream = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), "index." + name + ".zip")) { TestUtil.Unzip(zipFileStream, dir); } @@ -244,7 +241,7 @@ namespace Lucene.Net.Index Console.WriteLine("TEST: index " + UnsupportedNames[i]); } DirectoryInfo oldIndxeDir = CreateTempDir(UnsupportedNames[i]); - using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + "unsupported." + UnsupportedNames[i] + ".zip")) + using (Stream dataFile = GetDataFile("unsupported." + UnsupportedNames[i] + ".zip")) { TestUtil.Unzip(dataFile, oldIndxeDir); } @@ -953,7 +950,7 @@ namespace Lucene.Net.Index foreach (string name in OldIndexDirs.Keys) { DirectoryInfo dir = CreateTempDir(name); - using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + "index." + name + ".zip")) + using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), "index." + name + ".zip")) { TestUtil.Unzip(dataFile, dir); } @@ -1061,7 +1058,7 @@ namespace Lucene.Net.Index public virtual void TestMoreTerms() { DirectoryInfo oldIndexDir = CreateTempDir("moreterms"); - using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + MoreTermsIndex)) + using (Stream dataFile = GetDataFile(MoreTermsIndex)) { TestUtil.Unzip(dataFile, oldIndexDir); } http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs index 3dd44a9..f497ae3 100644 --- a/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs +++ b/src/Lucene.Net.Tests/Index/TestBackwardsCompatibility3x.cs @@ -142,7 +142,7 @@ namespace Lucene.Net.Index foreach (string name in names) { DirectoryInfo dir = CreateTempDir(name); - using (Stream zipFileStream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + "index." + name + ".zip")) + using (Stream zipFileStream = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), "index." + name + ".zip")) { TestUtil.Unzip(zipFileStream, dir); } @@ -173,7 +173,7 @@ namespace Lucene.Net.Index Console.WriteLine("TEST: index " + UnsupportedNames[i]); } DirectoryInfo oldIndexDir = CreateTempDir(UnsupportedNames[i]); - using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + "unsupported." + UnsupportedNames[i] + ".zip")) + using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), "unsupported." + UnsupportedNames[i] + ".zip")) { TestUtil.Unzip(dataFile, oldIndexDir); } @@ -960,7 +960,7 @@ namespace Lucene.Net.Index public virtual void TestSurrogates() { DirectoryInfo oldIndexDir = CreateTempDir("surrogates"); - using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + SurrogatesIndexName)) + using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), SurrogatesIndexName)) { TestUtil.Unzip(dataFile, oldIndexDir); } @@ -1022,7 +1022,7 @@ namespace Lucene.Net.Index public virtual void TestNegativePositions() { DirectoryInfo oldIndexDir = CreateTempDir("negatives"); - using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(CURRENT_RESOURCE_DIRECTORY + Bogus24IndexName)) + using (Stream dataFile = this.GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), Bogus24IndexName)) { TestUtil.Unzip(dataFile, oldIndexDir); } http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net.Tests/Support/TestDataInputStream.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Support/TestDataInputStream.cs b/src/Lucene.Net.Tests/Support/TestDataInputStream.cs index 614338e..cbb480a 100644 --- a/src/Lucene.Net.Tests/Support/TestDataInputStream.cs +++ b/src/Lucene.Net.Tests/Support/TestDataInputStream.cs @@ -13,10 +13,10 @@ namespace Lucene.Net.Support [Test, LuceneNetSpecific] public void TestReadFully() { - const string READFULLY_TEST_FILE = "Lucene.Net.Tests.Support.ReadFully.txt"; + const string READFULLY_TEST_FILE = "ReadFully.txt"; byte[] buffer = new byte[1367]; - Stream @in = GetType().GetTypeInfo().Assembly.GetManifestResourceStream(READFULLY_TEST_FILE); + Stream @in = GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), READFULLY_TEST_FILE); DataInputStream dis; using (dis = new DataInputStream(@in)) { @@ -25,7 +25,7 @@ namespace Lucene.Net.Support } // Read past the end of the stream - @in = GetType().GetTypeInfo().Assembly.GetManifestResourceStream(READFULLY_TEST_FILE); + @in = GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(), READFULLY_TEST_FILE); dis = new DataInputStream(@in); bool caughtException = false; try @@ -47,7 +47,7 @@ namespace Lucene.Net.Support // Ensure we get an IndexOutOfRangeException exception when length is negative @in = GetType().GetTypeInfo(). - Assembly.GetManifestResourceStream(READFULLY_TEST_FILE); + Assembly.FindAndGetManifestResourceStream(GetType(), READFULLY_TEST_FILE); dis = new DataInputStream(@in); caughtException = false; try http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net/Lucene.Net.csproj ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Lucene.Net.csproj b/src/Lucene.Net/Lucene.Net.csproj index d420b07..756bfe7 100644 --- a/src/Lucene.Net/Lucene.Net.csproj +++ b/src/Lucene.Net/Lucene.Net.csproj @@ -595,6 +595,7 @@ + @@ -889,7 +890,7 @@ - + Properties\CommonAssemblyInfo.cs http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net/Support/AssemblyExtensions.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/AssemblyExtensions.cs b/src/Lucene.Net/Support/AssemblyExtensions.cs new file mode 100644 index 0000000..1269ce1 --- /dev/null +++ b/src/Lucene.Net/Support/AssemblyExtensions.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Concurrent; +using System.IO; +using System.Linq; +using System.Reflection; + +namespace Lucene.Net.Support +{ + public static class AssemblyExtensions + { + private static ConcurrentDictionary resourceCache = new ConcurrentDictionary(); + + /// + /// Aggressively searches for a resource and, if found, returns an open + /// where it can be read. + /// + /// this assembly + /// a type in the same namespace as the resource + /// the resource name to locate + /// an open that can be used to read the resource, or null if the resource cannot be found. + public static Stream FindAndGetManifestResourceStream(this Assembly assembly, Type type, string name) + { + string resourceName = FindResource(assembly, type, name); + if (string.IsNullOrEmpty(resourceName)) + { + return null; + } + + return assembly.GetManifestResourceStream(resourceName); + } + + /// + /// Aggressively searches to find a resource based on a and resource name. + /// + /// this assembly + /// a type in the same namespace as the resource + /// the resource name to locate + /// the resource, if found; if not found, returns null + public static string FindResource(this Assembly assembly, Type type, string name) + { + string resourceName; + TypeAndResource key = new TypeAndResource(type, name); + if (!resourceCache.TryGetValue(key, out resourceName)) + { + string[] resourceNames = assembly.GetManifestResourceNames(); + resourceName = resourceNames.Where(x => x.Equals(name)).FirstOrDefault(); + + // If resourceName is not null, we have an exact match, don't search + if (resourceName == null) + { + string assemblyName = type.GetTypeInfo().Assembly.GetName().Name; + string namespaceName = type.GetTypeInfo().Namespace; + + // Search by assembly + namespace + string resourceToFind = string.Concat(namespaceName, ".", name); + if (!TryFindResource(resourceNames, assemblyName, resourceToFind, name, out resourceName)) + { + string found1 = resourceName; + + // Search by namespace only + if (!TryFindResource(resourceNames, null, resourceToFind, name, out resourceName)) + { + string found2 = resourceName; + + // Search by assembly name only + resourceToFind = string.Concat(assemblyName, ".", name); + if (!TryFindResource(resourceNames, null, resourceToFind, name, out resourceName)) + { + // Take the first match of multiple, if there are any + resourceName = found1 ?? found2 ?? resourceName; + } + } + } + } + + resourceCache[key] = resourceName; + } + + return resourceName; + } + + private static bool TryFindResource(string[] resourceNames, string prefix, string resourceName, string exactResourceName, out string result) + { + if (!resourceNames.Contains(resourceName)) + { + string nameToFind = null; + while (resourceName.Length > 0 && resourceName.Contains('.') && (!(string.IsNullOrEmpty(prefix)) || resourceName.Equals(exactResourceName))) + { + resourceName = resourceName.Substring(resourceName.IndexOf('.') + 1); + nameToFind = string.IsNullOrEmpty(prefix) + ? resourceName + : string.Concat(prefix, ".", resourceName); + string[] matches = resourceNames.Where(x => x.EndsWith(nameToFind, StringComparison.Ordinal)).ToArray(); + if (matches.Length == 1) + { + result = matches[0]; // Exact match + return true; + } + else if (matches.Length > 1) + { + result = matches[0]; // First of many + return false; + } + } + result = null; // No match + return false; + } + + result = resourceName; + return true; + } + + private class TypeAndResource + { + private readonly Type type; + private readonly string name; + + public TypeAndResource(Type type, string name) + { + this.type = type; + this.name = name; + } + + public override bool Equals(object obj) + { + if (!(obj is TypeAndResource)) + { + return false; + } + + var other = obj as TypeAndResource; + return this.type.Equals(other.type) + && this.name.Equals(other.name); + } + + public override int GetHashCode() + { + return this.type.GetHashCode() ^ this.name.GetHashCode(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net/Support/BundleResourceManagerFactory.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Support/BundleResourceManagerFactory.cs b/src/Lucene.Net/Support/BundleResourceManagerFactory.cs index 8af2212..85b5414 100644 --- a/src/Lucene.Net/Support/BundleResourceManagerFactory.cs +++ b/src/Lucene.Net/Support/BundleResourceManagerFactory.cs @@ -29,7 +29,7 @@ namespace Lucene.Net.Support /// A new instance. public virtual ResourceManager Create(Type resourceSource) { - return new ResourceManager(GetResourceType(resourceSource)); + return new ResourceManager(GetResourceName(resourceSource), resourceSource.GetTypeInfo().Assembly); } /// @@ -46,17 +46,20 @@ namespace Lucene.Net.Support } /// - /// Converts the Java NLS type to the .NET resource type. - /// In Java, these were one and the same, but in .NET it is not possible to create resources - /// in Visual Studio with the same class name as a resource class because the resource generation process already - /// creates a backing class with the same name as the resource. So, by convention the resources must be - /// named <messages class name> + (default value "Bundle") in order to be found by NLS. + /// Gets the fully-qualified name of the bundle as it would appear + /// using , without the + /// .resources extension. This is the name that is passed to the + /// baseName parameter of + /// . /// - /// The type of the NLS class where the field strings are located that identify resources. - /// The type of resources (the class name + ), as a .NET instance. - protected virtual Type GetResourceType(Type clazz) + /// The type of the NLS-derived class where the field strings are located that identify resources. + /// The resource name. + protected virtual string GetResourceName(Type clazz) { - return Type.GetType(string.Concat(clazz.Namespace, ".", clazz.Name, ResourceSuffix, ", ", clazz.GetTypeInfo().Assembly.FullName)); + string resource = clazz.GetTypeInfo().Assembly.FindResource(clazz, string.Concat(clazz.Name, ResourceSuffix, ".resources")); + return !string.IsNullOrEmpty(resource) + ? resource.Substring(0, resource.Length - 10) + : null; } /// http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1743c9d8/src/Lucene.Net/Util/IOUtils.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net/Util/IOUtils.cs b/src/Lucene.Net/Util/IOUtils.cs index afc3c09..380f54c 100644 --- a/src/Lucene.Net/Util/IOUtils.cs +++ b/src/Lucene.Net/Util/IOUtils.cs @@ -336,7 +336,7 @@ namespace Lucene.Net.Util bool success = false; try { - stream = clazz.GetTypeInfo().Assembly.GetManifestResourceStream(resource); + stream = clazz.GetTypeInfo().Assembly.FindAndGetManifestResourceStream(clazz, resource); TextReader reader = GetDecodingReader(stream, charSet); success = true; return reader;