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