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.struts;
016    
017    import com.liferay.portal.kernel.concurrent.ConcurrentHashSet;
018    import com.liferay.portal.kernel.struts.path.AuthPublicPath;
019    import com.liferay.portal.kernel.struts.path.DefaultAuthPublicPath;
020    import com.liferay.registry.Registry;
021    import com.liferay.registry.RegistryUtil;
022    import com.liferay.registry.ServiceReference;
023    import com.liferay.registry.ServiceRegistration;
024    import com.liferay.registry.ServiceTracker;
025    import com.liferay.registry.ServiceTrackerCustomizer;
026    import com.liferay.registry.collections.StringServiceRegistrationMap;
027    
028    import java.util.Set;
029    
030    /**
031     * @author Mika Koivisto
032     * @author Raymond Aug??
033     */
034    public class AuthPublicPathRegistry {
035    
036            public static boolean contains(String path) {
037                    return _instance._contains(path);
038            }
039    
040            public static void register(String... paths) {
041                    _instance._register(paths);
042            }
043    
044            public static void unregister(String... paths) {
045                    _instance._unregister(paths);
046            }
047    
048            private AuthPublicPathRegistry() {
049                    Registry registry = RegistryUtil.getRegistry();
050    
051                    _serviceTracker = registry.trackServices(
052                            AuthPublicPath.class, new AuthPublicTrackerCustomizer());
053    
054                    _serviceTracker.open();
055            }
056    
057            private boolean _contains(String path) {
058                    return _paths.contains(path);
059            }
060    
061            private void _register(String... paths) {
062                    Registry registry = RegistryUtil.getRegistry();
063    
064                    for (String path : paths) {
065                            ServiceRegistration<AuthPublicPath> serviceRegistration =
066                                    registry.registerService(
067                                            AuthPublicPath.class, new DefaultAuthPublicPath(path));
068    
069                            _serviceRegistrations.put(path, serviceRegistration);
070                    }
071            }
072    
073            private void _unregister(String... paths) {
074                    for (String path : paths) {
075                            ServiceRegistration<AuthPublicPath> serviceRegistration =
076                                    _serviceRegistrations.remove(path);
077    
078                            if (serviceRegistration != null) {
079                                    serviceRegistration.unregister();
080                            }
081                    }
082            }
083    
084            private static final AuthPublicPathRegistry _instance =
085                    new AuthPublicPathRegistry();
086    
087            private final Set<String> _paths = new ConcurrentHashSet<>();
088            private final StringServiceRegistrationMap<AuthPublicPath>
089                    _serviceRegistrations = new StringServiceRegistrationMap<>();
090            private final ServiceTracker<AuthPublicPath, AuthPublicPath>
091                    _serviceTracker;
092    
093            private class AuthPublicTrackerCustomizer
094                    implements ServiceTrackerCustomizer<AuthPublicPath, AuthPublicPath> {
095    
096                    @Override
097                    public AuthPublicPath addingService(
098                            ServiceReference<AuthPublicPath> serviceReference) {
099    
100                            Registry registry = RegistryUtil.getRegistry();
101    
102                            AuthPublicPath authPublicPath = registry.getService(
103                                    serviceReference);
104    
105                            _paths.add(authPublicPath.path());
106    
107                            return authPublicPath;
108                    }
109    
110                    @Override
111                    public void modifiedService(
112                            ServiceReference<AuthPublicPath> serviceReference,
113                            AuthPublicPath authPublicPath) {
114                    }
115    
116                    @Override
117                    public void removedService(
118                            ServiceReference<AuthPublicPath> serviceReference,
119                            AuthPublicPath authPublicPath) {
120    
121                            Registry registry = RegistryUtil.getRegistry();
122    
123                            registry.ungetService(serviceReference);
124    
125                            _paths.remove(authPublicPath.path());
126                    }
127    
128            }
129    
130    }