Added RedirectUsingNamedRoute
[castle.git] / InversionOfControl / Castle.MicroKernel / ModelBuilder / Inspectors / LifestyleModelInspector.cs
blob8c65d04c55b065f97b6b3edc80dadd3e2a87b110
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.MicroKernel.ModelBuilder.Inspectors
17 using System;
18 using System.Configuration;
20 using Castle.Core;
22 /// <summary>
23 /// Inspects the component configuration and the type looking for a
24 /// definition of lifestyle type. The configuration preceeds whatever
25 /// is defined in the component.
26 /// </summary>
27 /// <remarks>
28 /// This inspector is not guarantee to always set up an lifestyle type.
29 /// If nothing could be found it wont touch the model. In this case is up to
30 /// the kernel to estabish a default lifestyle for components.
31 /// </remarks>
32 [Serializable]
33 public class LifestyleModelInspector : IContributeComponentModelConstruction
35 /// <summary>
36 /// Seaches for the lifestyle in the configuration and, if unsuccessful
37 /// look for the lifestyle attribute in the implementation type.
38 /// </summary>
39 public virtual void ProcessModel(IKernel kernel, ComponentModel model)
41 if (!ReadLifestyleFromConfiguration(model))
43 ReadLifestyleFromType(model);
47 /// <summary>
48 /// Reads the attribute "lifestyle" associated with the
49 /// component configuration and tries to convert to <see cref="LifestyleType"/>
50 /// enum type.
51 /// </summary>
52 protected virtual bool ReadLifestyleFromConfiguration(ComponentModel model)
54 if (model.Configuration != null)
56 String lifestyle = model.Configuration.Attributes["lifestyle"];
58 if (lifestyle != null)
60 try
62 LifestyleType type = (LifestyleType)
63 Enum.Parse(typeof(LifestyleType), lifestyle, true);
65 model.LifestyleType = type;
68 catch(Exception)
70 String message = String.Format(
71 "Could not convert the specified attribute value " +
72 "{0} to a valid LifestyleType enum type", lifestyle);
74 throw new ConfigurationErrorsException(message);
77 if (model.LifestyleType == LifestyleType.Pooled)
79 ExtractPoolConfig(model);
81 else if(model.LifestyleType == LifestyleType.Custom)
83 ExtractCustomConfig(model);
87 return true;
91 return false;
94 private void ExtractPoolConfig(ComponentModel model)
96 String initial = model.Configuration.Attributes["initialPoolSize"];
97 String maxSize = model.Configuration.Attributes["maxPoolSize"];
99 if (initial != null)
101 model.ExtendedProperties[ExtendedPropertiesConstants.Pool_InitialPoolSize] = Convert.ToInt32(initial);
103 if (maxSize != null)
105 model.ExtendedProperties[ExtendedPropertiesConstants.Pool_MaxPoolSize] = Convert.ToInt32(maxSize);
109 private void ExtractCustomConfig(ComponentModel model)
111 String customLifestyleType = model.Configuration.Attributes["customLifestyleType"];
113 if(customLifestyleType != null)
117 model.CustomLifestyle = Type.GetType(customLifestyleType, true, false);
119 catch(Exception)
121 String message = String.Format(
122 "The Type {0} specified in the customLifestyleType attribute could not be loaded.", customLifestyleType);
124 throw new ConfigurationErrorsException(message);
127 else
129 string message = @"The attribute 'customLifestyleType' must be specified in conjunction with the 'lifestyle' attribute set to ""custom"".";
131 throw new ConfigurationErrorsException(message);
135 /// <summary>
136 /// Check if the type expose one of the lifestyle attributes
137 /// defined in Castle.Model namespace.
138 /// </summary>
139 protected virtual void ReadLifestyleFromType(ComponentModel model)
141 object[] attributes = model.Implementation.GetCustomAttributes(
142 typeof(LifestyleAttribute), true );
144 if (attributes.Length != 0)
146 LifestyleAttribute attribute = (LifestyleAttribute)
147 attributes[0];
149 model.LifestyleType = attribute.Lifestyle;
151 if (model.LifestyleType == LifestyleType.Custom)
153 CustomLifestyleAttribute custom = (CustomLifestyleAttribute)
154 attribute;
155 model.CustomLifestyle = custom.LifestyleHandlerType;
157 else if (model.LifestyleType == LifestyleType.Pooled)
159 PooledAttribute pooled = (PooledAttribute) attribute;
160 model.ExtendedProperties[ExtendedPropertiesConstants.Pool_InitialPoolSize] = pooled.InitialPoolSize;
161 model.ExtendedProperties[ExtendedPropertiesConstants.Pool_MaxPoolSize] = pooled.MaxPoolSize;