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