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.kernel.trash;
016    
017    import com.liferay.portal.kernel.util.ListUtil;
018    import com.liferay.registry.Registry;
019    import com.liferay.registry.RegistryUtil;
020    import com.liferay.registry.ServiceReference;
021    import com.liferay.registry.ServiceRegistration;
022    import com.liferay.registry.ServiceTracker;
023    import com.liferay.registry.ServiceTrackerCustomizer;
024    import com.liferay.registry.collections.ServiceRegistrationMap;
025    import com.liferay.registry.collections.ServiceRegistrationMapImpl;
026    
027    import java.util.List;
028    import java.util.Map;
029    import java.util.concurrent.ConcurrentSkipListMap;
030    
031    /**
032     * @author Alexander Chow
033     */
034    public class TrashHandlerRegistryUtil {
035    
036            public static TrashHandler getTrashHandler(String className) {
037                    return _instance._getTrashHandler(className);
038            }
039    
040            public static List<TrashHandler> getTrashHandlers() {
041                    return _instance._getTrashHandlers();
042            }
043    
044            public static void register(List<TrashHandler> trashHandlers) {
045                    for (TrashHandler trashHandler : trashHandlers) {
046                            register(trashHandler);
047                    }
048            }
049    
050            public static void register(TrashHandler trashHandler) {
051                    _instance._register(trashHandler);
052            }
053    
054            public static void unregister(List<TrashHandler> trashHandlers) {
055                    for (TrashHandler trashHandler : trashHandlers) {
056                            unregister(trashHandler);
057                    }
058            }
059    
060            public static void unregister(TrashHandler trashHandler) {
061                    _instance._unregister(trashHandler);
062            }
063    
064            private TrashHandlerRegistryUtil() {
065                    Registry registry = RegistryUtil.getRegistry();
066    
067                    _serviceTracker = registry.trackServices(
068                            TrashHandler.class, new TrashHandlerServiceTrackerCustomizer());
069    
070                    _serviceTracker.open();
071            }
072    
073            private TrashHandler _getTrashHandler(String className) {
074                    return _trashHandlers.get(className);
075            }
076    
077            private List<TrashHandler> _getTrashHandlers() {
078                    return ListUtil.fromMapValues(_trashHandlers);
079            }
080    
081            private void _register(TrashHandler trashHandler) {
082                    Registry registry = RegistryUtil.getRegistry();
083    
084                    ServiceRegistration<TrashHandler> serviceRegistration =
085                            registry.registerService(TrashHandler.class, trashHandler);
086    
087                    _serviceRegistrations.put(trashHandler, serviceRegistration);
088            }
089    
090            private void _unregister(TrashHandler trashHandler) {
091                    ServiceRegistration<TrashHandler> serviceRegistration =
092                            _serviceRegistrations.remove(trashHandler);
093    
094                    if (serviceRegistration != null) {
095                            serviceRegistration.unregister();
096                    }
097            }
098    
099            private static final TrashHandlerRegistryUtil _instance =
100                    new TrashHandlerRegistryUtil();
101    
102            private final ServiceRegistrationMap<TrashHandler> _serviceRegistrations =
103                    new ServiceRegistrationMapImpl<>();
104            private final ServiceTracker<TrashHandler, TrashHandler> _serviceTracker;
105            private final Map<String, TrashHandler> _trashHandlers =
106                    new ConcurrentSkipListMap<>();
107    
108            private class TrashHandlerServiceTrackerCustomizer
109                    implements ServiceTrackerCustomizer<TrashHandler, TrashHandler> {
110    
111                    @Override
112                    public TrashHandler addingService(
113                            ServiceReference<TrashHandler> serviceReference) {
114    
115                            Registry registry = RegistryUtil.getRegistry();
116    
117                            TrashHandler trashHandler = registry.getService(serviceReference);
118    
119                            _trashHandlers.put(trashHandler.getClassName(), trashHandler);
120    
121                            return trashHandler;
122                    }
123    
124                    @Override
125                    public void modifiedService(
126                            ServiceReference<TrashHandler> serviceReference,
127                            TrashHandler trashHandler) {
128                    }
129    
130                    @Override
131                    public void removedService(
132                            ServiceReference<TrashHandler> serviceReference,
133                            TrashHandler trashHandler) {
134    
135                            Registry registry = RegistryUtil.getRegistry();
136    
137                            registry.ungetService(serviceReference);
138    
139                            _trashHandlers.remove(trashHandler.getClassName());
140                    }
141    
142            }
143    
144    }