Changed to use Authenticate asp.net event instead of Authorize
[castle.git] / Samples / InversionOfControl / ExtendingSample / StartableFacility.cs
blobdbb1275f8860f23d7756c4f7b9656f35778f9740
1 // Copyright 2004-2007 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 ExtendingSample
17 using System;
18 using System.Collections;
20 using Castle.Core;
21 using Castle.Core.Configuration;
23 using Castle.MicroKernel;
24 using Castle.MicroKernel.LifecycleConcerns;
25 using Castle.MicroKernel.ModelBuilder;
27 /// <summary>
28 /// Summary description for StartableFacility.
29 /// </summary>
30 public class StartableFacility : IFacility
32 private ArrayList _waitList = new ArrayList();
33 private IKernel _kernel;
35 public void Init(IKernel kernel, IConfiguration facilityConfig)
37 _kernel = kernel;
39 kernel.ComponentModelBuilder.AddContributor( new StartableInspector() );
40 kernel.ComponentRegistered +=
41 new ComponentDataDelegate(OnComponentRegistered);
44 public void Terminate()
46 // Nothing to do
49 private class StartableInspector : IContributeComponentModelConstruction
51 public void ProcessModel(IKernel kernel, ComponentModel model)
53 bool startable =
54 typeof(IStartable).IsAssignableFrom(model.Implementation);
56 model.ExtendedProperties["startable"] = startable;
58 if (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"];
79 if (startable)
81 if (handler.CurrentState == HandlerState.WaitingDependency)
83 _waitList.Add( handler );
85 else
87 Start( key );
91 CheckWaitingList();
94 /// <summary>
95 /// For each new component registered,
96 /// some components in the WaitingDependency
97 /// state may have became valid, so we check them
98 /// </summary>
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);
115 /// <summary>
116 /// Request the component instance
117 /// </summary>
118 /// <param name="key"></param>
119 private void Start(String key)
121 object instance = _kernel[key];