Exemplo n.º 1
0
        /// <summary>
        /// Initializes the specified is webservice.
        /// </summary>
        /// <param name="isWebservice">if set to <c>true</c> [is webservice].</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">
        /// Could not initialize ConfigurationManager!
        /// or
        /// Could not initialize PropertyManager!
        /// or
        /// Could not initialize PluginManager!
        /// or
        /// Could not initialize DisplayManager!
        /// or
        /// Could not initialize DataStorageManager!
        /// </exception>
        public static bool Initialize(bool isWebservice)
        {
            bool result = false;

            try {
                BaseManager.IsWebservice = isWebservice;

                //Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + ";" + BaseManager.AssemblyPath + Path.DirectorySeparatorChar + "Libs");

                AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, ResolveEventArgs args) {
                    // Find name (first argument)
                    string assemblyName = args.Name.Substring(0, args.Name.IndexOf(','));
                    try {
                        // Build the path to DLL and load it
                        // WARNING: The path has to be absolute otherwise it will raise an ArgumentException (security)
                        string libsPath = BaseManager.AssemblyPath + Path.DirectorySeparatorChar + "Libs" + Path.DirectorySeparatorChar + assemblyName + ".dll";
                        string basePath = BaseManager.AssemblyPath + Path.DirectorySeparatorChar + assemblyName + ".dll";

                        if (File.Exists(libsPath))
                            return Assembly.LoadFile(libsPath);
                        else if (File.Exists(basePath))
                            return Assembly.LoadFile(basePath);
                        else
                            return null;
                    } catch (Exception ex) {
                        Trace.WriteLine(ex.Message, ex.StackTrace, LogCategory.Error);
                        throw ex;
                    }
                };

                _traceListener = new TraceListener(BaseManager.LogPath, BaseManager.DaysToKeepLogFiles);
                _traceListener.SetLoggingCategoties(new List<string> {
            #if DEBUG
                    LogCategory.Debug.GetDescription(),
            #endif
                    LogCategory.Info.GetDescription(),
                    LogCategory.Warning.GetDescription(),
                    LogCategory.Error.GetDescription() });
                System.Diagnostics.Trace.Listeners.Add(_traceListener);
                Trace.WriteLine("Initialize CoreSystem ...", LogCategory.Info);
                _managers.Clear();

                // Default managers must be added to the CoreSystem.
                ProjectManager projectManager = new ProjectManager();
                PropertyManager propertyManager = new PropertyManager();
                PluginManager pluginManager = new PluginManager();
                DisplayManager displayManager = new DisplayManager();
                DataStorageManager dataStorageManager = new DataStorageManager();

                _managers.Add(projectManager);
                _managers.Add(propertyManager);
                _managers.Add(pluginManager);
                _managers.Add(displayManager);
                _managers.Add(dataStorageManager);

                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

                if (pluginManager.Initialize() == false)
                    throw new Exception("Could not initialize PluginManager!");

                if (propertyManager.Initialize() == false)
                    throw new Exception("Could not initialize PropertyManager!");

                if (projectManager.Initialize() == false)
                    throw new Exception("Could not initialize ConfigurationManager!");

                _traceListener.SetLoggingCategoties(projectManager.Configuration.LogConfiguration.Categories);

                if (displayManager.Initialize() == false)
                    throw new Exception("Could not initialize DisplayManager!");

                if (dataStorageManager.Initialize() == false)
                    throw new Exception("Could not initialize DataStorageManager!");

                ExtensionManager extensionManager = CoreSystem.Managers.Find(m => m.Name.Contains("ExtensionManager")) as ExtensionManager;

                if (extensionManager.Initialize() == false)
                    throw new Exception("Could not initialize ExtensionManager!");

                _processor = new Core.Processor();
                _shell = new Core.Shell();
                _shell.Initialize();
                result = true;
            } catch (Exception ex) {
                Trace.WriteLine(ex.Message, ex.StackTrace, LogCategory.Error);
                throw ex;
            }

            return result;
        }