3 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 // This facility was a contribution kindly
20 // donated by Gilles Bayon <gilles.bayon@gmail.com>
26 namespace Castle
.Facilities
.IBatisNetIntegration
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
)
47 public void Intercept(IInvocation invocation
)
49 MethodInfo info
= invocation
.MethodInvocationTarget
;
51 if (info
.IsDefined(typeof (NoSessionAttribute
), true))
57 String key
= ObtainSqlMapKeyFor(info
);
59 ISqlMapper sqlMap
= ObtainSqlMapperFor(key
);
61 if (sqlMap
.IsSessionStarted
)
67 if (_logger
.IsDebugEnabled
)
69 _logger
.Debug("Automatic Open connection on method :" + invocation
.Method
.Name
);
72 sqlMap
.OpenConnection();
74 if (EnlistSessionIfHasTransactionActive(key
, sqlMap
))
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
)))
102 bool enlisted
= false;
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
));
124 _kernel
.ReleaseComponent(manager
);
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
;
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
)];
151 return (ISqlMapper
) _kernel
[key
];