Changed to use Authenticate asp.net event instead of Authorize
[castle.git] / Components / Validator / Castle.Components.Validator / CachedValidationRegistry.cs
blobd47508e9f18dc31133774a71dd1e1d70ea62ae0a
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 Castle.Components.Validator
17 using System;
18 using System.Collections;
19 using System.Reflection;
20 using System.Resources;
21 using System.Threading;
23 /// <summary>
24 /// <see cref="IValidatorRegistry"/> implementation that
25 /// caches the reflection and custom attributes calls for better performance.
26 /// </summary>
27 public class CachedValidationRegistry : IValidatorRegistry
29 private static ResourceManager defaultResourceManager;
30 private ResourceManager resourceManager;
32 private readonly IDictionary propertiesPerType = Hashtable.Synchronized(new Hashtable());
33 private readonly IDictionary attrsPerProperty = Hashtable.Synchronized(new Hashtable());
35 /// <summary>
36 /// Initializes the <see cref="CachedValidationRegistry"/> class.
37 /// </summary>
38 static CachedValidationRegistry()
40 defaultResourceManager =
41 new ResourceManager("Castle.Components.Validator.Messages",
42 typeof(CachedValidationRegistry).Assembly);
45 /// <summary>
46 /// Initializes a new instance of the <see cref="CachedValidationRegistry"/> class.
47 /// </summary>
48 public CachedValidationRegistry()
53 /// <summary>
54 /// Initializes a new instance of the <see cref="CachedValidationRegistry"/> class.
55 /// </summary>
56 /// <param name="resourceManager">The resource manager.</param>
57 public CachedValidationRegistry(ResourceManager resourceManager)
59 this.resourceManager = resourceManager;
62 #region IValidatorRegistry Members
64 /// <summary>
65 /// Gets all validators associated with a <see cref="Type"/>.
66 /// <para>
67 /// The validators returned are initialized.
68 /// </para>
69 /// </summary>
70 /// <param name="validatorRunner">The validator runner.</param>
71 /// <param name="targetType">Target type.</param>
72 /// <param name="runWhen">Restrict the set returned to the phase specified</param>
73 /// <returns>A Validator array</returns>
74 public IValidator[] GetValidators(ValidatorRunner validatorRunner, Type targetType, RunWhen runWhen)
76 PropertyInfo[] properties = (PropertyInfo[]) propertiesPerType[targetType];
78 if (properties == null)
80 properties = targetType.GetProperties();
81 propertiesPerType[targetType] = properties;
84 ArrayList list = new ArrayList();
86 foreach (PropertyInfo prop in properties)
88 list.AddRange(GetValidators(validatorRunner, targetType, prop, runWhen));
91 return (IValidator[]) list.ToArray(typeof (IValidator));
94 /// <summary>
95 /// Gets all validators associated with a property.
96 /// <para>
97 /// The validators returned are initialized.
98 /// </para>
99 /// </summary>
100 /// <param name="validatorRunner">The validator runner.</param>
101 /// <param name="targetType">Target type.</param>
102 /// <param name="property">The property.</param>
103 /// <param name="runWhen">Restrict the set returned to the phase specified</param>
104 /// <returns>A Validator array</returns>
105 public IValidator[] GetValidators(
106 ValidatorRunner validatorRunner, Type targetType, PropertyInfo property,
107 RunWhen runWhen)
109 object[] builders = (object[]) attrsPerProperty[property];
111 if (builders == null)
113 builders = property.GetCustomAttributes(typeof (IValidatorBuilder), true);
114 attrsPerProperty[property] = builders;
117 ArrayList validators = new ArrayList();
119 foreach (IValidatorBuilder builder in builders)
121 IValidator validator = builder.Build(validatorRunner, targetType);
123 if (!IsValidatorOnPhase(validator, runWhen)) continue;
125 validator.Initialize(this, property);
126 validators.Add(validator);
129 return (IValidator[]) validators.ToArray(typeof (IValidator));
132 /// <summary>
133 /// Gets the string from resource by key
134 /// </summary>
135 /// <param name="key">The key.</param>
136 /// <returns></returns>
137 public string GetStringFromResource(string key)
139 if(resourceManager!=null)
141 ResourceSet resourceSet = resourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true);
142 string result = resourceSet.GetString(key);
143 if (result != null)
144 return result;
146 ResourceSet defaultResourceSetForCurrentThread = defaultResourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true);
147 return defaultResourceSetForCurrentThread.GetString(key);
150 #endregion
152 private static bool IsValidatorOnPhase(IValidator validator, RunWhen when)
154 if (validator.RunWhen == RunWhen.Everytime) return true;
156 return ((validator.RunWhen & when) != 0);