Esempio n. 1
0
        /// <param name="pinfo">info of property to work with</param>
        /// <param name="config">configuration containing the instance to set to</param>
        public PropertyRequirement(PropertyInfo pinfo, IRequiresConfiguration config)
        {
            this.propertyInfo   = pinfo;
            this.configuration  = config;
            this.type           = pinfo.PropertyType.FullName;
            this.name           = pinfo.Name;
            this.valueTypesOnly = pinfo.PropertyType.IsValueType;
            this.hard           = false;

            foreach (Attribute attr in pinfo.GetCustomAttributes(true))
            {
                if (attr is RequiredAttribute)
                {
                    this.hard = true;
                }
                if (attr is DefaultClassAttribute)
                {
                    this.defaultValue = new ProviderValue(
                        new ConfiguredComponent((attr as DefaultClassAttribute).ClassName));
                }
                if (attr is DefaultNameAttribute)
                {
                    this.defaultValue = new ComponentRef("${" + (attr as DefaultNameAttribute).ComponentName + "}");
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Constructs from a type name and component name
 /// </summary>
 public ConfiguredComponent(string typeName, string componentName)
 {
     this.typeName = typeName;
     this.requiredConfiguration = new TypeRequiredConfiguration(typeName);
     this.configuration         = new StandardConfiguration();
     this.matchedName           = componentName;
 }
Esempio n. 3
0
        private static object ConfigureComponentWithRequirements(
            IRequiresConfiguration requiredConfiguration,
            IComponentConfiguration configuration,
            IComponentDirectory componentDirectory)
        {
            // 3. fill in the other parameters from the component directory
            foreach (IRequirement req in requiredConfiguration.Requirements)
            {
                // Console.Out.WriteLine(">> Requirement: {0}", req.Name);
                if (configuration.Values.ContainsKey(req.Name))
                {
                    // Console.Out.WriteLine(" ... Contains Key");
                    req.SetValue(configuration.Values[req.Name].GetInstance(req.Type));
                }
                else
                {
                    if (req.Default != null)
                    {
                        // Console.Out.WriteLine(" ... Has Default");
                        // NEW CONFIGURED COMPONENT THAT IS
                        req.SetValue(
                            componentDirectory.ConfigureInlineComponent(
                                new Instance(
                                    req.Default.GetInstance(req.Type))));
                    }
                    else if (HasComponent(componentDirectory, req.Type))
                    {
                        // Console.Out.WriteLine(" ... Setting By ICD");
                        req.SetValue(componentDirectory.GetInstanceByType(req.Type));
                    }
                    else if (req.Hard)
                    {
                        throw new ComponentConfigurationException(
                                  null,
                                  String.Format("ComponentOS KERNEL PANIC! I have no value for: {0}", req.Name));
                    }
                }
            }

            if (requiredConfiguration.Instance is IComponentPostInitialization)
            {
                (requiredConfiguration.Instance as IComponentPostInitialization).PostComponentInit(componentDirectory);
            }

            return(requiredConfiguration.Instance);
        }
        public override object GetInstance(
            IComponentDirectory componentDirectory,
            object clientInstance,
            string requirementName,
            string requirementType)
        {
            IRequiresConfiguration old = this.requiredConfiguration;

            try
            {
                requiredConfiguration = requiredConfiguration.Clone();
                return(base.GetInstance(componentDirectory, clientInstance, requirementName, requirementType));
            }
            finally
            {
                requiredConfiguration = old;
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Constructs from XML
        /// </summary>
        /// <param name="typeName"></param>
        public ConfiguredComponent(XmlNode node)
        {
            string typeName = (node as XmlElement).GetAttribute("Type");

            this.typeName              = typeName;
            this.matchedName           = (node as XmlElement).GetAttribute("Id");
            this.configuration         = new StandardConfiguration(node["Parameters"]);
            this.requiredConfiguration = new TypeRequiredConfiguration(typeName);

            XmlElement element = node as XmlElement;

            if (element.HasAttribute("MatchAgainstType"))
            {
                this.matchAgainstType = Boolean.Parse(element.GetAttribute("MatchAgainstType"));
            }
            if (element.HasAttribute("MatchAgainstName"))
            {
                this.matchAgainstName = Boolean.Parse(element.GetAttribute("MatchAgainstName"));
            }
        }
        public void Preinitialize(IRequiresConfiguration config)
        {
            // call the constructor, get the instance
            bool found = false;

            foreach (IRequiresConfiguration cfg in preinitializers)
            {
                if (cfg == config)
                {
                    found = true; break;
                }
            }

            if (!found || !(config is ConstructorConfiguration))
            {
                throw new Exception("Preinitializer does not belong to this instance");
            }

            instance = config.Instance;
        }
Esempio n. 7
0
        public virtual object GetInstance(
            IComponentDirectory componentDirectory,
            object clientInstance,
            string requirementName,
            string requirementType)
        {
            ComponentDirectory = componentDirectory;

            {
                object instance = requiredConfiguration.Instance;
                if (instance != null)
                {
                    return(instance);
                }
            }


            // 1. compute the best pre-initializer to use

            int currentScore = 0;
            int maxScore     = Int32.MinValue;
            IRequiresConfiguration currentInitializer = null;

            foreach (IRequiresConfiguration initializer in requiredConfiguration.PreInitializers)
            {
                // Console.Out.WriteLine("Initializer...");
                currentScore = 0;
                bool canUse = true;
                foreach (IRequirement req in initializer.Requirements)
                {
                    if (req.Default == null)
                    {
                        /* find the value */
                        if (configuration.Values.ContainsKey(req.Name))
                        {
                            currentScore++;
                        }
                        else if (HasComponent(componentDirectory, req.Type))
                        {
                            currentScore++;
                        }
                        else if (req.Hard)
                        {
                            /* DEBUG: Console.Out.WriteLine("Missing Parameter: {0} {1}", req.Type, req.Name); */
                            if (req.Hard)
                            {
                                canUse = false; break;
                            }
                        }
                        else
                        {
                            currentScore--;
                        }
                    }
                }

                if (canUse && (currentScore > maxScore))
                {
                    currentInitializer = initializer;
                    maxScore           = currentScore;
                }
            }
            // 1.1 give up if no pre-initializer can be used (because of unfulfillable hard requirements)
            if (currentInitializer == null)
            {
                throw new ComponentConfigurationException(this, "The component cannot be instantiated; No compatible pre-initializer exists");
            }

            // 1.2 give up if after pre-initialization, the object cannot be used (because of unfulfillable hard requirements)
            foreach (IRequirement req in requiredConfiguration.Requirements)
            {
                if (req.Default == null && req.Hard)
                {
                    /* if it's been solved through a pre-initializer */
                    bool found = false;
                    foreach (IRequirement reqpre in currentInitializer.Requirements)
                    {
                        if (reqpre.Name == req.Name)
                        {
                            found = true; break;
                        }
                    }

                    if (!found)
                    {
                        if (!req.ValueTypesOnly)
                        {
                            // query the ICD by type
                            if (HasComponent(componentDirectory, req.Type))
                            {
                                continue;
                            }
                        }

                        throw new ComponentConfigurationException(
                                  this,
                                  String.Format(
                                      "The component cannot be instantiated; After pre-initialization, the {0} requirement is not satisfied",
                                      req.Name));
                    }
                }
            }
            // 2. fill in the pre-initializer
            foreach (IRequirement req in currentInitializer.Requirements)
            {
                // Console.Out.WriteLine(">> PreInitializer Requirement: {0}", req.Name);
                if (configuration.Values.ContainsKey(req.Name))
                {
                    // Console.Out.WriteLine(" ... Contains Key");
                    req.SetValue(configuration.Values[req.Name].GetInstance(req.Type));
                }
                else
                {
                    if (req.Default != null)
                    {
                        // Console.Out.WriteLine(" ... Has Default");
                        req.SetValue(
                            componentDirectory.ConfigureInlineComponent(
                                new Instance(
                                    req.Default.GetInstance(req.Type))));
                    }
                    else if (HasComponent(componentDirectory, req.Type))
                    {
                        // Console.Out.WriteLine(" ... Setting By ICD (type={0})", req.Type);
                        req.SetValue(componentDirectory.GetInstanceByType(req.Type));
                    }
                    else if (req.Hard)
                    {
                        throw new ComponentConfigurationException(
                                  this,
                                  String.Format("ComponentOS KERNEL PANIC! I have no value for: {0}", req.Name));
                    }
                }
            }
            requiredConfiguration.Preinitialize(currentInitializer);

            return(ConfigureComponentWithRequirements(requiredConfiguration, configuration, componentDirectory));
        }
Esempio n. 8
0
 /// <summary>
 /// Creates a new configured component from required configuration and provided configuration
 /// </summary>
 public ConfiguredComponent(IRequiresConfiguration configRequired, IComponentConfiguration configProvided)
 {
     this.matchedTypes          = new string[0];
     this.requiredConfiguration = configRequired;
     this.configuration         = configProvided;
 }
Esempio n. 9
0
 /// <summary>
 /// Constructs from required configuration
 /// </summary>
 public ConfiguredComponent(IRequiresConfiguration configRequired)
 {
     this.matchedTypes          = new string[0];
     this.requiredConfiguration = configRequired;
     this.configuration         = new StandardConfiguration();
 }
Esempio n. 10
0
 /// <summary>
 /// Constructs from a type name and a provided configuration
 /// </summary>
 public ConfiguredComponent(string typeName, IComponentConfiguration configProvided)
 {
     this.typeName              = typeName;
     this.configuration         = configProvided;
     this.requiredConfiguration = new TypeRequiredConfiguration(typeName);
 }
Esempio n. 11
0
 public void Preinitialize(IRequiresConfiguration config)
 {
     throw new Exception("The method or operation is not implemented.");
 }
 /// <summary>
 /// Creates a new configured component from required configuration and provided configuration
 /// </summary>
 public AlwaysNewConfiguredComponent(IRequiresConfiguration configRequired, IComponentConfiguration configProvided) : base(configRequired, configProvided)
 {
 }