using System; using System.Threading; using System.Net; using System.Collections; using System.IO; using System.Xml; using System.Diagnostics; using System.Web.Services.Protocols; namespace TestRepoCleanup { /// ///

Worker thread implementation.
/// Reads cleanup.xml and invokes the Retry web service.

///

Copyright (c) 2002 Alexis Grandemange
/// Mail: alexis.grandemange@pagebox.net

///
This program is free software; you can redistribute it and/or
	/// modify it under the terms of the GNU Lesser General Public
	/// License as published by the Free Software Foundation; version
	/// 2.1 of the License.
	/// This library is distributed in the hope that it will be useful,
	/// but WITHOUT ANY WARRANTY; without even the implied warranty of
	/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	/// GNU Lesser General Public License for more details.
	/// A copy of the GNU Lesser General Public License lesser.txt should be
	/// included in the distribution.
///
public class CleanupThread { /// /// ThreadRun loops up to the time toStop = true /// public bool toStop = false; /// /// If true, log events (debug mode) /// private bool toLog; /// /// Repository list. Contains RepositoryEntry objects. /// private ArrayList reps = new ArrayList(); /// /// Constructor. Restores cleanup.xml /// /// Full path of cleanup.xml /// If true, log events public CleanupThread(string path, bool tl) { if (!File.Exists(path)) { log("CleanupThread.CleanupThread " + path + " not found"); return; } toLog = tl; XmlTextReader r = new XmlTextReader(path); r.WhitespaceHandling = WhitespaceHandling.None; try { r.ReadStartElement("cleanup"); while (true) { if (r.NodeType == XmlNodeType.Element) { if (r.LocalName.Equals("repository")) { r.Read(); string url = r.ReadElementString("url"); string user = r.ReadElementString("user"); string password = r.ReadElementString("password"); RepositoryEntry re = new RepositoryEntry(url, user, password); reps.Add(re); } else break; } if ((r.NodeType == XmlNodeType.EndElement) && r.LocalName.Equals("cleanup")) break; if (r.NodeType == XmlNodeType.None) break; r.Read(); } } catch(XmlException xe) { error("CleanupThread.CleanupThread " + xe.Message); reps.Clear(); r.Close(); return; } r.Close(); log("CleanupThread.CleanupThread restored cleanup.xml (" + reps.Count + ")"); } /// /// Write an Error message on the Application Event Log /// /// message string public static void error(string msg) { EventLog el = new EventLog("Application"); el.Source = "RepoCleanup"; el.WriteEntry(msg, EventLogEntryType.Error); } /// /// Write an Information message on the Application Event Log /// /// message string public void log(string msg) { if (!toLog) return; EventLog el = new EventLog("Application"); el.Source = "RepoCleanup"; el.WriteEntry(msg, EventLogEntryType.Information); } /// /// Called when the thread is started. Calls the Retry web service using the Retry proxy. /// public void ThreadRun() { while(!toStop) { TimeSpan to = new TimeSpan(0, 30, 0); log("CleanupThread.ThreadRun cleanup"); lock(this) { try { IEnumerator ie = reps.GetEnumerator(); while(ie.MoveNext()) { RepositoryEntry re = (RepositoryEntry)ie.Current; Retry rt = new Retry(re.url); string rc = null; try { rt.Discover(); } catch(Exception e) { rc = re.url + " Discovery failed " + e.Message; } if (rc == null) { try { rc = rt.Cleanup(re.user, re.password); } catch(System.Net.WebException we) { rc = re.url + " Cleanup failed WebException " + we.Message; } catch(System.Web.HttpException he) { rc = re.url + " Cleanup failed HttpException " + he.Message; } catch(SoapException se) { rc = re.url + " Cleanup failed SoapException " + se.Message; } } if (!rc.EndsWith("performed")) error(rc); } Monitor.Wait(this, to); } catch (SynchronizationLockException e) { error("CleanupThread.ThreadRun " + e.Message); } catch (ThreadInterruptedException e) { error("CleanupThread.ThreadRun " + e.Message); } } } } } /// /// Describes a repository. Element of the reps list. /// public sealed class RepositoryEntry { /// /// Uri of the Retry web service on the target repository /// public string url; /// /// User name /// public string user; /// /// Password /// public string password; /// /// Constructor /// /// Uri of the Retry web service on the target repository /// User name /// Password public RepositoryEntry(string ru, string u, string p) { url = ru; user = u; password = p; } } }