com.qwazr.scripts.ScriptServiceImpl 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.AbstractServiceImpl;
import com.qwazr.server.ServerException;
import com.qwazr.utils.IOUtils;
import com.qwazr.utils.LoggerUtils;
import javax.ws.rs.core.Response.Status;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
class ScriptServiceImpl extends AbstractServiceImpl implements ScriptServiceInterface {
private static final Logger LOGGER = LoggerUtils.getLogger(ScriptServiceImpl.class);
private final ScriptManager scriptManager;
ScriptServiceImpl(final ScriptManager scriptManager) {
this.scriptManager = scriptManager;
}
@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,
final TargetRuleEnum rule, final Map variables) {
try {
return Arrays.asList(scriptManager.runAsync(scriptPath, variables));
} catch (Exception e) {
throw ServerException.getJsonException(LOGGER, e);
}
}
private RunThreadAbstract getRunThread(final String runId) throws ServerException {
final RunThreadAbstract runThread = scriptManager.getRunThread(runId);
if (runThread == null)
throw new ServerException(Status.NOT_FOUND, "Running script not found: " + runId);
return runThread;
}
@Override
public ScriptRunStatus getRunStatus(final String runId) {
try {
return getRunThread(runId).getStatus();
} catch (ServerException e) {
throw ServerException.getJsonException(LOGGER, e);
}
}
@Override
public InputStream getRunOut(final String runId) {
try {
return IOUtils.toInputStream(getRunThread(runId).getOut(), StandardCharsets.UTF_8);
} catch (ServerException e) {
throw ServerException.getTextException(LOGGER, e);
}
}
@Override
public InputStream getRunErr(final String runId) {
try {
return IOUtils.toInputStream(getRunThread(runId).getErr(), StandardCharsets.UTF_8);
} catch (ServerException e) {
throw ServerException.getTextException(LOGGER, e);
}
}
@Override
public Map getRunsStatus() {
return scriptManager.getRunsStatus();
}
@Override
public RunThreadAbstract runSync(String scriptPath, Map objects)
throws IOException, ClassNotFoundException {
return scriptManager.runSync(scriptPath, objects);
}
@Override
public ScriptRunStatus runAsync(final String scriptPath, final Map objects)
throws IOException, ClassNotFoundException {
return scriptManager.runAsync(scriptPath, objects);
}
}