/** * 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(); } }
/** * 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); }
/** * Creates the the math service. * * @return math service */ private static Service createMathService() { return new ServiceDescription("math", "Simple math service for VNumber") .addServiceMethod(addMethod()) .addServiceMethod(multiplyMethod()) .createService(); }
@Override public Map<String, Object> syncExecImpl(Map<String, Object> parameters) throws Exception { try (Connection connection = getDataSource().getConnection()) { try (PreparedStatement preparedStatement = connection.prepareStatement(getQuery())) { int i = 0; for (String parameterName : getParameterNames()) { Object value = parameters.get(parameterName); if (value instanceof VString) { preparedStatement.setString(i+1, ((VString) value).getValue()); } else if (value instanceof VNumber) { preparedStatement.setDouble(i+1, ((VNumber) value).getValue().doubleValue()); } else { throw new RuntimeException("JDBC mapping support for " + value.getClass().getSimpleName() + " not implemented"); } i++; } if (isResultQuery()) { ResultSet resultSet = preparedStatement.executeQuery(); VTable table = resultSetToVTable(resultSet); return Collections.<String, Object>singletonMap(getResults().get(0).getName(), table); } else { preparedStatement.execute(); return new HashMap<>(); } } } catch (Exception ex) { throw ex; } }
/** * Creates the service method description for the multiplication service * method. * * @return multiplication service method description */ public static ServiceMethodDescription multiplyMethod() { return new ServiceMethodDescription("multiply", "Multiplies two numbers.") { @Override public ServiceMethod createServiceMethod(ServiceDescription serviceDescription) { return new MultiplyServiceMethod(this, serviceDescription); } }.addArgument("arg1", "First argument", VNumber.class) .addArgument("arg2", "Second argument", VNumber.class) .addResult("result", "The product of arg1 and arg2", VNumber.class); }
/** * Adds a result for the pvAccess RPC Service. * * @param name the result name * @param description the result description * @return this */ public RPCServiceMethodDescription addRPCResult(String name, String fieldName, String description, Class<?> type) { if (resultAdded) { throw new IllegalArgumentException("The pvAccess RPC rpcservice can only have one result"); } if (fieldName != null) { this.fieldNames.put(name, fieldName); } else { this.fieldNames.put(name, FIELD_NAME_EQUALS_NAME); } super.addResult(name, description, type); this.resultAdded = true; return this; }
/** * Sample execution of Math services. * * @param args ignored */ public static void main(String[] args) { System.out.println("Locating service method..."); ServiceMethod method = ServiceRegistry.getDefault().findServiceMethod("math/add"); System.out.println("Service method found: " + method); System.out.println("Preparing arguments..."); Map<String, Object> arguments = new HashMap<>(); arguments.put("arg1", ValueFactory.newVDouble(1.0)); arguments.put("arg2", ValueFactory.newVDouble(2.0)); System.out.println("Arguments: " + arguments); System.out.println("Executing service..."); Map<String, Object> result = method.executeSync(arguments); System.out.println("Result: " + result); }
/** * Sample execution of Math services. * * @param args ignored */ public static void main(String[] args) { System.out.println("Locating service method..."); ServiceMethod method = ServiceRegistry.getDefault().findServiceMethod("math/add"); System.out.println("Service method found: " + method); System.out.println("Preparing arguments..."); Map<String, Object> arguments = new HashMap<>(); arguments.put("arg1", ValueFactory.newVDouble(1.0)); arguments.put("arg2", ValueFactory.newVDouble(2.0)); System.out.println("Arguments: " + arguments); System.out.println("Executing service..."); method.executeAsync(arguments, result -> { System.out.println("Result: " + result); }, ex -> { ex.printStackTrace(); }); }
public static void main(String[] args) throws Exception { // Increasing logging at CONFIG level Logger.getLogger("").getHandlers()[0].setLevel(Level.CONFIG); Logger.getLogger("").setLevel(Level.CONFIG); System.out.println("Listing all services"); for (String service : ServiceRegistry.getDefault().getRegisteredServiceNames()) { System.out.println("- " + service); } System.out.println("Done"); } }
/** * Adds an argument for pvAccess RPC rpcservice. * * @param name argument name * @param description argument description * @param type the expected type of the argument * @return this */ public RPCServiceMethodDescription addArgument(String name, String fieldName, String description, Class<?> type) { super.addArgument(name, description, relaxArgumentType(type)); strictArguments.put(name, type); orderedParameterNames.put(name, fieldName != null ? fieldName : FIELD_NAME_EQUALS_NAME); return this; }
/** * Closes the shared resources: executor and JDBC datasource. */ @Override public void close() { // Executor close super.close(); // Datasource close try { dataSource.getConnection().close(); } catch (SQLException ex) { throw new RuntimeException("Unable to close the JDBC data source connection", ex); } } }
/** * Creates the service method description for the addition service method. * * @return addition service method description */ private static ServiceMethodDescription addMethod() { return new ServiceMethodDescription("add", "Adds two numbers.") { @Override public ServiceMethod createServiceMethod(ServiceDescription serviceDescription) { return new AddServiceMethod(this, serviceDescription); } }.addArgument("arg1", "First argument", VNumber.class) .addArgument("arg2", "Second argument", VNumber.class) .addResult("result", "The sum of arg1 and arg2", VNumber.class); }