Beispiel #1
0
        internal bool AddIndex(string dllPath)
        {
            if (!File.Exists(dllPath))
            {
                return(false);
            }

            string newIndexFileName = dllPath.Split('\\').Last();
            int    tries            = 0;

            while (File.Exists(string.Format("{0}\\{1}", this._indexesStoragePath, newIndexFileName)))
            {
                newIndexFileName = string.Format("{0}({1}).{2}", dllPath.Split('\\').Last().Split('.').First(), ++tries,
                                                 dllPath.Split('\\').Last().Split('.').Last());
            }


            AppDomain tempDomain = AppDomain.CreateDomain("tempDomain");
            Assembly  assembly   = tempDomain.Load(AssemblyName.GetAssemblyName(dllPath));
            //Assembly assembly = Assembly.LoadFile(dllPath);
            bool ret = false;

            Type[] assemblyTypes;
            try
            {
                assemblyTypes = assembly.GetTypes();
            }
            catch (ReflectionTypeLoadException ex)
            {
                if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger() != null)
                {
                    MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger().Log("IndexMechanism", string.Format("Iterating types in new index plugin assembly failed\n{0}", ex.LoaderExceptions.First().Message),
                                                                                           MessageLevel.Warning);
                }
                return(false);
            }
            catch (Exception ex)
            {
                if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger() != null)
                {
                    MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger().Log("IndexMechanism", string.Format("Iterating types in new index plugin assembly failed\n{0}", ex.Message),
                                                                                           MessageLevel.Warning);
                }
                return(false);
            }

            foreach (Type type in assemblyTypes)
            {
                if (type.IsClass && type.GetInterfaces().Contains(typeof(IndexPlugin.IIndex <object>)))
                {
                    try
                    {
                        IndexPlugin.IIndex <object> i = (IndexPlugin.IIndex <object>)Activator.CreateInstance(type);

                        if (!i.EmptyIndexData.GetType().IsSerializable)
                        {
                            throw new Exception();
                        }

                        IndexInfo <T> ixi = new IndexInfo <T>
                        {
                            IndexFileName  = newIndexFileName,
                            IndexName      = i.Name,
                            IndexClassName = type.FullName,
                            IndexID        = CORE.Settings <T> .GetInstance().NextIndexesIdentity
                        };

                        if (IndexInfo <T> .Save(ixi,
                                                string.Format("{0}\\{1}-{2}.xml", this._indexesStoragePath, type.GUID,
                                                              newIndexFileName)))
                        {
                            _indexes.Add(ixi);
                            ret = true;
                        }
                        i.Dispose();
                    }
                    catch (Exception e)
                    {
                    }
                }
            }

            if (ret)
            {
                try
                {
                    File.Copy(dllPath, string.Format("{0}\\{1}", this._indexesStoragePath, newIndexFileName));
                    File.SetAttributes(string.Format("{0}\\{1}", this._indexesStoragePath, newIndexFileName),
                                       FileAttributes.Normal);
                    if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger() != null)
                    {
                        MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger().Log("IndexMechanism", string.Format("Added new index plugin assembly: {0}", newIndexFileName),
                                                                                               MessageLevel.Info);
                    }
                }
                catch (Exception ex)
                {
                    if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger() != null)
                    {
                        MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger().Log("IndexMechanism", string.Format("Unable to add new index plugin assebly {0}", dllPath),
                                                                                               MessageLevel.Error);
                    }
                    ret = false;
                }
            }

            AppDomain.Unload(tempDomain);
            return(ret);
        }
Beispiel #2
0
        internal IIndex <T> GetIndex(int indexId)
        {
            IndexInfo <T> index;

            var i = _indexes.Where(p => p.IndexID == indexId);

            if (i.Count() != 1)
            {
                throw new WronxIndexIdException(indexId);
            }
            else
            {
                index = i.Single();
            }

            if (_indexesObjects.ContainsKey(index))
            {
                return(_indexesObjects[index]);
            }

            Monitor.Enter(_instance);
            try
            {
                Assembly assembly =
                    _pluginsDomain.Load(
                        AssemblyName.GetAssemblyName(string.Format("{0}\\{1}", this._indexesStoragePath,
                                                                   index.IndexFileName)));

                Type[] assemblyTypes;
                try
                {
                    assemblyTypes = assembly.GetTypes();
                }
                catch (Exception ex)
                {
                    if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger() != null)
                    {
                        MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger().Log("IndexMechanism",
                                                                                               string.Format("Iterating types in index plugin {0} failed\n{1}", index.IndexFileName, ex.Message),
                                                                                               MessageLevel.Error);
                    }
                    Monitor.Exit(_instance);
                    return(null);
                }

                var t = assemblyTypes
                        .Where(p => p.IsClass)
                        .Where(p => p.FullName == index.IndexClassName);

                if (t.Count() == 1)
                {
                    Type type = t.Single();
                    IndexPlugin.IIndex <T> ixi = (IndexPlugin.IIndex <T>)Activator.CreateInstance(type);
                    _indexesObjects.Add(index, ixi);
                    ixi.SettingsChanged += new settingsChangedHandler <T>(IIndexSettingsChanged);
                    ixi.SetSettings(index.IndexSettings);
                }
            }
            catch (Exception ex)
            {
                if (MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger() != null)
                {
                    MUTDOD.Server.Common.IndexMechanism.IndexMechanism <T> .GetLoger().Log("IndexMechanism",
                                                                                           string.Format("Unable to read index assembly {0} from file {1}\n{2}", index.IndexClassName,
                                                                                                         index.IndexFileName, ex), MessageLevel.Error);
                }

                Monitor.Exit(_instance);
                return(null);
            }

            Monitor.Exit(_instance);
            return(_indexesObjects[index]);
        }