DirectorySearcher.cs :
authorBoris Kirzner <borisk@mono-cvs.ximian.com>
Thu, 31 Mar 2005 06:16:18 +0000 (06:16 -0000)
committerBoris Kirzner <borisk@mono-cvs.ximian.com>
Thu, 31 Mar 2005 06:16:18 +0000 (06:16 -0000)
- Use common TimeSpan object to initialize properties default values.
- DoSearch uses SizeLimit and ServerTimeLimit. Do not throw if sie limit or time limit occurs on the server.
- Construct ADsPath property from resulting entry path.
- Implemented Dispose method.

svn path=/trunk/mcs/; revision=42414

mcs/class/System.DirectoryServices/System.DirectoryServices/ChangeLog
mcs/class/System.DirectoryServices/System.DirectoryServices/DirectorySearcher.cs

index ba77e64302e191ae265f7db274cdc0f9bc5e021c..538bd04c89386a4bdba26dfcd8db8f7bd49abe1f 100644 (file)
@@ -1,3 +1,11 @@
+2005-03-31  Boris Kirzner <borisk@mainsoft.com>
+
+       * DirectorySearcher.cs :
+               - Use common TimeSpan object to initialize properties default values.
+               - DoSearch uses SizeLimit and ServerTimeLimit. Do not throw if sie limit or time limit occurs on the server.
+               - Construct ADsPath property from resulting entry path.
+               - Implemented Dispose method.
+
 2005-03-28  Boris Kirzner <borisk@mainsoft.com>
 
        * DirectoryEntries.cs : 
index 5fef08eca68c8eabdf4b3c1282134e10998f73fc..f4a6072a0c8eeaf0c19fb64f572c7de8632e1031 100644 (file)
@@ -44,10 +44,10 @@ namespace System.DirectoryServices
        /// </summary>
        public class DirectorySearcher : Component      
        {
-
+               private static readonly TimeSpan DefaultTimeSpan = new TimeSpan(-TimeSpan.TicksPerSecond);
                private DirectoryEntry _SearchRoot=null;
                private bool _CacheResults=true;
-               private TimeSpan _ClientTimeout = new TimeSpan(-10000000);
+               private TimeSpan _ClientTimeout = DefaultTimeSpan;
                private string _Filter="(objectClass=*)";
                private int _PageSize=0;
                private StringCollection _PropertiesToLoad=new StringCollection();
@@ -56,8 +56,8 @@ namespace System.DirectoryServices
                                                System.DirectoryServices.ReferralChasingOption.External;
                private SearchScope _SearchScope=
                                                System.DirectoryServices.SearchScope.Subtree;
-               private TimeSpan _ServerPageTimeLimit=new TimeSpan(-10000000);
-               private TimeSpan _serverTimeLimit = new TimeSpan(-10000000);
+               private TimeSpan _ServerPageTimeLimit = DefaultTimeSpan;
+               private TimeSpan _serverTimeLimit = DefaultTimeSpan;
                private int _SizeLimit=0;
                private LdapConnection _conn = null;
                private string _Host=null;
@@ -602,6 +602,7 @@ namespace System.DirectoryServices
                /// </returns>
                public SearchResult FindOne()
                {
+                       // TBD : should search for no more than single result
                        if (SrchColl.Count == 0) {
                                return null;
                        }
@@ -627,36 +628,42 @@ namespace System.DirectoryServices
                        }
                        String[] attrs= new String[PropertiesToLoad.Count];
                        PropertiesToLoad.CopyTo(attrs,0);
-
-                       int connScope = LdapConnection.SCOPE_SUB;
-                       switch (_SearchScope)
-                       {
-                       case SearchScope.Base:
-                         connScope = LdapConnection.SCOPE_BASE;
-                         break;
-
-                       case SearchScope.OneLevel:
-                         connScope = LdapConnection.SCOPE_ONE;
-                         break;
-
-                       case SearchScope.Subtree:
-                         connScope = LdapConnection.SCOPE_SUB;
-                         break;
-
-                       default:
-                         connScope = LdapConnection.SCOPE_SUB;
-                         break;
+                       
+                       LdapSearchConstraints cons = _conn.SearchConstraints;
+                       if (SizeLimit > 0) {
+                               cons.MaxResults = SizeLimit;
+                       }
+                       if (ServerTimeLimit != DefaultTimeSpan) {
+                               cons.ServerTimeLimit = (int)ServerTimeLimit.TotalSeconds;
                        }
 
+                       int connScope = LdapConnection.SCOPE_SUB;\r
+                       switch (_SearchScope)\r
+                       {\r
+                       case SearchScope.Base:\r
+                         connScope = LdapConnection.SCOPE_BASE;\r
+                         break;\r
+\r
+                       case SearchScope.OneLevel:\r
+                         connScope = LdapConnection.SCOPE_ONE;\r
+                         break;\r
+\r
+                       case SearchScope.Subtree:\r
+                         connScope = LdapConnection.SCOPE_SUB;\r
+                         break;\r
+\r
+                       default:\r
+                         connScope = LdapConnection.SCOPE_SUB;\r
+                         break;\r
+                       }
                        LdapSearchResults lsc=_conn.Search(     SearchRoot.Fdn,
-                                                               connScope,
+                                                                                               connScope,
                                                                                                Filter,
                                                                                                attrs,
-                                                                                               false);
+                                                                                               false,cons);
 
                        while(lsc.hasMore())                                            
                        {
-
                                LdapEntry nextEntry = null;
                                try                                                     
                                {
@@ -664,9 +671,14 @@ namespace System.DirectoryServices
                                }
                                catch(LdapException e)                                                  
                                {
-                                       Console.WriteLine("Error: " + e.LdapErrorMessage);
-                                       // Exception is thrown, go for next entry
-                                       throw e;
+                                       switch (e.ResultCode) {
+                                               // in case of this return codes exception should not be thrown
+                                               case LdapException.SIZE_LIMIT_EXCEEDED:
+                                               case LdapException.TIME_LIMIT_EXCEEDED:
+                                                       continue;
+                                               default :
+                                                       throw e;
+                                       }
                                }
                                DirectoryEntry de = new DirectoryEntry(_conn);
                                PropertyCollection pcoll = new PropertyCollection();
@@ -686,7 +698,7 @@ namespace System.DirectoryServices
                                        }
                                }
                                if (!pcoll.Contains("ADsPath")) {
-                                       pcoll["ADsPath"].Add(nextEntry.DN);
+                                       pcoll["ADsPath"].Add(de.Path);
                                }
 //                             _SrchColl.Add(new SearchResult(de,PropertiesToLoad));
                                _SrchColl.Add(new SearchResult(de,pcoll));
@@ -697,7 +709,12 @@ namespace System.DirectoryServices
                [MonoTODO]
                protected override void Dispose(bool disposing)
                {
-                       throw new NotImplementedException();
+                       if (disposing) {\r
+                               if(_conn.Connected) {\r
+                                       _conn.Disconnect();\r
+                               }\r
+                       }\r
+                       base.Dispose(disposing);
                }
 
                private void ClearCachedResults()