Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
LuceneServerIndex.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Collections;
20 using System.Configuration;
21 using System.IO;
22 using System.Xml;
23 
24 namespace Lucene.Net.Distributed.Configuration
25 {
26  /// <summary>
27  /// Definition of a configurable search index made accessible by the
28  /// LuceneServer windows service.
29  ///
30  /// An example configuration would look like the following:
31  /// <code>
32  /// <LuceneServerIndexes Port="1089">
33  /// <LuceneServerIndex ObjectUri="RemoteIndexes1">
34  /// <Directory indexA="c:\lucene\indexA\index1" indexB="c:\lucene\indexB\index1" />
35  /// <Directory indexA="c:\lucene\indexA\index2" indexB="c:\lucene\indexB\index2" />
36  /// </LuceneServerIndex>
37  /// <LuceneServerIndex ObjectUri="RemoteIndexes2">
38  /// <Directory indexA="c:\lucene\indexA\index3" indexB="c:\lucene\indexB\index3" />
39  /// <Directory indexA="c:\lucene\indexA\index4" indexB="c:\lucene\indexB\index4" />
40  /// </LuceneServerIndex>
41  /// </LuceneServerIndexes>
42  /// </code>
43  /// </summary>
44  public class LuceneServerIndex
45  {
46  private string _strObjectUri;
47  private int _intPort;
48  private DirectoryInfo[] _arIndexADirectories;
49  private DirectoryInfo[] _arIndexBDirectories;
50  private DirectoryInfo[] _arRefreshDirectories;
51 
52  /// <summary>
53  /// Public constructor for LuceneServerIndex. A LuceneServerIndex is defined
54  /// in XML configuration and is loaded via a custom configuration handler.
55  /// </summary>
56  /// <param name="xSection">The Xml definition in the configuration file</param>
57  /// <param name="defaultPort">The default Port value, as defined in the contained
58  /// LuceneServerIndexes configuration</param>
59  public LuceneServerIndex(XmlNode xSection, int defaultPort)
60  {
61  XmlAttributeCollection attributeCollection = xSection.Attributes;
62  try
63  {
64  this._strObjectUri = attributeCollection["ObjectUri"].Value;
65  }
66  catch (Exception)
67  {
68  throw new ConfigurationErrorsException("ObjectUri invalid: "+Environment.NewLine + xSection.OuterXml);
69  }
70 
71  try
72  {
73  this._intPort = (attributeCollection["port"] != null ? Convert.ToInt32(attributeCollection["port"].Value) : defaultPort);
74  }
75  catch (Exception)
76  {
77  throw new ConfigurationErrorsException("port invalid: " + Environment.NewLine + xSection.OuterXml);
78  }
79 
80  if (xSection.ChildNodes.Count == 0)
81  throw new ConfigurationErrorsException("LuceneServerIndex configuration missing: " + Environment.NewLine + xSection.OuterXml);
82 
83  _arIndexADirectories = new DirectoryInfo[xSection.ChildNodes.Count];
84  _arIndexBDirectories = new DirectoryInfo[xSection.ChildNodes.Count];
85  DirectoryInfo diA;
86  DirectoryInfo diB;
87  int x=0;
88 
89  foreach (XmlNode c in xSection.ChildNodes)
90  {
91  if (c.Name.ToLower()=="directory")
92  {
93  try
94  {
95  diA = new DirectoryInfo(c.Attributes["indexA"].Value);
96  _arIndexADirectories[x] = diA;
97  if (!diA.Exists)
98  throw new DirectoryNotFoundException("Directory not found: indexA=" + c.Attributes["indexA"].Value + Environment.NewLine + xSection.OuterXml);
99  }
100  catch (Exception)
101  {
102  throw new ConfigurationErrorsException("LuceneServerIndex configuration Directory error: indexA=" + c.Attributes["indexA"].Value + Environment.NewLine + xSection.OuterXml);
103  }
104 
105  try
106  {
107  diB = new DirectoryInfo(c.Attributes["indexB"].Value);
108  _arIndexBDirectories[x] = diB;
109  if (!diB.Exists)
110  throw new DirectoryNotFoundException("Directory not found: indexA=" + c.Attributes["indexA"].Value + Environment.NewLine + xSection.OuterXml);
111  }
112  catch (Exception)
113  {
114  throw new ConfigurationErrorsException("LuceneServerIndex configuration Directory error: indexA=" + c.Attributes["indexB"].Value + Environment.NewLine + xSection.OuterXml);
115  }
116  x++;
117  }
118  }
119  }
120 
121  /// <summary>
122  /// The published Uri name for a collective set of indexes. The ObjectUri
123  /// is referenced by clients consuming the well-known service type. As an example,
124  /// an ObjectUri of "RemoteSearchIndex" on a system located at 192.168.1.100, exposed
125  /// on port 1089, would be accessed at "tcp://192.168.1.100:1089/RemoteSearchIndex".
126  /// <para>This value is required in configuration.</para>
127  /// </summary>
128  public string ObjectUri
129  {
130  get {return this._strObjectUri;}
131  }
132 
133  /// <summary>
134  /// A definable port number for the published Uri. Use this value to override the default
135  /// Port setting for all published URIs.
136  /// <para>This value is optional in configuration.</para>
137  /// </summary>
138  public int Port
139  {
140  get {return this._intPort;}
141  }
142 
143  /// <summary>
144  /// File-system path to the "IndexA" location of the index files.
145  /// </summary>
146  public DirectoryInfo[] IndexADirectories
147  {
148  get {return this._arIndexADirectories;}
149  }
150 
151  /// <summary>
152  /// File-system path to the "IndexB" location of the index files.
153  /// </summary>
154  public DirectoryInfo[] IndexBDirectories
155  {
156  get {return this._arIndexBDirectories;}
157  }
158 
159  /// <summary>
160  /// Instance method that returns an array of directory paths associated
161  /// with the given IndexSetting.
162  /// </summary>
163  /// <param name="oIndexSettingRefresh">IndexSetting enumeration value</param>
164  /// <returns>DirectoryInfo[] of directory paths</returns>
165  public DirectoryInfo[] RefreshDirectories(IndexSetting oIndexSettingRefresh)
166  {
167  this._arRefreshDirectories=null;
168  if (oIndexSettingRefresh==IndexSetting.IndexA)
169  this._arRefreshDirectories = this._arIndexADirectories;
170  else if (oIndexSettingRefresh==IndexSetting.IndexB)
171  this._arRefreshDirectories = this._arIndexBDirectories;
172  return this._arRefreshDirectories;
173  }
174  }
175 }