Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Lazy Exception in WildFly/Hibernate #199

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ bin/
data
.DS_Store
cargo-tracker-test-data
cargo-tracker-data
101 changes: 101 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@
<artifactId>jakarta.jakartaee-api</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.33</version>
<scope>provided</scope>
</dependency>

<!-- Required by Java SE 9+ -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
Expand Down Expand Up @@ -259,5 +266,99 @@
</plugins>
</build>
</profile>
<profile>
<id>wildfly</id>
<properties>
<wildfly.version>23.0.0.Final</wildfly.version>
<wildfly-maven-plugin.version>2.1.0.Beta1</wildfly-maven-plugin.version>
<wildfly-arquillian.version>3.0.1.Final</wildfly-arquillian.version>
<resteasy.version>3.14.0.Final</resteasy.version>
<jackson.version>2.11.3</jackson.version>
<!-- WildFly requires a DataSource impl -->
<db.driverClass>org.h2.jdbcx.JdbcDataSource</db.driverClass>
<!-- H2 file engine requires an absolute path, eg. ~/name, ./name or a full-qualified
path name (it should include driver name under Windows). -->
<db.jdbcUrl>jdbc:h2:file:./cargo-tracker-data/cargo-tracker-database;create=true</db.jdbcUrl>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>${resteasy.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson.version}</version>
<scope>provided</scope>
</dependency>
<!-- H2 per deployment -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.wildfly.arquillian</groupId>
<artifactId>wildfly-arquillian-container-embedded</artifactId>
<version>${wildfly-arquillian.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>
src/main/java-wildfly
</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${wildfly-maven-plugin.version}</version>
<configuration>
<server-config>standalone-full.xml</server-config>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.eclipse.cargotracker.infrastructure.routing.client;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper objectMapper;

public JacksonObjectMapperContextResolver() {
this.objectMapper =
new ObjectMapper()
.findAndRegisterModules()
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

@Override
public ObjectMapper getContext(Class<?> objectType) {
return objectMapper;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.eclipse.cargotracker.application.util;

import java.util.HashMap;
import java.util.Map;
import org.glassfish.jersey.server.ServerProperties;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.server.ServerProperties;
import java.util.HashMap;
import java.util.Map;

/** Jakarta REST configuration. */
@ApplicationPath("rest")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@
*/
@Entity
@NamedQuery(name = "Cargo.findAll", query = "Select c from Cargo c")
@NamedQuery(name = "Cargo.findAllWithItineraryLegs", query = "Select c from Cargo c join fetch c.itinerary.legs")
@NamedQuery(
name = "Cargo.findByTrackingId",
query = "Select c from Cargo c where c.trackingId = :trackingId")
@NamedQuery(
name = "Cargo.findByTrackingIdWithItineraryLegs",
query = "Select c from Cargo c join fetch c.itinerary.legs where c.trackingId = :trackingId")
public class Cargo implements Serializable {

private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ public interface CargoRepository {

List<Cargo> findAll();

Cargo findByTrackingIdWithItineraryLegs(TrackingId trackingId);

void store(Cargo cargo);

TrackingId nextTrackingId();

List<Cargo> findAllWithItineraryLegs();
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ public Cargo find(TrackingId trackingId) {
return cargo;
}

@Override
public Cargo findByTrackingIdWithItineraryLegs(TrackingId trackingId) {
Cargo cargo;

try {
cargo =
entityManager
.createNamedQuery("Cargo.findByTrackingIdWithItineraryLegs", Cargo.class)
.setParameter("trackingId", trackingId)
.getSingleResult();
} catch (NoResultException e) {
logger.log(Level.FINE, "Find called on non-existant tracking ID.", e);
cargo = null;
}

return cargo;
}

@Override
public void store(Cargo cargo) {
// TODO [Clean Code] See why cascade is not working correctly for legs.
Expand All @@ -66,4 +84,9 @@ public TrackingId nextTrackingId() {
public List<Cargo> findAll() {
return entityManager.createNamedQuery("Cargo.findAll", Cargo.class).getResultList();
}

@Override
public List<Cargo> findAllWithItineraryLegs() {
return entityManager.createNamedQuery("Cargo.findAllWithItineraryLegs", Cargo.class).getResultList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
Expand Down Expand Up @@ -44,7 +45,16 @@ public class ExternalRoutingService implements RoutingService {

@PostConstruct
public void init() {
graphTraversalResource = ClientBuilder.newClient().target(graphTraversalUrl);
Client jaxrsClient = ClientBuilder.newClient();
try {
Class<?> clazz =
Class.forName(
"org.eclipse.cargotracker.infrastructure.routing.client.JacksonObjectMapperContextResolver");
jaxrsClient.register(clazz);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
graphTraversalResource = jaxrsClient.target(graphTraversalUrl);
}

@Override
Expand All @@ -64,8 +74,7 @@ public List<Itinerary> fetchRoutesForSpecification(RouteSpecification routeSpeci
List<Itinerary> itineraries = new ArrayList<>();

// Use the specification to safe-guard against invalid itineraries
transitPaths
.stream()
transitPaths.stream()
.map(this::toItinerary)
.forEach(
itinerary -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.stream.Collectors;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;

import org.eclipse.cargotracker.application.BookingService;
import org.eclipse.cargotracker.domain.model.cargo.Cargo;
import org.eclipse.cargotracker.domain.model.cargo.CargoRepository;
Expand Down Expand Up @@ -59,7 +61,7 @@ public String bookNewCargo(String origin, String destination, LocalDate arrivalD

@Override
public CargoRoute loadCargoForRouting(String trackingId) {
Cargo cargo = cargoRepository.find(new TrackingId(trackingId));
Cargo cargo = cargoRepository.findByTrackingIdWithItineraryLegs(new TrackingId(trackingId));
return cargoRouteDtoAssembler.toDto(cargo);
}

Expand All @@ -86,7 +88,7 @@ public void changeDeadline(String trackingId, LocalDate arrivalDeadline) {
@Override
// TODO [DDD] Is this the correct DTO here?
public List<CargoRoute> listAllCargos() {
List<Cargo> cargos = cargoRepository.findAll();
List<Cargo> cargos = cargoRepository.findAllWithItineraryLegs();
List<CargoRoute> routes;

routes = cargos.stream().map(cargoRouteDtoAssembler::toDto).collect(Collectors.toList());
Expand All @@ -107,7 +109,7 @@ public List<String> listAllTrackingIds() {
@Override
public CargoStatus loadCargoForTracking(String trackingIdValue) {
TrackingId trackingId = new TrackingId(trackingIdValue);
Cargo cargo = cargoRepository.find(trackingId);
Cargo cargo = cargoRepository.findByTrackingIdWithItineraryLegs(trackingId);

if (cargo == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.eclipse.cargotracker.interfaces.booking.facade.BookingServiceFacade;
import org.eclipse.cargotracker.interfaces.booking.facade.dto.CargoRoute;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,13 @@ public String getCompletionTimePattern() {
}

@PostConstruct
@Transactional
public void init() {
List<Cargo> cargos = cargoRepository.findAll();
List<Cargo> cargos = cargoRepository.findAllWithItineraryLegs();

trackingIds = new ArrayList<>(cargos.size());

// List only routed cargo that is not claimed yet.
cargos
.stream()
cargos.stream()
.filter(
cargo ->
!cargo.getItinerary().getLegs().isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import javax.inject.Inject;
import javax.inject.Named;
import javax.json.bind.JsonbBuilder;

import org.eclipse.cargotracker.domain.model.cargo.Cargo;
import org.eclipse.cargotracker.domain.model.cargo.CargoRepository;
import org.eclipse.cargotracker.domain.model.cargo.TrackingId;
import org.eclipse.cargotracker.domain.model.handling.HandlingEvent;
import org.eclipse.cargotracker.domain.model.handling.HandlingEventRepository;


/**
* Backing bean for tracking cargo. This interface sits immediately on top of the domain layer,
* unlike the booking interface which has a facade and supporting DTOs in between.
Expand Down Expand Up @@ -69,7 +71,7 @@ public String getCargoAsJson() {
}

public void onTrackById() {
Cargo cargo = cargoRepository.find(new TrackingId(trackingId));
Cargo cargo = cargoRepository.findByTrackingIdWithItineraryLegs(new TrackingId(trackingId));

if (cargo != null) {
List<HandlingEvent> handlingEvents =
Expand Down
11 changes: 11 additions & 0 deletions src/main/webapp/WEB-INF/jboss-deployment-structure.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclusions>
</exclusions>
<dependencies>
<module name="com.fasterxml.jackson.datatype.jackson-datatype-jsr310" services="import"/>
<module name="com.fasterxml.jackson.datatype.jackson-datatype-jdk8" services="import"/>
</dependencies>
</deployment>
</jboss-deployment-structure>
20 changes: 20 additions & 0 deletions src/main/webapp/WEB-INF/jboss-logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Additional loggers to configure (the root logger is always configured)
loggers=
# Root logger configuration
logger.level=INFO
logger.handlers=FILE
# A handler configuration
handler.FILE=org.jboss.logmanager.handlers.FileHandler
handler.FILE.level=ALL
handler.FILE.formatter=PATTERN
handler.FILE.properties=append,autoFlush,enabled,suffix,fileName
handler.FILE.constructorProperties=fileName,append
handler.FILE.append=true
handler.FILE.autoFlush=true
handler.FILE.enabled=true
handler.FILE.fileName=${jboss.server.log.dir}/cargo-tracker.log
# The formatter to use
formatter.PATTERN=org.jboss.logmanager.formatters.PatternFormatter
formatter.PATTERN.properties=pattern
formatter.PATTERN.constructorProperties=pattern
formatter.PATTERN.pattern=%d %-5p %c: %m%n
Loading