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.lar;
24  
25  import javax.portlet.PortletPreferences;
26  
27  /**
28   * <a href="InvokerPortletDataHandler.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Bruno Farache
31   * @author Raymond Augé
32   *
33   */
34  public class InvokerPortletDataHandler implements PortletDataHandler {
35  
36      public InvokerPortletDataHandler(
37          PortletDataHandler portletDataHandler, ClassLoader classLoader) {
38  
39          _portletDataHandler = portletDataHandler;
40          _classLoader = classLoader;
41      }
42  
43      public PortletPreferences deleteData(
44              PortletDataContext context, String portletId,
45              PortletPreferences preferences)
46          throws PortletDataException {
47  
48          Thread currentThread = Thread.currentThread();
49  
50          ClassLoader threadClassLoader = currentThread.getContextClassLoader();
51          ClassLoader contextClassLoader = context.getClassLoader();
52  
53          try {
54              currentThread.setContextClassLoader(_classLoader);
55              context.setClassLoader(_classLoader);
56  
57              return _portletDataHandler.deleteData(
58                  context, portletId, preferences);
59          }
60          finally {
61              currentThread.setContextClassLoader(threadClassLoader);
62              context.setClassLoader(contextClassLoader);
63          }
64      }
65  
66      public String exportData(
67              PortletDataContext context, String portletId,
68              PortletPreferences preferences)
69          throws PortletDataException {
70  
71          Thread currentThread = Thread.currentThread();
72  
73          ClassLoader threadClassLoader = currentThread.getContextClassLoader();
74          ClassLoader contextClassLoader = context.getClassLoader();
75  
76          try {
77              currentThread.setContextClassLoader(_classLoader);
78              context.setClassLoader(_classLoader);
79  
80              return _portletDataHandler.exportData(
81                  context, portletId, preferences);
82          }
83          finally {
84              currentThread.setContextClassLoader(threadClassLoader);
85              context.setClassLoader(contextClassLoader);
86          }
87      }
88  
89      public PortletDataHandlerControl[] getExportControls()
90          throws PortletDataException {
91  
92          Thread currentThread = Thread.currentThread();
93  
94          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
95  
96          try {
97              currentThread.setContextClassLoader(_classLoader);
98  
99              return _portletDataHandler.getExportControls();
100         }
101         finally {
102             currentThread.setContextClassLoader(contextClassLoader);
103         }
104     }
105 
106     public PortletDataHandlerControl[] getImportControls()
107         throws PortletDataException {
108 
109         Thread currentThread = Thread.currentThread();
110 
111         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
112 
113         try {
114             currentThread.setContextClassLoader(_classLoader);
115 
116             return _portletDataHandler.getImportControls();
117         }
118         finally {
119             currentThread.setContextClassLoader(contextClassLoader);
120         }
121     }
122 
123     public PortletPreferences importData(
124             PortletDataContext context, String portletId,
125             PortletPreferences preferences, String data)
126         throws PortletDataException {
127 
128         Thread currentThread = Thread.currentThread();
129 
130         ClassLoader threadClassLoader = currentThread.getContextClassLoader();
131         ClassLoader contextClassLoader = context.getClassLoader();
132 
133         try {
134             currentThread.setContextClassLoader(_classLoader);
135             context.setClassLoader(_classLoader);
136 
137             return _portletDataHandler.importData(
138                 context, portletId, preferences, data);
139         }
140         finally {
141             currentThread.setContextClassLoader(threadClassLoader);
142             context.setClassLoader(contextClassLoader);
143         }
144     }
145 
146     public boolean isAlwaysExportable() {
147         Thread currentThread = Thread.currentThread();
148 
149         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
150 
151         try {
152             currentThread.setContextClassLoader(_classLoader);
153 
154             return _portletDataHandler.isAlwaysExportable();
155         }
156         finally {
157             currentThread.setContextClassLoader(contextClassLoader);
158         }
159     }
160 
161     public boolean isPublishToLiveByDefault() {
162         Thread currentThread = Thread.currentThread();
163 
164         ClassLoader contextClassLoader = currentThread.getContextClassLoader();
165 
166         try {
167             currentThread.setContextClassLoader(_classLoader);
168 
169             return _portletDataHandler.isPublishToLiveByDefault();
170         }
171         finally {
172             currentThread.setContextClassLoader(contextClassLoader);
173         }
174     }
175 
176     private PortletDataHandler _portletDataHandler;
177     private ClassLoader _classLoader;
178 
179 }