This commit is contained in:
2026-04-17 14:55:32 -04:00
commit bc3ac1d4c9
18017 changed files with 4371742 additions and 0 deletions

View File

@@ -0,0 +1,261 @@
/*
AttributeCoder.cs
* 12/18/2008 Relo'd from Dave to SVN-controlled utilities project.
$Log: AttributeCoder.cs,v $
Revision 1.1 2006/09/14 18:41:54 Paul Hrissikopoulos
Moved AttributeCoder files from Dave solution.
Revision 1.3 2006/09/07 20:43:08 Paul Hrissikopoulos
Expanded commentary.
Revision 1.2 2006/08/17 23:44:46 Paul Hrissikopoulos
Removed freeloading DCX XML prototype code.
Revision 1.1 2006/08/17 23:05:11 Paul Hrissikopoulos
Added generically-derived SelectionCriterion.ComparisonType enumeration attributes.
Copyright © 2006
Diversified Technical Systems
All Rights Reserved
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using DTS.Common.Utilities.DotNetProgrammingConstructs;
using DTS.Common.Utilities.Properties;
namespace DTS.Common.Utilities
{
/// <summary>
/// Object for manipulating object-attached attributes and attribute values.
/// </summary>
///
/// <typeparam name="TargetType">
/// The type of <see cref="object"/> the attributes we are concerned with are
/// are attached to.
/// </typeparam>
///
/// <typeparam name="AttributeType">
/// The type of attribute to be manipulated.
/// </typeparam>
///
/// <typeparam name="AttributeValueType">
/// The type of value contained by the attribute to be manipulated.
/// </typeparam>
///
/// <remarks>
/// This class was created to make it easier to "match" attributes and the data
/// types they're attached to in a somewhat generic manner. It is intended that
/// attribute coder classes that deal with specific attribute types be derived
/// from this one.
/// </remarks>
///
public class AttributeCoder<TargetType, AttributeType, AttributeValueType> : Exceptional
{
/// <summary>
/// Method that specifies how to access the value of a given "AttributeType".
/// </summary>
///
/// <param name="attribute">
/// The "AttributeType" attribute to have its value extracted.
/// </param>
///
/// <returns>
/// The "AttributeValueType" value of the specified attribute.
/// </returns>
///
public delegate AttributeValueType AttributeValueExtractionMethod(AttributeType attribute);
/// <summary>
/// Method for determining the equality of two "AttributeValueType" values.
/// </summary>
///
/// <param name="value1">
/// A "AttributeValueType" to be equality-compared.
/// </param>
///
/// <param name="value2">
/// A "AttributeValueType" to be equality-compared.
/// </param>
///
/// <returns>
/// True if the two values are equal; false otherwise.
/// </returns>
///
public delegate bool AttributeValueEqualityComparisonMethod(AttributeValueType value1, AttributeValueType value2);
private readonly AttributeValueExtractionMethod _extractAttributeValue;
private readonly AttributeValueEqualityComparisonMethod _areAttributeValuesEqual;
/// <summary>
/// Initializes an instance of the AttributeCoder class.
/// </summary>
///
/// <param name="attributeValueExtractionMethod">
/// A <see cref="AttributeValueExtractionMethod"/> that defines how "AttributeValueType"
/// values are to be extracted from "AttributeType" attributes.
/// </param>
///
/// <param name="attributeValueEqualityComparisonMethod">
/// Optional parameter for specifying a custom comparison method to override default
/// "AttributeValueType" equality determination. Pass null to use default
/// comparison.
/// </param>
///
public AttributeCoder(AttributeValueExtractionMethod attributeValueExtractionMethod,
AttributeValueEqualityComparisonMethod attributeValueEqualityComparisonMethod)
{
try
{
if (null == attributeValueExtractionMethod)
throw new Exception(Resources.AttributeCoder_AttributeCoder_NullAttributeValueExtractionMethodReferenceString);
_extractAttributeValue = attributeValueExtractionMethod;
if (null != attributeValueEqualityComparisonMethod)
_areAttributeValuesEqual = attributeValueEqualityComparisonMethod;
}
catch (System.Exception ex)
{
throw new Exception(
string.Format(Resources.Generic_EncounteredProblemConstructingClassString, GetType().FullName),
ex);
}
}
/// <summary>
/// Return the "AttributeValueType" of the "AttributeType" attribute
/// attached to the specified "TargetType".
/// </summary>
///
/// <param name="target">
/// The "TargetType" object whose "AttributeType" attribute is to be
/// evaluated.
/// </param>
///
/// <returns>
/// The "AttributeValueType" of the "AttributeType" attribute attached
/// to the specified "TargetType".
/// </returns>
///
public AttributeValueType DecodeAttributeValue(TargetType target)
{
try
{
var attributes = DecodeAttributeValues(target);
if (attributes.Count > 0) return attributes[0];
throw new Exception(Resources.AttributeCoder_NoTypeAttributesFoundOnTargetString);
}
catch (System.Exception ex)
{
throw new Exception(Resources.AttributeCoder_DecodeAttributeExceptionString, ex);
}
}
/// <summary>
/// Return the <see cref="T:List"/> of "AttributeValueType"s of the
/// "AttributeType" attached to the specified "TargetType".
/// </summary>
///
/// <param name="target">
/// The "TargetType" object whose "AttributeType" attribute is to be
/// evaluated.
/// </param>
///
/// <returns>
/// The "AttributeValueType" of the "AttributeType" attribute attached
/// to the specified "TargetType".
/// </returns>
///
public List<AttributeValueType> DecodeAttributeValues(TargetType target)
{
try
{
var info = target.GetType().GetField(target.ToString());
var attributes = (info.GetCustomAttributes(typeof(AttributeType), false) as AttributeType[]);
var attributeList = new List<AttributeValueType>();
if (attributes != null)
{
attributeList.AddRange(attributes.Select(t => _extractAttributeValue(t)));
}
return attributeList;
}
catch (System.Exception ex)
{
throw new Exception(Resources.AttributeCoder_DecodeAttributesExceptionString, ex);
}
}
/// <summary>
/// Return the "TargetType" value that has the "AttributeType"
/// attribute with the specified "AttributeValueType" value attached
/// to it.
/// </summary>
///
/// <param name="attributeValue">
/// The value of the "AttributeType" attribute attached to the
/// "TargetType" target object we're looking for.
/// </param>
///
/// <returns>
/// The "TargetType" target object that has the "AttributeType"
/// attribute attached to it that has a "AttributeValueType" value of
/// attributeValue.
/// </returns>
///
public TargetType EncodeAttributeValue(AttributeValueType attributeValue)
{
try
{
var targets = DehashAttributeValue(attributeValue);
if (1 != targets.Count) throw new Exception(Resources.AttributeCoder_UnableToFindTargetTypeMappingString);
return targets[0];
}
catch (System.Exception ex)
{
throw new Exception(Resources.AttributeCoder_EncodeAttributeExceptionString, ex);
}
}
/// <summary>
/// Return a list of "TargetType" values that have the
/// "AttributeType" attribute with the specified
/// "AttributeValueType" value attached to them.
/// </summary>
///
/// <param name="attributeValue">
/// The value of the "AttributeType" attribute attached to the
/// "TargetType" target object we're looking for.
/// </param>
///
/// <returns>
/// The <see cref="T:List"/> of "TargetType" target objects that has
/// the "AttributeType" attribute attached to it that has a
/// "AttributeValueType" value of attributeValue.
/// </returns>
///
public List<TargetType> DehashAttributeValue(AttributeValueType attributeValue)
{
try
{
var targets = Enum.GetValues(typeof(TargetType)).Cast<TargetType>()
.Where(target => (null != _areAttributeValuesEqual) ?
_areAttributeValuesEqual(DecodeAttributeValue(target), attributeValue) : DecodeAttributeValue(target).Equals(attributeValue)).ToList();
if (targets.Count <= 0) throw new Exception(Resources.AttributeCoder_DehashAttributeValue_UnableToMatchAttributeValueWithTargetString);
return targets;
}
catch (System.Exception ex)
{
throw new Exception(Resources.AttributeCoder_DehashAttributeValue_DehashAttributeValueExceptionString, ex);
}
}
}
}

View File

@@ -0,0 +1,225 @@
/*
* TextLogger.WriteCycleHandle.cs
*
* Copyright © 2010
* Diversified Technical Systems, Inc.
* All Rights Reserved.
*/
using System;
using System.Collections.Generic;
using System.Threading;
namespace DTS.Common.Utilities.Logging
{ // *** see TextLogger.cs ***
public partial class TextLogger
{
/// <summary>
/// "Handle" containing the state information for a log message write session.
/// </summary>
protected class WriteCycleHandle
{
/// <summary>
/// The filename of the log file receiving the messages being supplied to this
/// write session.
/// </summary>
public string FullLogFileName { get; set; }
/// <summary>
/// The <see cref="System.Threading.AutoResetEvent"/> whose "set" kicks off
/// a write cycle.
/// </summary>
public AutoResetEvent CycleTrigger { get; set; }
/// <summary>
/// The <see cref="System.Threading.AutoResetEvent"/> that is signaled when
/// a write cycle completes.
/// </summary>
public AutoResetEvent CycleCompletionTrigger { get; set; }
/// <summary>
/// The <see cref="Logging.TextLogger.WriteCycleExceptionHandler"/> to be
/// invoked when the write cycle encounters an exception.
/// </summary>
public WriteCycleExceptionHandler OnWriteCycleException { get; set; }
/// <summary>
/// <see cref="bool"/> flag that controls whether or not the write cycle should
/// auto-terminate whenever it encounters an exception.
/// </summary>
public bool TerminateCycleOnException { get; set; }
private static readonly object MyLock = new object();
/// <summary>
/// Get/set the <see cref="bool"/> flag that will terminate the associated write cycle when set to 'true'.
/// </summary>
public bool TerminateWriteCycle
{
get
{
try
{
lock (MyLock)
{
if (!_IsTerminateWriteCycleInitialized)
throw new ApplicationException("TerminateWriteCycle value has not been initialized");
else return _TerminateWriteCycle;
}
}
catch (Exception ex)
{
throw new ApplicationException("encountered problem getting value for property " + GetType().FullName + ".TerminateWriteCycle", ex);
}
}
set
{
try
{
lock (MyLock)
{ //
// Set the termination flag and then kick off a cycle so it goes into
// effect asap.
//
_IsTerminateWriteCycleInitialized = true;
_TerminateWriteCycle = value;
if (_TerminateWriteCycle)
CycleTrigger.Set();
}
}
catch (Exception ex)
{
throw new ApplicationException("encountered problem setting value for property " + GetType().FullName + ".TerminateWriteCycle", ex);
}
}
}
private bool _TerminateWriteCycle;
private bool _IsTerminateWriteCycleInitialized = false;
/// <summary>
/// The actually queue of log message <see cref="string"/>s to be written to the active log file.
/// </summary>
public Queue<string> WriteQueue
{
get
{
try
{
if (!_IsWriteQueueInitialized)
throw new ApplicationException("WriteQueue has not been initialized");
else return _WriteQueue;
}
catch (Exception ex)
{
throw new ApplicationException("encountered problem getting value for property " + GetType().FullName + ".WriteQueue", ex);
}
}
set
{
try
{
_IsWriteQueueInitialized = true;
_WriteQueue = value;
}
catch (Exception ex)
{
throw new ApplicationException("encountered problem setting value for property " + GetType().FullName + ".WriteQueue", ex);
}
}
}
private Queue<string> _WriteQueue = null;
private bool _IsWriteQueueInitialized = false;
/// <summary>
/// Callback to be invoked when the write cycle has closed it's associated log file's
/// write handle for the last time.
/// </summary>
public WriteCycleCallback OnWriteCycleTermination
{
get
{
try
{
lock (MyLock)
{
return _OnWriteCycleTermination;
}
}
catch (Exception ex)
{
throw new ApplicationException("encountered problem getting value for property " + GetType().FullName + ".OnWriteCycleTermination", ex);
}
}
set
{
try
{
lock (MyLock)
{ //
// Set the cycle termination callback. If we're undergoing termination and there's
// nothing left in the write queue, then go ahead and immediately call the termination
// callback. Null the reference after the call so we don't accidentally somehow end up
// calling it twice.
//
_OnWriteCycleTermination = value;
if (_IsTerminateWriteCycleInitialized && _TerminateWriteCycle &&
_IsWriteQueueInitialized && _WriteQueue.Count <= 0)
{
_OnWriteCycleTermination(_WriteQueue);
_OnWriteCycleTermination = null;
}
}
}
catch (Exception ex)
{
throw new ApplicationException("encountered problem setting value for property " + GetType().FullName + ".OnWriteCycleTermination", ex);
}
}
}
private WriteCycleCallback _OnWriteCycleTermination = null;
/// <summary>
/// Initialize an instance of the <see cref="Logging.TextLogger.WriteCycleHandle"/> class.
/// </summary>
///
/// <param name="fullLogFileName">
/// The filename of the log file receiving the messages being supplied to this
/// write session.
/// </param>
///
/// <param name="writeQueue">
/// The actually queue of log message <see cref="string"/>s to be written to the active log file.
/// </param>
///
/// <param name="onWriteCycleTermination">
/// Callback to be invoked when the write cycle has closed it's associated log file's
/// write handle for the last time.
/// </param>
///
/// <param name="onWriteCycleException">
/// Callback to be invoked when the write cycle has closed it's associated log file's
/// write handle for the last time.
/// </param>
///
/// <param name="terminateCycleOnException">
/// <see cref="bool"/> flag that controls whether or not the write cycle should
/// auto-terminate whenever it encounters an exception.
/// </param>
///
public WriteCycleHandle(string fullLogFileName,
Queue<string> writeQueue,
WriteCycleCallback onWriteCycleTermination,
WriteCycleExceptionHandler onWriteCycleException,
bool terminateCycleOnException)
{
TerminateWriteCycle = false;
FullLogFileName = fullLogFileName;
WriteQueue = writeQueue;
CycleTrigger = new AutoResetEvent(false);
CycleCompletionTrigger = new AutoResetEvent(false);
OnWriteCycleTermination = onWriteCycleTermination;
OnWriteCycleException = onWriteCycleException;
TerminateCycleOnException = terminateCycleOnException;
}
}
}
}