KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > toplink > SessionReadCallback


1 /*
2  * Copyright 2002-2005 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.toplink;
18
19 import oracle.toplink.exceptions.TopLinkException;
20 import oracle.toplink.sessions.Session;
21 import oracle.toplink.sessions.UnitOfWork;
22
23 /**
24  * Convenient abstract implementation of the TopLinkCallback interface,
25  * exposing either the plain TopLink Session or the TopLink UnitOfWork
26  * (which extends the Session interface) to code that reads persistent objects.
27  *
28  * <p>Exposes the UnitOfWork if there is an active one (that is, if we're running
29  * within a non-read-only transaction); else exposes the Session itself.
30  * This allows to modify returned objects within a transaction, which is
31  * often desired, while the same code will return shared cache objects
32  * if running outside a transaction.
33  *
34  * <p>If "enforceReadOnly" is demanded, the callback will always expose the
35  * Session itself, avoiding the UnitOfWork overhead in any case.
36  *
37  * @author Juergen Hoeller
38  * @since 1.2
39  * @see oracle.toplink.sessions.Session#getActiveUnitOfWork()
40  * @see #readFromSession(oracle.toplink.sessions.Session)
41  */

42 public abstract class SessionReadCallback implements TopLinkCallback {
43
44     private final boolean enforceReadOnly;
45
46     /**
47      * Create a new SessionReadCallback, not enforcing read-only objects.
48      */

49     public SessionReadCallback() {
50         this.enforceReadOnly = false;
51     }
52
53     /**
54      * Create a new SessionReadCallback, enforcing read-only objects if demanded.
55      * @param enforceReadOnly whether to enforce returning read-only objects,
56      * even if running within a non-read-only transaction
57      */

58     public SessionReadCallback(boolean enforceReadOnly) {
59         this.enforceReadOnly = enforceReadOnly;
60     }
61
62     /**
63      * Determines the Session to work on (either the active UnitOfWork
64      * or the plain Session) and delegates to <code>readFromSession</code>.
65      * @see #readFromSession(oracle.toplink.sessions.Session)
66      */

67     public final Object JavaDoc doInTopLink(Session session) throws TopLinkException {
68         Session sessionToUse = session;
69         if (!this.enforceReadOnly) {
70             UnitOfWork unitOfWork = session.getActiveUnitOfWork();
71             if (unitOfWork != null) {
72                 sessionToUse = unitOfWork;
73             }
74         }
75         return readFromSession(sessionToUse);
76     }
77
78     /**
79      * Called with a Session to work on, either the active UnitOfWork
80      * or the plain Session (as determined by the transaction status).
81      * @param session the TopLink Session to perform read operations on
82      * @return a result object, or <code>null</code> if none
83      * @throws TopLinkException in case of TopLink errors
84      */

85     protected abstract Object JavaDoc readFromSession(Session session) throws TopLinkException;
86
87 }
88
Popular Tags