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.fabric.agent;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    import java.util.concurrent.CopyOnWriteArrayList;
020    
021    /**
022     * @author Shuyang Zhou
023     */
024    public class FabricAgentRegistry {
025    
026            public FabricAgentRegistry(FabricAgent defaultFabricAgent) {
027                    if (defaultFabricAgent == null) {
028                            throw new NullPointerException("Default fabric agent is null");
029                    }
030    
031                    _defaultFabricAgent = defaultFabricAgent;
032            }
033    
034            public FabricAgent getDefaultFabricAgent() {
035                    return _defaultFabricAgent;
036            }
037    
038            public List<FabricAgentListener> getFabricAgentListeners() {
039                    return new ArrayList<>(_fabricAgentListeners);
040            }
041    
042            public List<FabricAgent> getFabricAgents() {
043                    return new ArrayList<>(_fabricAgents);
044            }
045    
046            public boolean registerFabricAgent(
047                    FabricAgent fabricAgent, Runnable runnable) {
048    
049                    if (_fabricAgents.addIfAbsent(fabricAgent)) {
050                            if (runnable != null) {
051                                    runnable.run();
052                            }
053    
054                            for (FabricAgentListener fabricAgentListener :
055                                            _fabricAgentListeners) {
056    
057                                    fabricAgentListener.registered(fabricAgent);
058                            }
059    
060                            return true;
061                    }
062    
063                    return false;
064            }
065    
066            public boolean registerFabricAgentListener(
067                    FabricAgentListener fabricAgentListener) {
068    
069                    return _fabricAgentListeners.addIfAbsent(fabricAgentListener);
070            }
071    
072            public boolean unregisterFabricAgent(
073                    FabricAgent fabricAgent, Runnable runnable) {
074    
075                    if (_fabricAgents.remove(fabricAgent)) {
076                            if (runnable != null) {
077                                    runnable.run();
078                            }
079    
080                            for (FabricAgentListener fabricAgentListener :
081                                            _fabricAgentListeners) {
082    
083                                    fabricAgentListener.unregistered(fabricAgent);
084                            }
085    
086                            return true;
087                    }
088    
089                    return false;
090            }
091    
092            public boolean unregisterFabricAgentListener(
093                    FabricAgentListener fabricAgentListener) {
094    
095                    return _fabricAgentListeners.remove(fabricAgentListener);
096            }
097    
098            private final FabricAgent _defaultFabricAgent;
099            private final CopyOnWriteArrayList<FabricAgentListener>
100                    _fabricAgentListeners = new CopyOnWriteArrayList<>();
101            private final CopyOnWriteArrayList<FabricAgent> _fabricAgents =
102                    new CopyOnWriteArrayList<>();
103    
104    }