1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.model;
24  
25  import com.liferay.portal.ModelListenerException;
26  
27  /**
28   * <a href="ModelListenerWrapper.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   *
32   */
33  public class ModelListenerWrapper implements ModelListener {
34  
35      public ModelListenerWrapper(
36          ModelListener modelListener, ClassLoader classLoader) {
37  
38          _modelListener = modelListener;
39          _classLoader = classLoader;
40      }
41  
42      public void onAfterAddAssociation(
43              Object classPK, String associationClassName,
44              Object associationClassPK)
45          throws ModelListenerException {
46  
47          Thread currentThread = Thread.currentThread();
48  
49          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
50  
51          try {
52              currentThread.setContextClassLoader(_classLoader);
53  
54              _modelListener.onAfterAddAssociation(
55                  classPK, associationClassName, associationClassPK);
56          }
57          finally {
58              currentThread.setContextClassLoader(contextClassLoader);
59          }
60      }
61  
62      public void onAfterCreate(BaseModel model) throws ModelListenerException {
63          Thread currentThread = Thread.currentThread();
64  
65          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
66  
67          try {
68              currentThread.setContextClassLoader(_classLoader);
69  
70              _modelListener.onAfterCreate(model);
71          }
72          finally {
73              currentThread.setContextClassLoader(contextClassLoader);
74          }
75      }
76  
77      public void onAfterRemove(BaseModel model) throws ModelListenerException {
78          Thread currentThread = Thread.currentThread();
79  
80          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
81  
82          try {
83              currentThread.setContextClassLoader(_classLoader);
84  
85              _modelListener.onAfterRemove(model);
86          }
87          finally {
88              currentThread.setContextClassLoader(contextClassLoader);
89          }
90      }
91  
92      public void onAfterRemoveAssociation(
93              Object classPK, String associationClassName,
94              Object associationClassPK)
95          throws ModelListenerException {
96  
97          Thread currentThread = Thread.currentThread();
98  
99          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
100 
101         try {
102             currentThread.setContextClassLoader(_classLoader);
103 
104             _modelListener.onAfterRemoveAssociation(
105                 classPK, associationClassName, associationClassPK);
106         }
107         finally {
108             currentThread.setContextClassLoader(contextClassLoader);
109         }
110     }
111 
112     public void onAfterUpdate(BaseModel model) throws ModelListenerException {
113         Thread currentThread = Thread.currentThread();
114 
115         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
116 
117         try {
118             currentThread.setContextClassLoader(_classLoader);
119 
120             _modelListener.onAfterUpdate(model);
121         }
122         finally {
123             currentThread.setContextClassLoader(contextClassLoader);
124         }
125     }
126 
127     public void onBeforeAddAssociation(
128             Object classPK, String associationClassName,
129             Object associationClassPK)
130         throws ModelListenerException {
131 
132         Thread currentThread = Thread.currentThread();
133 
134         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
135 
136         try {
137             currentThread.setContextClassLoader(_classLoader);
138 
139             _modelListener.onBeforeAddAssociation(
140                 classPK, associationClassName, associationClassPK);
141         }
142         finally {
143             currentThread.setContextClassLoader(contextClassLoader);
144         }
145     }
146 
147     public void onBeforeCreate(BaseModel model) throws ModelListenerException {
148         Thread currentThread = Thread.currentThread();
149 
150         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
151 
152         try {
153             currentThread.setContextClassLoader(_classLoader);
154 
155             _modelListener.onBeforeCreate(model);
156         }
157         finally {
158             currentThread.setContextClassLoader(contextClassLoader);
159         }
160     }
161 
162     public void onBeforeRemove(BaseModel model) throws ModelListenerException {
163         Thread currentThread = Thread.currentThread();
164 
165         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
166 
167         try {
168             currentThread.setContextClassLoader(_classLoader);
169 
170             _modelListener.onBeforeRemove(model);
171         }
172         finally {
173             currentThread.setContextClassLoader(contextClassLoader);
174         }
175     }
176 
177     public void onBeforeRemoveAssociation(
178             Object classPK, String associationClassName,
179             Object associationClassPK)
180         throws ModelListenerException {
181 
182         Thread currentThread = Thread.currentThread();
183 
184         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
185 
186         try {
187             currentThread.setContextClassLoader(_classLoader);
188 
189             _modelListener.onBeforeRemoveAssociation(
190                 classPK, associationClassName, associationClassPK);
191         }
192         finally {
193             currentThread.setContextClassLoader(contextClassLoader);
194         }
195     }
196 
197     public void onBeforeUpdate(BaseModel model) throws ModelListenerException {
198         Thread currentThread = Thread.currentThread();
199 
200         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
201 
202         try {
203             currentThread.setContextClassLoader(_classLoader);
204 
205             _modelListener.onBeforeUpdate(model);
206         }
207         finally {
208             currentThread.setContextClassLoader(contextClassLoader);
209         }
210     }
211 
212     private ModelListener _modelListener;
213     private ClassLoader _classLoader;
214 
215 }