KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jdo > JdoInterceptor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.jdo;
18
19 import javax.jdo.JDOException;
20 import javax.jdo.PersistenceManager;
21
22 import org.aopalliance.intercept.MethodInterceptor;
23 import org.aopalliance.intercept.MethodInvocation;
24
25 import org.springframework.transaction.support.TransactionSynchronizationManager;
26
27 /**
28  * This interceptor binds a new JDO PersistenceManager to the thread before a method
29  * call, closing and removing it afterwards in case of any method outcome.
30  * If there already is a pre-bound PersistenceManager (e.g. from JdoTransactionManager,
31  * or from a surrounding JDO-intercepted method), the interceptor simply participates in it.
32  *
33  * <p>Application code must retrieve a JDO PersistenceManager via the
34  * <code>PersistenceManagerFactoryUtils.getPersistenceManager</code> method,
35  * to be able to detect a thread-bound PersistenceManager. It is preferable to use
36  * <code>getPersistenceManager</code> with allowCreate=false, if the code relies on
37  * the interceptor to provide proper PersistenceManager handling. Typically, the code
38  * will look like as follows:
39  *
40  * <pre>
41  * public void doSomeDataAccessAction() {
42  * PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(this.pmf, false);
43  * ...
44  * }</pre>
45  *
46  * <p>Note that this interceptor automatically translates JDOExceptions, via
47  * delegating to the <code>PersistenceManagerFactoryUtils.convertJdoAccessException</code>
48  * method that converts them to exceptions that are compatible with the
49  * <code>org.springframework.dao</code> exception hierarchy (like JdoTemplate does).
50  * This can be turned off if the raw exceptions are preferred.
51  *
52  * <p>This class can be considered a declarative alternative to JdoTemplate's
53  * callback approach. The advantages are:
54  * <ul>
55  * <li>no anonymous classes necessary for callback implementations;
56  * <li>the possibility to throw any application exceptions from within data access code.
57  * </ul>
58  *
59  * <p>The drawback is the dependency on interceptor configuration. However, note
60  * that this interceptor is usually <i>not</i> necessary in scenarios where the
61  * data access code always executes within transactions. A transaction will always
62  * have a thread-bound PersistenceManager in the first place, so adding this interceptor
63  * to the configuration just adds value when fine-tuning PersistenceManager settings
64  * like the flush mode - or when relying on exception translation.
65  *
66  * @author Juergen Hoeller
67  * @since 13.06.2003
68  * @see PersistenceManagerFactoryUtils#getPersistenceManager
69  * @see JdoTransactionManager
70  * @see JdoTemplate
71  */

72 public class JdoInterceptor extends JdoAccessor implements MethodInterceptor {
73
74     private boolean exceptionConversionEnabled = true;
75
76
77     /**
78      * Set whether to convert any JDOException raised to a Spring DataAccessException,
79      * compatible with the <code>org.springframework.dao</code> exception hierarchy.
80      * <p>Default is "true". Turn this flag off to let the caller receive raw exceptions
81      * as-is, without any wrapping.
82      * @see org.springframework.dao.DataAccessException
83      */

84     public void setExceptionConversionEnabled(boolean exceptionConversionEnabled) {
85         this.exceptionConversionEnabled = exceptionConversionEnabled;
86     }
87
88
89     public Object JavaDoc invoke(MethodInvocation methodInvocation) throws Throwable JavaDoc {
90         boolean existingTransaction = false;
91         PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), true);
92         if (TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory())) {
93             logger.debug("Found thread-bound PersistenceManager for JDO interceptor");
94             existingTransaction = true;
95         }
96         else {
97             logger.debug("Using new PersistenceManager for JDO interceptor");
98             TransactionSynchronizationManager.bindResource(getPersistenceManagerFactory(), new PersistenceManagerHolder(pm));
99         }
100         try {
101             Object JavaDoc retVal = methodInvocation.proceed();
102             flushIfNecessary(pm, existingTransaction);
103             return retVal;
104         }
105         catch (JDOException ex) {
106             if (this.exceptionConversionEnabled) {
107                 throw convertJdoAccessException(ex);
108             }
109             else {
110                 throw ex;
111             }
112         }
113         finally {
114             if (existingTransaction) {
115                 logger.debug("Not closing pre-bound JDO PersistenceManager after interceptor");
116             }
117             else {
118                 TransactionSynchronizationManager.unbindResource(getPersistenceManagerFactory());
119                 PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory());
120             }
121         }
122     }
123
124 }
125
Popular Tags