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.scheduler.messaging.SchedulerEventMessageListener;
032    import com.liferay.portal.kernel.search.Indexer;
033    import com.liferay.portal.kernel.search.OpenSearch;
034    import com.liferay.portal.kernel.security.permission.PermissionPropagator;
035    import com.liferay.portal.kernel.servlet.URLEncoder;
036    import com.liferay.portal.kernel.template.TemplateHandler;
037    import com.liferay.portal.kernel.trash.TrashHandler;
038    import com.liferay.portal.kernel.util.ClassUtil;
039    import com.liferay.portal.kernel.util.LocaleUtil;
040    import com.liferay.portal.kernel.util.ResourceBundleLoader;
041    import com.liferay.portal.kernel.util.ResourceBundleLoaderUtil;
042    import com.liferay.portal.kernel.webdav.WebDAVStorage;
043    import com.liferay.portal.kernel.workflow.WorkflowHandler;
044    import com.liferay.portal.kernel.xmlrpc.Method;
045    import com.liferay.social.kernel.model.SocialActivityInterpreter;
046    import com.liferay.social.kernel.model.SocialRequestInterpreter;
047    
048    import java.io.Closeable;
049    
050    import java.util.List;
051    import java.util.Locale;
052    import java.util.ResourceBundle;
053    
054    import javax.portlet.Portlet;
055    import javax.portlet.PreferencesValidator;
056    
057    import javax.servlet.ServletContext;
058    
059    /**
060     * @author Brian Wing Shun Chan
061     * @author Jorge Ferrer
062     */
063    public class PortletBagImpl implements PortletBag {
064    
065            public PortletBagImpl(
066                    String portletName, ServletContext servletContext,
067                    Portlet portletInstance, String resourceBundleBaseName,
068                    List<ConfigurationAction> configurationActionInstances,
069                    List<Indexer<?>> indexerInstances, List<OpenSearch> openSearchInstances,
070                    List<SchedulerEventMessageListener> schedulerEventMessageListeners,
071                    FriendlyURLMapperTracker friendlyURLMapperTracker,
072                    List<URLEncoder> urlEncoderInstances,
073                    List<PortletDataHandler> portletDataHandlerInstances,
074                    List<StagedModelDataHandler<?>> stagedModelDataHandlerInstances,
075                    List<TemplateHandler> templateHandlerInstances,
076                    List<PortletLayoutListener> portletLayoutListenerInstances,
077                    List<PollerProcessor> pollerProcessorInstances,
078                    List<MessageListener> popMessageListenerInstances,
079                    List<SocialActivityInterpreter> socialActivityInterpreterInstances,
080                    List<SocialRequestInterpreter> socialRequestInterpreterInstances,
081                    List<UserNotificationDefinition> userNotificationDefinitionInstances,
082                    List<UserNotificationHandler> userNotificationHandlerInstances,
083                    List<WebDAVStorage> webDAVStorageInstances,
084                    List<Method> xmlRpcMethodInstances,
085                    List<ControlPanelEntry> controlPanelEntryInstances,
086                    List<AssetRendererFactory<?>> assetRendererFactoryInstances,
087                    List<AtomCollectionAdapter<?>> atomCollectionAdapters,
088                    List<CustomAttributesDisplay> customAttributesDisplayInstances,
089                    List<PermissionPropagator> permissionPropagatorInstances,
090                    List<TrashHandler> trashHandlerInstances,
091                    List<WorkflowHandler<?>> workflowHandlerInstances,
092                    List<PreferencesValidator> preferencesValidatorInstances) {
093    
094                    _portletName = portletName;
095                    _servletContext = servletContext;
096                    _portletInstance = portletInstance;
097                    _resourceBundleBaseName = resourceBundleBaseName;
098                    _configurationActionInstances = configurationActionInstances;
099                    _indexerInstances = indexerInstances;
100                    _openSearchInstances = openSearchInstances;
101                    _schedulerEventMessageListeners = schedulerEventMessageListeners;
102                    _friendlyURLMapperTracker = friendlyURLMapperTracker;
103                    _urlEncoderInstances = urlEncoderInstances;
104                    _portletDataHandlerInstances = portletDataHandlerInstances;
105                    _stagedModelDataHandlerInstances = stagedModelDataHandlerInstances;
106                    _templateHandlerInstances = templateHandlerInstances;
107                    _portletLayoutListenerInstances = portletLayoutListenerInstances;
108                    _pollerProcessorInstances = pollerProcessorInstances;
109                    _popMessageListenerInstances = popMessageListenerInstances;
110                    _socialActivityInterpreterInstances =
111                            socialActivityInterpreterInstances;
112                    _socialRequestInterpreterInstances = socialRequestInterpreterInstances;
113                    _userNotificationDefinitionInstances =
114                            userNotificationDefinitionInstances;
115                    _userNotificationHandlerInstances = userNotificationHandlerInstances;
116                    _webDAVStorageInstances = webDAVStorageInstances;
117                    _xmlRpcMethodInstances = xmlRpcMethodInstances;
118                    _controlPanelEntryInstances = controlPanelEntryInstances;
119                    _assetRendererFactoryInstances = assetRendererFactoryInstances;
120                    _atomCollectionAdapterInstances = atomCollectionAdapters;
121                    _customAttributesDisplayInstances = customAttributesDisplayInstances;
122                    _permissionPropagatorInstances = permissionPropagatorInstances;
123                    _trashHandlerInstances = trashHandlerInstances;
124                    _workflowHandlerInstances = workflowHandlerInstances;
125                    _preferencesValidatorInstances = preferencesValidatorInstances;
126            }
127    
128            @Override
129            public Object clone() {
130                    return new PortletBagImpl(
131                            getPortletName(), getServletContext(), getPortletInstance(),
132                            getResourceBundleBaseName(), getConfigurationActionInstances(),
133                            getIndexerInstances(), getOpenSearchInstances(),
134                            getSchedulerEventMessageListeners(), getFriendlyURLMapperTracker(),
135                            getURLEncoderInstances(), getPortletDataHandlerInstances(),
136                            getStagedModelDataHandlerInstances(), getTemplateHandlerInstances(),
137                            getPortletLayoutListenerInstances(), getPollerProcessorInstances(),
138                            getPopMessageListenerInstances(),
139                            getSocialActivityInterpreterInstances(),
140                            getSocialRequestInterpreterInstances(),
141                            getUserNotificationDefinitionInstances(),
142                            getUserNotificationHandlerInstances(), getWebDAVStorageInstances(),
143                            getXmlRpcMethodInstances(), getControlPanelEntryInstances(),
144                            getAssetRendererFactoryInstances(),
145                            getAtomCollectionAdapterInstances(),
146                            getCustomAttributesDisplayInstances(),
147                            getPermissionPropagatorInstances(), getTrashHandlerInstances(),
148                            getWorkflowHandlerInstances(), getPreferencesValidatorInstances());
149            }
150    
151            @Override
152            public void destroy() {
153                    close(_assetRendererFactoryInstances);
154                    close(_atomCollectionAdapterInstances);
155                    close(_configurationActionInstances);
156                    close(_controlPanelEntryInstances);
157                    close(_customAttributesDisplayInstances);
158                    close(_friendlyURLMapperTracker);
159                    close(_indexerInstances);
160                    close(_openSearchInstances);
161                    close(_permissionPropagatorInstances);
162                    close(_pollerProcessorInstances);
163                    close(_popMessageListenerInstances);
164                    close(_portletDataHandlerInstances);
165                    close(_portletLayoutListenerInstances);
166                    close(_preferencesValidatorInstances);
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                    ResourceBundleLoader resourceBundleLoader =
264                            ResourceBundleLoaderUtil.
265                                    getResourceBundleLoaderByServletContextNameAndBaseName(
266                                            _servletContext.getServletContextName(),
267                                            getResourceBundleBaseName());
268    
269                    if (resourceBundleLoader == null) {
270                            return null;
271                    }
272    
273                    return resourceBundleLoader.loadResourceBundle(
274                            LocaleUtil.toLanguageId(locale));
275            }
276    
277            @Override
278            public String getResourceBundleBaseName() {
279                    return _resourceBundleBaseName;
280            }
281    
282            @Override
283            public List<SchedulerEventMessageListener>
284                    getSchedulerEventMessageListeners() {
285    
286                    return _schedulerEventMessageListeners;
287            }
288    
289            @Override
290            public ServletContext getServletContext() {
291                    return _servletContext;
292            }
293    
294            @Override
295            public List<SocialActivityInterpreter>
296                    getSocialActivityInterpreterInstances() {
297    
298                    return _socialActivityInterpreterInstances;
299            }
300    
301            @Override
302            public List<SocialRequestInterpreter>
303                    getSocialRequestInterpreterInstances() {
304    
305                    return _socialRequestInterpreterInstances;
306            }
307    
308            @Override
309            public List<StagedModelDataHandler<?>>
310                    getStagedModelDataHandlerInstances() {
311    
312                    return _stagedModelDataHandlerInstances;
313            }
314    
315            @Override
316            public List<TemplateHandler> getTemplateHandlerInstances() {
317                    return _templateHandlerInstances;
318            }
319    
320            @Override
321            public List<TrashHandler> getTrashHandlerInstances() {
322                    return _trashHandlerInstances;
323            }
324    
325            @Override
326            public List<URLEncoder> getURLEncoderInstances() {
327                    return _urlEncoderInstances;
328            }
329    
330            @Override
331            public List<UserNotificationDefinition>
332                    getUserNotificationDefinitionInstances() {
333    
334                    return _userNotificationDefinitionInstances;
335            }
336    
337            @Override
338            public List<UserNotificationHandler> getUserNotificationHandlerInstances() {
339                    return _userNotificationHandlerInstances;
340            }
341    
342            @Override
343            public List<WebDAVStorage> getWebDAVStorageInstances() {
344                    return _webDAVStorageInstances;
345            }
346    
347            @Override
348            public List<WorkflowHandler<?>> getWorkflowHandlerInstances() {
349                    return _workflowHandlerInstances;
350            }
351    
352            @Override
353            public List<Method> getXmlRpcMethodInstances() {
354                    return _xmlRpcMethodInstances;
355            }
356    
357            @Override
358            public void setPortletInstance(Portlet portletInstance) {
359                    _portletInstance = portletInstance;
360            }
361    
362            @Override
363            public void setPortletName(String portletName) {
364                    _portletName = portletName;
365            }
366    
367            protected void close(Object object) {
368                    try {
369                            Closeable closeable = (Closeable)object;
370    
371                            closeable.close();
372                    }
373                    catch (Exception e) {
374                            throw new RuntimeException(
375                                    "Unable to close " + ClassUtil.getClassName(object), e);
376                    }
377            }
378    
379            private final List<AssetRendererFactory<?>> _assetRendererFactoryInstances;
380            private final List<AtomCollectionAdapter<?>>
381                    _atomCollectionAdapterInstances;
382            private final List<ConfigurationAction> _configurationActionInstances;
383            private final List<ControlPanelEntry> _controlPanelEntryInstances;
384            private final List<CustomAttributesDisplay>
385                    _customAttributesDisplayInstances;
386            private final FriendlyURLMapperTracker _friendlyURLMapperTracker;
387            private final List<Indexer<?>> _indexerInstances;
388            private final List<OpenSearch> _openSearchInstances;
389            private final List<PermissionPropagator> _permissionPropagatorInstances;
390            private final List<PollerProcessor> _pollerProcessorInstances;
391            private final List<MessageListener> _popMessageListenerInstances;
392            private final List<PortletDataHandler> _portletDataHandlerInstances;
393            private Portlet _portletInstance;
394            private final List<PortletLayoutListener> _portletLayoutListenerInstances;
395            private String _portletName;
396            private final List<PreferencesValidator> _preferencesValidatorInstances;
397            private final String _resourceBundleBaseName;
398            private final List<SchedulerEventMessageListener>
399                    _schedulerEventMessageListeners;
400            private final ServletContext _servletContext;
401            private final List<SocialActivityInterpreter>
402                    _socialActivityInterpreterInstances;
403            private final List<SocialRequestInterpreter>
404                    _socialRequestInterpreterInstances;
405            private final List<StagedModelDataHandler<?>>
406                    _stagedModelDataHandlerInstances;
407            private final List<TemplateHandler> _templateHandlerInstances;
408            private final List<TrashHandler> _trashHandlerInstances;
409            private final List<URLEncoder> _urlEncoderInstances;
410            private final List<UserNotificationDefinition>
411                    _userNotificationDefinitionInstances;
412            private final List<UserNotificationHandler>
413                    _userNotificationHandlerInstances;
414            private final List<WebDAVStorage> _webDAVStorageInstances;
415            private final List<WorkflowHandler<?>> _workflowHandlerInstances;
416            private final List<Method> _xmlRpcMethodInstances;
417    
418    }