001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.dao.orm;
016    
017    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
018    import com.liferay.portal.kernel.security.pacl.NotPrivileged;
019    
020    import java.io.Serializable;
021    
022    import java.sql.Connection;
023    
024    /**
025     * @author Shuyang Zhou
026     * @author Brian Wing Shun Chan
027     */
028    @DoPrivileged
029    public class ClassLoaderSession implements Session {
030    
031            public ClassLoaderSession(Session session, ClassLoader classLoader) {
032                    _session = session;
033                    _classLoader = classLoader;
034            }
035    
036            @NotPrivileged
037            @Override
038            public void clear() throws ORMException {
039                    Thread currentThread = Thread.currentThread();
040    
041                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
042    
043                    try {
044                            if (contextClassLoader != _classLoader) {
045                                    currentThread.setContextClassLoader(_classLoader);
046                            }
047    
048                            _session.clear();
049                    }
050                    finally {
051                            if (contextClassLoader != _classLoader) {
052                                    currentThread.setContextClassLoader(contextClassLoader);
053                            }
054                    }
055            }
056    
057            @NotPrivileged
058            @Override
059            public Connection close() throws ORMException {
060                    Thread currentThread = Thread.currentThread();
061    
062                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
063    
064                    try {
065                            if (contextClassLoader != _classLoader) {
066                                    currentThread.setContextClassLoader(_classLoader);
067                            }
068    
069                            return _session.close();
070                    }
071                    finally {
072                            if (contextClassLoader != _classLoader) {
073                                    currentThread.setContextClassLoader(contextClassLoader);
074                            }
075                    }
076            }
077    
078            @NotPrivileged
079            @Override
080            public boolean contains(Object object) throws ORMException {
081                    Thread currentThread = Thread.currentThread();
082    
083                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
084    
085                    try {
086                            if (contextClassLoader != _classLoader) {
087                                    currentThread.setContextClassLoader(_classLoader);
088                            }
089    
090                            return _session.contains(object);
091                    }
092                    finally {
093                            if (contextClassLoader != _classLoader) {
094                                    currentThread.setContextClassLoader(contextClassLoader);
095                            }
096                    }
097            }
098    
099            @Override
100            public Query createQuery(String queryString) throws ORMException {
101                    Thread currentThread = Thread.currentThread();
102    
103                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
104    
105                    try {
106                            if (contextClassLoader != _classLoader) {
107                                    currentThread.setContextClassLoader(_classLoader);
108                            }
109    
110                            return _session.createQuery(queryString);
111                    }
112                    finally {
113                            if (contextClassLoader != _classLoader) {
114                                    currentThread.setContextClassLoader(contextClassLoader);
115                            }
116                    }
117            }
118    
119            @Override
120            public Query createQuery(String queryString, boolean strictName)
121                    throws ORMException {
122    
123                    Thread currentThread = Thread.currentThread();
124    
125                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
126    
127                    try {
128                            if (contextClassLoader != _classLoader) {
129                                    currentThread.setContextClassLoader(_classLoader);
130                            }
131    
132                            return _session.createQuery(queryString, strictName);
133                    }
134                    finally {
135                            if (contextClassLoader != _classLoader) {
136                                    currentThread.setContextClassLoader(contextClassLoader);
137                            }
138                    }
139            }
140    
141            @Override
142            public SQLQuery createSQLQuery(String queryString) throws ORMException {
143                    Thread currentThread = Thread.currentThread();
144    
145                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
146    
147                    try {
148                            if (contextClassLoader != _classLoader) {
149                                    currentThread.setContextClassLoader(_classLoader);
150                            }
151    
152                            return _session.createSQLQuery(queryString);
153                    }
154                    finally {
155                            if (contextClassLoader != _classLoader) {
156                                    currentThread.setContextClassLoader(contextClassLoader);
157                            }
158                    }
159            }
160    
161            @Override
162            public SQLQuery createSQLQuery(String queryString, boolean strictName)
163                    throws ORMException {
164    
165                    Thread currentThread = Thread.currentThread();
166    
167                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
168    
169                    try {
170                            if (contextClassLoader != _classLoader) {
171                                    currentThread.setContextClassLoader(_classLoader);
172                            }
173    
174                            return _session.createSQLQuery(queryString, strictName);
175                    }
176                    finally {
177                            if (contextClassLoader != _classLoader) {
178                                    currentThread.setContextClassLoader(contextClassLoader);
179                            }
180                    }
181            }
182    
183            @Override
184            public SQLQuery createSynchronizedSQLQuery(String queryString)
185                    throws ORMException {
186    
187                    Thread currentThread = Thread.currentThread();
188    
189                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
190    
191                    try {
192                            if (contextClassLoader != _classLoader) {
193                                    currentThread.setContextClassLoader(_classLoader);
194                            }
195    
196                            return _session.createSynchronizedSQLQuery(queryString);
197                    }
198                    finally {
199                            if (contextClassLoader != _classLoader) {
200                                    currentThread.setContextClassLoader(contextClassLoader);
201                            }
202                    }
203            }
204    
205            @Override
206            public SQLQuery createSynchronizedSQLQuery(
207                            String queryString, boolean strictName)
208                    throws ORMException {
209    
210                    Thread currentThread = Thread.currentThread();
211    
212                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
213    
214                    try {
215                            if (contextClassLoader != _classLoader) {
216                                    currentThread.setContextClassLoader(_classLoader);
217                            }
218    
219                            return _session.createSynchronizedSQLQuery(queryString, strictName);
220                    }
221                    finally {
222                            if (contextClassLoader != _classLoader) {
223                                    currentThread.setContextClassLoader(contextClassLoader);
224                            }
225                    }
226            }
227    
228            @NotPrivileged
229            @Override
230            public void delete(Object object) throws ORMException {
231                    Thread currentThread = Thread.currentThread();
232    
233                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
234    
235                    try {
236                            if (contextClassLoader != _classLoader) {
237                                    currentThread.setContextClassLoader(_classLoader);
238                            }
239    
240                            _session.delete(object);
241                    }
242                    finally {
243                            if (contextClassLoader != _classLoader) {
244                                    currentThread.setContextClassLoader(contextClassLoader);
245                            }
246                    }
247            }
248    
249            @NotPrivileged
250            @Override
251            public void evict(Object object) throws ORMException {
252                    Thread currentThread = Thread.currentThread();
253    
254                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
255    
256                    try {
257                            if (contextClassLoader != _classLoader) {
258                                    currentThread.setContextClassLoader(_classLoader);
259                            }
260    
261                            _session.evict(object);
262                    }
263                    finally {
264                            if (contextClassLoader != _classLoader) {
265                                    currentThread.setContextClassLoader(contextClassLoader);
266                            }
267                    }
268            }
269    
270            @NotPrivileged
271            @Override
272            public void flush() throws ORMException {
273                    Thread currentThread = Thread.currentThread();
274    
275                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
276    
277                    try {
278                            if (contextClassLoader != _classLoader) {
279                                    currentThread.setContextClassLoader(_classLoader);
280                            }
281    
282                            _session.flush();
283                    }
284                    finally {
285                            if (contextClassLoader != _classLoader) {
286                                    currentThread.setContextClassLoader(contextClassLoader);
287                            }
288                    }
289            }
290    
291            @NotPrivileged
292            @Override
293            public Object get(Class<?> clazz, Serializable id) throws ORMException {
294                    Thread currentThread = Thread.currentThread();
295    
296                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
297    
298                    try {
299                            if (contextClassLoader != _classLoader) {
300                                    currentThread.setContextClassLoader(_classLoader);
301                            }
302    
303                            return _session.get(clazz, id);
304                    }
305                    finally {
306                            if (contextClassLoader != _classLoader) {
307                                    currentThread.setContextClassLoader(contextClassLoader);
308                            }
309                    }
310            }
311    
312            @NotPrivileged
313            @Override
314            public Object get(Class<?> clazz, Serializable id, LockMode lockMode)
315                    throws ORMException {
316    
317                    Thread currentThread = Thread.currentThread();
318    
319                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
320    
321                    try {
322                            if (contextClassLoader != _classLoader) {
323                                    currentThread.setContextClassLoader(_classLoader);
324                            }
325    
326                            return _session.get(clazz, id, lockMode);
327                    }
328                    finally {
329                            if (contextClassLoader != _classLoader) {
330                                    currentThread.setContextClassLoader(contextClassLoader);
331                            }
332                    }
333            }
334    
335            @NotPrivileged
336            @Override
337            public Object getWrappedSession() throws ORMException {
338                    Thread currentThread = Thread.currentThread();
339    
340                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
341    
342                    try {
343                            if (contextClassLoader != _classLoader) {
344                                    currentThread.setContextClassLoader(_classLoader);
345                            }
346    
347                            return _session.getWrappedSession();
348                    }
349                    finally {
350                            if (contextClassLoader != _classLoader) {
351                                    currentThread.setContextClassLoader(contextClassLoader);
352                            }
353                    }
354            }
355    
356            @NotPrivileged
357            @Override
358            public boolean isDirty() throws ORMException {
359                    Thread currentThread = Thread.currentThread();
360    
361                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
362    
363                    try {
364                            if (contextClassLoader != _classLoader) {
365                                    currentThread.setContextClassLoader(_classLoader);
366                            }
367    
368                            return _session.isDirty();
369                    }
370                    finally {
371                            if (contextClassLoader != _classLoader) {
372                                    currentThread.setContextClassLoader(contextClassLoader);
373                            }
374                    }
375            }
376    
377            @NotPrivileged
378            @Override
379            public Object load(Class<?> clazz, Serializable id) throws ORMException {
380                    Thread currentThread = Thread.currentThread();
381    
382                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
383    
384                    try {
385                            if (contextClassLoader != _classLoader) {
386                                    currentThread.setContextClassLoader(_classLoader);
387                            }
388    
389                            return _session.load(clazz, id);
390                    }
391                    finally {
392                            if (contextClassLoader != _classLoader) {
393                                    currentThread.setContextClassLoader(contextClassLoader);
394                            }
395                    }
396            }
397    
398            @NotPrivileged
399            @Override
400            public Object merge(Object object) throws ORMException {
401                    Thread currentThread = Thread.currentThread();
402    
403                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
404    
405                    try {
406                            if (contextClassLoader != _classLoader) {
407                                    currentThread.setContextClassLoader(_classLoader);
408                            }
409    
410                            return _session.merge(object);
411                    }
412                    finally {
413                            if (contextClassLoader != _classLoader) {
414                                    currentThread.setContextClassLoader(contextClassLoader);
415                            }
416                    }
417            }
418    
419            @NotPrivileged
420            @Override
421            public Serializable save(Object object) throws ORMException {
422                    Thread currentThread = Thread.currentThread();
423    
424                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
425    
426                    try {
427                            if (contextClassLoader != _classLoader) {
428                                    currentThread.setContextClassLoader(_classLoader);
429                            }
430    
431                            return _session.save(object);
432                    }
433                    finally {
434                            if (contextClassLoader != _classLoader) {
435                                    currentThread.setContextClassLoader(contextClassLoader);
436                            }
437                    }
438            }
439    
440            @NotPrivileged
441            @Override
442            public void saveOrUpdate(Object object) throws ORMException {
443                    Thread currentThread = Thread.currentThread();
444    
445                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
446    
447                    try {
448                            if (contextClassLoader != _classLoader) {
449                                    currentThread.setContextClassLoader(_classLoader);
450                            }
451    
452                            _session.saveOrUpdate(object);
453                    }
454                    finally {
455                            if (contextClassLoader != _classLoader) {
456                                    currentThread.setContextClassLoader(contextClassLoader);
457                            }
458                    }
459            }
460    
461            private final ClassLoader _classLoader;
462            private final Session _session;
463    
464    }