Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public virtual Type GetCompiledDslType(string url)
        {
            url = _storage.Normalize(url);
            if (_typeCache.Count == 0)
            {
                try
                {
                    string[] urls = _storage.GetScriptUrls().ToArray();
                    if (urls != null && urls.Contains(url))
                    {
                        TryRecompile(urls, CompilationMode.Compile);
                    }
                }
                catch (Exception ex)
                {
                    _typeCache["_123compile_error"] = new TypeCacheEntry {
                        Url = ex.Message
                    };
                    Console.WriteLine("Error when compiling all urls - will try to recompile only {1}: {0}", ex, url);
                }
            }
            TypeCacheEntry tp;

            if (_typeCache.TryGetValue(url, out tp))
            {
                return(tp.DslType);
            }
            Console.WriteLine("type not found in cache - compiling only {0}", url);
            Type t2 = TryRecompile(url, CompilationMode.Compile);

            return(t2);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public virtual Type GetCompiledDslType(string url)
        {
            url = _storage.Normalize(url);
            if (_typeCache.Count == 0)
            {
                try
                {
                    string[] urls = _storage.GetScriptUrls().ToArray();
                    if (urls != null && urls.Contains(url))
                    {
                        TryRecompile(urls, CompilationMode.Compile);
                    }
                }
                catch (Exception ex)
                {
                    _typeCache["_123compile_error"] = new TypeCacheEntry {
                        Url = ex.Message
                    };
                }
            }
            TypeCacheEntry tp;

            if (_typeCache.TryGetValue(url, out tp))
            {
                return(tp.DslType);
            }
            Type t2 = TryRecompile(url, CompilationMode.Compile);

            return(t2);
        }
        private static void AddToCache(string typeName, Type type)
        {
            lock (Cache)
            {
                Cache[typeName] = new TypeCacheEntry {
                    Type = type, Order = ++_cacheOrder
                };
                if (_cacheOrder > Cachemaxorder)
                {
                    ReorderCache();
                }
            }

            if (Cache.Count > Cachemaxsize)
            {
                lock (Cache)
                {
                    if (Cache.Count <= Cachemaxsize)
                    {
                        return;
                    }

                    var orderedTypeNames = Cache.OrderBy(c => c.Value.Order).Take(Cache.Count - Cachemaxsize).Select(c => c.Key).ToArray();
                    foreach (var cachedTypeName in orderedTypeNames)
                    {
                        Cache.Remove(cachedTypeName);
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="urls"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        protected virtual Assembly TryRecompile(string[] urls, CompilationMode mode)
        {
            CompilerContext cc = TryCompile(urls, mode == CompilationMode.CheckErrors ? true : false);

            if (cc.GeneratedAssembly == null && (mode == CompilationMode.Compile || mode == CompilationMode.CompileNoReplace))
            {
                throw new Exception("Generated assembly missing");
            }
            if (mode == CompilationMode.Compile)
            {
                foreach (var url in urls)
                {
                    string tn = _storage.GetTypeNameFromUrl(url);
                    Type   tp = cc.GeneratedAssembly.GetType(tn);
                    if (tp == null)
                    {
                        throw new Exception("Type not found for url: " + url);
                    }
                    TypeCacheEntry tce = new TypeCacheEntry
                    {
                        DslType  = tp,
                        Modified = false,
                        Url      = url
                    };
                    lock (_typeCache)
                    {
                        _typeCache.Remove(url);
                        _typeCache[url] = tce;
                    }
                }
            }
            return(cc.GeneratedAssembly);
        }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public virtual Type GetCompiledDslType(string url)
        {
            url = _storage.Normalize(url);
            var  dAll      = false;
            bool recompile = false;

            lock (_typeCache)
            {
                //recompile all only when first time
                if (_typeCache.Count == 0 && !CompileSeparately)
                {
                    try
                    {
                        string[] urls = _storage.GetScriptUrls().ToArray();
                        if (urls != null && urls.Contains(url))
                        {
                            log.Warn("Attempt to recompile all scripts for url {0} (in all: {1}). ", string.Join(",", urls), url);
                            TryRecompile(urls, CompilationMode.Compile);
                            dAll = true;
                        }
                        else
                        {
                            log.Warn("Will not recompile all scripts for url {0} ({1}). ", url, urls);
                        }
                    }
                    catch (Exception ex)
                    {
                        _typeCache["_123compile_error"] = new TypeCacheEntry {
                            Url = ex.Message, CompiledDate = DateTime.Now
                        };
                        log.Error("Error when compiling all urls - will try to recompile only {1}: {0}", ex, url);
                    }
                }
                TypeCacheEntry tce;
                if (!_typeCache.TryGetValue(url, out tce))
                {
                    recompile = true;
                }
                if (!recompile)
                {
                    recompile = CheckScriptModifications(url);
                }
                if (!recompile)
                {
                    return(tce.DslType);
                }
                log.Warn("dsl type not found in cache - compiling only {0}. CACHE: {1}", url, string.Join(",", _typeCache.Keys));
                if (dAll)
                {
                    throw new Exception("DSL type not found in cache after recompiling all: " + url);
                }
                Type t2 = TryRecompile(url, CompilationMode.Compile);
                return(t2);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="urls"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        protected virtual Assembly TryRecompile(string[] urls, CompilationMode mode)
        {
            log.Warn("TryRecompile {0}", string.Join(",", urls));
            var             d0 = DateTime.Now;
            CompilerContext cc = TryCompile(urls, mode == CompilationMode.CheckErrors ? true : false);

            log.Warn("TryRecompile compilation OK {0}", string.Join(",", urls));
            if (cc.GeneratedAssembly == null && (mode == CompilationMode.Compile || mode == CompilationMode.CompileNoReplace))
            {
                throw new Exception("Generated assembly missing");
            }

            if (mode == CompilationMode.Compile)
            {
                List <TypeCacheEntry> ents = new List <TypeCacheEntry>();
                foreach (var url in urls)
                {
                    string tn = _storage.GetTypeNameFromUrl(url);
                    Type   tp = cc.GeneratedAssembly.GetType(tn);
                    if (tp == null)
                    {
                        foreach (var t in cc.GeneratedAssembly.GetTypes())
                        {
                            Console.WriteLine("Type: {0}", t.FullName);
                        }

                        throw new Exception("Type not found for url: " + url + ", type name: " + tn);
                    }
                    TypeCacheEntry tce = new TypeCacheEntry
                    {
                        DslType      = tp,
                        Modified     = false,
                        Url          = url,
                        CompiledDate = DateTime.Now
                    };
                    ents.Add(tce);
                }
                lock (_typeCache)
                {
                    foreach (var ent in ents)
                    {
                        _typeCache.Remove(ent.Url);
                        _typeCache[ent.Url] = ent;
                        log.Warn("TC:{0}=>{1}, CTime: {2}", ent.Url, ent.DslType, DateTime.Now - d0);
                    }
                }
            }

            return(cc.GeneratedAssembly);
        }
Exemplo n.º 7
0
        private static TypeCacheEntry GetEntry(string className)
        {
            Util.ThrowIfStringParameterNullOrEmpty(className, "className");

            TypeCacheEntry entry;

            lock (Lck)
            {
                if (!Entries.TryGetValue(className, out entry))
                {
                    entry = new TypeCacheEntry(className);
                    Entries.Add(className, entry);
                }
            }
            return(entry);
        }
Exemplo n.º 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="urls"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        protected virtual Assembly TryRecompile(string[] urls, CompilationMode mode)
        {
            var cc = TryCompile(urls, mode == CompilationMode.CheckErrors ? true : false);

            if (cc.GeneratedAssembly == null && (mode == CompilationMode.Compile || mode == CompilationMode.CompileNoReplace))
            {
                throw new Exception("Generated assembly missing");
            }
            if (mode != CompilationMode.Compile)
            {
                return(cc.GeneratedAssembly);
            }
            foreach (var url in urls)
            {
                var tn = _storage.GetTypeNameFromUrl(url);
                var tp = cc.GeneratedAssembly.GetType(tn);
                if (tp == null)
                {
                    foreach (var t in cc.GeneratedAssembly.GetTypes())
                    {
                        Console.WriteLine($"Type: {t.FullName}");
                    }

                    throw new Exception("Type not found for url: " + url + ", type name: " + tn);
                }
                var tce = new TypeCacheEntry
                {
                    DslType  = tp,
                    Modified = false,
                    Url      = url
                };
                lock (_typeCache)
                {
                    _typeCache.Remove(url);
                    _typeCache[url] = tce;
                }
            }
            return(cc.GeneratedAssembly);
        }
Exemplo n.º 9
0
        private static bool TryGetCacheEntry(string className, out TypeCacheEntry entry)
        {
            Util.ThrowIfStringParameterNullOrEmpty(className, "className");

            lock (Lck)
            {
                if (Entries.TryGetValue(className, out entry))
                {
                    return(true);
                }

                try
                {
                    entry = new TypeCacheEntry(className);
                    Entries.Add(className, entry);
                    return(true);
                }
                catch (TypeLoadException)
                {
                    log.Warn($"The class '{className}' is not exposed in the XenAPI.");
                    return(false);
                }
            }
        }
Exemplo n.º 10
0
        private static TypeCacheEntry GetEntry(string className)
        {
            Util.ThrowIfStringParameterNullOrEmpty(className, "className");

            TypeCacheEntry entry;
            lock (Lck)
            {
                if (!Entries.TryGetValue(className, out entry))
                {
                    entry = new TypeCacheEntry(className);
                    Entries.Add(className, entry);
                }
            }
            return entry;
        }