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.asset.validator;
016    
017    import com.liferay.asset.kernel.validator.AssetEntryValidator;
018    import com.liferay.portal.kernel.util.ListUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.registry.collections.ServiceTrackerCollections;
021    import com.liferay.registry.collections.ServiceTrackerMap;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    /**
027     * @author Julio Camarero
028     */
029    public class AssetEntryValidatorRegistry {
030    
031            public void afterPropertiesSet() {
032                    _serviceTrackerMap = ServiceTrackerCollections.openMultiValueMap(
033                            AssetEntryValidator.class, "model.class.name");
034            }
035    
036            public void destroy() {
037                    _serviceTrackerMap.close();
038            }
039    
040            public List<AssetEntryValidator> getAssetEntryValidators(String className) {
041                    List<AssetEntryValidator> assetEntryValidators = new ArrayList<>();
042    
043                    List<AssetEntryValidator> generalAssetEntryValidators =
044                            _serviceTrackerMap.getService("*");
045    
046                    if (!ListUtil.isEmpty(generalAssetEntryValidators)) {
047                            assetEntryValidators.addAll(generalAssetEntryValidators);
048                    }
049    
050                    if (Validator.isNotNull(className)) {
051                            List<AssetEntryValidator> classNameAssetEntryValidators =
052                                    _serviceTrackerMap.getService(className);
053    
054                            if (!ListUtil.isEmpty(classNameAssetEntryValidators)) {
055                                    assetEntryValidators.addAll(classNameAssetEntryValidators);
056                            }
057                    }
058    
059                    return assetEntryValidators;
060            }
061    
062            private ServiceTrackerMap<String, List<AssetEntryValidator>>
063                    _serviceTrackerMap;
064    
065    }