1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.dao.orm;
16  
17  import java.io.Serializable;
18  
19  import java.sql.Connection;
20  
21  /**
22   * <a href="ClassLoaderSession.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Shuyang Zhou
25   * @author Brian Wing Shun Chan
26   */
27  public class ClassLoaderSession implements Session {
28  
29      public ClassLoaderSession(Session session, ClassLoader classLoader) {
30          _session = session;
31          _classLoader = classLoader;
32      }
33  
34      public void clear() throws ORMException {
35          Thread currentThread = Thread.currentThread();
36  
37          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
38  
39          try {
40              if (contextClassLoader != _classLoader) {
41                  currentThread.setContextClassLoader(_classLoader);
42              }
43  
44              _session.clear();
45          }
46          finally {
47              if (contextClassLoader != _classLoader) {
48  
49                  currentThread.setContextClassLoader(contextClassLoader);
50              }
51          }
52      }
53  
54      public Connection close() throws ORMException {
55          Thread currentThread = Thread.currentThread();
56  
57          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
58  
59          try {
60              if (contextClassLoader != _classLoader) {
61                  currentThread.setContextClassLoader(_classLoader);
62              }
63  
64              return _session.close();
65          }
66          finally {
67              if (contextClassLoader != _classLoader) {
68                  currentThread.setContextClassLoader(contextClassLoader);
69              }
70          }
71      }
72  
73      public boolean contains(Object object) throws ORMException {
74          Thread currentThread = Thread.currentThread();
75  
76          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
77  
78          try {
79              if (contextClassLoader != _classLoader) {
80                  currentThread.setContextClassLoader(_classLoader);
81              }
82  
83              return _session.contains(object);
84          }
85          finally {
86              if (contextClassLoader != _classLoader) {
87                  currentThread.setContextClassLoader(contextClassLoader);
88              }
89          }
90      }
91  
92      public Query createQuery(String queryString) throws ORMException {
93          Thread currentThread = Thread.currentThread();
94  
95          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
96  
97          try {
98              if (contextClassLoader != _classLoader) {
99                  currentThread.setContextClassLoader(_classLoader);
100             }
101 
102             return _session.createQuery(queryString);
103         }
104         finally {
105             if (contextClassLoader != _classLoader) {
106                 currentThread.setContextClassLoader(contextClassLoader);
107             }
108         }
109     }
110 
111     public SQLQuery createSQLQuery(String queryString) throws ORMException {
112         Thread currentThread = Thread.currentThread();
113 
114         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
115 
116         try {
117             if (contextClassLoader != _classLoader) {
118                 currentThread.setContextClassLoader(_classLoader);
119             }
120 
121             return _session.createSQLQuery(queryString);
122         }
123         finally {
124             if (contextClassLoader != _classLoader) {
125                 currentThread.setContextClassLoader(contextClassLoader);
126             }
127         }
128     }
129 
130     public void delete(Object object) throws ORMException {
131         Thread currentThread = Thread.currentThread();
132 
133         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
134 
135         try {
136             if (contextClassLoader != _classLoader) {
137                 currentThread.setContextClassLoader(_classLoader);
138             }
139 
140             _session.delete(object);
141         }
142         finally {
143             if (contextClassLoader != _classLoader) {
144                 currentThread.setContextClassLoader(contextClassLoader);
145             }
146         }
147     }
148 
149     public void evict(Object object) throws ORMException {
150         Thread currentThread = Thread.currentThread();
151 
152         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
153 
154         try {
155             if (contextClassLoader != _classLoader) {
156                 currentThread.setContextClassLoader(_classLoader);
157             }
158 
159             _session.evict(object);
160         }
161         finally {
162             if (contextClassLoader != _classLoader) {
163                 currentThread.setContextClassLoader(contextClassLoader);
164             }
165         }
166     }
167 
168     public void flush() throws ORMException {
169         Thread currentThread = Thread.currentThread();
170 
171         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
172 
173         try {
174             if (contextClassLoader != _classLoader) {
175                 currentThread.setContextClassLoader(_classLoader);
176             }
177 
178             _session.flush();
179         }
180         finally {
181             if (contextClassLoader != _classLoader) {
182                 currentThread.setContextClassLoader(contextClassLoader);
183             }
184         }
185     }
186 
187     public Object get(Class<?> classObject, Serializable id)
188         throws ORMException {
189 
190         Thread currentThread = Thread.currentThread();
191 
192         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
193 
194         try {
195             if (contextClassLoader != _classLoader) {
196                 currentThread.setContextClassLoader(_classLoader);
197             }
198 
199             return _session.get(classObject, id);
200         }
201         finally {
202             if (contextClassLoader != _classLoader) {
203                 currentThread.setContextClassLoader(contextClassLoader);
204             }
205         }
206     }
207 
208     public Object get(Class<?> classObject, Serializable id, LockMode lockMode)
209         throws ORMException {
210 
211         Thread currentThread = Thread.currentThread();
212 
213         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
214 
215         try {
216             if (contextClassLoader != _classLoader) {
217                 currentThread.setContextClassLoader(_classLoader);
218             }
219 
220             return _session.get(classObject, id, lockMode);
221         }
222         finally {
223             if (contextClassLoader != _classLoader) {
224                 currentThread.setContextClassLoader(contextClassLoader);
225             }
226         }
227     }
228 
229     public Object getWrappedSession() throws ORMException {
230         Thread currentThread = Thread.currentThread();
231 
232         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
233 
234         try {
235             if (contextClassLoader != _classLoader) {
236                 currentThread.setContextClassLoader(_classLoader);
237             }
238 
239             return _session.getWrappedSession();
240         }
241         finally {
242             if (contextClassLoader != _classLoader) {
243                 currentThread.setContextClassLoader(contextClassLoader);
244             }
245         }
246     }
247 
248     public Object load(Class<?> classObject, Serializable id)
249         throws ORMException {
250 
251         Thread currentThread = Thread.currentThread();
252 
253         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
254 
255         try {
256             if (contextClassLoader != _classLoader) {
257                 currentThread.setContextClassLoader(_classLoader);
258             }
259 
260             return _session.load(classObject, id);
261         }
262         finally {
263             if (contextClassLoader != _classLoader) {
264                 currentThread.setContextClassLoader(contextClassLoader);
265             }
266         }
267     }
268 
269     public Object merge(Object object) throws ORMException {
270         Thread currentThread = Thread.currentThread();
271 
272         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
273 
274         try {
275             if (contextClassLoader != _classLoader) {
276                 currentThread.setContextClassLoader(_classLoader);
277             }
278 
279             return _session.merge(object);
280         }
281         finally {
282             if (contextClassLoader != _classLoader) {
283                 currentThread.setContextClassLoader(contextClassLoader);
284             }
285         }
286     }
287 
288     public Serializable save(Object object) throws ORMException {
289         Thread currentThread = Thread.currentThread();
290 
291         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
292 
293         try {
294             if (contextClassLoader != _classLoader) {
295                 currentThread.setContextClassLoader(_classLoader);
296             }
297 
298             return _session.save(object);
299         }
300         finally {
301             if (contextClassLoader != _classLoader) {
302                 currentThread.setContextClassLoader(contextClassLoader);
303             }
304         }
305     }
306 
307     public void saveOrUpdate(Object object) throws ORMException {
308         Thread currentThread = Thread.currentThread();
309 
310         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
311 
312         try {
313             if (contextClassLoader != _classLoader) {
314                 currentThread.setContextClassLoader(_classLoader);
315             }
316 
317             _session.saveOrUpdate(object);
318         }
319         finally {
320             if (contextClassLoader != _classLoader) {
321                 currentThread.setContextClassLoader(contextClassLoader);
322             }
323         }
324     }
325 
326     private ClassLoader _classLoader;
327     private Session _session;
328 
329 }