Ejemplo n.º 1
0
        //public static void RemoveAttributeIfExists(DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeFunction function)
        //{
        //    function.RemoveAttributeIfExists("Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.CachingCallHandler");
        //}

        public void SetAttribute(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeFunction function)
        {
            function.AddAttribute("Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.CachingCallHandler",
                                  context.Strategy.StrategyId, false,
                                  String.Empty, expirationTime.Hours.ToString(),
                                  String.Empty, expirationTime.Minutes.ToString(),
                                  String.Empty, expirationTime.Seconds.ToString());
        }
Ejemplo n.º 2
0
        //public static void RemoveAttributeIfExists(DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeFunction function)
        //{
        //    function.RemoveAttributeIfExists("Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.PerformanceCounterCallHandler");
        //}

        public void SetAttribute(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeFunction function)
        {
            Dictionary <string, string> args = new Dictionary <string, string>();

            args.Add(CandleCodeElement.EmptyAttributeNameSig + "0", "\"" + categoryName + "\"");
            args.Add(CandleCodeElement.EmptyAttributeNameSig + "1", "\"" + instanceName + "\"");
            if (incrementAverageCallDuration)
            {
                args.Add("IncrementAverageCallDuration", "true");
            }
            if (incrementCallsPerSecond)
            {
                args.Add("IncrementCallsPerSecond", "true");
            }
            if (incrementExceptionsPerSecond)
            {
                args.Add("IncrementExceptionsPerSecond", "true");
            }
            if (incrementNumberOfCalls)
            {
                args.Add("IncrementNumberOfCalls", "true");
            }
            if (incrementTotalExceptions)
            {
                args.Add("IncrementTotalExceptions", "true");
            }
            if (useTotalCounter)
            {
                args.Add("UseTotalCounter", "true");
            }

            function.AddAttribute("Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.PerformanceCounterCallHandler",
                                  context.Strategy.StrategyId,
                                  false,
                                  args);
        }
Ejemplo n.º 3
0
 public void SetAttribute(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeFunction function)
 {
     function.AddAttribute("Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler", context.Strategy.StrategyId, false, PrepareLogAttribute(function.ToString()));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Applique les stratégies d'injection de code
        /// </summary>
        /// <param name="projectItem">The project item.</param>
        /// <param name="element">The element.</param>
        /// <param name="context">The context.</param>
        public static void ApplyCodeInjectionStrategies(ProjectItem projectItem, ICustomizableElement element, GenerationContext context)
        {
            ILogger logger = ServiceLocator.Instance.GetService <ILogger>();

            if (logger != null)
            {
                logger.BeginStep("Injection code generation", LogType.Info);
                logger.Write("Injection code generation", String.Concat("Current element is ", element.Name, " (id=", element.Id, ")"), LogType.Debug);
            }

            EnvDTE80.FileCodeModel2 fcm = null;
            if (projectItem != null)
            {
                fcm = projectItem.FileCodeModel as EnvDTE80.FileCodeModel2;
                if (fcm == null)
                {
                    if (logger != null)
                    {
                        logger.Write("Injection code generation", "No code model associated with this element - exit", LogType.Debug);
                        logger.EndStep();
                    }
                    return;
                }
                //fcm.BeginBatch();
            }

            try
            {
                CodeInjectionContext gc = new CodeInjectionContext(context);

                List <StrategyBase> strategies = new List <StrategyBase>();

                // Execution des stratégies
                foreach (StrategyBase strategy in element.GetStrategies(false))
                {
                    if (!(strategy is IStrategyCodeInjector))
                    {
                        continue;
                    }

                    if (logger != null)
                    {
                        logger.Write("Injection code generation", String.Concat("Strategy ", strategy.StrategyId), LogType.Debug);
                    }
                    if (context.SelectedStrategies != null && (context.SelectedStrategies != null && !context.SelectedStrategies.Contains(strategy)))
                    {
                        if (logger != null)
                        {
                            logger.Write("Injection code generation", "Strategy skipped because this is not a selected strategy", LogType.Debug);
                        }
                        continue;
                    }
                    strategies.Add(strategy);
                }

                // Tri dans l'ordre d'exécution
//                strategies.Sort(delegate(IStrategyCodeInjector a, IStrategyCodeInjector b) { return a.ExecutionOrder.CompareTo(b.ExecutionOrder); });

                foreach (StrategyBase injector in strategies)
                {
                    if (logger != null)
                    {
                        logger.BeginStep(String.Concat("Run strategy ", injector.DisplayName, " id=", injector.StrategyId), LogType.Debug);
                    }
                    gc.CurrentElement = element;
                    gc.Strategy       = (IStrategyCodeInjector)injector;

                    try
                    {
                        if (gc.GenerationContext.GenerationPass == GenerationPass.MetaModelUpdate)
                        {
                            ((IStrategyCodeInjector)injector).OnMetaModelUpdate(gc);
                        }
                        else
                        {
                            CodeModelWalker walker = new CodeModelWalker(new CodeInjectorVisitor(gc));
                            walker.Traverse(fcm);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (logger != null)
                        {
                            logger.WriteError(injector.DisplayName, "Code Injection error", ex);
                        }
                    }
                    finally
                    {
                        gc.Strategy       = null;
                        gc.CurrentElement = null;
                        if (logger != null)
                        {
                            logger.EndStep();
                        }
                    }
                }
            }
            finally
            {
                if (fcm != null)
                {
                    //    fcm.EndBatch();
                    //    fcm.Synchronize();
                }
            }
            if (logger != null)
            {
                logger.EndStep();
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeInjectorVisitor"/> class.
 /// </summary>
 /// <param name="context">The context.</param>
 public CodeInjectorVisitor(CodeInjectionContext context)
 {
     _context  = context;
     _injector = context.Strategy;
 }
Ejemplo n.º 6
0
 void IStrategyCodeInjector.OnGenerateClass(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeClass clazz)
 {
 }
Ejemplo n.º 7
0
        void IStrategyCodeInjector.OnMetaModelUpdate(CodeInjectionContext context)
        {
            // Reference pour le ServiceLocator
            if (context.CurrentElement is Layer)
            {
                Layer layer = context.CurrentElement as Layer;
                foreach (ClassImplementation clazz in layer.Classes)
                {
                    foreach (ClassUsesOperations service in ClassUsesOperations.GetLinksToServicesUsed(clazz))
                    {
                        if (!context.GenerationContext.Mode.CheckConfigurationMode(service.ConfigurationMode) || service.TargetService is ExternalServiceContract)
                        {
                            continue;
                        }

                        foreach (Implementation impl in Implementation.GetLinksToImplementations((ServiceContract)service.TargetService))
                        {
                            if (context.GenerationContext.Mode.CheckConfigurationMode(impl.ConfigurationMode))
                            {
                                if (CheckAllHandler(impl.ClassImplementation, impl.ClassImplementation.Layer))
                                {
                                    layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.PolicyInjection");
                                    return;
                                }
                            }
                        }
                    }
                }
            }

            if (context.CurrentElement is ClassImplementation)
            {
                ClassImplementation clazz = context.CurrentElement as ClassImplementation;
                if (clazz.Contract == null)
                {
                    return;
                }

                bool flag = false;
                foreach (Operation op in clazz.Contract.Operations)
                {
                    IPIABHandler handler = CheckHandler <LogCallHandler>(LogCallHandlerProperty, op, clazz);
                    if (handler != null && handler.Enabled)
                    {
                        ((ClassImplementation)context.CurrentElement).Layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.Logging", ReferenceScope.Runtime, "*");
                        flag = true;
                    }

                    handler = CheckHandler <CacheCallHandler>(CacheCallHandlerProperty, op, clazz);
                    if (handler != null && handler.Enabled)
                    {
                        ((ClassImplementation)context.CurrentElement).Layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.Caching", ReferenceScope.Runtime, "*");
                        flag = true;
                    }

                    // PerformanceCounterCallHandler
                    handler = CheckHandler <PerformanceCounterCallHandler>(PerformanceCounterCallHandlerProperty, op, clazz);
                    if (handler != null && handler.Enabled)
                    {
                        ((ClassImplementation)context.CurrentElement).Layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation", ReferenceScope.Runtime, "*");
                        flag = true;
                    }
                }

                if (flag)
                {
                    ((ClassImplementation)context.CurrentElement).Layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.PolicyInjection");
                    ((ClassImplementation)context.CurrentElement).Layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers", ReferenceScope.Runtime | ReferenceScope.Compilation, "*");
                }
            }
        }
Ejemplo n.º 8
0
 void IStrategyCodeInjector.OnGenerateVariable(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeVariable variable)
 {
 }
Ejemplo n.º 9
0
 string[] IStrategyCodeInjector.OnGenerateUsing(CodeInjectionContext context)
 {
     return(null);
 }
Ejemplo n.º 10
0
 void IStrategyCodeInjector.OnGenerateStruct(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeStruct structure)
 {
 }
Ejemplo n.º 11
0
 void IStrategyCodeInjector.OnGenerateProperty(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeProperty prop)
 {
 }
Ejemplo n.º 12
0
 void IStrategyCodeInjector.OnGenerateParameter(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeParameter parm)
 {
 }
Ejemplo n.º 13
0
 void IStrategyCodeInjector.OnGenerateInterface(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeInterface interf)
 {
 }
Ejemplo n.º 14
0
        void IStrategyCodeInjector.OnGenerateFunction(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeFunction function)
        {
            // Service factory
            if (context.CurrentElement is Layer)
            {
                // On part du principe que le template du servicelocator à généré une classe nommée ServiceLocator et
                // une méthode par création d'instance dont le nom commence par CreateInstanceOfxxxxxx
                if (function.Parent.Name == "ServiceLocatorBase" && function.Name.StartsWith("CreateInstanceOf"))
                {
                    string currentName = function.Name.Substring("CreateInstanceOf".Length);
                    foreach (ClassImplementation clazz in ((Layer)context.CurrentElement).Classes)
                    {
                        foreach (ClassUsesOperations service in ClassUsesOperations.GetLinksToServicesUsed(clazz))
                        {
                            if (!context.GenerationContext.Mode.CheckConfigurationMode(service.ConfigurationMode) || service.TargetService is ExternalServiceContract)
                            {
                                continue;
                            }

                            ServiceContract contract = service.TargetService as ServiceContract;
                            if (contract == null)
                            {
                                continue;
                            }

                            foreach (Implementation impl in Implementation.GetLinksToImplementations(contract))
                            {
                                if (context.GenerationContext.Mode.CheckConfigurationMode(impl.ConfigurationMode) && impl.ClassImplementation.Name == currentName)
                                {
                                    if (impl.ClassImplementation.Contract != null)
                                    {
                                        foreach (Operation op in impl.ClassImplementation.Contract.Operations)
                                        {
                                            if (CheckAllHandler(op, impl.ClassImplementation))
                                            {
                                                function.ReplaceBody(
                                                    @"           string path = ConfigurationManager.AppSettings[key];
            Type t = Assembly.Load(path).GetType(""" + impl.ClassImplementation.FullName + @""");
            PropertyInfo p = typeof(Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection).GetProperty(""DefaultPolicyInjector"", BindingFlags.Static | BindingFlags.NonPublic);
            Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjector pi = (Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjector)p.GetValue(null, null);
            return (" + contract.FullName + ")pi.Create(t, typeof(" + contract.FullName + @"));
");
                                                return;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Attribut
            if (context.CurrentElement is ClassImplementation)
            {
                ClassImplementation clazz = context.CurrentElement as ClassImplementation;
                if (clazz.Name + "Base" == function.Parent.Name || clazz.Name == function.Parent.Name)
                {
                    Operation op = function.FindOperationFromContract(clazz.Contract);
                    if (op != null)
                    {
                        // LogCallHandler
                        IPIABHandler handler = CheckHandler <LogCallHandler>(LogCallHandlerProperty, op, clazz);
                        if (handler != null && handler.Enabled)
                        {
                            handler.SetAttribute(context, function);
                        }


                        // CacheCallHandler
                        handler = CheckHandler <CacheCallHandler>(CacheCallHandlerProperty, op, clazz);
                        if (handler != null && handler.Enabled)
                        {
                            handler.SetAttribute(context, function);
                        }

                        // PerformanceCounterCallHandler
                        handler = CheckHandler <PerformanceCounterCallHandler>(PerformanceCounterCallHandlerProperty, op, clazz);
                        if (handler != null && handler.Enabled)
                        {
                            handler.SetAttribute(context, function);
                        }
                    }
                }
            }
        }
Ejemplo n.º 15
0
 void IStrategyCodeInjector.OnGenerateEnum(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeEnum enumeration)
 {
 }