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.selectors;
016    
017    import com.liferay.portal.fabric.agent.FabricAgent;
018    import com.liferay.portal.fabric.status.FabricStatus;
019    import com.liferay.portal.kernel.process.ProcessCallable;
020    
021    import java.lang.management.RuntimeMXBean;
022    
023    import java.util.Collection;
024    import java.util.Iterator;
025    import java.util.Map;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public abstract class SystemPropertiesFilterFabricAgentSelector
031            implements FabricAgentSelector {
032    
033            @Override
034            public Collection<FabricAgent> select(
035                    Collection<FabricAgent> fabricAgents,
036                    ProcessCallable<?> processCallable) {
037    
038                    Iterator<FabricAgent> iterator = fabricAgents.iterator();
039    
040                    while (iterator.hasNext()) {
041                            FabricAgent fabricAgent = iterator.next();
042    
043                            FabricStatus fabricStatus = fabricAgent.getFabricStatus();
044    
045                            RuntimeMXBean runtimeMXBean = fabricStatus.getRuntimeMXBean();
046    
047                            if (!accept(runtimeMXBean.getSystemProperties(), processCallable)) {
048                                    iterator.remove();
049                            }
050                    }
051    
052                    return fabricAgents;
053            }
054    
055            protected abstract boolean accept(
056                    Map<String, String> systemProperties,
057                    ProcessCallable<?> processCallable);
058    
059    }