From ed0be9162b8b64ec84157e3b97e53124cae9842b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 15 Sep 2023 01:29:22 -0400 Subject: [PATCH] refactor: Move PrimitiveManager to DataStreamCore So DSQA can reference it --- .../DynamicLoading/PrimitiveManager.cs | 193 ------------------ .../DataStreamUserRespresentation.cs | 0 .../DynamicLoading/PrimitiveManager.cs | 191 +++++++++++++++++ 3 files changed, 191 insertions(+), 193 deletions(-) delete mode 100644 src/DataStreamSuite/DataStream/DynamicLoading/PrimitiveManager.cs rename src/DataStreamSuite/{DataStream => DataStreamCore}/DynamicLoading/DataStreamUserRespresentation.cs (100%) create mode 100644 src/DataStreamSuite/DataStreamCore/DynamicLoading/PrimitiveManager.cs diff --git a/src/DataStreamSuite/DataStream/DynamicLoading/PrimitiveManager.cs b/src/DataStreamSuite/DataStream/DynamicLoading/PrimitiveManager.cs deleted file mode 100644 index 3cb2a4b84..000000000 --- a/src/DataStreamSuite/DataStream/DynamicLoading/PrimitiveManager.cs +++ /dev/null @@ -1,193 +0,0 @@ -using System; -using System.Collections; -using System.Reflection; -using System.IO; -using System.Collections.Generic; - -//using BCG.Utilities.BlueConfiguration; -//using BCG.Utilities; - -namespace BCG -{ - public class PrimitiveManager - { - private IDictionary myTypeLookupTable = new Hashtable (); - private static PrimitiveManager mySelf; - private ArrayList myAssemblyNames = new ArrayList (); - - private PrimitiveManager() - { - myLoadAllTypes (); - } - - private void myLoadAllTypes () - { - DataStreamLoadFileConfig config = DataStreamLoadFileConfig.instance; - - AppDomain currentDomain = AppDomain.CurrentDomain; - currentDomain.AssemblyResolve += new ResolveEventHandler(myAssemblyResolver); - loadAssembly (this.GetType ().Assembly.FullName); - - foreach (string assemblyName in config.loadedAssemblies.empty_if_null()) - { - string resolvedAssemblyPath = assemblyName; - if (!System.IO.Path.IsPathRooted(assemblyName)) - { - string appPath = Path.GetDirectoryName( - Assembly.GetAssembly(typeof(PrimitiveManager)).CodeBase.Substring("file:///".Length)); - resolvedAssemblyPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(appPath, assemblyName)); - } - loadAssembly(resolvedAssemblyPath); - } - } - - private static Assembly myAssemblyResolver(object sender, ResolveEventArgs args) - { - foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) - if (args.Name.Equals(a.FullName)) - return a; - return null; - } - - /// - /// Loads all the Stream types associated with the assembly - /// - /// - public void loadAssembly (string assemblyName) - { - if (myAssemblyNames.Contains (assemblyName)) - return; - myAssemblyNames.Add (assemblyName); - Assembly loadedAssembly = GetType ().Assembly.FullName.Equals (assemblyName) ? - GetType ().Assembly : - Assembly.LoadFrom (assemblyName); - - foreach (Type type in loadedAssembly.GetTypes()) - if (!type.IsInterface - && !type.IsAbstract - && type.IsSubclassOf (typeof(ViewStream)) - && type.IsPublic) - { - bool isVisible = true; - string userRep = createDefaultName (type.Name); - foreach (Attribute att in type.GetCustomAttributes (false)) - if (att is DataStreamUserRespresentation) - { - DataStreamUserRespresentation dsatt = att as DataStreamUserRespresentation; - isVisible = dsatt.isVisible; - userRep = isVisible ? - dsatt.friendlyName == null ? userRep : dsatt.friendlyName - : null; - break; - } - if (isVisible) - { - // BlueEventLog.logAssert(myTypeLookupTable [userRep] == null); - myTypeLookupTable[userRep] = type; - } - } - } - - public static string createDefaultName (string typeName) - { - if (!typeName.EndsWith ("Stream")) - return typeName; - string retVal = typeName.Remove (typeName.Length - "Stream".Length, "Stream".Length); - string startString = new String (new char[] {retVal[0]}).ToLower (); - retVal = retVal.Remove (0, 1); - retVal = retVal.Insert (0, startString); - return retVal; - } - - - - public static PrimitiveManager instance - { - get {return mySelf = mySelf == null ? new PrimitiveManager () : mySelf;} - } - - /// - /// Returns the Type name corresponding to the primitive name - /// - /// - /// - public string find (string friendlyName) - { - if (myTypeLookupTable[friendlyName] == null) - throw new ApplicationException (friendlyName + " is not the friendly name of any of the loaded types"); - return (myTypeLookupTable[friendlyName] as Type).Name; - } - - /// - /// Constructs and returns a ViewStream of type corresponding to the friendly name - /// - /// friendly primitive name - /// args can be null or an empty ICollection if the paramterless constructor - /// is the desired one - /// - public ViewStream build (string friendlyName, ICollection args) - { - object[] constructorArgs = null; - if (args != null && args.Count > 0) - { - constructorArgs = new object [args.Count]; - args.CopyTo (constructorArgs, 0); - } - if (myTypeLookupTable[friendlyName] == null) - throw new ApplicationException (friendlyName + " is not the friendly name of any of the loaded types"); - return - constructorArgs == null ? - Activator.CreateInstance (myTypeLookupTable[friendlyName] as Type) as ViewStream : - Activator.CreateInstance (myTypeLookupTable[friendlyName] as Type, constructorArgs) as ViewStream; - } - - /// - /// Returns the name of all the primitives - /// - /// - public string[] getPrimitiveNames () - { - string[] retVal = new string [myTypeLookupTable.Count]; - myTypeLookupTable.Keys.CopyTo (retVal, 0); - return retVal; - } - - /// - /// Return the types of all the streams - /// - /// - public Type[] getStreamTypes () - { - Type[] retVal = new Type [myTypeLookupTable.Count]; - myTypeLookupTable.Values.CopyTo (retVal, 0); - return retVal; - } - - public ICollection getConstructorSigns (string primitiveName) - { - ArrayList retVal = new ArrayList (); - // BlueEventLog.logAssert (myTypeLookupTable[primitiveName] != null); - Type t = myTypeLookupTable[primitiveName] as Type; - foreach (ConstructorInfo ci in t.GetConstructors()) - { - string sign = ""; - sign = t.Name + " ("; - foreach (ParameterInfo pi in ci.GetParameters ()) - sign += pi.ParameterType.Name + - " " + - pi.Name + ", "; - sign = sign.Length == (t.Name + " (").Length ? sign + ")": sign.Remove (sign.Length - 2, 2) + ")"; - retVal.Add (sign); - } - return retVal; - } - - public string getAssemblyName (string primitiveName) - { - // BlueEventLog.logAssert (myTypeLookupTable[primitiveName] != null); - return (myTypeLookupTable[primitiveName] as Type).Assembly.GetName().Name; - - } - - } -} diff --git a/src/DataStreamSuite/DataStream/DynamicLoading/DataStreamUserRespresentation.cs b/src/DataStreamSuite/DataStreamCore/DynamicLoading/DataStreamUserRespresentation.cs similarity index 100% rename from src/DataStreamSuite/DataStream/DynamicLoading/DataStreamUserRespresentation.cs rename to src/DataStreamSuite/DataStreamCore/DynamicLoading/DataStreamUserRespresentation.cs diff --git a/src/DataStreamSuite/DataStreamCore/DynamicLoading/PrimitiveManager.cs b/src/DataStreamSuite/DataStreamCore/DynamicLoading/PrimitiveManager.cs new file mode 100644 index 000000000..448bd2005 --- /dev/null +++ b/src/DataStreamSuite/DataStreamCore/DynamicLoading/PrimitiveManager.cs @@ -0,0 +1,191 @@ +namespace BCG +{ + using System; + using System.Collections; + using System.Reflection; + using System.IO; + + // using BCG.Utilities.BlueConfiguration; + using bts; + using System.Collections.Generic; + + public class PrimitiveManager + { + private IDictionary myTypeLookupTable = new Hashtable(); + private static PrimitiveManager mySelf; + private ArrayList myAssemblyNames = new ArrayList(); + + private PrimitiveManager() + { + myLoadAllTypes(); + } + + private void myLoadAllTypes() + { + DataStreamLoadFileConfig config = DataStreamLoadFileConfig.instance; + + AppDomain currentDomain = AppDomain.CurrentDomain; + currentDomain.AssemblyResolve += new ResolveEventHandler(myAssemblyResolver); + loadAssembly(this.GetType().Assembly.FullName); + + foreach (string assemblyName in config.loadedAssemblies ?? new string[] {"DataStream.dll"}) + { + string resolvedAssemblyPath = assemblyName; + if (!System.IO.Path.IsPathRooted(assemblyName)) + { + string appPath = Path.GetDirectoryName( + Assembly.GetAssembly(typeof(PrimitiveManager)).CodeBase.Substring("file:///".Length)); + resolvedAssemblyPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(appPath, assemblyName)); + } + loadAssembly(resolvedAssemblyPath); + } + } + + private static Assembly myAssemblyResolver(object sender, ResolveEventArgs args) + { + foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) + if (args.Name.Equals(a.FullName)) + return a; + return null; + } + + /// + /// Loads all the Stream types associated with the assembly + /// + /// + public void loadAssembly(string assemblyName) + { + if (myAssemblyNames.Contains(assemblyName)) + return; + myAssemblyNames.Add(assemblyName); + Assembly loadedAssembly = GetType().Assembly.FullName.Equals(assemblyName) ? + GetType().Assembly : + Assembly.LoadFrom(assemblyName); + + foreach (Type type in loadedAssembly.GetTypes()) + if (!type.IsInterface + && !type.IsAbstract + && type.IsSubclassOf(typeof(ViewStream)) + && type.IsPublic) + { + bool isVisible = true; + string userRep = createDefaultName(type.Name); + foreach (Attribute att in type.GetCustomAttributes(false)) + if (att is DataStreamUserRespresentation) + { + DataStreamUserRespresentation dsatt = att as DataStreamUserRespresentation; + isVisible = dsatt.isVisible; + userRep = isVisible ? + dsatt.friendlyName == null ? userRep : dsatt.friendlyName + : null; + break; + } + if (isVisible) + { + Log.assert(myTypeLookupTable[userRep] == null); + myTypeLookupTable[userRep] = type; + } + } + } + + public static string createDefaultName(string typeName) + { + if (!typeName.EndsWith("Stream")) + return typeName; + string retVal = typeName.Remove(typeName.Length - "Stream".Length, "Stream".Length); + string startString = new String(new char[] { retVal[0] }).ToLower(); + retVal = retVal.Remove(0, 1); + retVal = retVal.Insert(0, startString); + return retVal; + } + + + + public static PrimitiveManager instance + { + get { return mySelf = mySelf == null ? new PrimitiveManager() : mySelf; } + } + + /// + /// Returns the Type name corresponding to the primitive name + /// + /// + /// + public string find(string friendlyName) + { + if (myTypeLookupTable[friendlyName] == null) + throw new ApplicationException(friendlyName + " is not the friendly name of any of the loaded types"); + return (myTypeLookupTable[friendlyName] as Type).Name; + } + + /// + /// Constructs and returns a ViewStream of type corresponding to the friendly name + /// + /// friendly primitive name + /// args can be null or an empty ICollection if the paramterless constructor + /// is the desired one + /// + public ViewStream build(string friendlyName, ICollection args) + { + object[] constructorArgs = null; + if (args != null && args.Count > 0) + { + constructorArgs = new object[args.Count]; + args.CopyTo(constructorArgs, 0); + } + if (myTypeLookupTable[friendlyName] == null) + throw new ApplicationException(friendlyName + " is not the friendly name of any of the loaded types"); + return + constructorArgs == null ? + Activator.CreateInstance(myTypeLookupTable[friendlyName] as Type) as ViewStream : + Activator.CreateInstance(myTypeLookupTable[friendlyName] as Type, constructorArgs) as ViewStream; + } + + /// + /// Returns the name of all the primitives + /// + /// + public string[] getPrimitiveNames() + { + string[] retVal = new string[myTypeLookupTable.Count]; + myTypeLookupTable.Keys.CopyTo(retVal, 0); + return retVal; + } + + /// + /// Return the types of all the streams + /// + /// + public Type[] getStreamTypes() + { + Type[] retVal = new Type[myTypeLookupTable.Count]; + myTypeLookupTable.Values.CopyTo(retVal, 0); + return retVal; + } + + public ICollection getConstructorSigns(string primitiveName) + { + ArrayList retVal = new ArrayList(); + Log.assert(myTypeLookupTable[primitiveName] != null); + Type t = myTypeLookupTable[primitiveName] as Type; + foreach (ConstructorInfo ci in t.GetConstructors()) + { + string sign = ""; + sign = t.Name + " ("; + foreach (ParameterInfo pi in ci.GetParameters()) + sign += pi.ParameterType.Name + + " " + + pi.Name + ", "; + sign = sign.Length == (t.Name + " (").Length ? sign + ")" : sign.Remove(sign.Length - 2, 2) + ")"; + retVal.Add(sign); + } + return retVal; + } + + public string getAssemblyName(string primitiveName) + { + Log.assert(myTypeLookupTable[primitiveName] != null); + return (myTypeLookupTable[primitiveName] as Type).Assembly.GetName().Name; + } + } +}