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;
016    
017    import com.liferay.portal.fabric.agent.FabricAgent;
018    import com.liferay.portal.fabric.agent.FabricAgentRegistry;
019    import com.liferay.portal.fabric.agent.selectors.FabricAgentSelector;
020    import com.liferay.portal.fabric.worker.FabricWorker;
021    import com.liferay.portal.kernel.process.ProcessCallable;
022    import com.liferay.portal.kernel.process.ProcessConfig;
023    import com.liferay.portal.kernel.process.ProcessException;
024    import com.liferay.portal.kernel.process.ProcessExecutor;
025    
026    import java.io.Serializable;
027    
028    import java.util.Collection;
029    import java.util.Iterator;
030    
031    /**
032     * @author Shuyang Zhou
033     */
034    public class FabricProcessExecutor implements ProcessExecutor {
035    
036            public FabricProcessExecutor(
037                    FabricAgentRegistry fabricAgentRegistry,
038                    FabricAgentSelector fabricAgentSelector) {
039    
040                    if (fabricAgentRegistry == null) {
041                            throw new NullPointerException("Fabric agent registry is null");
042                    }
043    
044                    if (fabricAgentSelector == null) {
045                            throw new NullPointerException("Fabric agent selector is null");
046                    }
047    
048                    _fabricAgentRegistry = fabricAgentRegistry;
049                    _fabricAgentSelector = fabricAgentSelector;
050            }
051    
052            @Override
053            public <T extends Serializable> FabricWorker<T> execute(
054                            ProcessConfig processConfig, ProcessCallable<T> processCallable)
055                    throws ProcessException {
056    
057                    FabricAgent fabricAgent = getFabricAgent(processCallable);
058    
059                    return fabricAgent.execute(processConfig, processCallable);
060            }
061    
062            protected FabricAgent getFabricAgent(ProcessCallable<?> processCallable) {
063                    Collection<FabricAgent> fabricAgents = _fabricAgentSelector.select(
064                            _fabricAgentRegistry.getFabricAgents(), processCallable);
065    
066                    if (fabricAgents.isEmpty()) {
067                            return _fabricAgentRegistry.getDefaultFabricAgent();
068                    }
069    
070                    Iterator<FabricAgent> iterator = fabricAgents.iterator();
071    
072                    return iterator.next();
073            }
074    
075            private final FabricAgentRegistry _fabricAgentRegistry;
076            private final FabricAgentSelector _fabricAgentSelector;
077    
078    }