The Labs \ Source Viewer \ SSCLI \ System.Configuration \ UpdateConfigHost

  1. //------------------------------------------------------------------------------
  2. // <copyright file="UpdateConfigHost.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Configuration
  16. {
  17.     using System.Collections.Specialized;
  18.     using System.Configuration.Internal;
  19.     using System.IO;
  20.    
  21.     //
  22.     // Configuration host that intercepts calls to filename functions
  23.     // to support SaveAs to an alternate file stream.
  24.     //
  25.     internal class UpdateConfigHost : DelegatingConfigHost
  26.     {
  27.         private HybridDictionary _streams;
  28.         // oldStreamname -> StreamUpdate
  29.         internal UpdateConfigHost(IInternalConfigHost host)
  30.         {
  31.             // Delegate to the host provided.
  32.             Host = host;
  33.         }
  34.        
  35.         // Add a stream to the list of streams to intercept.
  36.         //
  37.         // Parameters:
  38.         // alwaysIntercept - If true, then don't check whether the old stream and the new stream are the same.
  39.         // SaveAs() will set this to true if oldStreamname is actually referring to a stream
  40.         // on a remote machine.
  41.         internal void AddStreamname(string oldStreamname, string newStreamname, bool alwaysIntercept)
  42.         {
  43.             Debug.Assert(!String.IsNullOrEmpty(oldStreamname));
  44.            
  45.             if (String.IsNullOrEmpty(oldStreamname)) {
  46.                 return;
  47.             }
  48.            
  49.             if (!alwaysIntercept && StringUtil.EqualsIgnoreCase(oldStreamname, newStreamname)) {
  50.                 return;
  51.             }
  52.            
  53.             if (_streams == null) {
  54.                 _streams = new HybridDictionary(true);
  55.             }
  56.            
  57.             _streams[oldStreamname] = new StreamUpdate(newStreamname);
  58.         }
  59.        
  60.         // Get the new stream name for a stream if a new name exists, otherwise
  61.         // return the original stream name.
  62.         internal string GetNewStreamname(string oldStreamname)
  63.         {
  64.             StreamUpdate streamUpdate = GetStreamUpdate(oldStreamname, false);
  65.             if (streamUpdate != null) {
  66.                 return streamUpdate.NewStreamname;
  67.             }
  68.            
  69.             return oldStreamname;
  70.         }
  71.        
  72.         //
  73.         // Get the StreamUpdate for a stream.
  74.         // If alwaysIntercept is true, then the StreamUpdate is
  75.         // always returned if it exists.
  76.         // If alwaysIntercept is false, then only return the StreamUpdate
  77.         // if the new stream has been successfully written to.
  78.         //
  79.         private StreamUpdate GetStreamUpdate(string oldStreamname, bool alwaysIntercept)
  80.         {
  81.             if (_streams == null)
  82.                 return null;
  83.            
  84.             StreamUpdate streamUpdate = (StreamUpdate)_streams[oldStreamname];
  85.             if (streamUpdate != null && !alwaysIntercept && !streamUpdate.WriteCompleted) {
  86.                 streamUpdate = null;
  87.             }
  88.            
  89.             return streamUpdate;
  90.         }
  91.        
  92.         public override object GetStreamVersion(string streamName)
  93.         {
  94.             StreamUpdate streamUpdate = GetStreamUpdate(streamName, false);
  95.             if (streamUpdate != null) {
  96.                 return InternalConfigHost.StaticGetStreamVersion(streamUpdate.NewStreamname);
  97.             }
  98.             else {
  99.                 return Host.GetStreamVersion(streamName);
  100.             }
  101.         }
  102.        
  103.         public override Stream OpenStreamForRead(string streamName)
  104.         {
  105.             StreamUpdate streamUpdate = GetStreamUpdate(streamName, false);
  106.             if (streamUpdate != null) {
  107.                 return InternalConfigHost.StaticOpenStreamForRead(streamUpdate.NewStreamname);
  108.             }
  109.             else {
  110.                 return Host.OpenStreamForRead(streamName);
  111.             }
  112.         }
  113.        
  114.         public override Stream OpenStreamForWrite(string streamName, string templateStreamName, ref object writeContext)
  115.         {
  116.             // Always attempt to write to the new stream name if it exists.
  117.             StreamUpdate streamUpdate = GetStreamUpdate(streamName, true);
  118.             if (streamUpdate != null) {
  119.                 return InternalConfigHost.StaticOpenStreamForWrite(streamUpdate.NewStreamname, templateStreamName, ref writeContext, false);
  120.             }
  121.             else {
  122.                 return Host.OpenStreamForWrite(streamName, templateStreamName, ref writeContext);
  123.             }
  124.         }
  125.        
  126.         public override void WriteCompleted(string streamName, bool success, object writeContext)
  127.         {
  128.             StreamUpdate streamUpdate = GetStreamUpdate(streamName, true);
  129.             if (streamUpdate != null) {
  130.                 InternalConfigHost.StaticWriteCompleted(streamUpdate.NewStreamname, success, writeContext, false);
  131.                 //
  132.                 // Mark the write as having successfully completed, so that subsequent calls
  133.                 // to Read() will use the new stream name.
  134.                 //
  135.                 if (success) {
  136.                     streamUpdate.WriteCompleted = true;
  137.                 }
  138.             }
  139.             else {
  140.                 Host.WriteCompleted(streamName, success, writeContext);
  141.             }
  142.         }
  143.        
  144.         public override bool IsConfigRecordRequired(string configPath)
  145.         {
  146.             return true;
  147.         }
  148.        
  149.         public override void DeleteStream(string streamName)
  150.         {
  151.             StreamUpdate streamUpdate = GetStreamUpdate(streamName, false);
  152.             if (streamUpdate != null) {
  153.                 InternalConfigHost.StaticDeleteStream(streamUpdate.NewStreamname);
  154.             }
  155.             else {
  156.                 Host.DeleteStream(streamName);
  157.             }
  158.         }
  159.        
  160.         public override bool IsFile(string streamName)
  161.         {
  162.             StreamUpdate streamUpdate = GetStreamUpdate(streamName, false);
  163.             if (streamUpdate != null) {
  164.                 return InternalConfigHost.StaticIsFile(streamUpdate.NewStreamname);
  165.             }
  166.             else {
  167.                 return Host.IsFile(streamName);
  168.             }
  169.         }
  170.     }
  171. }

Developer Fusion