static String defaultShellArg() { if (isWindows()) { return "/c"; } else { return "-c"; } }
@Override public ServiceMethod createServiceMethod(ServiceDescription serviceDescription) { return new GenericExecServiceMethod(this, serviceDescription); }
@Override public ServiceMethod createServiceMethod(ServiceDescription serviceDescription) { return new ExecServiceMethod(this, (ExecServiceDescription) serviceDescription); }
ExecServiceDescription service = new ExecServiceDescription(serviceName, serviceDesecription); service.executorService(GenericExecService.defaultExecutor); String resultDescription = xPath.evaluate("result/@description", method); ExecServiceMethodDescription execMethod = new ExecServiceMethodDescription(methodName, methodDescription); execMethod.command(command); if (!resultName.trim().isEmpty()) { execMethod.queryResult(resultName, resultDescription); execMethod.addArgument(argName, argDescription, argClass); service.addServiceMethod(execMethod); return service.createService(); } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException ex) { Logger.getLogger(ExecServices.class.getName()).log(Level.FINEST, "Couldn't create service", ex);
@Override public Map<String, Object> syncExecImpl(final Map<String, Object> parameters) throws Exception{ String shell = defaultShell(); String shellArg = defaultShellArg(); String command = ((VString) parameters.get("command")).getValue(); return syncExecuteCommand(parameters, shell, shellArg, command); }
/** * Adds a result for the script; cannot have multiple query results. * * @param name the result name; can't be null * @param description the result description; can't be null * @return this */ public ExecServiceMethodDescription queryResult(String name, String description) { if (resultAdded) { throw new IllegalArgumentException("The query can only have one result"); } resultAdded = true; addResult(name, description, VType.class); return this; }
@Override public Collection<Service> additionalServices() { if (includeGenericExecService) { return Collections.<Service>singleton(GenericExecService.createGenericExecService()); } else { return Collections.emptySet(); } }
@Override public Service createService(File file) throws Exception { if (file.getName().endsWith(".xml")) { return ExecServices.createFromXml(new FileInputStream(file)); } else { return null; } }
/** * Creates description for the "run" service method; which simply runs the * command on the shell and returns the output. * * @return service method description for exec */ public static ServiceMethodDescription runMethod() { return new ServiceMethodDescription("run", "Executes a command.") { @Override public ServiceMethod createServiceMethod(ServiceDescription serviceDescription) { return new GenericExecServiceMethod(this, serviceDescription); } }.addArgument("command", "The command", VString.class) .addResult("output", "The output of the command", VType.class); }
@Override public Map<String, Object> syncExecImpl(final Map<String, Object> parameters) throws Exception { String expandedCommand = command; for (Map.Entry<String, Object> entry : parameters.entrySet()) { String name = entry.getKey(); Object object = entry.getValue(); String value = null; if (object instanceof VString) { value = ((VString) object).getValue(); } else if (object == null) { value = ""; } else { throw new IllegalArgumentException("Can't map parameter '" + name + "': was " + object); } expandedCommand = expandedCommand.replaceAll("#" + name + "#", value); } return GenericExecServiceMethod.syncExecuteCommand(parameters, shell, shellArg, expandedCommand); } }
/** * Creates a service for generic shell execution, containing the "run" service * method. * * @return generic exec service */ public static Service createGenericExecService() { return new ServiceDescription("exec", "Command execution service") .addServiceMethod(runMethod()) .executorService(defaultExecutor) .createService(); } }
static String defaultShell() { if (isWindows()) { return "cmd"; } else { return "/bin/bash"; } }