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.portal.util;
016    
017    import com.liferay.portal.model.Layout;
018    import com.liferay.portal.model.LayoutTypeAccessPolicy;
019    import com.liferay.portal.model.impl.DefaultLayoutTypeAccessPolicyImpl;
020    import com.liferay.registry.ServiceReference;
021    import com.liferay.registry.collections.ServiceReferenceMapper;
022    import com.liferay.registry.collections.ServiceTrackerCollections;
023    import com.liferay.registry.collections.ServiceTrackerMap;
024    
025    /**
026     * @author Adolfo P??rez
027     */
028    public class LayoutTypeAccessPolicyTracker {
029    
030            public static LayoutTypeAccessPolicy getLayoutTypeAccessPolicy(
031                    Layout layout) {
032    
033                    return getLayoutTypeAccessPolicy(layout.getType());
034            }
035    
036            public static LayoutTypeAccessPolicy getLayoutTypeAccessPolicy(
037                    String type) {
038    
039                    return _instance._getLayoutTypeAccessPolicy(type);
040            }
041    
042            private LayoutTypeAccessPolicyTracker() {
043                    _serviceTrackerMap.open();
044            }
045    
046            private LayoutTypeAccessPolicy _getLayoutTypeAccessPolicy(String type) {
047                    LayoutTypeAccessPolicy layoutTypeAccessPolicy =
048                            _serviceTrackerMap.getService(type);
049    
050                    if (layoutTypeAccessPolicy == null) {
051                            return DefaultLayoutTypeAccessPolicyImpl.create();
052                    }
053    
054                    return layoutTypeAccessPolicy;
055            }
056    
057            private static final LayoutTypeAccessPolicyTracker _instance =
058                    new LayoutTypeAccessPolicyTracker();
059    
060            private final ServiceTrackerMap<String, LayoutTypeAccessPolicy>
061                    _serviceTrackerMap = ServiceTrackerCollections.singleValueMap(
062                            LayoutTypeAccessPolicy.class,
063                            "(&(layout.type=*)(objectClass=" +
064                                    LayoutTypeAccessPolicy.class.getName() + "))",
065                            new ServiceReferenceMapper<String, LayoutTypeAccessPolicy>() {
066    
067                                    @Override
068                                    public void map(
069                                            ServiceReference<LayoutTypeAccessPolicy> serviceReference,
070                                            Emitter<String> emitter) {
071    
072                                            String layoutType = (String)serviceReference.getProperty(
073                                                    "layout.type");
074    
075                                            emitter.emit(layoutType);
076                                    }
077    
078                            });
079    
080    }