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.
18 using System
.Collections
;
19 using System
.Reflection
;
21 using Extending2
.Components
;
24 using Castle
.Core
.Configuration
;
26 using Castle
.MicroKernel
;
29 /// Summary description for TransactionFacility.
31 public class TransactionFacility
: IFacility
33 TransactionConfigHolder _transactionConfigHolder
;
35 public void Init(IKernel kernel
, IConfiguration facilityConfig
)
37 kernel
.AddComponent( "transactionmanager",
38 typeof(ITransactionManager
), typeof(DefaultTransactionManager
) );
39 kernel
.AddComponent( "transaction.interceptor",
40 typeof(TransactionInterceptor
) );
41 kernel
.AddComponent( "transaction.configholder", typeof(TransactionConfigHolder
) );
43 _transactionConfigHolder
=
44 kernel
[ typeof(TransactionConfigHolder
) ] as TransactionConfigHolder
;
46 kernel
.ComponentModelCreated
+= new ComponentModelDelegate(OnModelCreated
);
49 public void Terminate()
53 private void OnModelCreated(ComponentModel model
)
55 if (IsTransactional(model
))
57 TransactionConfig config
= CreateTransactionConfig(model
);
59 _transactionConfigHolder
.Register(model
.Implementation
, config
);
61 model
.Interceptors
.Add(
62 new InterceptorReference(typeof(TransactionInterceptor
)) );
66 private TransactionConfig
CreateTransactionConfig(ComponentModel model
)
68 TransactionConfig config
= new TransactionConfig();
69 GatherTransactionAttributes(config
, model
.Implementation
);
70 GatherTransactionConfiguration(config
, model
);
74 private bool IsTransactional(ComponentModel model
)
76 if (model
.Configuration
!= null)
78 String attrValue
= model
.Configuration
.Attributes
["transactional"];
80 if ("true".Equals(attrValue
))
86 if ( model
.Implementation
.IsDefined( typeof(TransactionalAttribute
), true ) )
94 private void GatherTransactionConfiguration(TransactionConfig config
, ComponentModel model
)
96 if (model
.Configuration
== null) return;
98 IConfiguration transactionNode
= model
.Configuration
.Children
["transaction"];
100 if (transactionNode
== null) return;
102 foreach(IConfiguration methodNode
in transactionNode
.Children
)
104 config
.AddMethodName( methodNode
.Value
);
108 private void GatherTransactionAttributes(TransactionConfig config
, Type implementation
)
110 MethodInfo
[] methods
= implementation
.GetMethods(
111 BindingFlags
.Instance
|BindingFlags
.Public
|BindingFlags
.NonPublic
);
113 foreach(MethodInfo method
in methods
)
115 if (method
.IsDefined( typeof(RequiresTransactionAttribute
), true ))
117 config
.AddMethod( method
);
124 public class TransactionConfig
126 private IList _methods
= new ArrayList();
127 private IList _methodName
= new ArrayList();
129 public void AddMethodName(string value)
131 _methodName
.Add(value);
134 public void AddMethod(MethodInfo method
)
136 _methods
.Add(method
);
142 /// <param name="method"></param>
143 /// <returns></returns>
144 public bool IsMethodTransactional(MethodInfo method
)
146 if (_methods
.Contains(method
)) return true;
148 foreach(String methodName
in _methodName
)
150 if (method
.Name
.Equals(methodName
))