001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.dao.orm;
016    
017    import java.io.Serializable;
018    
019    import java.sql.Connection;
020    
021    /**
022     * @author Shuyang Zhou
023     * @author Brian Wing Shun Chan
024     */
025    public class ClassLoaderSession implements Session {
026    
027            public ClassLoaderSession(Session session, ClassLoader classLoader) {
028                    _session = session;
029                    _classLoader = classLoader;
030            }
031    
032            public void clear() throws ORMException {
033                    Thread currentThread = Thread.currentThread();
034    
035                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
036    
037                    try {
038                            if (contextClassLoader != _classLoader) {
039                                    currentThread.setContextClassLoader(_classLoader);
040                            }
041    
042                            _session.clear();
043                    }
044                    finally {
045                            if (contextClassLoader != _classLoader) {
046    
047                                    currentThread.setContextClassLoader(contextClassLoader);
048                            }
049                    }
050            }
051    
052            public Connection close() throws ORMException {
053                    Thread currentThread = Thread.currentThread();
054    
055                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
056    
057                    try {
058                            if (contextClassLoader != _classLoader) {
059                                    currentThread.setContextClassLoader(_classLoader);
060                            }
061    
062                            return _session.close();
063                    }
064                    finally {
065                            if (contextClassLoader != _classLoader) {
066                                    currentThread.setContextClassLoader(contextClassLoader);
067                            }
068                    }
069            }
070    
071            public boolean contains(Object object) throws ORMException {
072                    Thread currentThread = Thread.currentThread();
073    
074                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
075    
076                    try {
077                            if (contextClassLoader != _classLoader) {
078                                    currentThread.setContextClassLoader(_classLoader);
079                            }
080    
081                            return _session.contains(object);
082                    }
083                    finally {
084                            if (contextClassLoader != _classLoader) {
085                                    currentThread.setContextClassLoader(contextClassLoader);
086                            }
087                    }
088            }
089    
090            public Query createQuery(String queryString) throws ORMException {
091                    Thread currentThread = Thread.currentThread();
092    
093                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
094    
095                    try {
096                            if (contextClassLoader != _classLoader) {
097                                    currentThread.setContextClassLoader(_classLoader);
098                            }
099    
100                            return _session.createQuery(queryString);
101                    }
102                    finally {
103                            if (contextClassLoader != _classLoader) {
104                                    currentThread.setContextClassLoader(contextClassLoader);
105                            }
106                    }
107            }
108    
109            public SQLQuery createSQLQuery(String queryString) throws ORMException {
110                    Thread currentThread = Thread.currentThread();
111    
112                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
113    
114                    try {
115                            if (contextClassLoader != _classLoader) {
116                                    currentThread.setContextClassLoader(_classLoader);
117                            }
118    
119                            return _session.createSQLQuery(queryString);
120                    }
121                    finally {
122                            if (contextClassLoader != _classLoader) {
123                                    currentThread.setContextClassLoader(contextClassLoader);
124                            }
125                    }
126            }
127    
128            public void delete(Object object) throws ORMException {
129                    Thread currentThread = Thread.currentThread();
130    
131                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
132    
133                    try {
134                            if (contextClassLoader != _classLoader) {
135                                    currentThread.setContextClassLoader(_classLoader);
136                            }
137    
138                            _session.delete(object);
139                    }
140                    finally {
141                            if (contextClassLoader != _classLoader) {
142                                    currentThread.setContextClassLoader(contextClassLoader);
143                            }
144                    }
145            }
146    
147            public void evict(Object object) throws ORMException {
148                    Thread currentThread = Thread.currentThread();
149    
150                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
151    
152                    try {
153                            if (contextClassLoader != _classLoader) {
154                                    currentThread.setContextClassLoader(_classLoader);
155                            }
156    
157                            _session.evict(object);
158                    }
159                    finally {
160                            if (contextClassLoader != _classLoader) {
161                                    currentThread.setContextClassLoader(contextClassLoader);
162                            }
163                    }
164            }
165    
166            public void flush() throws ORMException {
167                    Thread currentThread = Thread.currentThread();
168    
169                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
170    
171                    try {
172                            if (contextClassLoader != _classLoader) {
173                                    currentThread.setContextClassLoader(_classLoader);
174                            }
175    
176                            _session.flush();
177                    }
178                    finally {
179                            if (contextClassLoader != _classLoader) {
180                                    currentThread.setContextClassLoader(contextClassLoader);
181                            }
182                    }
183            }
184    
185            public Object get(Class<?> classObject, Serializable id)
186                    throws ORMException {
187    
188                    Thread currentThread = Thread.currentThread();
189    
190                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
191    
192                    try {
193                            if (contextClassLoader != _classLoader) {
194                                    currentThread.setContextClassLoader(_classLoader);
195                            }
196    
197                            return _session.get(classObject, id);
198                    }
199                    finally {
200                            if (contextClassLoader != _classLoader) {
201                                    currentThread.setContextClassLoader(contextClassLoader);
202                            }
203                    }
204            }
205    
206            public Object get(Class<?> classObject, Serializable id, LockMode lockMode)
207                    throws ORMException {
208    
209                    Thread currentThread = Thread.currentThread();
210    
211                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
212    
213                    try {
214                            if (contextClassLoader != _classLoader) {
215                                    currentThread.setContextClassLoader(_classLoader);
216                            }
217    
218                            return _session.get(classObject, id, lockMode);
219                    }
220                    finally {
221                            if (contextClassLoader != _classLoader) {
222                                    currentThread.setContextClassLoader(contextClassLoader);
223                            }
224                    }
225            }
226    
227            public Object getWrappedSession() throws ORMException {
228                    Thread currentThread = Thread.currentThread();
229    
230                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
231    
232                    try {
233                            if (contextClassLoader != _classLoader) {
234                                    currentThread.setContextClassLoader(_classLoader);
235                            }
236    
237                            return _session.getWrappedSession();
238                    }
239                    finally {
240                            if (contextClassLoader != _classLoader) {
241                                    currentThread.setContextClassLoader(contextClassLoader);
242                            }
243                    }
244            }
245    
246            public Object load(Class<?> classObject, Serializable id)
247                    throws ORMException {
248    
249                    Thread currentThread = Thread.currentThread();
250    
251                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
252    
253                    try {
254                            if (contextClassLoader != _classLoader) {
255                                    currentThread.setContextClassLoader(_classLoader);
256                            }
257    
258                            return _session.load(classObject, id);
259                    }
260                    finally {
261                            if (contextClassLoader != _classLoader) {
262                                    currentThread.setContextClassLoader(contextClassLoader);
263                            }
264                    }
265            }
266    
267            public Object merge(Object object) throws ORMException {
268                    Thread currentThread = Thread.currentThread();
269    
270                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
271    
272                    try {
273                            if (contextClassLoader != _classLoader) {
274                                    currentThread.setContextClassLoader(_classLoader);
275                            }
276    
277                            return _session.merge(object);
278                    }
279                    finally {
280                            if (contextClassLoader != _classLoader) {
281                                    currentThread.setContextClassLoader(contextClassLoader);
282                            }
283                    }
284            }
285    
286            public Serializable save(Object object) throws ORMException {
287                    Thread currentThread = Thread.currentThread();
288    
289                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
290    
291                    try {
292                            if (contextClassLoader != _classLoader) {
293                                    currentThread.setContextClassLoader(_classLoader);
294                            }
295    
296                            return _session.save(object);
297                    }
298                    finally {
299                            if (contextClassLoader != _classLoader) {
300                                    currentThread.setContextClassLoader(contextClassLoader);
301                            }
302                    }
303            }
304    
305            public void saveOrUpdate(Object object) throws ORMException {
306                    Thread currentThread = Thread.currentThread();
307    
308                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
309    
310                    try {
311                            if (contextClassLoader != _classLoader) {
312                                    currentThread.setContextClassLoader(_classLoader);
313                            }
314    
315                            _session.saveOrUpdate(object);
316                    }
317                    finally {
318                            if (contextClassLoader != _classLoader) {
319                                    currentThread.setContextClassLoader(contextClassLoader);
320                            }
321                    }
322            }
323    
324            private ClassLoader _classLoader;
325            private Session _session;
326    
327    }