1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using Castle
.MicroKernel
;
20 using Castle
.MicroKernel
.Facilities
;
21 using Castle
.MicroKernel
.SubSystems
.Conversion
;
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
)
42 CheckIfComponentImplementsIStartable(model
) || HasStartableAttributeSet(model
);
44 model
.ExtendedProperties
["startable"] = 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"];
61 if (handler
.CurrentState
== HandlerState
.WaitingDependency
)
63 AddHandlerToWaitingList(handler
);
74 private void OnHandlerStateChanged(object source
, EventArgs args
)
79 private void AddHandlerToWaitingList(IHandler handler
)
81 waitList
.Add(handler
);
83 handler
.OnHandlerStateChanged
+= new HandlerStateDelegate(OnHandlerStateChanged
);
87 /// For each new component registered,
88 /// some components in the WaitingDependency
89 /// state may have became valid, so we check them
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
);
115 /// Request the component instance
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
)
132 if (model
.Configuration
!= null)
134 String startable
= model
.Configuration
.Attributes
["startable"];
136 if (startable
!= null)
138 result
= (bool) converter
.PerformConversion(startable
, typeof(bool));