Fixing an issue with output parameters that are of type IntPtr
[castle.git] / InversionOfControl / Castle.MicroKernel / Facilities / Startable / StartableFacility.cs
blobafc239daf44541e5ed24f2aafff99d654f7124b0
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Facilities.Startable
17 using System;
18 using System.Collections;
19 using Castle.MicroKernel;
20 using Castle.MicroKernel.Facilities;
21 using Castle.MicroKernel.SubSystems.Conversion;
22 using Castle.Core;
24 public class StartableFacility : AbstractFacility
26 private ArrayList waitList = new ArrayList();
27 private ITypeConverter converter;
29 protected override void Init()
31 converter = (ITypeConverter) Kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey);
33 Kernel.ComponentModelCreated +=
34 new ComponentModelDelegate(OnComponentModelCreated);
35 Kernel.ComponentRegistered +=
36 new ComponentDataDelegate(OnComponentRegistered);
39 private void OnComponentModelCreated(ComponentModel model)
41 bool startable =
42 CheckIfComponentImplementsIStartable(model) || HasStartableAttributeSet(model);
44 model.ExtendedProperties["startable"] = startable;
46 if (startable)
48 model.LifecycleSteps.Add(
49 LifecycleStepType.Commission, StartConcern.Instance);
50 model.LifecycleSteps.Add(
51 LifecycleStepType.Decommission, StopConcern.Instance);
55 private void OnComponentRegistered(String key, IHandler handler)
57 bool startable = (bool) handler.ComponentModel.ExtendedProperties["startable"];
59 if (startable)
61 if (handler.CurrentState == HandlerState.WaitingDependency)
63 AddHandlerToWaitingList(handler);
65 else
67 Start(key);
71 CheckWaitingList();
74 private void OnHandlerStateChanged(object source, EventArgs args)
76 CheckWaitingList();
79 private void AddHandlerToWaitingList(IHandler handler)
81 waitList.Add(handler);
83 handler.OnHandlerStateChanged += new HandlerStateDelegate(OnHandlerStateChanged);
86 /// <summary>
87 /// For each new component registered,
88 /// some components in the WaitingDependency
89 /// state may have became valid, so we check them
90 /// </summary>
91 private void CheckWaitingList()
93 IHandler[] handlers = (IHandler[]) waitList.ToArray(typeof(IHandler));
95 IList validList = new ArrayList();
97 foreach(IHandler handler in handlers)
99 if (handler.CurrentState == HandlerState.Valid)
101 validList.Add(handler);
102 waitList.Remove(handler);
104 handler.OnHandlerStateChanged -= new HandlerStateDelegate(OnHandlerStateChanged);
108 foreach(IHandler handler in validList)
110 Start(handler.ComponentModel.Name);
114 /// <summary>
115 /// Request the component instance
116 /// </summary>
117 /// <param name="key"></param>
118 private void Start(String key)
120 object instance = Kernel[key];
123 private bool CheckIfComponentImplementsIStartable(ComponentModel model)
125 return typeof(IStartable).IsAssignableFrom(model.Implementation);
128 private bool HasStartableAttributeSet(ComponentModel model)
130 bool result = false;
132 if (model.Configuration != null)
134 String startable = model.Configuration.Attributes["startable"];
136 if (startable != null)
138 result = (bool) converter.PerformConversion(startable, typeof(bool));
142 return result;