001    /**
002     * Copyright (c) 2000-2013 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            public void clear() throws ORMException {
038                    Thread currentThread = Thread.currentThread();
039    
040                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
041    
042                    try {
043                            if (contextClassLoader != _classLoader) {
044                                    currentThread.setContextClassLoader(_classLoader);
045                            }
046    
047                            _session.clear();
048                    }
049                    finally {
050                            if (contextClassLoader != _classLoader) {
051    
052                                    currentThread.setContextClassLoader(contextClassLoader);
053                            }
054                    }
055            }
056    
057            @NotPrivileged
058            public Connection close() throws ORMException {
059                    Thread currentThread = Thread.currentThread();
060    
061                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
062    
063                    try {
064                            if (contextClassLoader != _classLoader) {
065                                    currentThread.setContextClassLoader(_classLoader);
066                            }
067    
068                            return _session.close();
069                    }
070                    finally {
071                            if (contextClassLoader != _classLoader) {
072                                    currentThread.setContextClassLoader(contextClassLoader);
073                            }
074                    }
075            }
076    
077            @NotPrivileged
078            public boolean contains(Object object) throws ORMException {
079                    Thread currentThread = Thread.currentThread();
080    
081                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
082    
083                    try {
084                            if (contextClassLoader != _classLoader) {
085                                    currentThread.setContextClassLoader(_classLoader);
086                            }
087    
088                            return _session.contains(object);
089                    }
090                    finally {
091                            if (contextClassLoader != _classLoader) {
092                                    currentThread.setContextClassLoader(contextClassLoader);
093                            }
094                    }
095            }
096    
097            public Query createQuery(String queryString) throws ORMException {
098                    Thread currentThread = Thread.currentThread();
099    
100                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
101    
102                    try {
103                            if (contextClassLoader != _classLoader) {
104                                    currentThread.setContextClassLoader(_classLoader);
105                            }
106    
107                            return _session.createQuery(queryString);
108                    }
109                    finally {
110                            if (contextClassLoader != _classLoader) {
111                                    currentThread.setContextClassLoader(contextClassLoader);
112                            }
113                    }
114            }
115    
116            public Query createQuery(String queryString, boolean strictName)
117                    throws ORMException {
118    
119                    Thread currentThread = Thread.currentThread();
120    
121                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
122    
123                    try {
124                            if (contextClassLoader != _classLoader) {
125                                    currentThread.setContextClassLoader(_classLoader);
126                            }
127    
128                            return _session.createQuery(queryString, strictName);
129                    }
130                    finally {
131                            if (contextClassLoader != _classLoader) {
132                                    currentThread.setContextClassLoader(contextClassLoader);
133                            }
134                    }
135            }
136    
137            public SQLQuery createSQLQuery(String queryString) throws ORMException {
138                    Thread currentThread = Thread.currentThread();
139    
140                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
141    
142                    try {
143                            if (contextClassLoader != _classLoader) {
144                                    currentThread.setContextClassLoader(_classLoader);
145                            }
146    
147                            return _session.createSQLQuery(queryString);
148                    }
149                    finally {
150                            if (contextClassLoader != _classLoader) {
151                                    currentThread.setContextClassLoader(contextClassLoader);
152                            }
153                    }
154            }
155    
156            public SQLQuery createSQLQuery(String queryString, boolean strictName)
157                    throws ORMException {
158    
159                    Thread currentThread = Thread.currentThread();
160    
161                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
162    
163                    try {
164                            if (contextClassLoader != _classLoader) {
165                                    currentThread.setContextClassLoader(_classLoader);
166                            }
167    
168                            return _session.createSQLQuery(queryString, strictName);
169                    }
170                    finally {
171                            if (contextClassLoader != _classLoader) {
172                                    currentThread.setContextClassLoader(contextClassLoader);
173                            }
174                    }
175            }
176    
177            @NotPrivileged
178            public void delete(Object object) throws ORMException {
179                    Thread currentThread = Thread.currentThread();
180    
181                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
182    
183                    try {
184                            if (contextClassLoader != _classLoader) {
185                                    currentThread.setContextClassLoader(_classLoader);
186                            }
187    
188                            _session.delete(object);
189                    }
190                    finally {
191                            if (contextClassLoader != _classLoader) {
192                                    currentThread.setContextClassLoader(contextClassLoader);
193                            }
194                    }
195            }
196    
197            @NotPrivileged
198            public void evict(Object object) throws ORMException {
199                    Thread currentThread = Thread.currentThread();
200    
201                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
202    
203                    try {
204                            if (contextClassLoader != _classLoader) {
205                                    currentThread.setContextClassLoader(_classLoader);
206                            }
207    
208                            _session.evict(object);
209                    }
210                    finally {
211                            if (contextClassLoader != _classLoader) {
212                                    currentThread.setContextClassLoader(contextClassLoader);
213                            }
214                    }
215            }
216    
217            @NotPrivileged
218            public void flush() throws ORMException {
219                    Thread currentThread = Thread.currentThread();
220    
221                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
222    
223                    try {
224                            if (contextClassLoader != _classLoader) {
225                                    currentThread.setContextClassLoader(_classLoader);
226                            }
227    
228                            _session.flush();
229                    }
230                    finally {
231                            if (contextClassLoader != _classLoader) {
232                                    currentThread.setContextClassLoader(contextClassLoader);
233                            }
234                    }
235            }
236    
237            @NotPrivileged
238            public Object get(Class<?> clazz, Serializable id) throws ORMException {
239                    Thread currentThread = Thread.currentThread();
240    
241                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
242    
243                    try {
244                            if (contextClassLoader != _classLoader) {
245                                    currentThread.setContextClassLoader(_classLoader);
246                            }
247    
248                            return _session.get(clazz, id);
249                    }
250                    finally {
251                            if (contextClassLoader != _classLoader) {
252                                    currentThread.setContextClassLoader(contextClassLoader);
253                            }
254                    }
255            }
256    
257            @NotPrivileged
258            public Object get(Class<?> clazz, Serializable id, LockMode lockMode)
259                    throws ORMException {
260    
261                    Thread currentThread = Thread.currentThread();
262    
263                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
264    
265                    try {
266                            if (contextClassLoader != _classLoader) {
267                                    currentThread.setContextClassLoader(_classLoader);
268                            }
269    
270                            return _session.get(clazz, id, lockMode);
271                    }
272                    finally {
273                            if (contextClassLoader != _classLoader) {
274                                    currentThread.setContextClassLoader(contextClassLoader);
275                            }
276                    }
277            }
278    
279            @NotPrivileged
280            public Object getWrappedSession() throws ORMException {
281                    Thread currentThread = Thread.currentThread();
282    
283                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
284    
285                    try {
286                            if (contextClassLoader != _classLoader) {
287                                    currentThread.setContextClassLoader(_classLoader);
288                            }
289    
290                            return _session.getWrappedSession();
291                    }
292                    finally {
293                            if (contextClassLoader != _classLoader) {
294                                    currentThread.setContextClassLoader(contextClassLoader);
295                            }
296                    }
297            }
298    
299            @NotPrivileged
300            public Object load(Class<?> clazz, Serializable id) throws ORMException {
301                    Thread currentThread = Thread.currentThread();
302    
303                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
304    
305                    try {
306                            if (contextClassLoader != _classLoader) {
307                                    currentThread.setContextClassLoader(_classLoader);
308                            }
309    
310                            return _session.load(clazz, id);
311                    }
312                    finally {
313                            if (contextClassLoader != _classLoader) {
314                                    currentThread.setContextClassLoader(contextClassLoader);
315                            }
316                    }
317            }
318    
319            @NotPrivileged
320            public Object merge(Object object) throws ORMException {
321                    Thread currentThread = Thread.currentThread();
322    
323                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
324    
325                    try {
326                            if (contextClassLoader != _classLoader) {
327                                    currentThread.setContextClassLoader(_classLoader);
328                            }
329    
330                            return _session.merge(object);
331                    }
332                    finally {
333                            if (contextClassLoader != _classLoader) {
334                                    currentThread.setContextClassLoader(contextClassLoader);
335                            }
336                    }
337            }
338    
339            @NotPrivileged
340            public Serializable save(Object object) throws ORMException {
341                    Thread currentThread = Thread.currentThread();
342    
343                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
344    
345                    try {
346                            if (contextClassLoader != _classLoader) {
347                                    currentThread.setContextClassLoader(_classLoader);
348                            }
349    
350                            return _session.save(object);
351                    }
352                    finally {
353                            if (contextClassLoader != _classLoader) {
354                                    currentThread.setContextClassLoader(contextClassLoader);
355                            }
356                    }
357            }
358    
359            @NotPrivileged
360            public void saveOrUpdate(Object object) throws ORMException {
361                    Thread currentThread = Thread.currentThread();
362    
363                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
364    
365                    try {
366                            if (contextClassLoader != _classLoader) {
367                                    currentThread.setContextClassLoader(_classLoader);
368                            }
369    
370                            _session.saveOrUpdate(object);
371                    }
372                    finally {
373                            if (contextClassLoader != _classLoader) {
374                                    currentThread.setContextClassLoader(contextClassLoader);
375                            }
376                    }
377            }
378    
379            private ClassLoader _classLoader;
380            private Session _session;
381    
382    }