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 LayoutTypeAccessPolicy _getLayoutTypeAccessPolicy(String type) {
043                    LayoutTypeAccessPolicy layoutTypeAccessPolicy =
044                            _serviceTrackerMap.getService(type);
045    
046                    if (layoutTypeAccessPolicy == null) {
047                            return DefaultLayoutTypeAccessPolicyImpl.create();
048                    }
049    
050                    return layoutTypeAccessPolicy;
051            }
052    
053            private static final LayoutTypeAccessPolicyTracker _instance =
054                    new LayoutTypeAccessPolicyTracker();
055    
056            private final ServiceTrackerMap<String, LayoutTypeAccessPolicy>
057                    _serviceTrackerMap = ServiceTrackerCollections.openSingleValueMap(
058                            LayoutTypeAccessPolicy.class,
059                            "(&(layout.type=*)(objectClass=" +
060                                    LayoutTypeAccessPolicy.class.getName() + "))",
061                            new ServiceReferenceMapper<String, LayoutTypeAccessPolicy>() {
062    
063                                    @Override
064                                    public void map(
065                                            ServiceReference<LayoutTypeAccessPolicy> serviceReference,
066                                            Emitter<String> emitter) {
067    
068                                            String layoutType = (String)serviceReference.getProperty(
069                                                    "layout.type");
070    
071                                            emitter.emit(layoutType);
072                                    }
073    
074                            });
075    
076    }