KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > persistence > interceptors > NoTxMethodCallOnlyEntityManagerInterceptor


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: NoTxMethodCallOnlyEntityManagerInterceptor.java 326 2006-04-14 08:20:59Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.persistence.interceptors;
27
28 import org.objectweb.easybeans.api.EasyBeansInterceptor;
29 import org.objectweb.easybeans.api.EasyBeansInvocationContext;
30
31 /**
32  * This interceptor is used for the Transaction-scoped persistence context when
33  * there is no transaction. In this case the persistence context is used only
34  * for the method call. Interceptor adds in the thread local a new EntityManager
35  * that is closed at the end of the method.
36  * @author Florent Benoit
37  */

38 public class NoTxMethodCallOnlyEntityManagerInterceptor implements EasyBeansInterceptor {
39
40     /**
41      * Creates a new EntityManager before the method and close it at the end.
42      * @param invocationContext context with useful attributes on the current
43      * invocation
44      * @return result of the next invocation (to chain interceptors)
45      * @throws Exception if interceptor fails
46      */

47     public Object JavaDoc intercept(final EasyBeansInvocationContext invocationContext) throws Exception JavaDoc {
48         boolean persistentManagerPresent = (invocationContext.getFactory().getContainer().getPersistenceUnitManager() != null);
49         if (persistentManagerPresent) {
50             try {
51                 invocationContext.getFactory().getContainer().getPersistenceUnitManager().addCurrent();
52                 return invocationContext.proceed();
53             } finally {
54                 invocationContext.getFactory().getContainer().getPersistenceUnitManager()
55                         .closeCurrentAndReturnToPrevious();
56             }
57         }
58         // No persistent manager, do nothing.
59
return invocationContext.proceed();
60     }
61
62 }
63
Popular Tags