Skip to content

Commit

Permalink
Ordering of interposed synchronizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mmusgrov committed Nov 20, 2023
1 parent 7ebe3dc commit add7f19
Show file tree
Hide file tree
Showing 9 changed files with 704 additions and 8 deletions.
5 changes: 5 additions & 0 deletions extensions/hibernate-orm/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
<artifactId>quarkus-resteasy-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.quarkus.narayana.quarkus;

import jakarta.persistence.Cacheable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.QueryHint;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;

@Entity
@Table(name = "known_fruits")
@NamedQuery(name = "Fruits.findAll", query = "SELECT f FROM Fruit f ORDER BY f.name", hints = @QueryHint(name = "org.hibernate.cacheable", value = "true"))
@Cacheable
public class Fruit {
@Id
@SequenceGenerator(name = "fruitsSequence", sequenceName = "known_fruits_id_seq", allocationSize = 1, initialValue = 10)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "fruitsSequence")
private Integer id;

@Column(length = 40, unique = true)
private String name;

public Fruit() {
}

public Fruit(String name) {
this.name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package io.quarkus.narayana.quarkus;

import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Event;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

import org.jboss.logging.Logger;
import org.jboss.resteasy.annotations.jaxrs.PathParam;

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

@Path("fruits")
@ApplicationScoped
@Produces("application/json")
@Consumes("application/json")
public class FruitResource {

private static final Logger LOGGER = Logger.getLogger(FruitResource.class.getName());

@Inject
EntityManager entityManager;

@Inject
Event<Fruit> eventBus;

@GET
public List<Fruit> get() {
return entityManager.createNamedQuery("Fruits.findAll", Fruit.class)
.getResultList();
}

@GET
@Path("{id}")
public Fruit getSingle(@PathParam Integer id) {
Fruit entity = entityManager.find(Fruit.class, id);
if (entity == null) {
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
}
return entity;
}

@POST
@Transactional
public Response create(Fruit fruit) {
if (fruit.getId() != null) {
throw new WebApplicationException("Id was invalidly set on request.", 422);
}

entityManager.persist(fruit);
eventBus.fire(fruit);
return Response.ok(fruit).status(201).build();
}

@PUT
@Path("{id}")
@Transactional
public Fruit update(@PathParam Integer id, Fruit fruit) {
if (fruit.getName() == null) {
throw new WebApplicationException("Fruit Name was not set on request.", 422);
}

Fruit entity = entityManager.find(Fruit.class, id);

if (entity == null) {
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
}

entity.setName(fruit.getName());
return entity;
}

@DELETE
@Path("{id}")
@Transactional
public Response delete(@PathParam Integer id) {
Fruit entity = entityManager.getReference(Fruit.class, id);
if (entity == null) {
throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
}
entityManager.remove(entity);
return Response.status(204).build();
}

@Provider
public static class ErrorMapper implements ExceptionMapper<Exception> {

@Inject
ObjectMapper objectMapper;

@Override
public Response toResponse(Exception exception) {
LOGGER.error("Failed to handle request", exception);

int code = 500;
if (exception instanceof WebApplicationException) {
code = ((WebApplicationException) exception).getResponse().getStatus();
}

ObjectNode exceptionJson = objectMapper.createObjectNode();
exceptionJson.put("exceptionType", exception.getClass().getName());
exceptionJson.put("code", code);

if (exception.getMessage() != null) {
exceptionJson.put("error", exception.getMessage());
}

return Response.status(code)
.entity(exceptionJson)
.build();
}

}
}
Loading

0 comments on commit add7f19

Please sign in to comment.