Refactored the Kernel registration fluent interface to be more readable, better suppo...
[castle.git] / Facilities / IBatisNet / Castle.Facilities.IBatisNetIntegration / AutomaticSessionInterceptor.cs
blob93c99051349765000b20af0b3c8e3c451b81d250
1 #region License
3 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 // --
18 //
19 // This facility was a contribution kindly
20 // donated by Gilles Bayon <gilles.bayon@gmail.com>
21 //
22 // --
24 #endregion
26 namespace Castle.Facilities.IBatisNetIntegration
28 using System;
29 using System.Reflection;
30 using Castle.Core.Interceptor;
31 using Castle.MicroKernel;
32 using Castle.Services.Transaction;
33 using IBatisNet.Common.Logging;
34 using IBatisNet.DataMapper;
35 using Transaction = Services.Transaction.ITransaction;
37 public class AutomaticSessionInterceptor : IInterceptor
39 private IKernel _kernel = null;
40 private static readonly ILog _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
42 public AutomaticSessionInterceptor(IKernel kernel)
44 _kernel = kernel;
47 public void Intercept(IInvocation invocation)
49 MethodInfo info = invocation.MethodInvocationTarget;
51 if (info.IsDefined(typeof (NoSessionAttribute), true))
53 invocation.Proceed();
54 return;
57 String key = ObtainSqlMapKeyFor(info);
59 ISqlMapper sqlMap = ObtainSqlMapperFor(key);
61 if (sqlMap.IsSessionStarted)
63 invocation.Proceed();
64 return;
67 if (_logger.IsDebugEnabled)
69 _logger.Debug("Automatic Open connection on method :" + invocation.Method.Name);
72 sqlMap.OpenConnection();
74 if (EnlistSessionIfHasTransactionActive(key, sqlMap))
76 invocation.Proceed();
77 return;
80 try
82 invocation.Proceed();
83 return;
85 finally
87 if (_logger.IsDebugEnabled)
89 _logger.Debug("Close connection on method :" + invocation.Method.Name);
91 sqlMap.CloseConnection();
95 private bool EnlistSessionIfHasTransactionActive(String key, ISqlMapper sqlMap)
97 if (!_kernel.HasComponent(typeof (ITransactionManager)))
99 return false;
102 bool enlisted = false;
104 if (key == null)
106 key = "iBATIS.DataMapper";
109 ITransactionManager manager = (ITransactionManager) _kernel[typeof (ITransactionManager)];
111 Transaction transaction = manager.CurrentTransaction;
113 if (transaction != null)
115 if (!transaction.Context.Contains(key))
117 transaction.Context[key] = true;
118 transaction.Enlist(new ResourceSqlMapAdapter(sqlMap.BeginTransaction(false)));
119 transaction.RegisterSynchronization(new SqlMapKeeper(sqlMap));
120 enlisted = true;
124 _kernel.ReleaseComponent(manager);
126 return enlisted;
129 protected String ObtainSqlMapKeyFor(MethodInfo info)
131 String sqlMapID = String.Empty;
133 if (info.IsDefined(typeof (SessionAttribute), true))
135 SessionAttribute[] attributs = info.GetCustomAttributes(typeof (SessionAttribute), true) as SessionAttribute[];
136 sqlMapID = attributs[0].SqlMapId;
139 return sqlMapID;
142 protected ISqlMapper ObtainSqlMapperFor(String key)
144 // Use the key specified in the attribute - if any
145 if (String.Empty.Equals(key))
147 return (ISqlMapper) _kernel[typeof (ISqlMapper)];
149 else
151 return (ISqlMapper) _kernel[key];