All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.portlet.internal.PortletBagImpl Maven / Gradle / Ivy

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portlet.internal;

import com.liferay.asset.kernel.model.AssetRendererFactory;
import com.liferay.expando.kernel.model.CustomAttributesDisplay;
import com.liferay.exportimport.kernel.lar.PortletDataHandler;
import com.liferay.exportimport.kernel.lar.StagedModelDataHandler;
import com.liferay.osgi.service.tracker.collections.list.ServiceTrackerList;
import com.liferay.osgi.service.tracker.collections.list.ServiceTrackerListFactory;
import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.module.util.SystemBundleUtil;
import com.liferay.portal.kernel.notifications.UserNotificationDefinition;
import com.liferay.portal.kernel.notifications.UserNotificationHandler;
import com.liferay.portal.kernel.pop.MessageListener;
import com.liferay.portal.kernel.portlet.ConfigurationAction;
import com.liferay.portal.kernel.portlet.ControlPanelEntry;
import com.liferay.portal.kernel.portlet.FriendlyURLMapperTracker;
import com.liferay.portal.kernel.portlet.PortletBag;
import com.liferay.portal.kernel.portlet.PortletConfigurationListener;
import com.liferay.portal.kernel.portlet.PortletLayoutListener;
import com.liferay.portal.kernel.resource.bundle.ResourceBundleLoader;
import com.liferay.portal.kernel.search.Indexer;
import com.liferay.portal.kernel.search.OpenSearch;
import com.liferay.portal.kernel.security.permission.propagator.PermissionPropagator;
import com.liferay.portal.kernel.servlet.URLEncoder;
import com.liferay.portal.kernel.template.TemplateHandler;
import com.liferay.portal.kernel.trash.TrashHandler;
import com.liferay.portal.kernel.util.HashMapDictionary;
import com.liferay.portal.kernel.util.ServiceProxyFactory;
import com.liferay.portal.kernel.webdav.WebDAVStorage;
import com.liferay.portal.kernel.workflow.WorkflowHandler;
import com.liferay.portal.kernel.xmlrpc.Method;
import com.liferay.portal.language.LanguageResources;
import com.liferay.social.kernel.model.SocialActivityInterpreter;
import com.liferay.social.kernel.model.SocialRequestInterpreter;

import java.util.Collections;
import java.util.Dictionary;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.portlet.Portlet;
import javax.portlet.PreferencesValidator;

import javax.servlet.ServletContext;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/**
 * @author Brian Wing Shun Chan
 * @author Jorge Ferrer
 */
public class PortletBagImpl implements PortletBag {

	public PortletBagImpl(
		String portletName, ServletContext servletContext,
		Portlet portletInstance, String resourceBundleBaseName,
		FriendlyURLMapperTracker friendlyURLMapperTracker,
		List> serviceRegistrations) {

		_portletName = portletName;
		_servletContext = servletContext;
		_portletInstance = portletInstance;
		_resourceBundleBaseName = resourceBundleBaseName;
		_friendlyURLMapperTracker = friendlyURLMapperTracker;
		_serviceRegistrations = serviceRegistrations;

		_filterString =
			"(|(javax.portlet.name=" + portletName +
				")(javax.portlet.name=ALL))";
	}

	@Override
	public Object clone() {
		return new PortletBagImpl(
			getPortletName(), getServletContext(), getPortletInstance(),
			getResourceBundleBaseName(), getFriendlyURLMapperTracker(), null);
	}

	@Override
	public void destroy() {
		if (_serviceRegistrations == null) {
			return;
		}

		_friendlyURLMapperTracker.close();

		for (ServiceRegistration serviceRegistration :
				_serviceRegistrations) {

			serviceRegistration.unregister();
		}

		_serviceRegistrations.clear();
	}

	@Override
	public List> getAssetRendererFactoryInstances() {
		if (_assetRendererFactoryInstances == null) {
			synchronized (this) {
				if (_assetRendererFactoryInstances == null) {
					_assetRendererFactoryInstances =
						ServiceTrackerListFactory.open(
							_bundleContext,
							(Class>)
								(Class)AssetRendererFactory.class,
							_filterString);
				}
			}
		}

		return _toList(_assetRendererFactoryInstances);
	}

	@Override
	public List getConfigurationActionInstances() {
		if (_configurationActionInstances == null) {
			synchronized (this) {
				if (_configurationActionInstances == null) {
					_configurationActionInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, ConfigurationAction.class,
							_filterString);
				}
			}
		}

		return _toList(_configurationActionInstances);
	}

	@Override
	public List getControlPanelEntryInstances() {
		if (_controlPanelEntryInstances == null) {
			synchronized (this) {
				if (_controlPanelEntryInstances == null) {
					_controlPanelEntryInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, ControlPanelEntry.class,
							_filterString);
				}
			}
		}

		return _toList(_controlPanelEntryInstances);
	}

	@Override
	public List getCustomAttributesDisplayInstances() {
		if (_customAttributesDisplayInstances == null) {
			synchronized (this) {
				if (_customAttributesDisplayInstances == null) {
					_customAttributesDisplayInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, CustomAttributesDisplay.class,
							_filterString);
				}
			}
		}

		return _toList(_customAttributesDisplayInstances);
	}

	@Override
	public FriendlyURLMapperTracker getFriendlyURLMapperTracker() {
		return _friendlyURLMapperTracker;
	}

	@Override
	public List> getIndexerInstances() {
		if (_indexerInstances == null) {
			synchronized (this) {
				if (_indexerInstances == null) {
					_indexerInstances = ServiceTrackerListFactory.open(
						_bundleContext,
						(Class>)(Class)Indexer.class,
						_filterString);
				}
			}
		}

		return _toList(_indexerInstances);
	}

	@Override
	public List getOpenSearchInstances() {
		if (_openSearchInstances == null) {
			synchronized (this) {
				if (_openSearchInstances == null) {
					_openSearchInstances = ServiceTrackerListFactory.open(
						_bundleContext, OpenSearch.class, _filterString);
				}
			}
		}

		return _toList(_openSearchInstances);
	}

	@Override
	public List getPermissionPropagatorInstances() {
		if (_permissionPropagatorInstances == null) {
			synchronized (this) {
				if (_permissionPropagatorInstances == null) {
					_permissionPropagatorInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, PermissionPropagator.class,
							_filterString);
				}
			}
		}

		return _toList(_permissionPropagatorInstances);
	}

	@Override
	public List getPopMessageListenerInstances() {
		if (_popMessageListenerInstances == null) {
			synchronized (this) {
				if (_popMessageListenerInstances == null) {
					_popMessageListenerInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, MessageListener.class,
							_filterString);
				}
			}
		}

		return _toList(_popMessageListenerInstances);
	}

	@Override
	public List
		getPortletConfigurationListenerInstances() {

		if (_portletConfigurationListenerInstances == null) {
			synchronized (this) {
				if (_portletConfigurationListenerInstances == null) {
					_portletConfigurationListenerInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, PortletConfigurationListener.class,
							_filterString);
				}
			}
		}

		return _toList(_portletConfigurationListenerInstances);
	}

	@Override
	public List getPortletDataHandlerInstances() {
		if (_portletDataHandlerInstances == null) {
			synchronized (this) {
				if (_portletDataHandlerInstances == null) {
					_portletDataHandlerInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, PortletDataHandler.class,
							_filterString);
				}
			}
		}

		return _toList(_portletDataHandlerInstances);
	}

	@Override
	public Portlet getPortletInstance() {
		return _portletInstance;
	}

	@Override
	public List getPortletLayoutListenerInstances() {
		if (_portletLayoutListenerInstances == null) {
			synchronized (this) {
				if (_portletLayoutListenerInstances == null) {
					_portletLayoutListenerInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, PortletLayoutListener.class,
							_filterString);
				}
			}
		}

		return _toList(_portletLayoutListenerInstances);
	}

	@Override
	public String getPortletName() {
		return _portletName;
	}

	@Override
	public List getPreferencesValidatorInstances() {
		if (_preferencesValidatorInstances == null) {
			synchronized (this) {
				if (_preferencesValidatorInstances == null) {
					_preferencesValidatorInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, PreferencesValidator.class,
							_filterString);
				}
			}
		}

		return _toList(_preferencesValidatorInstances);
	}

	@Override
	public ResourceBundle getResourceBundle(Locale locale) {
		ResourceBundleLoader resourceBundleLoader = _resourceBundleLoader;

		if (resourceBundleLoader == null) {
			synchronized (this) {
				if (_resourceBundleLoader == null) {
					_resourceBundleLoader =
						ServiceProxyFactory.newServiceTrackedInstance(
							ResourceBundleLoader.class, PortletBagImpl.class,
							this, "_resourceBundleLoader",
							StringBundler.concat(
								"(resource.bundle.base.name=",
								getResourceBundleBaseName(),
								")(servlet.context.name=",
								_servletContext.getServletContextName(), ")"),
							false);
				}

				resourceBundleLoader = _resourceBundleLoader;
			}
		}

		ResourceBundle resourceBundle = resourceBundleLoader.loadResourceBundle(
			locale);

		if (resourceBundle == null) {
			resourceBundle = LanguageResources.getResourceBundle(locale);
		}

		return resourceBundle;
	}

	@Override
	public String getResourceBundleBaseName() {
		return _resourceBundleBaseName;
	}

	@Override
	public ServletContext getServletContext() {
		return _servletContext;
	}

	@Override
	public List
		getSocialActivityInterpreterInstances() {

		if (_socialActivityInterpreterInstances == null) {
			synchronized (this) {
				if (_socialActivityInterpreterInstances == null) {
					_socialActivityInterpreterInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, SocialActivityInterpreter.class,
							_filterString);
				}
			}
		}

		return _toList(_socialActivityInterpreterInstances);
	}

	@Override
	public List
		getSocialRequestInterpreterInstances() {

		if (_socialRequestInterpreterInstances == null) {
			synchronized (this) {
				if (_socialRequestInterpreterInstances == null) {
					_socialRequestInterpreterInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, SocialRequestInterpreter.class,
							_filterString);
				}
			}
		}

		return _toList(_socialRequestInterpreterInstances);
	}

	@Override
	public List>
		getStagedModelDataHandlerInstances() {

		if (_stagedModelDataHandlerInstances == null) {
			synchronized (this) {
				if (_stagedModelDataHandlerInstances == null) {
					_stagedModelDataHandlerInstances =
						ServiceTrackerListFactory.open(
							_bundleContext,
							(Class>)
								(Class)StagedModelDataHandler.class,
							_filterString);
				}
			}
		}

		return _toList(_stagedModelDataHandlerInstances);
	}

	@Override
	public List getTemplateHandlerInstances() {
		if (_templateHandlerInstances == null) {
			synchronized (this) {
				if (_templateHandlerInstances == null) {
					_templateHandlerInstances = ServiceTrackerListFactory.open(
						_bundleContext, TemplateHandler.class, _filterString);
				}
			}
		}

		return _toList(_templateHandlerInstances);
	}

	@Override
	public List getTrashHandlerInstances() {
		if (_trashHandlerInstances == null) {
			synchronized (this) {
				if (_trashHandlerInstances == null) {
					_trashHandlerInstances = ServiceTrackerListFactory.open(
						_bundleContext, TrashHandler.class, _filterString);
				}
			}
		}

		return _toList(_trashHandlerInstances);
	}

	@Override
	public List getURLEncoderInstances() {
		if (_urlEncoderInstances == null) {
			synchronized (this) {
				if (_urlEncoderInstances == null) {
					_urlEncoderInstances = ServiceTrackerListFactory.open(
						_bundleContext, URLEncoder.class, _filterString);
				}
			}
		}

		return _toList(_urlEncoderInstances);
	}

	@Override
	public List
		getUserNotificationDefinitionInstances() {

		if (_userNotificationDefinitionInstances == null) {
			synchronized (this) {
				if (_userNotificationDefinitionInstances == null) {
					_userNotificationDefinitionInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, UserNotificationDefinition.class,
							_filterString);
				}
			}
		}

		return _toList(_userNotificationDefinitionInstances);
	}

	@Override
	public List getUserNotificationHandlerInstances() {
		if (_userNotificationHandlerInstances == null) {
			synchronized (this) {
				if (_userNotificationHandlerInstances == null) {
					_userNotificationHandlerInstances =
						ServiceTrackerListFactory.open(
							_bundleContext, UserNotificationHandler.class,
							_filterString);
				}
			}
		}

		return _toList(_userNotificationHandlerInstances);
	}

	@Override
	public List getWebDAVStorageInstances() {
		if (_webDAVStorageInstances == null) {
			synchronized (this) {
				if (_webDAVStorageInstances == null) {
					_webDAVStorageInstances = ServiceTrackerListFactory.open(
						_bundleContext, WebDAVStorage.class, _filterString);
				}
			}
		}

		return _toList(_webDAVStorageInstances);
	}

	@Override
	public List> getWorkflowHandlerInstances() {
		if (_workflowHandlerInstances == null) {
			synchronized (this) {
				if (_workflowHandlerInstances == null) {
					_workflowHandlerInstances = ServiceTrackerListFactory.open(
						_bundleContext,
						(Class>)
							(Class)WorkflowHandler.class,
						_filterString);
				}
			}
		}

		return _toList(_workflowHandlerInstances);
	}

	@Override
	public List getXmlRpcMethodInstances() {
		if (_xmlRpcMethodInstances == null) {
			synchronized (this) {
				if (_xmlRpcMethodInstances == null) {
					_xmlRpcMethodInstances = ServiceTrackerListFactory.open(
						_bundleContext, Method.class, _filterString);
				}
			}
		}

		return _toList(_xmlRpcMethodInstances);
	}

	@Override
	public void setPortletInstance(Portlet portletInstance) {
		_portletInstance = portletInstance;
	}

	@Override
	public void setPortletName(String portletName) {
		_portletName = portletName;
	}

	private  List _toList(ServiceTrackerList serviceTrackerList) {
		return Collections.unmodifiableList(serviceTrackerList.toList());
	}

	private static final BundleContext _bundleContext =
		SystemBundleUtil.getBundleContext();

	private volatile ServiceTrackerList>
		_assetRendererFactoryInstances;
	private volatile ServiceTrackerList
		_configurationActionInstances;
	private volatile ServiceTrackerList
		_controlPanelEntryInstances;
	private volatile ServiceTrackerList
		_customAttributesDisplayInstances;
	private final String _filterString;
	private final FriendlyURLMapperTracker _friendlyURLMapperTracker;
	private volatile ServiceTrackerList> _indexerInstances;
	private volatile ServiceTrackerList _openSearchInstances;
	private volatile ServiceTrackerList
		_permissionPropagatorInstances;
	private volatile ServiceTrackerList
		_popMessageListenerInstances;
	private volatile ServiceTrackerList
		_portletConfigurationListenerInstances;
	private volatile ServiceTrackerList
		_portletDataHandlerInstances;
	private Portlet _portletInstance;
	private volatile ServiceTrackerList
		_portletLayoutListenerInstances;
	private String _portletName;
	private volatile ServiceTrackerList
		_preferencesValidatorInstances;
	private final String _resourceBundleBaseName;
	private volatile ResourceBundleLoader _resourceBundleLoader;
	private final List> _serviceRegistrations;
	private final ServletContext _servletContext;
	private volatile ServiceTrackerList
		_socialActivityInterpreterInstances;
	private volatile ServiceTrackerList
		_socialRequestInterpreterInstances;
	private volatile ServiceTrackerList>
		_stagedModelDataHandlerInstances;
	private volatile ServiceTrackerList
		_templateHandlerInstances;
	private volatile ServiceTrackerList _trashHandlerInstances;
	private volatile ServiceTrackerList _urlEncoderInstances;
	private volatile ServiceTrackerList
		_userNotificationDefinitionInstances;
	private volatile ServiceTrackerList
		_userNotificationHandlerInstances;
	private volatile ServiceTrackerList _webDAVStorageInstances;
	private volatile ServiceTrackerList>
		_workflowHandlerInstances;
	private volatile ServiceTrackerList _xmlRpcMethodInstances;

	@SuppressWarnings("deprecation")
	private static class PermissionPropagatorServiceTrackerCustomizer
		implements ServiceTrackerCustomizer
			> {

		@Override
		public ServiceRegistration addingService(
			ServiceReference
				 serviceReference) {

			return _bundleContext.registerService(
				PermissionPropagator.class,
				_bundleContext.getService(serviceReference),
				_toProperties(serviceReference));
		}

		@Override
		public void modifiedService(
			ServiceReference
				 serviceReference,
			ServiceRegistration serviceRegistration) {

			serviceRegistration.setProperties(_toProperties(serviceReference));
		}

		@Override
		public void removedService(
			ServiceReference
				 serviceReference,
			ServiceRegistration serviceRegistration) {

			serviceRegistration.unregister();

			_bundleContext.ungetService(serviceReference);
		}

		private Dictionary _toProperties(
			ServiceReference serviceReference) {

			Dictionary properties = new HashMapDictionary<>();

			for (String key : serviceReference.getPropertyKeys()) {
				Object value = serviceReference.getProperty(key);

				properties.put(key, value);
			}

			return properties;
		}

	}

	static {
		ServiceTracker serviceTracker = new ServiceTracker<>(
			_bundleContext,
			com.liferay.portal.kernel.security.permission.PermissionPropagator.
				class,
			new PermissionPropagatorServiceTrackerCustomizer());

		serviceTracker.open();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy