RemoteSearchable.cs
上传用户:zhangkuixh
上传日期:2013-09-30
资源大小:5473k
文件大小:6k
源码类别:

搜索引擎

开发平台:

C#

  1. /*
  2.  * Copyright 2004 The Apache Software Foundation
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. using System;
  17. using Document = Lucene.Net.Documents.Document;
  18. using Term = Lucene.Net.Index.Term;
  19. namespace Lucene.Net.Search
  20. {
  21. /// <summary> A remote searchable implementation.
  22. /// 
  23. /// </summary>
  24. /// <version>  $Id: RemoteSearchable.java 351472 2005-12-01 21:15:53Z bmesser $
  25. /// </version>
  26. [Serializable]
  27. public class RemoteSearchable : System.MarshalByRefObject, Lucene.Net.Search.Searchable
  28. {
  29. private Lucene.Net.Search.Searchable local;
  30. /// <summary>Constructs and exports a remote searcher. </summary>
  31. public RemoteSearchable(Lucene.Net.Search.Searchable local) : base()
  32. {
  33. this.local = local;
  34. }
  35. // this implementation should be removed when the deprecated
  36. // Searchable#search(Query,Filter,HitCollector) is removed
  37. public virtual void  Search(Query query, Filter filter, HitCollector results)
  38. {
  39. local.Search(query, filter, results);
  40. }
  41. public virtual void  Search(Weight weight, Filter filter, HitCollector results)
  42. {
  43. local.Search(weight, filter, results);
  44. }
  45. public virtual void  Close()
  46. {
  47. local.Close();
  48. }
  49. public virtual int DocFreq(Term term)
  50. {
  51. return local.DocFreq(term);
  52. }
  53. public virtual int[] DocFreqs(Term[] terms)
  54. {
  55. return local.DocFreqs(terms);
  56. }
  57. public virtual int MaxDoc()
  58. {
  59. return local.MaxDoc();
  60. }
  61. // this implementation should be removed when the deprecated
  62. // Searchable#search(Query,Filter,int) is removed
  63. public virtual TopDocs Search(Query query, Filter filter, int n)
  64. {
  65. return local.Search(query, filter, n);
  66. }
  67. public virtual TopDocs Search(Weight weight, Filter filter, int n)
  68. {
  69. return local.Search(weight, filter, n);
  70. }
  71. // this implementation should be removed when the deprecated
  72. // Searchable#search(Query,Filter,int,Sort) is removed
  73. public virtual TopFieldDocs Search(Query query, Filter filter, int n, Sort sort)
  74. {
  75. return local.Search(query, filter, n, sort);
  76. }
  77. public virtual TopFieldDocs Search(Weight weight, Filter filter, int n, Sort sort)
  78. {
  79. return local.Search(weight, filter, n, sort);
  80. }
  81. public virtual Document Doc(int i)
  82. {
  83. return local.Doc(i);
  84. }
  85. public virtual Query Rewrite(Query original)
  86. {
  87. return local.Rewrite(original);
  88. }
  89. // this implementation should be removed when the deprecated
  90. // Searchable#explain(Query,int) is removed
  91. public virtual Explanation Explain(Query query, int doc)
  92. {
  93. return local.Explain(query, doc);
  94. }
  95. public virtual Explanation Explain(Weight weight, int doc)
  96. {
  97. return local.Explain(weight, doc);
  98. }
  99.         public override System.Object InitializeLifetimeService()
  100.         {
  101.             long initialLeaseTime, sponsorshipTimeout, renewOnCallTime;
  102.             initialLeaseTime = SupportClass.AppSettings.Get("Lucene.Net.Remoting.Lifetime.initialLeaseTime", -1);
  103.             sponsorshipTimeout = SupportClass.AppSettings.Get("Lucene.Net.Remoting.Lifetime.sponsorshipTimeout", -1);
  104.             renewOnCallTime = SupportClass.AppSettings.Get("Lucene.Net.Remoting.Lifetime.renewOnCallTime", -1);
  105.             if ((initialLeaseTime == -1) || (sponsorshipTimeout == -1) || (renewOnCallTime == -1))
  106.             {
  107.                 return null;
  108.             }
  109.             else
  110.             {
  111.                 System.Runtime.Remoting.Lifetime.ILease lease = 
  112.                     (System.Runtime.Remoting.Lifetime.ILease) base.InitializeLifetimeService();
  113.                 if (lease.CurrentState == System.Runtime.Remoting.Lifetime.LeaseState.Initial)
  114.                 {
  115.                     lease.InitialLeaseTime = System.TimeSpan.FromMinutes(initialLeaseTime);
  116.                     lease.SponsorshipTimeout = System.TimeSpan.FromMinutes(sponsorshipTimeout);
  117.                     lease.RenewOnCallTime = System.TimeSpan.FromSeconds(renewOnCallTime);
  118.                 }
  119.                 return lease;
  120.             }
  121.         }
  122. /// <summary>Exports a searcher for the index in args[0] named
  123. /// "//localhost/Searchable". 
  124. /// </summary>
  125. [STAThread]
  126. public static void  Main(System.String[] args)
  127. {
  128. System.Runtime.Remoting.RemotingConfiguration.Configure("Lucene.Net.Search.RemoteSearchable.config");
  129. System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(new System.Runtime.Remoting.Channels.Http.HttpChannel(1099));
  130. System.String indexName = null;
  131. if (args != null && args.Length == 1)
  132. indexName = args[0];
  133. if (indexName == null)
  134. {
  135. System.Console.Out.WriteLine("Usage: Lucene.Net.search.RemoteSearchable <index>");
  136. return ;
  137. }
  138. // create and install a security manager
  139. if (true)  // if (System_Renamed.getSecurityManager() == null) // {{Aroush-1.4.3}} Do we need this line?!
  140. {
  141. // System_Renamed.setSecurityManager(new RMISecurityManager());     // {{Aroush-1.4.3}} Do we need this line?!
  142. }
  143. Lucene.Net.Search.Searchable local = new IndexSearcher(indexName);
  144. RemoteSearchable impl = new RemoteSearchable(local);
  145. // bind the implementation to "Searchable"
  146. System.Runtime.Remoting.RemotingServices.Marshal(impl, "tcp://localhost:1099/Searchable");
  147. System.Console.ReadLine();
  148. }
  149. }
  150. }