public void Initialise(BaseDto configuration, DictionaryParameters parameters)
        {
            Contract.Assert(configuration is ScheduledJobsWorkerConfiguration);
            
            parameters = parameters ?? new DictionaryParameters();

            var cfg = configuration as ScheduledJobsWorkerConfiguration;

            // get communication update and retry variables
            cfg.UpdateIntervalInMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["UpdateIntervalMinutes"]);
            cfg.UpdateIntervalInMinutes = 
                (0 != cfg.UpdateIntervalInMinutes) ? 
                cfg.UpdateIntervalInMinutes : 
                ScheduledJobsWorkerConfiguration.UPDATE_INTERVAL_IN_MINUTES_DEFAULT;

            cfg.ServerNotReachableRetries = Convert.ToInt32(ConfigurationManager.AppSettings["ServerNotReachableRetries"]);
            cfg.ServerNotReachableRetries = 
                cfg.UpdateIntervalInMinutes * (0 != cfg.ServerNotReachableRetries ?
                cfg.ServerNotReachableRetries : 
                ScheduledJobsWorkerConfiguration.SERVER_NOT_REACHABLE_RETRIES_DEFAULT);

            // apply parameters if overridden on command line
            var uri = ConfigurationManager.AppSettings["Uri"];
            if(parameters.ContainsKey("args0"))
            {
                uri = parameters["args0"] as string;
            }
            Contract.Assert(!string.IsNullOrWhiteSpace(uri));
            cfg.Uri = new Uri(uri);

            cfg.ManagementUriName = ConfigurationManager.AppSettings["ManagementUri"];
            if(parameters.ContainsKey("args1"))
            {
                cfg.ManagementUriName = parameters["args1"] as string;
            }
            Contract.Assert(!string.IsNullOrWhiteSpace(cfg.ManagementUriName));

            // load plugins
            var configurationLoader = new PluginLoaderConfigurationFromAppSettingsLoader();
            var pluginLoader = new PluginLoader(configurationLoader, cfg.Logger);
            cfg.Plugins = pluginLoader.InitialiseAndLoad();
            Contract.Assert(0 < cfg.Plugins.Count, "No plugins loaded. Cannot continue.");

            // get credentials to connect to Appclusive HOST server
            var credentialSection = ConfigurationManager.GetSection(AppclusiveCredentialSection.SECTION_NAME) as AppclusiveCredentialSection;
            if(null == credentialSection)
            {
                Trace.WriteLine("No credential in app.config section '{0}' defined. Using 'DefaultNetworkCredentials'.", AppclusiveCredentialSection.SECTION_NAME, "");
                
                cfg.Credential = CredentialCache.DefaultNetworkCredentials;
            }
            else
            {
                Trace.WriteLine("Credential in app.config section '{0}' found. Using '{1}\\{2}'.", AppclusiveCredentialSection.SECTION_NAME, credentialSection.Domain, credentialSection.Username);

                var networkCredential = new NetworkCredential(credentialSection.Username, credentialSection.Password, credentialSection.Domain);
                Contract.Assert(null != networkCredential);
                cfg.Credential = networkCredential;
            }
        }
        public void NewDictionaryParametersFromJsonStringSucceeds()
        {
            // Arrange
            var jsonString = "{\"NullKey7\":null,\"ArrayKey6\":[\"val1\",\"val2\",\"val3\"],\"IntKey4\":5,\"StringKey1\":\"arbitrary-value1\",\"LongKey3\":42,\"ArbitraryObjectKey5\":{\"UserName\":\"arbitrary-user\",\"Password\":\"arbitrary-password\",\"SecurePassword\":{\"Length\":18},\"Domain\":\"\"},\"EmptyStringKey2\":\"\"}";

            // Act
            var sut = new DictionaryParameters(jsonString);

            // Assert
            Assert.IsTrue(sut.ContainsKey("StringKey1"));
            Assert.AreEqual("arbitrary-value1", sut["StringKey1"]);
            Assert.IsTrue(sut.ContainsKey("EmptyStringKey2"));
            Assert.AreEqual("", sut["EmptyStringKey2"]);
            Assert.IsTrue(sut.ContainsKey("LongKey3"));
            Assert.AreEqual(42L, sut["LongKey3"]);
            Assert.IsTrue(sut.ContainsKey("IntKey4"));
            Assert.AreEqual(5L, sut["IntKey4"]);
            Assert.IsTrue(sut.ContainsKey("ArbitraryObjectKey5"));
            Assert.IsTrue(sut.ContainsKey("ArrayKey6"));
            Assert.IsTrue(sut["ArrayKey6"] is List <object>);
            var list = sut["ArrayKey6"] as List <object>;

            Assert.AreEqual(3, list.Count);
            Assert.IsTrue(list.Contains("val1"));
            Assert.IsTrue(list.Contains("val2"));
            Assert.IsTrue(list.Contains("val3"));
            Assert.IsTrue(sut.ContainsKey("NullKey7"));
            Assert.IsNull(sut["NullKey7"]);
        }
Exemplo n.º 3
0
        public DictionaryParameters Convert <TAttribute>(ConvertibleBaseDto convertibleDto, bool includeAllProperties)
            where TAttribute : ConversionKeyBaseAttribute
        {
            // create Dto to be returned
            var dictionaryParameters = new DictionaryParameters();

            // get all defined properties in Dto
            var propertyInfos = convertibleDto
                                .GetType()
                                .GetProperties
                                (
                BindingFlags.Static |
                BindingFlags.Instance |
                BindingFlags.Public |
                BindingFlags.FlattenHierarchy
                                );

            Contract.Assert(null != propertyInfos);

            foreach (var propertyInfo in propertyInfos)
            {
                // get annotation of property
                var attribute = (TAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(TAttribute));

                // skip if no attribute found ...
                if (null == attribute)
                {
                    // but add property if we should include all properties
                    if (includeAllProperties)
                    {
                        dictionaryParameters.Add(propertyInfo.Name, propertyInfo.GetValue(convertibleDto, null));
                    }
                    continue;
                }

                // assert that we do not have the annotated property in our dictionary
                Contract.Assert(!dictionaryParameters.ContainsKey(attribute.Name));

                // add value to dictionary
                dictionaryParameters.Add(attribute.Name, propertyInfo.GetValue(convertibleDto, null));
            }

            return(dictionaryParameters);
        }
Exemplo n.º 4
0
        public T Convert <T, TAttribute>(DictionaryParameters dictionaryParameters, bool includeAllProperties)
            where T : ConvertibleBaseDto, new()
            where TAttribute : ConversionKeyBaseAttribute
        {
            // create Dto to be returned
            var t = new T();

            // get all defined properties in Dto
            var propertyInfos = typeof(T)
                                .GetProperties
                                (
                BindingFlags.Static |
                BindingFlags.Instance |
                BindingFlags.Public |
                BindingFlags.FlattenHierarchy
                                );

            Contract.Assert(null != propertyInfos);

            foreach (var propertyInfo in propertyInfos)
            {
                object value;

                // get annotation of property
                var attribute = (TAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(TAttribute));

                // skip if no attribute found ...
                if (null == attribute)
                {
                    // but add property if we should include all properties
                    if (includeAllProperties)
                    {
                        // get value from DictionaryParameters
                        var couldValueFromPropertyInDicionaryParametersBeRetrieved =
                            dictionaryParameters.TryGetValue(propertyInfo.Name, out value);
                        if (!couldValueFromPropertyInDicionaryParametersBeRetrieved)
                        {
                            continue;
                        }

                        // assign value from Dictionary key to our Dto
                        SetPropertyValue(propertyInfo, t, value);
                    }
                    continue;
                }

                // find property in DictionaryParameters
                // skip if key not found
                if (!dictionaryParameters.ContainsKey(attribute.Name))
                {
                    continue;
                }

                // get value from DictionaryParameters
                var couldValueFromExistingKeyInDicionaryParametersBeRetrieved = dictionaryParameters.TryGetValue(attribute.Name, out value);
                Contract.Assert(couldValueFromExistingKeyInDicionaryParametersBeRetrieved);

                // assign value from Dictionary key to our Dto
                SetPropertyValue(propertyInfo, t, value);
            }

            return(t);
        }
        public override bool Invoke(DictionaryParameters parameters, IInvocationResult invocationResult)
        {
            Contract.Requires("2" == invocationResult.Version, "This plugin only supports non-serialisable invocation results.");

            var fReturn = false;

            var result = base.Invoke(parameters, invocationResult);
            if(!result)
            {
                return result;
            }

            var message = new StringBuilder();
            message.AppendLine("PowerShellScriptPlugin.Invoke ...");
            message.AppendLine();

            Logger.WriteLine(message.ToString());
            message.Clear();

            Contract.Assert(parameters.ContainsKey(SCRIPT_NAME_KEY));
            var scriptPathAndName = parameters.GetOrDefault(SCRIPT_NAME_KEY, "") as string;
            parameters.Remove(SCRIPT_NAME_KEY);
            Contract.Assert(!parameters.ContainsKey(SCRIPT_NAME_KEY));

            var scriptParameters = (Dictionary<string, object>) parameters;
            Contract.Assert(null != scriptParameters);

            var activityId = Trace.CorrelationManager.ActivityId;

            foreach(var item in scriptParameters)
            {
                message.AppendFormat("{0} - {1}", item.Key, item.Value);
                message.AppendLine();
            }
            Logger.WriteLine(message.ToString());
            message.Clear();

            var data = new ThreadPoolUserWorkItemParameters()
            {
                ActivityId = activityId
                ,
                Logger = Logger
                ,
                ScriptParameters = scriptParameters
                ,
                ScriptPathAndName = scriptPathAndName
            };
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolUserWorkItem.ThreadProc), data);

            message.AppendLine("PowerShellScriptPlugin.Invoke DISPATCHED.");
            message.AppendLine();
            
            Logger.WriteLine(message.ToString());

            fReturn = true;
            
            invocationResult.Succeeded = fReturn;
            invocationResult.Code = 1;
            invocationResult.Message = "PowerShellScriptPlugin.Invoke COMPLETED and logged the intended operation to a tracing facility.";
            invocationResult.Description = message.ToString();
            invocationResult.InnerJobResult = null;

            return fReturn;
        }