com.qwazr.scripts.ScriptMultiClient Maven / Gradle / Ivy
Show all versions of qwazr-scripts Show documentation
/*
* Copyright 2015-2017 Emmanuel Keller / QWAZR
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qwazr.scripts;
import com.qwazr.server.RemoteService;
import com.qwazr.server.client.MultiClient;
import com.qwazr.server.client.MultiWebApplicationException;
import com.qwazr.utils.LoggerUtils;
import com.qwazr.utils.concurrent.FunctionEx;
import javax.ws.rs.NotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger;
public class ScriptMultiClient extends MultiClient implements ScriptServiceInterface {
private static final Logger LOGGER = LoggerUtils.getLogger(ScriptMultiClient.class);
public ScriptMultiClient(ExecutorService executorService, RemoteService... remotes) {
super(getClients(remotes), executorService);
}
private static ScriptSingleClient[] getClients(final RemoteService... remotes) {
if (remotes == null)
return null;
final ScriptSingleClient[] clients = new ScriptSingleClient[remotes.length];
int i = 0;
for (RemoteService remote : remotes)
clients[i++] = new ScriptSingleClient(remote);
return clients;
}
private FunctionEx, Exception> getRunScriptAction(final String scriptPath,
final String group, final TargetRuleEnum rule, final Map variables) {
if (variables == null)
return c -> c.runScript(scriptPath, group, rule);
else
return c -> c.runScriptVariables(scriptPath, group, rule, variables);
}
private List runScriptRuleAll(final String scriptPath, final String group,
final TargetRuleEnum rule, final Map variables) {
final List> statusList =
forEachParallel(getRunScriptAction(scriptPath, group, rule, variables), LOGGER);
final List results = new ArrayList<>();
statusList.forEach(results::addAll);
return results;
}
private List runScriptRuleOne(final String scriptPath, final String group,
final TargetRuleEnum rule, final Map variables) {
final MultiWebApplicationException.Builder exceptions = MultiWebApplicationException.of(LOGGER);
final List result =
firstRandomSuccess(getRunScriptAction(scriptPath, group, rule, variables), exceptions::add);
if (result != null)
return result;
if (exceptions.isEmpty())
return Collections.emptyList();
throw exceptions.build();
}
@Override
public List runScript(final String scriptPath, final String group, final TargetRuleEnum rule) {
return runScriptVariables(scriptPath, group, rule, null);
}
@Override
public List runScriptVariables(final String scriptPath, final String group, TargetRuleEnum rule,
Map variables) {
if (rule == null)
rule = TargetRuleEnum.one;
switch (rule) {
case all:
return runScriptRuleAll(scriptPath, group, rule, variables);
default:
case one:
return runScriptRuleOne(scriptPath, group, rule, variables);
}
}
@Override
public Map getRunsStatus() {
final Map finalResult = new TreeMap<>();
final MultiWebApplicationException.Builder exceptions = MultiWebApplicationException.of(LOGGER);
final List