Example #1
0
        public void CreateEventsTest()
        {
            _log = new BdoLog();

            for (int i = 0; i < _testData.itemNumber; i++)
            {
                _log.AddError("Error" + i);
                _log.AddException("Exception" + i);
                _log.AddMessage("Message" + i);
                _log.AddWarning("Warning" + i);
                _log.AddSubLog(new BdoLog());
            }

            Test(_log);
        }
        /// <summary>
        /// Loads the specified BindOpen extension dictionary.
        /// </summary>
        /// <param name="assembly">The assembly to consider.</param>
        /// <param name="log">The log to consider.</param>
        /// <returns>The created library.</returns>
        private ITBdoExtensionDictionaryDto <T> ExtractDictionaryFromAssembly <T>(
            Assembly assembly,
            IBdoLog log = null) where T : BdoExtensionItemDefinitionDto
        {
            TBdoExtensionDictionaryDto <T> dictionary = default;

            if (assembly != null)
            {
                string resourceFileName = GetDictionaryResourceName <T>();

                string resourceFullName = Array.Find(
                    assembly.GetManifestResourceNames(), p => p.EndsWith(resourceFileName, StringComparison.OrdinalIgnoreCase));

                Stream stream = null;
                if (resourceFullName == null)
                {
                    log?.AddWarning("No dictionary named '" + resourceFileName + "' found in assembly");
                }
                else
                {
                    try
                    {
                        stream = assembly.GetManifestResourceStream(resourceFullName);
                        if (stream == null)
                        {
                            log?.AddError("Could not open the item dictionary named '" + resourceFullName + "' in assembly");
                        }
                        else
                        {
                            Type          type       = GetDictionaryType <T>();
                            XmlSerializer serializer = new XmlSerializer(type);
                            dictionary = (TBdoExtensionDictionaryDto <T>)serializer.Deserialize(stream);
                        }
                    }
                    catch (Exception ex)
                    {
                        log?.AddException(ex);
                    }
                    finally
                    {
                        stream?.Close();
                    }
                }
            }

            return(dictionary);
        }
        /// <summary>
        /// Loads the script word dictionary from the specified assembly.
        /// </summary>
        /// <param name="assembly">The assembly to consider.</param>
        /// <param name="extensionDefinition">The extension definition to consider.</param>
        /// <param name="log">The log to consider.</param>
        /// <returns></returns>
        private int LoadScripwordDictionaryFromAssembly(
            Assembly assembly,
            IBdoExtensionDefinition extensionDefinition,
            IBdoLog log = null)
        {
            if (assembly == null)
            {
                return(-1);
            }

            // we load the carrier dictionary from the assembly

            IBdoScriptwordDictionaryDto dictionaryDto = (IBdoScriptwordDictionaryDto)ExtractDictionaryFromAssembly <BdoScriptwordDefinitionDto>(assembly, log);

            // we define definitions

            int count = 0;

            if (dictionaryDto == null)
            {
                log?.AddWarning(title: "No script word dictionary was found");
            }
            else
            {
                List <BdoScriptwordDefinition> scriptwordDefinitions = new List <BdoScriptwordDefinition>();

                var types = assembly.GetTypes().Where(p => p.GetCustomAttributes(typeof(BdoScriptwordDefinitionAttribute)).Any());
                foreach (Type type in types)
                {
                    // we feach methods
                    var methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
                    foreach (MethodInfo methodInfo in methodInfos)
                    {
                        if (methodInfo.GetCustomAttribute(typeof(BdoScriptwordAttribute)) is BdoScriptwordAttribute scriptWordAttribute)
                        {
                            // we determine the name of the definition

                            string definitionName = scriptWordAttribute.Name;

                            // we update the definition with the dictionary if there is one

                            if (dictionaryDto != null)
                            {
                                IBdoScriptwordDefinitionDto definitionDto = dictionaryDto.GetDefinition(definitionName, methodInfo.Name);

                                if (definitionDto == null)
                                {
                                    log?.AddError(title: "Script word '" + methodInfo.Name + "' not found in dictionary");
                                }
                                else
                                {
                                    definitionDto.CallingClass = type.FullName;
                                    definitionDto.LibraryId    = extensionDefinition?.Dto?.Id;

                                    // we create the runtime definition

                                    BdoScriptwordDefinition itemDefinition = new BdoScriptwordDefinition(extensionDefinition, definitionDto);

                                    try
                                    {
                                        if (methodInfo.GetParameters().Length == 0)
                                        {
                                            itemDefinition.RuntimeBasicFunction += methodInfo.CreateDelegate(
                                                typeof(BdoScriptwordBasicDelegare)) as BdoScriptwordBasicDelegare;
                                        }
                                        else
                                        {
                                            itemDefinition.RuntimeScopedFunction += methodInfo.CreateDelegate(
                                                typeof(BdoScriptwordScopedDelegate)) as BdoScriptwordScopedDelegate;
                                        }

                                        scriptwordDefinitions.Add(itemDefinition);

                                        count++;
                                    }
                                    catch (ArgumentException)
                                    {
                                        log?.AddError(
                                            title: "Incompatible function ('" + methodInfo.Name + "')",
                                            description: "Function '" + definitionDto.RuntimeFunctionName + "' in class '" + definitionDto.CallingClass + "' has inexpected parameters.");
                                    }
                                }
                            }
                        }
                    }
                }

                // we build the script word tree

                BuildScriptwordTree(dictionaryDto, scriptwordDefinitions, log);
            }

            return(count);
        }
Example #4
0
        /// <summary>
        /// Loads the specified library.
        /// </summary>
        /// <param name="libraryReference">The library reference to consider.</param>
        /// <returns>Returns the loaded library.</returns>
        private IBdoLog LoadLibrary(IBdoAssemblyReference libraryReference)
        {
            var log = new BdoLog();

            if (libraryReference != null && _loadOptions?.SourceKinds != null)
            {
                try
                {
                    Assembly assembly = null;

                    // first we load the assembly

                    IBdoLog firstLog = new BdoLog()
                    {
                        DisplayName = "Loading library '" + libraryReference.Name + "'"
                    };

                    foreach (DatasourceKind dataSourceKind in _loadOptions?.SourceKinds)
                    {
                        IBdoLog subLog = firstLog.AddSubLog(title: "Loading assembly from '" + dataSourceKind.ToString() + "'", eventKind: EventKinds.Message);

                        switch (dataSourceKind)
                        {
                        case DatasourceKind.Memory:
                            if (!string.IsNullOrEmpty(libraryReference.Name))
                            {
                                assembly = AppDomainPool.LoadAssembly(_appDomain, libraryReference.Name, subLog);
                            }
                            else
                            {
                                subLog?.AddWarning("File name missing");
                            }
                            break;

                        case DatasourceKind.Repository:
                            string fileName = libraryReference.FileName;
                            if (string.IsNullOrEmpty(fileName))
                            {
                                fileName = libraryReference.Name + ".dll";
                            }

                            string filePath = _loadOptions.LibraryFolderPath.EndingWith(@"\").ToPath() + fileName;
                            if (!File.Exists(filePath))
                            {
                                subLog?.AddError("Could not find the assembly file path '" + filePath + "'");
                            }
                            else
                            {
                                assembly = AppDomainPool.LoadAssemblyFromFile(_appDomain, filePath, subLog);

                                if (assembly == null)
                                {
                                    subLog?.AddError("Could not load assembly '" + filePath + "'");
                                }
                                else
                                {
                                    subLog?.AddCheckpoint("Loading assembly from file '" + filePath + "'");
                                    assembly = Assembly.LoadFrom(filePath);
                                }
                            }
                            break;

                        case DatasourceKind.RestApi:
                            break;
                        }

                        if (assembly != null)
                        {
                            subLog?.AddMessage("Assembly loaded");
                            break;
                        }
                    }

                    // if we have an assembly then we index library items

                    if (assembly == null)
                    {
                        log?.AddSubLog(firstLog, p => p.HasErrorsOrExceptionsOrWarnings());
                    }
                    else
                    {
                        firstLog.GetEvents(true, EventKinds.Error, EventKinds.Exception).ForEach(p => p.Kind = EventKinds.Warning);
                        log?.AddSubLog(firstLog);

                        // we get the extension definition

                        IBdoExtensionDefinition extensionDefinition = ExtractExtensionDefinition(assembly, null, log);

                        // we load the using assemblies

                        if (extensionDefinition?.Dto?.UsingAssemblyFileNames != null)
                        {
                            foreach (var st in extensionDefinition.Dto.UsingAssemblyFileNames)
                            {
                                var fileName = st;
                                if (!fileName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
                                {
                                    fileName += ".dll";
                                }
                                IBdoAssemblyReference reference = BdoAssemblyReferenceFactory.Create(st).WithFileName(fileName);

                                log.AddSubLog(LoadExtensionsInStore(reference), title: "Loading using extensions...");
                            }
                        }

                        // we load the item definition specifiying the extension definition

                        foreach (BdoExtensionItemKind kind in new[] {
                            BdoExtensionItemKind.Carrier,
                            BdoExtensionItemKind.Connector,
                            BdoExtensionItemKind.Entity,
                            BdoExtensionItemKind.Handler,
                            BdoExtensionItemKind.Metrics,
                            BdoExtensionItemKind.Routine,
                            BdoExtensionItemKind.Scriptword,
                            BdoExtensionItemKind.Task
                        })
                        {
                            IBdoLog subSubLog = new BdoLog();
                            int     count     = LoadDictionary(assembly, kind, extensionDefinition, subSubLog);

                            if (subSubLog.HasErrorsOrExceptionsOrWarnings())
                            {
                                log.AddSubLog(
                                    subSubLog,
                                    title: "Dictionary '" + kind.ToString() + "' loaded (" + count.ToString() + " items added)");
                            }
                            else
                            {
                                log.AddMessage("Dictionary '" + kind.ToString() + "' loaded (" + count.ToString() + " items added)");
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    log?.AddException(exception);
                }
            }

            return(log);
        }