Stash changes

This commit is contained in:
Anshul BANSAL 2020-01-09 11:25:29 +02:00
parent 09095200db
commit 16f7133f21
13 changed files with 359 additions and 211 deletions

View File

@ -1,109 +1,120 @@
<?xml version='1.0' encoding='utf-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>open-liberty</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<groupId>com.baeldung</groupId>
<artifactId>open-liberty</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- Plugin versions -->
<version.liberty-maven-plugin>3.1</version.liberty-maven-plugin>
<version.maven-war-plugin>3.2.3</version.maven-war-plugin>
<version.maven-surefire-plugin>2.22.2</version.maven-surefire-plugin>
<version.maven-failsafe-plugin>2.22.2</version.maven-failsafe-plugin>
<!-- Liberty configuration -->
<liberty.var.default.http.port>9080</liberty.var.default.http.port>
<liberty.var.default.https.port>9443</liberty.var.default.https.port>
</properties>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- Plugin versions -->
<version.liberty-maven-plugin>3.1</version.liberty-maven-plugin>
<version.maven-war-plugin>3.2.3</version.maven-war-plugin>
<version.maven-surefire-plugin>2.22.2</version.maven-surefire-plugin>
<version.maven-failsafe-plugin>2.22.2</version.maven-failsafe-plugin>
<!-- Liberty configuration -->
<liberty.var.default.http.port>9080</liberty.var.default.http.port>
<liberty.var.default.https.port>9443</liberty.var.default.https.port>
<liberty.var.app.context.root>openliberty</liberty.var.app.context.root>
</properties>
<dependencies>
<!-- Provided dependencies -->
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>8.0.0</version>
<scope>provided</scope>
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.2.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.186</version>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<version>3.0</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<!-- For tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
<scope>test</scope>
</dependency>
<!-- Support for JDK 9 and above -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Provided dependencies -->
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>8.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<version>3.0</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<!-- For tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
<scope>test</scope>
</dependency>
<!-- Support for JDK 9 and above -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Enable liberty-maven plugin -->
<plugin>
<groupId>io.openliberty.tools</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>${version.liberty-maven-plugin}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${version.maven-war-plugin}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.maven-surefire-plugin}</version>
</plugin>
<!-- Plugin to run functional tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${version.maven-failsafe-plugin}</version>
<configuration>
<systemPropertyVariables>
<http.port>${liberty.var.default.http.port}</http.port>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Enable liberty-maven plugin -->
<plugin>
<groupId>io.openliberty.tools</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>${version.liberty-maven-plugin}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${version.maven-war-plugin}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.maven-surefire-plugin}</version>
</plugin>
<!-- Plugin to run functional tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${version.maven-failsafe-plugin}</version>
<configuration>
<systemPropertyVariables>
<http.port>${liberty.var.default.http.port}</http.port>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -3,7 +3,7 @@ package com.baeldung.openliberty;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/system")
public class SystemApplication extends Application {
@ApplicationPath("/api")
public class ApiApplication extends Application {
}

View File

@ -1,29 +0,0 @@
package com.baeldung.openliberty;
import javax.enterprise.context.ApplicationScoped;
import java.lang.management.MemoryMXBean;
import java.lang.management.ManagementFactory;
import org.eclipse.microprofile.health.Liveness;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
@Liveness
@ApplicationScoped
public class SystemLivenessCheck implements HealthCheck {
@Override
public HealthCheckResponse call() {
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
long memUsed = memBean.getHeapMemoryUsage().getUsed();
long memMax = memBean.getHeapMemoryUsage().getMax();
return HealthCheckResponse.named(
SystemResource.class.getSimpleName() + " liveness check")
.withData("memory used", memUsed)
.withData("memory max", memMax)
.state(memUsed < memMax * 0.9).build();
}
}

View File

@ -1,32 +0,0 @@
package com.baeldung.openliberty;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Provider;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.health.Readiness;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
@Readiness
@ApplicationScoped
public class SystemReadinessCheck implements HealthCheck {
@Inject
@ConfigProperty(name = "io_openliberty_guides_system_inMaintenance")
Provider<String> inMaintenance;
@Override
public HealthCheckResponse call() {
HealthCheckResponseBuilder builder = HealthCheckResponse.named(
SystemResource.class.getSimpleName() + " readiness check");
if (inMaintenance != null && inMaintenance.get().equalsIgnoreCase("true")) {
return builder.withData("services", "not available").down().build();
}
return builder.withData("services", "available").up().build();
}
}

View File

@ -1,26 +0,0 @@
package com.baeldung.openliberty;
import javax.ws.rs.core.Response;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.eclipse.microprofile.metrics.annotation.Counted;
import org.eclipse.microprofile.metrics.annotation.Timed;
@RequestScoped
@Path("/properties")
public class SystemResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Timed(name = "getPropertiesTime", description = "Time needed to get the JVM system properties")
@Counted(absolute = true, description = "Number of times the JVM system properties are requested")
public Response getProperties() {
return Response.ok(System.getProperties()).build();
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.openliberty.hello;
package com.baeldung.openliberty.app;
import java.io.IOException;
@ -8,13 +8,13 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns="/hello")
public class HelloServlet extends HttpServlet {
@WebServlet(urlPatterns="/app")
public class AppServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().append("Hello! How are you today?\n");
response.getWriter().append("<html><head><title>Open Liberty: greet</title></head><body><h2>Hello! Welcome to Open Liberty</h2></body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)

View File

@ -0,0 +1,50 @@
package com.baeldung.openliberty.user;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.baeldung.openliberty.user.dao.UserDao;
import com.baeldung.openliberty.user.model.User;
@RequestScoped
@Path("user")
public class UserResource {
@Inject
UserService userService;
@Inject
private UserDao userDAO;
@GET
@Produces(MediaType.APPLICATION_JSON)
public User getUser() {
return userService.getUser();
}
/**
* This method creates a new user from the submitted data (firstName, lastName and
* email)
*/
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Transactional
public Response addNewUser(@FormParam("firstName") String firstName,
@FormParam("lastName") String lastName, @FormParam("email") String email) {
System.out.println(firstName);
System.out.println(lastName);
System.out.println(email);
User newUser = new User(firstName, lastName, email);
userDAO.createUser(newUser);
return Response.status(Response.Status.NO_CONTENT).build();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.openliberty.user;
import javax.enterprise.context.ApplicationScoped;
import com.baeldung.openliberty.user.model.User;
@ApplicationScoped
public class UserService {
public User getUser() {
User user = new User();
user.setFirstName("Norman");
user.setLastName("Lewis");
user.setEmail("normanlewis@email.com");
return user;
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.openliberty.user.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.baeldung.openliberty.user.model.User;
import javax.enterprise.context.RequestScoped;
@RequestScoped
public class UserDao {
@PersistenceContext(name = "jpa-unit")
private EntityManager em;
public void createUser(User user) {
em.persist(user);
}
public User readUser(int userId) {
return em.find(User.class, userId);
}
public void updateUser(User user) {
em.merge(user);
}
public void deleteUser(User user) {
em.remove(user);
}
public List<User> readAllUsers() {
return em.createNamedQuery("User.findAll", User.class).getResultList();
}
public List<User> findUser(String name, String location, String time) {
return em.createNamedQuery("User.findUser", User.class)
.setParameter("name", name)
.setParameter("location", location)
.setParameter("time", time).getResultList();
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.openliberty.user.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Event")
public class User {
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public User(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public User() {
super();
}
}

View File

@ -1,24 +1,51 @@
<server description="Sample Liberty server">
<featureManager>
<feature>jaxrs-2.1</feature>
<feature>jsonp-1.1</feature>
<feature>cdi-2.0</feature>
<feature>mpMetrics-2.0</feature>
<feature>mpHealth-2.0</feature>
<feature>mpConfig-1.3</feature>
</featureManager>
<featureManager>
<feature>appSecurity-2.0</feature>
<feature>jaxrs-2.1</feature>
<feature>jsonp-1.1</feature>
<feature>cdi-2.0</feature>
<feature>jpa-2.2</feature>
<feature>mpMetrics-2.0</feature>
<feature>mpHealth-2.0</feature>
<feature>mpConfig-1.3</feature>
<feature>restConnector-2.0</feature>
<feature>jdbc-4.2</feature>
</featureManager>
<variable name="default.http.port" defaultValue="9080"/>
<variable name="default.https.port" defaultValue="9443"/>
<variable name="app.context.root" defaultValue="sampleApp"/>
<keyStore id="defaultKeyStore" password="Liberty" />
<quickStartSecurity userName="blogAdmin"
userPassword="blogAdminPassword" />
<webApplication location="guide-getting-started.war" contextRoot="/" />
<mpMetrics authentication="false"/>
<logging traceSpecification="com.ibm.ws.microprofile.health.*=all" />
<webApplication location="open-liberty.war"
contextRoot="/" />
<mpMetrics authentication="false" />
<httpEndpoint host="*" httpPort="${default.http.port}"
httpsPort="${default.https.port}" id="defaultHttpEndpoint"/>
<logging
traceSpecification="com.ibm.ws.microprofile.health.*=all" />
<httpEndpoint host="*" httpPort="${default.http.port}"
httpsPort="${default.https.port}" id="defaultHttpEndpoint" />
<variable name="io_openliberty_guides_system_inMaintenance"
value="false" />
<library id="derby">
<fileset dir="${shared.resource.dir}" includes="derby*.jar" />
</library>
<library id="H2JDBCLib">
<fileset dir="/lib" includes="h2-1.4.186.jar" />
</library>
<!-- <dataSource id="DefaultDataSource"> <jdbcDriver libraryRef="derby"
/> Example properties referencing an in-memory Derby Embedded database <properties.derby.embedded
databaseName="memory:defaultdb" createDatabase="create" user="dbuser" password="dbpass"
/> </dataSource> -->
<dataSource id="h2test" jndiName="jdbc/h2test">
<!-- Define the DataSource class names on the <jdbcDriver> element -->
<jdbcDriver libraryRef="H2JDBCLib" />
<properties URL="jdbc:h2:mem:testdb" />
</dataSource>
<variable name="io_openliberty_guides_system_inMaintenance" value="false"/>
</server>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<!-- <persistence-unit name="jpa-unit"
transaction-type="JTA">
<jta-data-source>jdbc/libertyjpadatasource</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation"
value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="both" />
</properties>
</persistence-unit> -->
<persistence-unit name="h2test" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/h2test</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
</persistence>