@Test public void createFromXml1() { InputStream stream = getClass().getResourceAsStream("jdbcService.1.xml"); Service service = JDBCServices.createFromXml(stream); assertThat(service.getName(), equalTo("jdbcSample")); assertThat(service.getDescription(), equalTo("A test service")); assertThat(service.getServiceMethods().size(), equalTo(2)); assertThat(service.getServiceMethods().get("query").getName(), equalTo("query")); assertThat(service.getServiceMethods().get("query").getDescription(), equalTo("A test query")); assertThat(service.getServiceMethods().get("query").getArguments().size(), equalTo(0)); assertThat(service.getServiceMethods().get("query").getResultMap().get("result").getType(), equalTo((Class) VTable.class)); assertThat(service.getServiceMethods().get("query").getResultMap().get("result").getDescription(), equalTo("The query result")); assertThat(service.getServiceMethods().get("insert").getName(), equalTo("insert")); assertThat(service.getServiceMethods().get("insert").getDescription(), equalTo("A test insert query")); assertThat(service.getServiceMethods().get("insert").getResults().size(), equalTo(0)); assertThat(service.getServiceMethods().get("insert").getArgumentMap().get("name").getDescription(), equalTo("The name")); assertThat(service.getServiceMethods().get("insert").getArgumentMap().get("name").getType(), equalTo((Class) VString.class)); assertThat(service.getServiceMethods().get("insert").getArgumentMap().get("index").getDescription(), equalTo("The index")); assertThat(service.getServiceMethods().get("insert").getArgumentMap().get("index").getType(), equalTo((Class) VNumber.class)); assertThat(service.getServiceMethods().get("insert").getArgumentMap().get("value").getDescription(), equalTo("The value")); assertThat(service.getServiceMethods().get("insert").getArgumentMap().get("value").getType(), equalTo((Class) VNumber.class)); }
public static Service createFromXml(InputStream input) { if (input == null){ throw new IllegalArgumentException("Input must not be null"); } try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(input); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xPath = xpathFactory.newXPath(); String ver = xPath.evaluate("/jdbcService/@ver", document); String serviceName = xPath.evaluate("/jdbcService/@name", document); String serviceDesecription = xPath.evaluate("/jdbcService/@description", document); String jdbcUrl = xPath.evaluate("/jdbcService/jdbcUrl", document); if (!ver.equals("1")) { throw new IllegalArgumentException("Unsupported version " + ver); } JDBCServiceDescription service = new JDBCServiceDescription(serviceName, serviceDesecription); service.dataSource(new SimpleDataSource(jdbcUrl)); service.executorService(defaultExecutor); NodeList methods = (NodeList) xPath.evaluate("/jdbcService/methods/method", document, XPathConstants.NODESET); for (int i = 0; i < methods.getLength(); i++) { Node method = methods.item(i); String methodName = xPath.evaluate("@name", method); String methodDescription = xPath.evaluate("@description", method); String query = xPath.evaluate("query", method); String resultName = xPath.evaluate("result/@name", method); String resultDescription = xPath.evaluate("result/@description", method); JDBCServiceMethodDescription jdbcMethod = new JDBCServiceMethodDescription(methodName, methodDescription); jdbcMethod.query(query); if (!resultName.trim().isEmpty()) { jdbcMethod.queryResult(resultName, resultDescription); } NodeList arguments = (NodeList) xPath.evaluate("argument", method, XPathConstants.NODESET); for (int nArg = 0; nArg < arguments.getLength(); nArg++) { Node argument = arguments.item(nArg); String argName = xPath.evaluate("@name", argument); String argDescription = xPath.evaluate("@description", argument); String argType = xPath.evaluate("@type", argument); Class<?> argClass = null; switch(argType) { case "VNumber": argClass = VNumber.class; break; case "VString": argClass = VString.class; break; default: throw new IllegalArgumentException("Type " + argType + " not supported."); } if (!argName.trim().isEmpty()) { jdbcMethod.addArgument(argName, argDescription, argClass); } } service.addServiceMethod(jdbcMethod); } return service.createService(); } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException ex) { Logger.getLogger(JDBCServices.class.getName()).log(Level.FINEST, "Couldn't create service", ex); throw new IllegalArgumentException("Couldn't create service", ex); } }
JDBCServices { public static Service createFromXml(InputStream input) { if (input == null){ throw new IllegalArgumentException("Input must not be null"); } try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(input); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xPath = xpathFactory.newXPath(); String ver = xPath.evaluate("/jdbcService/@ver", document); String serviceName = xPath.evaluate("/jdbcService/@name", document); String serviceDesecription = xPath.evaluate("/jdbcService/@description", document); String jdbcUrl = xPath.evaluate("/jdbcService/jdbcUrl", document); if (!ver.equals("1")) { throw new IllegalArgumentException("Unsupported version " + ver); } JDBCServiceDescription service = new JDBCServiceDescription(serviceName, serviceDesecription); service.dataSource(new SimpleDataSource(jdbcUrl)); service.executorService(defaultExecutor); NodeList methods = (NodeList) xPath.evaluate("/jdbcService/methods/method", document, XPathConstants.NODESET); for (int i = 0; i < methods.getLength(); i++) { Node method = methods.item(i); String methodName = xPath.evaluate("@name", method); String methodDescription = xPath.evaluate("@description", method); String query = xPath.evaluate("query", method); String resultName = xPath.evaluate("result/@name", method); String resultDescription = xPath.evaluate("result/@description", method); JDBCServiceMethodDescription jdbcMethod = new JDBCServiceMethodDescription(methodName, methodDescription); jdbcMethod.query(query); if (!resultName.trim().isEmpty()) { jdbcMethod.queryResult(resultName, resultDescription); } NodeList arguments = (NodeList) xPath.evaluate("argument", method, XPathConstants.NODESET); for (int nArg = 0; nArg < arguments.getLength(); nArg++) { Node argument = arguments.item(nArg); String argName = xPath.evaluate("@name", argument); String argDescription = xPath.evaluate("@description", argument); String argType = xPath.evaluate("@type", argument); Class<?> argClass = null; switch(argType) { case "VNumber": argClass = VNumber.class; break; case "VString": argClass = VString.class; break; default: throw new IllegalArgumentException("Type " + argType + " not supported."); } if (!argName.trim().isEmpty()) { jdbcMethod.addArgument(argName, argDescription, argClass); } } service.addServiceMethod(jdbcMethod); } return service.createService(); } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException ex) { Logger.getLogger(JDBCServices.class.getName()).log(Level.FINEST, "Couldn't create service", ex); throw new IllegalArgumentException("Couldn't create service", ex); } } }
JDBCServices { public static Service createFromXml(InputStream input) { if (input == null){ throw new IllegalArgumentException("Input must not be null"); } try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(input); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xPath = xpathFactory.newXPath(); String ver = xPath.evaluate("/jdbcService/@ver", document); String serviceName = xPath.evaluate("/jdbcService/@name", document); String serviceDesecription = xPath.evaluate("/jdbcService/@description", document); String jdbcUrl = xPath.evaluate("/jdbcService/jdbcUrl", document); if (!ver.equals("1")) { throw new IllegalArgumentException("Unsupported version " + ver); } JDBCServiceDescription service = new JDBCServiceDescription(serviceName, serviceDesecription); service.dataSource(new SimpleDataSource(jdbcUrl)); service.executorService(defaultExecutor); NodeList methods = (NodeList) xPath.evaluate("/jdbcService/methods/method", document, XPathConstants.NODESET); for (int i = 0; i < methods.getLength(); i++) { Node method = methods.item(i); String methodName = xPath.evaluate("@name", method); String methodDescription = xPath.evaluate("@description", method); String query = xPath.evaluate("query", method); String resultName = xPath.evaluate("result/@name", method); String resultDescription = xPath.evaluate("result/@description", method); JDBCServiceMethodDescription jdbcMethod = new JDBCServiceMethodDescription(methodName, methodDescription); jdbcMethod.query(query); if (!resultName.trim().isEmpty()) { jdbcMethod.queryResult(resultName, resultDescription); } NodeList arguments = (NodeList) xPath.evaluate("argument", method, XPathConstants.NODESET); for (int nArg = 0; nArg < arguments.getLength(); nArg++) { Node argument = arguments.item(nArg); String argName = xPath.evaluate("@name", argument); String argDescription = xPath.evaluate("@description", argument); String argType = xPath.evaluate("@type", argument); Class<?> argClass = null; switch(argType) { case "VNumber": argClass = VNumber.class; break; case "VString": argClass = VString.class; break; default: throw new IllegalArgumentException("Type " + argType + " not supported."); } if (!argName.trim().isEmpty()) { jdbcMethod.addArgument(argName, argDescription, argClass); } } service.addServiceMethod(jdbcMethod); } return service.createService(); } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException ex) { Logger.getLogger(JDBCServices.class.getName()).log(Level.FINEST, "Couldn't create service", ex); throw new IllegalArgumentException("Couldn't create service", ex); } } private  JDBCServices(); }
JDBCServices { public static Service createFromXml(InputStream input) { if (input == null){ throw new IllegalArgumentException("Input must not be null"); } try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(input); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xPath = xpathFactory.newXPath(); String ver = xPath.evaluate("/jdbcService/@ver", document); String serviceName = xPath.evaluate("/jdbcService/@name", document); String serviceDesecription = xPath.evaluate("/jdbcService/@description", document); String jdbcUrl = xPath.evaluate("/jdbcService/jdbcUrl", document); if (!ver.equals("1")) { throw new IllegalArgumentException("Unsupported version " + ver); } JDBCServiceDescription service = new JDBCServiceDescription(serviceName, serviceDesecription); service.dataSource(new SimpleDataSource(jdbcUrl)); service.executorService(defaultExecutor); NodeList methods = (NodeList) xPath.evaluate("/jdbcService/methods/method", document, XPathConstants.NODESET); for (int i = 0; i < methods.getLength(); i++) { Node method = methods.item(i); String methodName = xPath.evaluate("@name", method); String methodDescription = xPath.evaluate("@description", method); String query = xPath.evaluate("query", method); String resultName = xPath.evaluate("result/@name", method); String resultDescription = xPath.evaluate("result/@description", method); JDBCServiceMethodDescription jdbcMethod = new JDBCServiceMethodDescription(methodName, methodDescription); jdbcMethod.query(query); if (!resultName.trim().isEmpty()) { jdbcMethod.queryResult(resultName, resultDescription); } NodeList arguments = (NodeList) xPath.evaluate("argument", method, XPathConstants.NODESET); for (int nArg = 0; nArg < arguments.getLength(); nArg++) { Node argument = arguments.item(nArg); String argName = xPath.evaluate("@name", argument); String argDescription = xPath.evaluate("@description", argument); String argType = xPath.evaluate("@type", argument); Class<?> argClass = null; switch(argType) { case "VNumber": argClass = VNumber.class; break; case "VString": argClass = VString.class; break; default: throw new IllegalArgumentException("Type " + argType + " not supported."); } if (!argName.trim().isEmpty()) { jdbcMethod.addArgument(argName, argDescription, argClass); } } service.addServiceMethod(jdbcMethod); } return service.createService(); } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException ex) { Logger.getLogger(JDBCServices.class.getName()).log(Level.FINEST, "Couldn't create service", ex); throw new IllegalArgumentException("Couldn't create service", ex); } } private  JDBCServices(); static Service createFromXml(InputStream input); }
JDBCServices { public static Service createFromXml(InputStream input) { if (input == null){ throw new IllegalArgumentException("Input must not be null"); } try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(input); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xPath = xpathFactory.newXPath(); String ver = xPath.evaluate("/jdbcService/@ver", document); String serviceName = xPath.evaluate("/jdbcService/@name", document); String serviceDesecription = xPath.evaluate("/jdbcService/@description", document); String jdbcUrl = xPath.evaluate("/jdbcService/jdbcUrl", document); if (!ver.equals("1")) { throw new IllegalArgumentException("Unsupported version " + ver); } JDBCServiceDescription service = new JDBCServiceDescription(serviceName, serviceDesecription); service.dataSource(new SimpleDataSource(jdbcUrl)); service.executorService(defaultExecutor); NodeList methods = (NodeList) xPath.evaluate("/jdbcService/methods/method", document, XPathConstants.NODESET); for (int i = 0; i < methods.getLength(); i++) { Node method = methods.item(i); String methodName = xPath.evaluate("@name", method); String methodDescription = xPath.evaluate("@description", method); String query = xPath.evaluate("query", method); String resultName = xPath.evaluate("result/@name", method); String resultDescription = xPath.evaluate("result/@description", method); JDBCServiceMethodDescription jdbcMethod = new JDBCServiceMethodDescription(methodName, methodDescription); jdbcMethod.query(query); if (!resultName.trim().isEmpty()) { jdbcMethod.queryResult(resultName, resultDescription); } NodeList arguments = (NodeList) xPath.evaluate("argument", method, XPathConstants.NODESET); for (int nArg = 0; nArg < arguments.getLength(); nArg++) { Node argument = arguments.item(nArg); String argName = xPath.evaluate("@name", argument); String argDescription = xPath.evaluate("@description", argument); String argType = xPath.evaluate("@type", argument); Class<?> argClass = null; switch(argType) { case "VNumber": argClass = VNumber.class; break; case "VString": argClass = VString.class; break; default: throw new IllegalArgumentException("Type " + argType + " not supported."); } if (!argName.trim().isEmpty()) { jdbcMethod.addArgument(argName, argDescription, argClass); } } service.addServiceMethod(jdbcMethod); } return service.createService(); } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException ex) { Logger.getLogger(JDBCServices.class.getName()).log(Level.FINEST, "Couldn't create service", ex); throw new IllegalArgumentException("Couldn't create service", ex); } } private  JDBCServices(); static Service createFromXml(InputStream input);  }