com.liferay.asset.kernel.AssetRendererFactoryRegistryUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.portal.kernel Show documentation
Show all versions of com.liferay.portal.kernel Show documentation
Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.
/**
* 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.asset.kernel;
import aQute.bnd.annotation.ProviderType;
import com.liferay.asset.kernel.model.AssetRendererFactory;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.registry.Registry;
import com.liferay.registry.RegistryUtil;
import com.liferay.registry.ServiceReference;
import com.liferay.registry.ServiceRegistration;
import com.liferay.registry.ServiceTracker;
import com.liferay.registry.ServiceTrackerCustomizer;
import com.liferay.registry.collections.ServiceRegistrationMap;
import com.liferay.registry.collections.ServiceRegistrationMapImpl;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Bruno Farache
* @author Marcellus Tavares
*/
@ProviderType
public class AssetRendererFactoryRegistryUtil {
public static List> getAssetRendererFactories(
long companyId) {
return _instance._getAssetRendererFactories(companyId);
}
public static AssetRendererFactory getAssetRendererFactoryByClass(
Class clazz) {
return _instance._getAssetRendererFactoryByClass(clazz);
}
public static AssetRendererFactory> getAssetRendererFactoryByClassName(
String className) {
return _instance._getAssetRendererFactoryByClassName(className);
}
public static AssetRendererFactory> getAssetRendererFactoryByClassNameId(
long classNameId) {
return _instance._getAssetRendererFactoryByClassNameId(classNameId);
}
public static AssetRendererFactory> getAssetRendererFactoryByType(
String type) {
return _instance._getAssetRendererFactoryByType(type);
}
public static long[] getClassNameIds(long companyId) {
return _instance._getClassNameIds(companyId, false);
}
public static long[] getClassNameIds(
long companyId, boolean filterSelectable) {
return _instance._getClassNameIds(companyId, filterSelectable);
}
public static void register(AssetRendererFactory> assetRendererFactory) {
_instance._register(assetRendererFactory);
}
public static void register(
List> assetRendererFactories) {
for (AssetRendererFactory> assetRendererFactory :
assetRendererFactories) {
register(assetRendererFactory);
}
}
public static void unregister(
AssetRendererFactory> assetRendererFactory) {
_instance._unregister(assetRendererFactory);
}
public static void unregister(
List> assetRendererFactories) {
for (AssetRendererFactory> assetRendererFactory :
assetRendererFactories) {
unregister(assetRendererFactory);
}
}
private AssetRendererFactoryRegistryUtil() {
Registry registry = RegistryUtil.getRegistry();
_serviceTracker = registry.trackServices(
(Class>)(Class>)
AssetRendererFactory.class,
new AssetRendererFactoryServiceTrackerCustomizer());
_serviceTracker.open();
}
private Map> _filterAssetRendererFactories(
long companyId,
Map> assetRendererFactories,
boolean filterSelectable) {
Map> filteredAssetRendererFactories =
new ConcurrentHashMap<>();
for (String className : assetRendererFactories.keySet()) {
AssetRendererFactory> assetRendererFactory =
assetRendererFactories.get(className);
if (assetRendererFactory.isActive(companyId) &&
(!filterSelectable || assetRendererFactory.isSelectable())) {
filteredAssetRendererFactories.put(
className, assetRendererFactory);
}
}
return filteredAssetRendererFactories;
}
private List> _getAssetRendererFactories(
long companyId) {
return ListUtil.fromMapValues(
_filterAssetRendererFactories(
companyId, _assetRenderFactoriesMapByClassName, false));
}
private AssetRendererFactory _getAssetRendererFactoryByClass(
Class clazz) {
return (AssetRendererFactory)_assetRenderFactoriesMapByClassName.get(
clazz.getName());
}
private AssetRendererFactory> _getAssetRendererFactoryByClassName(
String className) {
return _assetRenderFactoriesMapByClassName.get(className);
}
private AssetRendererFactory> _getAssetRendererFactoryByClassNameId(
long classNameId) {
return _getAssetRendererFactoryByClassName(
PortalUtil.getClassName(classNameId));
}
private AssetRendererFactory> _getAssetRendererFactoryByType(
String type) {
return _assetRenderFactoriesMapByClassType.get(type);
}
private long[] _getClassNameIds(long companyId, boolean filterSelectable) {
Map> assetRenderFactories =
_assetRenderFactoriesMapByClassName;
if (companyId > 0) {
assetRenderFactories = _filterAssetRendererFactories(
companyId, _assetRenderFactoriesMapByClassName,
filterSelectable);
}
long[] classNameIds = new long[assetRenderFactories.size()];
int i = 0;
for (AssetRendererFactory> assetRendererFactory :
assetRenderFactories.values()) {
classNameIds[i] = assetRendererFactory.getClassNameId();
i++;
}
return classNameIds;
}
private void _register(AssetRendererFactory> assetRendererFactory) {
Registry registry = RegistryUtil.getRegistry();
ServiceRegistration> serviceRegistration =
registry.registerService(
(Class>)(Class>)
AssetRendererFactory.class, assetRendererFactory);
_serviceRegistrations.put(assetRendererFactory, serviceRegistration);
}
private void _unregister(AssetRendererFactory> assetRendererFactory) {
ServiceRegistration> serviceRegistration =
_serviceRegistrations.remove(assetRendererFactory);
if (serviceRegistration != null) {
serviceRegistration.unregister();
}
}
private static final Log _log = LogFactoryUtil.getLog(
AssetRendererFactoryRegistryUtil.class);
private static final AssetRendererFactoryRegistryUtil _instance =
new AssetRendererFactoryRegistryUtil();
private final Map>
_assetRenderFactoriesMapByClassName = new ConcurrentHashMap<>();
private final Map>
_assetRenderFactoriesMapByClassType = new ConcurrentHashMap<>();
private final ServiceRegistrationMap>
_serviceRegistrations = new ServiceRegistrationMapImpl<>();
private final
ServiceTracker, AssetRendererFactory>>
_serviceTracker;
private class AssetRendererFactoryServiceTrackerCustomizer
implements ServiceTrackerCustomizer
, AssetRendererFactory>> {
@Override
public AssetRendererFactory> addingService(
ServiceReference> serviceReference) {
Registry registry = RegistryUtil.getRegistry();
AssetRendererFactory> assetRendererFactory = registry.getService(
serviceReference);
String className = assetRendererFactory.getClassName();
AssetRendererFactory> classNameAssetRendererFactory =
_assetRenderFactoriesMapByClassName.put(
className, assetRendererFactory);
if (_log.isWarnEnabled() &&
(classNameAssetRendererFactory != null)) {
_log.warn(
"Replacing " + classNameAssetRendererFactory +
" for class name " + className + " with " +
assetRendererFactory);
}
String type = assetRendererFactory.getType();
AssetRendererFactory> typeAssetRendererFactory =
_assetRenderFactoriesMapByClassType.put(
type, assetRendererFactory);
if (_log.isWarnEnabled() && (typeAssetRendererFactory != null)) {
_log.warn(
"Replacing " + typeAssetRendererFactory + " for type " +
type + " with " + assetRendererFactory);
}
return assetRendererFactory;
}
@Override
public void modifiedService(
ServiceReference> serviceReference,
AssetRendererFactory> assetRendererFactory) {
}
@Override
public void removedService(
ServiceReference> serviceReference,
AssetRendererFactory> assetRendererFactory) {
Registry registry = RegistryUtil.getRegistry();
registry.ungetService(serviceReference);
_assetRenderFactoriesMapByClassName.remove(
assetRendererFactory.getClassName());
_assetRenderFactoriesMapByClassType.remove(
assetRendererFactory.getType());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy