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