1 // Copyright 2004-2007 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 ExtendingSample
18 using System
.Collections
;
21 using Castle
.Core
.Configuration
;
23 using Castle
.MicroKernel
;
24 using Castle
.MicroKernel
.LifecycleConcerns
;
25 using Castle
.MicroKernel
.ModelBuilder
;
28 /// Summary description for StartableFacility.
30 public class StartableFacility
: IFacility
32 private ArrayList _waitList
= new ArrayList();
33 private IKernel _kernel
;
35 public void Init(IKernel kernel
, IConfiguration facilityConfig
)
39 kernel
.ComponentModelBuilder
.AddContributor( new StartableInspector() );
40 kernel
.ComponentRegistered
+=
41 new ComponentDataDelegate(OnComponentRegistered
);
44 public void Terminate()
49 private class StartableInspector
: IContributeComponentModelConstruction
51 public void ProcessModel(IKernel kernel
, ComponentModel model
)
54 typeof(IStartable
).IsAssignableFrom(model
.Implementation
);
56 model
.ExtendedProperties
["startable"] = startable
;
60 model
.LifecycleSteps
.Add(
61 LifecycleStepType
.Commission
, new StartableConcern() );
65 private class StartableConcern
: ILifecycleConcern
67 public void Apply(ComponentModel model
, object component
)
69 (component
as IStartable
).Start();
74 private void OnComponentRegistered(String key
, IHandler handler
)
76 bool startable
= (bool)
77 handler
.ComponentModel
.ExtendedProperties
["startable"];
81 if (handler
.CurrentState
== HandlerState
.WaitingDependency
)
83 _waitList
.Add( handler
);
95 /// For each new component registered,
96 /// some components in the WaitingDependency
97 /// state may have became valid, so we check them
99 private void CheckWaitingList()
101 IHandler
[] handlers
= (IHandler
[])
102 _waitList
.ToArray( typeof(IHandler
) );
104 foreach(IHandler handler
in handlers
)
106 if (handler
.CurrentState
== HandlerState
.Valid
)
108 Start( handler
.ComponentModel
.Name
);
110 _waitList
.Remove(handler
);
116 /// Request the component instance
118 /// <param name="key"></param>
119 private void Start(String key
)
121 object instance
= _kernel
[key
];