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
.MicroKernel
.ModelBuilder
.Inspectors
18 using System
.Configuration
;
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.
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.
33 public class LifestyleModelInspector
: IContributeComponentModelConstruction
36 /// Seaches for the lifestyle in the configuration and, if unsuccessful
37 /// look for the lifestyle attribute in the implementation type.
39 public virtual void ProcessModel(IKernel kernel
, ComponentModel model
)
41 if (!ReadLifestyleFromConfiguration(model
))
43 ReadLifestyleFromType(model
);
48 /// Reads the attribute "lifestyle" associated with the
49 /// component configuration and tries to convert to <see cref="LifestyleType"/>
52 protected virtual bool ReadLifestyleFromConfiguration(ComponentModel model
)
54 if (model
.Configuration
!= null)
56 String lifestyle
= model
.Configuration
.Attributes
["lifestyle"];
58 if (lifestyle
!= null)
62 LifestyleType type
= (LifestyleType
)
63 Enum
.Parse(typeof(LifestyleType
), lifestyle
, true);
65 model
.LifestyleType
= type
;
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
);
94 private void ExtractPoolConfig(ComponentModel model
)
96 String initial
= model
.Configuration
.Attributes
["initialPoolSize"];
97 String maxSize
= model
.Configuration
.Attributes
["maxPoolSize"];
101 model
.ExtendedProperties
[ExtendedPropertiesConstants
.Pool_InitialPoolSize
] = Convert
.ToInt32(initial
);
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);
121 String message
= String
.Format(
122 "The Type {0} specified in the customLifestyleType attribute could not be loaded.", customLifestyleType
);
124 throw new ConfigurationErrorsException(message
);
129 string message
= @"The attribute 'customLifestyleType' must be specified in conjunction with the 'lifestyle' attribute set to ""custom"".";
131 throw new ConfigurationErrorsException(message
);
136 /// Check if the type expose one of the lifestyle attributes
137 /// defined in Castle.Model namespace.
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
)
149 model
.LifestyleType
= attribute
.Lifestyle
;
151 if (model
.LifestyleType
== LifestyleType
.Custom
)
153 CustomLifestyleAttribute custom
= (CustomLifestyleAttribute
)
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
;