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.portlet;
016    
017    import com.liferay.asset.kernel.model.AssetRendererFactory;
018    import com.liferay.expando.kernel.model.CustomAttributesDisplay;
019    import com.liferay.exportimport.kernel.lar.PortletDataHandler;
020    import com.liferay.exportimport.kernel.lar.StagedModelDataHandler;
021    import com.liferay.portal.kernel.atom.AtomCollectionAdapter;
022    import com.liferay.portal.kernel.notifications.UserNotificationDefinition;
023    import com.liferay.portal.kernel.notifications.UserNotificationHandler;
024    import com.liferay.portal.kernel.poller.PollerProcessor;
025    import com.liferay.portal.kernel.pop.MessageListener;
026    import com.liferay.portal.kernel.portlet.ConfigurationAction;
027    import com.liferay.portal.kernel.portlet.ControlPanelEntry;
028    import com.liferay.portal.kernel.portlet.FriendlyURLMapperTracker;
029    import com.liferay.portal.kernel.portlet.PortletBag;
030    import com.liferay.portal.kernel.portlet.PortletLayoutListener;
031    import com.liferay.portal.kernel.portlet.ResourceBundleTracker;
032    import com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListener;
033    import com.liferay.portal.kernel.search.Indexer;
034    import com.liferay.portal.kernel.search.OpenSearch;
035    import com.liferay.portal.kernel.security.permission.PermissionPropagator;
036    import com.liferay.portal.kernel.servlet.URLEncoder;
037    import com.liferay.portal.kernel.template.TemplateHandler;
038    import com.liferay.portal.kernel.trash.TrashHandler;
039    import com.liferay.portal.kernel.util.ClassUtil;
040    import com.liferay.portal.kernel.util.LocaleUtil;
041    import com.liferay.portal.kernel.webdav.WebDAVStorage;
042    import com.liferay.portal.kernel.workflow.WorkflowHandler;
043    import com.liferay.portal.kernel.xmlrpc.Method;
044    import com.liferay.social.kernel.model.SocialActivityInterpreter;
045    import com.liferay.social.kernel.model.SocialRequestInterpreter;
046    
047    import java.io.Closeable;
048    
049    import java.util.List;
050    import java.util.Locale;
051    import java.util.ResourceBundle;
052    
053    import javax.portlet.Portlet;
054    import javax.portlet.PreferencesValidator;
055    
056    import javax.servlet.ServletContext;
057    
058    /**
059     * @author Brian Wing Shun Chan
060     * @author Jorge Ferrer
061     */
062    public class PortletBagImpl implements PortletBag {
063    
064            public PortletBagImpl(
065                    String portletName, ServletContext servletContext,
066                    Portlet portletInstance, ResourceBundleTracker resourceBundleTracker,
067                    List<ConfigurationAction> configurationActionInstances,
068                    List<Indexer<?>> indexerInstances, List<OpenSearch> openSearchInstances,
069                    List<SchedulerEventMessageListener> schedulerEventMessageListeners,
070                    FriendlyURLMapperTracker friendlyURLMapperTracker,
071                    List<URLEncoder> urlEncoderInstances,
072                    List<PortletDataHandler> portletDataHandlerInstances,
073                    List<StagedModelDataHandler<?>> stagedModelDataHandlerInstances,
074                    List<TemplateHandler> templateHandlerInstances,
075                    List<PortletLayoutListener> portletLayoutListenerInstances,
076                    List<PollerProcessor> pollerProcessorInstances,
077                    List<MessageListener> popMessageListenerInstances,
078                    List<SocialActivityInterpreter> socialActivityInterpreterInstances,
079                    List<SocialRequestInterpreter> socialRequestInterpreterInstances,
080                    List<UserNotificationDefinition> userNotificationDefinitionInstances,
081                    List<UserNotificationHandler> userNotificationHandlerInstances,
082                    List<WebDAVStorage> webDAVStorageInstances,
083                    List<Method> xmlRpcMethodInstances,
084                    List<ControlPanelEntry> controlPanelEntryInstances,
085                    List<AssetRendererFactory<?>> assetRendererFactoryInstances,
086                    List<AtomCollectionAdapter<?>> atomCollectionAdapters,
087                    List<CustomAttributesDisplay> customAttributesDisplayInstances,
088                    List<PermissionPropagator> permissionPropagatorInstances,
089                    List<TrashHandler> trashHandlerInstances,
090                    List<WorkflowHandler<?>> workflowHandlerInstances,
091                    List<PreferencesValidator> preferencesValidatorInstances) {
092    
093                    _portletName = portletName;
094                    _servletContext = servletContext;
095                    _portletInstance = portletInstance;
096                    _resourceBundleTracker = resourceBundleTracker;
097                    _configurationActionInstances = configurationActionInstances;
098                    _indexerInstances = indexerInstances;
099                    _openSearchInstances = openSearchInstances;
100                    _schedulerEventMessageListeners = schedulerEventMessageListeners;
101                    _friendlyURLMapperTracker = friendlyURLMapperTracker;
102                    _urlEncoderInstances = urlEncoderInstances;
103                    _portletDataHandlerInstances = portletDataHandlerInstances;
104                    _stagedModelDataHandlerInstances = stagedModelDataHandlerInstances;
105                    _templateHandlerInstances = templateHandlerInstances;
106                    _portletLayoutListenerInstances = portletLayoutListenerInstances;
107                    _pollerProcessorInstances = pollerProcessorInstances;
108                    _popMessageListenerInstances = popMessageListenerInstances;
109                    _socialActivityInterpreterInstances =
110                            socialActivityInterpreterInstances;
111                    _socialRequestInterpreterInstances = socialRequestInterpreterInstances;
112                    _userNotificationDefinitionInstances =
113                            userNotificationDefinitionInstances;
114                    _userNotificationHandlerInstances = userNotificationHandlerInstances;
115                    _webDAVStorageInstances = webDAVStorageInstances;
116                    _xmlRpcMethodInstances = xmlRpcMethodInstances;
117                    _controlPanelEntryInstances = controlPanelEntryInstances;
118                    _assetRendererFactoryInstances = assetRendererFactoryInstances;
119                    _atomCollectionAdapterInstances = atomCollectionAdapters;
120                    _customAttributesDisplayInstances = customAttributesDisplayInstances;
121                    _permissionPropagatorInstances = permissionPropagatorInstances;
122                    _trashHandlerInstances = trashHandlerInstances;
123                    _workflowHandlerInstances = workflowHandlerInstances;
124                    _preferencesValidatorInstances = preferencesValidatorInstances;
125            }
126    
127            @Override
128            public Object clone() {
129                    return new PortletBagImpl(
130                            getPortletName(), getServletContext(), getPortletInstance(),
131                            getResourceBundleTracker(), getConfigurationActionInstances(),
132                            getIndexerInstances(), getOpenSearchInstances(),
133                            getSchedulerEventMessageListeners(), getFriendlyURLMapperTracker(),
134                            getURLEncoderInstances(), getPortletDataHandlerInstances(),
135                            getStagedModelDataHandlerInstances(), getTemplateHandlerInstances(),
136                            getPortletLayoutListenerInstances(), getPollerProcessorInstances(),
137                            getPopMessageListenerInstances(),
138                            getSocialActivityInterpreterInstances(),
139                            getSocialRequestInterpreterInstances(),
140                            getUserNotificationDefinitionInstances(),
141                            getUserNotificationHandlerInstances(), getWebDAVStorageInstances(),
142                            getXmlRpcMethodInstances(), getControlPanelEntryInstances(),
143                            getAssetRendererFactoryInstances(),
144                            getAtomCollectionAdapterInstances(),
145                            getCustomAttributesDisplayInstances(),
146                            getPermissionPropagatorInstances(), getTrashHandlerInstances(),
147                            getWorkflowHandlerInstances(), getPreferencesValidatorInstances());
148            }
149    
150            @Override
151            public void destroy() {
152                    close(_assetRendererFactoryInstances);
153                    close(_atomCollectionAdapterInstances);
154                    close(_configurationActionInstances);
155                    close(_controlPanelEntryInstances);
156                    close(_customAttributesDisplayInstances);
157                    close(_friendlyURLMapperTracker);
158                    close(_indexerInstances);
159                    close(_openSearchInstances);
160                    close(_permissionPropagatorInstances);
161                    close(_pollerProcessorInstances);
162                    close(_popMessageListenerInstances);
163                    close(_portletDataHandlerInstances);
164                    close(_portletLayoutListenerInstances);
165                    close(_preferencesValidatorInstances);
166                    close(_resourceBundleTracker);
167                    close(_schedulerEventMessageListeners);
168                    close(_socialActivityInterpreterInstances);
169                    close(_socialRequestInterpreterInstances);
170                    close(_stagedModelDataHandlerInstances);
171                    close(_templateHandlerInstances);
172                    close(_trashHandlerInstances);
173                    close(_urlEncoderInstances);
174                    close(_userNotificationDefinitionInstances);
175                    close(_userNotificationHandlerInstances);
176                    close(_webDAVStorageInstances);
177                    close(_workflowHandlerInstances);
178                    close(_xmlRpcMethodInstances);
179            }
180    
181            @Override
182            public List<AssetRendererFactory<?>> getAssetRendererFactoryInstances() {
183                    return _assetRendererFactoryInstances;
184            }
185    
186            @Override
187            public List<AtomCollectionAdapter<?>> getAtomCollectionAdapterInstances() {
188                    return _atomCollectionAdapterInstances;
189            }
190    
191            @Override
192            public List<ConfigurationAction> getConfigurationActionInstances() {
193                    return _configurationActionInstances;
194            }
195    
196            @Override
197            public List<ControlPanelEntry> getControlPanelEntryInstances() {
198                    return _controlPanelEntryInstances;
199            }
200    
201            @Override
202            public List<CustomAttributesDisplay> getCustomAttributesDisplayInstances() {
203                    return _customAttributesDisplayInstances;
204            }
205    
206            @Override
207            public FriendlyURLMapperTracker getFriendlyURLMapperTracker() {
208                    return _friendlyURLMapperTracker;
209            }
210    
211            @Override
212            public List<Indexer<?>> getIndexerInstances() {
213                    return _indexerInstances;
214            }
215    
216            @Override
217            public List<OpenSearch> getOpenSearchInstances() {
218                    return _openSearchInstances;
219            }
220    
221            @Override
222            public List<PermissionPropagator> getPermissionPropagatorInstances() {
223                    return _permissionPropagatorInstances;
224            }
225    
226            @Override
227            public List<PollerProcessor> getPollerProcessorInstances() {
228                    return _pollerProcessorInstances;
229            }
230    
231            @Override
232            public List<MessageListener> getPopMessageListenerInstances() {
233                    return _popMessageListenerInstances;
234            }
235    
236            @Override
237            public List<PortletDataHandler> getPortletDataHandlerInstances() {
238                    return _portletDataHandlerInstances;
239            }
240    
241            @Override
242            public Portlet getPortletInstance() {
243                    return _portletInstance;
244            }
245    
246            @Override
247            public List<PortletLayoutListener> getPortletLayoutListenerInstances() {
248                    return _portletLayoutListenerInstances;
249            }
250    
251            @Override
252            public String getPortletName() {
253                    return _portletName;
254            }
255    
256            @Override
257            public List<PreferencesValidator> getPreferencesValidatorInstances() {
258                    return _preferencesValidatorInstances;
259            }
260    
261            @Override
262            public ResourceBundle getResourceBundle(Locale locale) {
263                    ResourceBundle resourceBundle =
264                            _resourceBundleTracker.getResourceBundle(
265                                    LocaleUtil.toLanguageId(locale));
266    
267                    if (resourceBundle == null) {
268                            resourceBundle = _resourceBundleTracker.getResourceBundle(
269                                    locale.getLanguage());
270    
271                            if (resourceBundle == null) {
272                                    resourceBundle = _resourceBundleTracker.getResourceBundle(
273                                            LocaleUtil.toLanguageId(LocaleUtil.getDefault()));
274                            }
275                    }
276    
277                    return resourceBundle;
278            }
279    
280            @Override
281            public ResourceBundleTracker getResourceBundleTracker() {
282                    return _resourceBundleTracker;
283            }
284    
285            @Override
286            public List<SchedulerEventMessageListener>
287                    getSchedulerEventMessageListeners() {
288    
289                    return _schedulerEventMessageListeners;
290            }
291    
292            @Override
293            public ServletContext getServletContext() {
294                    return _servletContext;
295            }
296    
297            @Override
298            public List<SocialActivityInterpreter>
299                    getSocialActivityInterpreterInstances() {
300    
301                    return _socialActivityInterpreterInstances;
302            }
303    
304            @Override
305            public List<SocialRequestInterpreter>
306                    getSocialRequestInterpreterInstances() {
307    
308                    return _socialRequestInterpreterInstances;
309            }
310    
311            @Override
312            public List<StagedModelDataHandler<?>>
313                    getStagedModelDataHandlerInstances() {
314    
315                    return _stagedModelDataHandlerInstances;
316            }
317    
318            @Override
319            public List<TemplateHandler> getTemplateHandlerInstances() {
320                    return _templateHandlerInstances;
321            }
322    
323            @Override
324            public List<TrashHandler> getTrashHandlerInstances() {
325                    return _trashHandlerInstances;
326            }
327    
328            @Override
329            public List<URLEncoder> getURLEncoderInstances() {
330                    return _urlEncoderInstances;
331            }
332    
333            @Override
334            public List<UserNotificationDefinition>
335                    getUserNotificationDefinitionInstances() {
336    
337                    return _userNotificationDefinitionInstances;
338            }
339    
340            @Override
341            public List<UserNotificationHandler>
342                    getUserNotificationHandlerInstances() {
343    
344                    return _userNotificationHandlerInstances;
345            }
346    
347            @Override
348            public List<WebDAVStorage> getWebDAVStorageInstances() {
349                    return _webDAVStorageInstances;
350            }
351    
352            @Override
353            public List<WorkflowHandler<?>> getWorkflowHandlerInstances() {
354                    return _workflowHandlerInstances;
355            }
356    
357            @Override
358            public List<Method> getXmlRpcMethodInstances() {
359                    return _xmlRpcMethodInstances;
360            }
361    
362            @Override
363            public void setPortletInstance(Portlet portletInstance) {
364                    _portletInstance = portletInstance;
365            }
366    
367            @Override
368            public void setPortletName(String portletName) {
369                    _portletName = portletName;
370            }
371    
372            protected void close(Object object) {
373                    try {
374                            Closeable closeable = (Closeable)object;
375    
376                            closeable.close();
377                    }
378                    catch (Exception e) {
379                            throw new RuntimeException(
380                                    "Unable to close " + ClassUtil.getClassName(object), e);
381                    }
382            }
383    
384            private final List<AssetRendererFactory<?>> _assetRendererFactoryInstances;
385            private final List<AtomCollectionAdapter<?>>
386                    _atomCollectionAdapterInstances;
387            private final List<ConfigurationAction> _configurationActionInstances;
388            private final List<ControlPanelEntry> _controlPanelEntryInstances;
389            private final List<CustomAttributesDisplay>
390                    _customAttributesDisplayInstances;
391            private final FriendlyURLMapperTracker _friendlyURLMapperTracker;
392            private final List<Indexer<?>> _indexerInstances;
393            private final List<OpenSearch> _openSearchInstances;
394            private final List<PermissionPropagator> _permissionPropagatorInstances;
395            private final List<PollerProcessor> _pollerProcessorInstances;
396            private final List<MessageListener> _popMessageListenerInstances;
397            private final List<PortletDataHandler> _portletDataHandlerInstances;
398            private Portlet _portletInstance;
399            private final List<PortletLayoutListener> _portletLayoutListenerInstances;
400            private String _portletName;
401            private final List<PreferencesValidator> _preferencesValidatorInstances;
402            private final ResourceBundleTracker _resourceBundleTracker;
403            private final List<SchedulerEventMessageListener>
404                    _schedulerEventMessageListeners;
405            private final ServletContext _servletContext;
406            private final List<SocialActivityInterpreter>
407                    _socialActivityInterpreterInstances;
408            private final List<SocialRequestInterpreter>
409                    _socialRequestInterpreterInstances;
410            private final List<StagedModelDataHandler<?>>
411                    _stagedModelDataHandlerInstances;
412            private final List<TemplateHandler> _templateHandlerInstances;
413            private final List<TrashHandler> _trashHandlerInstances;
414            private final List<URLEncoder> _urlEncoderInstances;
415            private final List<UserNotificationDefinition>
416                    _userNotificationDefinitionInstances;
417            private final List<UserNotificationHandler>
418                    _userNotificationHandlerInstances;
419            private final List<WebDAVStorage> _webDAVStorageInstances;
420            private final List<WorkflowHandler<?>> _workflowHandlerInstances;
421            private final List<Method> _xmlRpcMethodInstances;
422    
423    }