Skip to content

Commit

Permalink
Add simple-app project and update company-web-server to use pooli…
Browse files Browse the repository at this point in the history
…ng (#11)

* Add setup/usage instructions for company-web-server and rename docs to swagger

Signed-off-by: Sanjula Ganepola <[email protected]>

* Add simple-app

Signed-off-by: Sanjula Ganepola <[email protected]>

* Update README

Signed-off-by: Sanjula Ganepola <[email protected]>

* fix demo type table

Signed-off-by: Sanjula Ganepola <[email protected]>

* Add more endpoints

Signed-off-by: Sanjula Ganepola <[email protected]>

* Add endpoint for tracing

Signed-off-by: Sanjula Ganepola <[email protected]>

* Update to v0.0.4 and fix demo types

Signed-off-by: Sanjula Ganepola <[email protected]>

* Upgrade to 0.04, switch to use pool, and fix imports

Signed-off-by: Sanjula Ganepola <[email protected]>

* fix run app command

Signed-off-by: Sanjula Ganepola <[email protected]>

* fix indentation

Signed-off-by: Sanjula Ganepola <[email protected]>

* fix note not rendering

Signed-off-by: Sanjula Ganepola <[email protected]>

* fix

Signed-off-by: Sanjula Ganepola <[email protected]>

* Update LICENSE

Co-authored-by: Jonathan <[email protected]>

* Update instructions

Co-authored-by: Jonathan <[email protected]>

* minor fixes

Signed-off-by: Sanjula Ganepola <[email protected]>

---------

Signed-off-by: Sanjula Ganepola <[email protected]>
Co-authored-by: Jonathan <[email protected]>
  • Loading branch information
SanjulaGanepola and jonnyz32 authored Aug 30, 2024
1 parent b2c8a9c commit 9e1da38
Show file tree
Hide file tree
Showing 37 changed files with 791 additions and 46 deletions.
3 changes: 2 additions & 1 deletion java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

These sample projects leverage the [mapepire-java](https://github.com/Mapepire-IBMi/mapepire-java) client SDK:

* `company-web-server`: Company web server to manage departments, employees, and sales
* `simple-app`: Simple demo application of using the Mapepire Java client SDK
* `company-web-server`: Jetty company web server to manage departments, employees, and sales
8 changes: 8 additions & 0 deletions java/company-web-server/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"vscjava.vscode-java-pack",
"GitHub.vscode-github-actions",
"wayou.vscode-todo-highlight",
"streetsidesoftware.code-spell-checker"
]
}
8 changes: 6 additions & 2 deletions java/company-web-server/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
}
"java.configuration.updateBuildConfiguration": "automatic",
"files.insertFinalNewline": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "always"
}
}
2 changes: 1 addition & 1 deletion java/company-web-server/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2024 IBM Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
56 changes: 55 additions & 1 deletion java/company-web-server/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
# Company Web Server

Company web server to manage departments, employees, and sales
Jetty company web server to manage departments, employees, and sales

## Setup

1. Ensure the [Mapepire Server](https://mapepire-ibmi.github.io/guides/sysadmin) is installed and running on the IBM i.

2. Create the `SAMPLE` schema on the IBM i:

```sql
QSYS.CREATE_SQL_SAMPLE('sample')
```

3. Clone the repository and build the application:

```sh
git clone https://github.com/Mapepire-IBMi/samples.git
cd java/company-web-server
mvn clean package
```

4. Start the company web server:

```sh
cd target
java -jar company-web-server-1.0-SNAPSHOT-jar-with-dependencies.jar
```

5. Start a web server to launch the Swagger UI:

```sh
npm install -g http-server
cd swagger
http-server --cors
```

6. Access the Swagger UI at http://localhost:8080 in the browser.

## Usage

1. Connect to a database using the `/connect` endpoint.

2. Set the server trace level using the `/tracing` endpoint.

3. Experiment with the various endpoints:

| Method | Endpoint | Description |
| ------ | ----------------------- | --------------------------------------------------------- |
| `GET` | `/departments` | Get a list of departments |
| `GET` | `/departments/{deptNo}` | Get a department by department number |
| `GET` | `/employees` | Get a list of employees |
| `GET` | `/employees/{empNo}` | Get an employee by employee number |
| `GET` | `/sales` | Get a list of sales |
| `GET` | `/sales/{salesPerson}` | Get a list of sales associated with a sales person |

4. Disconnect from the database using the `/disconnect` endpoint.
37 changes: 30 additions & 7 deletions java/company-web-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<packaging>jar</packaging>

<name>Company Web Server</name>
<description>Company web server to manage departments, employees, and sales</description>
<description>Jetty company web server to manage departments, employees, and sales</description>
<url>https://github.com/Mapepire-IBMi/samples/blob/main/java/company-web-server</url>

<licenses>
Expand Down Expand Up @@ -44,6 +44,11 @@
</properties>

<dependencies>
<dependency>
<groupId>io.github.mapepire-ibmi</groupId>
<artifactId>mapepire-sdk</artifactId>
<version>0.0.4</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
Expand All @@ -59,11 +64,6 @@
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>io.github.mapepire-ibmi</groupId>
<artifactId>mapepire-sdk</artifactId>
<version>0.0.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -93,6 +93,29 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>io.github.company.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import io.github.company.servlets.ConnectServlet;
import io.github.company.servlets.DisconnectServlet;
import io.github.company.servlets.QueryServlet;
import io.github.company.servlets.TracingServlet;

public class CompanyServer {
private static int port = 3000;
private Server server;

public CompanyServer() throws InterruptedException, ExecutionException {
// Create Jetty server on port 8080
server = new Server(8080);
// Create Jetty server
server = new Server(port);

// Create context handler for servlets
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
Expand All @@ -28,20 +30,24 @@ public CompanyServer() throws InterruptedException, ExecutionException {

// Add servlets
context.addServlet(new ServletHolder(new ConnectServlet()), "/connect");
context.addServlet(new ServletHolder(new TracingServlet()), "/tracing");
context.addServlet(new ServletHolder(new DisconnectServlet()), "/disconnect");
context.addServlet(new ServletHolder(new QueryServlet("SELECT * FROM SAMPLE.DEPARTMENT")), "/departments");
context.addServlet(new ServletHolder(new QueryServlet("SELECT * FROM SAMPLE.DEPARTMENT WHERE DEPTNO = ?")), "/departments/*");
context.addServlet(new ServletHolder(new QueryServlet("SELECT * FROM SAMPLE.EMPLOYEE")), "/employees");
context.addServlet(new ServletHolder(new QueryServlet("SELECT * FROM SAMPLE.EMPLOYEE WHERE EMPNO = ?")), "/employees/*");
context.addServlet(new ServletHolder(new QueryServlet("SELECT * FROM SAMPLE.SALES")), "/sales");
context.addServlet(new ServletHolder(new DisconnectServlet()), "/disconnect");
context.addServlet(new ServletHolder(new QueryServlet("SELECT * FROM SAMPLE.SALES WHERE SALES_PERSON = ?")), "/sales/*");
}

public void start() throws Exception {
System.out.println("Starting company server...");
System.out.println("Started company server on port " + String.valueOf(CompanyServer.port) + "...");
server.start();
server.join();
}

public void stop() throws Exception {
System.out.println("Stopping company server...");
System.out.println("Stopped company server on port " + String.valueOf(CompanyServer.port) + "...");
server.stop();
server.join();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
package io.github.company;

import java.util.List;
import java.util.concurrent.CompletableFuture;

// import io.github.mapapire.Pool;
import io.github.mapapire.SqlJob;
// import io.github.mapapire.PoolOptions;
import io.github.mapapire.types.DaemonServer;
import io.github.mapapire.types.QueryResult;
import io.github.mapepire_ibmi.Pool;
import io.github.mapepire_ibmi.types.DaemonServer;
import io.github.mapepire_ibmi.types.PoolOptions;
import io.github.mapepire_ibmi.types.QueryOptions;
import io.github.mapepire_ibmi.types.QueryResult;
import io.github.mapepire_ibmi.types.ServerTraceLevel;

public class Database {
// private static Pool pool;
private static SqlJob job;
private static Pool pool;

public static void connect(String host, int port, String user, String password) throws Exception {
DaemonServer creds = new DaemonServer(host, port, user, password, true, "");
// pool = new Pool(new PoolOptions(creds, 5, 1));
// pool.waitForJob().get();
// pool.init().get();
pool = new Pool(new PoolOptions(creds, 5, 1));
pool.waitForJob().get();
pool.init().get();
}

public static boolean getReadyJob() {
return pool.getReadyJob() != null;
}

job = new SqlJob();
job.connect(creds).get();
public static <T> CompletableFuture<QueryResult<T>> execute(String sql) throws Exception {
return pool.execute(sql);
}

public static boolean isConnected() {
return job != null;
public static <T> CompletableFuture<QueryResult<T>> prepareAndExecute(String sql, List<Object> parameters)
throws Exception {
return pool.execute(sql, new QueryOptions(false, false, parameters));
}

public static <T> CompletableFuture<QueryResult<T>> query(String sql) throws Exception {
// return pool.execute(sql);
return job.execute(sql);
public static void setTraceLevel(ServerTraceLevel level) throws Exception {
pool.getReadyJob().setTraceLevel(level).get();
}

public static void disconnect() {
// pool.end();
job.close();
pool.end();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.company.servlets;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -10,7 +11,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.github.company.Database;
import io.github.mapapire.types.QueryResult;
import io.github.mapepire_ibmi.types.QueryResult;

public class QueryServlet extends HttpServlet {
private static final ObjectMapper objectMapper = new ObjectMapper();
Expand All @@ -28,14 +29,17 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
ObjectNode jsonResponse = objectMapper.createObjectNode();

try {
if (!Database.isConnected()) {
if (!Database.getReadyJob()) {
resp.setStatus(HttpServletResponse.SC_OK);
jsonResponse.put("error", "No connection.");
resp.getWriter().write(objectMapper.writeValueAsString(jsonResponse));
return;
}

QueryResult<Object> result = Database.query(this.sql).get();
String pathInfo = req.getPathInfo();

QueryResult<Object> result = pathInfo == null ? Database.execute(this.sql).get()
: Database.prepareAndExecute(this.sql, Arrays.asList(pathInfo.substring(1))).get();

if (result.getSuccess()) {
resp.setStatus(HttpServletResponse.SC_OK);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.company.servlets;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.github.company.Database;
import io.github.mapepire_ibmi.types.ServerTraceLevel;

public class TracingServlet extends HttpServlet {
private static final ObjectMapper objectMapper = new ObjectMapper();

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("application/json");
resp.setCharacterEncoding("UTF-8");
ObjectNode jsonResponse = objectMapper.createObjectNode();

try {
String level = req.getParameter("level");
Database.setTraceLevel(ServerTraceLevel.fromValue(level));

resp.setStatus(HttpServletResponse.SC_OK);
jsonResponse.put("message", "Successfully set server trace level.");
} catch (Exception e) {
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
jsonResponse.put("error", "Internal server error. Reason: " + e.toString());
}

resp.getWriter().write(objectMapper.writeValueAsString(jsonResponse));
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ externalDocs:
description: GitHub Repository
url: https://github.com/Mapepire-IBMi/samples/tree/main/java/company-web-server
servers:
- url: http://localhost:8080
- url: http://localhost:3000
description: Local server
tags:
- name: Database Services
Expand Down Expand Up @@ -61,16 +61,17 @@ paths:
put:
tags:
- Database Services
summary: Toggle database channel data tracing.
summary: Set server trace level.
parameters:
- name: enable
- name: level
in: query
required: true
schema:
type: boolean
type: string
enum: ['OFF', 'ON', 'ERRORS', 'DATASTREAM']
responses:
'200':
description: Successfully toggled.
description: Successfully set server trace level.
'500':
description: Internal server error
/disconnect:
Expand Down Expand Up @@ -184,4 +185,4 @@ paths:
'403':
description: No connection
'500':
description: Internal server error
description: Internal server error
Loading

0 comments on commit 9e1da38

Please sign in to comment.