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 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)); }