├── Chapter05 └── hello-cloud │ ├── Dockerfile │ ├── deployment │ ├── config-map │ │ ├── hello-cloud-config.yaml │ │ └── hello-cloud.yaml │ └── hello-cloud.yaml │ ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── hello_cloud │ │ │ ├── JAXRSConfiguration.java │ │ │ ├── boundary │ │ │ ├── HelloResource.java │ │ │ └── HelloGreeter.java │ │ │ └── control │ │ │ ├── Config.java │ │ │ ├── HelloCloudProcessor.java │ │ │ └── ConfigurationExposer.java │ │ └── webapp │ │ └── WEB-INF │ │ └── beans.xml │ └── pom.xml ├── Chapter02 └── example │ ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ ├── factories │ │ │ ├── entity │ │ │ │ ├── GermanCar.java │ │ │ │ ├── LogBook.java │ │ │ │ ├── BMWCar.java │ │ │ │ ├── Color.java │ │ │ │ ├── PorscheCar.java │ │ │ │ ├── EngineType.java │ │ │ │ ├── ElectricBMWCar.java │ │ │ │ ├── BMW.java │ │ │ │ ├── Porsche.java │ │ │ │ └── Car.java │ │ │ ├── control │ │ │ │ ├── injection │ │ │ │ │ ├── GermanCarManufacturer.java │ │ │ │ │ ├── BMWCarManufacturer.java │ │ │ │ │ └── PorscheCarManufacturer.java │ │ │ │ └── producer │ │ │ │ │ └── BMWCarManufacturer.java │ │ │ └── boundary │ │ │ │ └── CarEnthusiast.java │ │ │ ├── cdi_producers │ │ │ ├── CarFactory.java │ │ │ └── CarFactoryProducer.java │ │ │ ├── configuration │ │ │ ├── entity │ │ │ │ ├── Car.java │ │ │ │ └── Specification.java │ │ │ ├── control │ │ │ │ ├── Config.java │ │ │ │ └── ConfigurationExposer.java │ │ │ └── boundary │ │ │ │ └── CarManufacturer.java │ │ │ ├── cross_cutting │ │ │ ├── entity │ │ │ │ ├── Car.java │ │ │ │ └── Specification.java │ │ │ ├── control │ │ │ │ ├── ProcessTracker.java │ │ │ │ ├── Tracked.java │ │ │ │ └── TrackingInterceptor.java │ │ │ └── boundary │ │ │ │ └── CarManufacturer.java │ │ │ ├── async │ │ │ ├── jaxrs_resource │ │ │ │ ├── User.java │ │ │ │ ├── UserStore.java │ │ │ │ ├── completion_stage │ │ │ │ │ └── UsersResource.java │ │ │ │ └── async_response │ │ │ │ │ └── UsersResource.java │ │ │ ├── cdi_events │ │ │ │ ├── entity │ │ │ │ │ ├── Car.java │ │ │ │ │ ├── Specification.java │ │ │ │ │ └── CarCreated.java │ │ │ │ ├── control │ │ │ │ │ ├── CarFactory.java │ │ │ │ │ └── CreatedCarListener.java │ │ │ │ └── boundary │ │ │ │ │ └── CarManufacturer.java │ │ │ ├── jaxrs_client │ │ │ │ ├── Forecast.java │ │ │ │ └── plain │ │ │ │ │ └── WeatherForecast.java │ │ │ ├── ejb │ │ │ │ └── Calculator.java │ │ │ └── executor_service │ │ │ │ └── Calculator.java │ │ │ ├── http_client │ │ │ ├── entity │ │ │ │ ├── OrderId.java │ │ │ │ ├── BeanType.java │ │ │ │ ├── Purchase.java │ │ │ │ └── CoffeeOrder.java │ │ │ └── control │ │ │ │ ├── FailureToNullInterceptor.java │ │ │ │ └── CoffeePurchaser.java │ │ │ ├── cdi_events │ │ │ ├── entity │ │ │ │ ├── Specification.java │ │ │ │ ├── CarCreated.java │ │ │ │ └── Car.java │ │ │ ├── control │ │ │ │ ├── CarFactory.java │ │ │ │ └── CreatedCarListener.java │ │ │ └── boundary │ │ │ │ └── CarManufacturer.java │ │ │ ├── core_components │ │ │ ├── entity │ │ │ │ ├── Color.java │ │ │ │ ├── EngineType.java │ │ │ │ ├── Specification.java │ │ │ │ └── Car.java │ │ │ ├── control │ │ │ │ ├── CarFactory.java │ │ │ │ └── CarStorage.java │ │ │ └── boundary │ │ │ │ └── CarManufacturer.java │ │ │ ├── decorators │ │ │ ├── cdi │ │ │ │ ├── CoffeeMaker.java │ │ │ │ ├── CoffeeConsumer.java │ │ │ │ ├── FilterCoffeeMaker.java │ │ │ │ └── CountingCoffeeMaker.java │ │ │ └── plain │ │ │ │ ├── Coffee.java │ │ │ │ ├── BlackCoffee.java │ │ │ │ ├── MilkCoffee.java │ │ │ │ ├── CreamCoffee.java │ │ │ │ ├── SugarCoffee.java │ │ │ │ └── CoffeeGarnish.java │ │ │ ├── rdbms │ │ │ ├── cars │ │ │ │ ├── entity │ │ │ │ │ ├── EngineType.java │ │ │ │ │ ├── SeatShape.java │ │ │ │ │ ├── SeatMaterial.java │ │ │ │ │ ├── Engine.java │ │ │ │ │ ├── Seat.java │ │ │ │ │ └── Car.java │ │ │ │ └── control │ │ │ │ │ └── CarManagement.java │ │ │ └── persons │ │ │ │ ├── entity │ │ │ │ ├── Person.java │ │ │ │ └── Address.java │ │ │ │ └── control │ │ │ │ └── PersonAdministration.java │ │ │ ├── facades │ │ │ ├── Coffee.java │ │ │ ├── Barista.java │ │ │ └── BaristaCoffeeShop.java │ │ │ ├── strategies │ │ │ ├── cdi │ │ │ │ ├── GreetingStrategy.java │ │ │ │ ├── Greeter.java │ │ │ │ ├── EveningGreetingStrategy.java │ │ │ │ ├── MorningGreetingStrategy.java │ │ │ │ ├── AfternoonGreetingStrategy.java │ │ │ │ ├── GreetingStrategyExposer.java │ │ │ │ └── GreetingStrategySelector.java │ │ │ └── plain │ │ │ │ └── Greeter.java │ │ │ ├── jsonb │ │ │ ├── Organization.java │ │ │ ├── OrganizationStore.java │ │ │ ├── OrganizationTypeAdapter.java │ │ │ └── Employee.java │ │ │ ├── sse │ │ │ ├── JAXRSConfiguration.java │ │ │ ├── entity │ │ │ │ └── DomainEvent.java │ │ │ └── control │ │ │ │ ├── EventProducer.java │ │ │ │ └── SseClient.java │ │ │ ├── jaxrs │ │ │ ├── JAXRSConfiguration.java │ │ │ ├── boundary │ │ │ │ ├── UserStore.java │ │ │ │ ├── UsersResource.java │ │ │ │ └── jsonp │ │ │ │ │ └── UsersResource.java │ │ │ ├── control │ │ │ │ ├── ValidUserValidator.java │ │ │ │ └── UserNameNotTakenValidator.java │ │ │ ├── ValidationExceptionMapper.java │ │ │ └── entity │ │ │ │ ├── ValidUser.java │ │ │ │ ├── UserNameNotTaken.java │ │ │ │ └── User.java │ │ │ ├── jaxrs_hypermedia │ │ │ ├── JAXRSConfiguration.java │ │ │ ├── boundary │ │ │ │ ├── BookStore.java │ │ │ │ ├── programmatic_builder │ │ │ │ │ ├── EntityBuilder.java │ │ │ │ │ └── BooksResource.java │ │ │ │ ├── declarative │ │ │ │ │ └── BooksResource.java │ │ │ │ └── programmatic │ │ │ │ │ └── BooksResource.java │ │ │ └── entity │ │ │ │ └── Book.java │ │ │ ├── ejb_exceptions │ │ │ └── GreetingException.java │ │ │ ├── rdbms_datasources │ │ │ ├── CarManagement.java │ │ │ ├── OrderDB.java │ │ │ ├── VehicleDB.java │ │ │ └── EntityManagerExposer.java │ │ │ ├── timed_execution │ │ │ ├── ejb │ │ │ │ └── PeriodicJob.java │ │ │ └── mses │ │ │ │ └── Periodic.java │ │ │ ├── websockets │ │ │ ├── ChatHandler.java │ │ │ ├── ChatMessage.java │ │ │ ├── ChatMessageEncoder.java │ │ │ ├── ChatMessageDecoder.java │ │ │ └── ChatServer.java │ │ │ ├── hypermedia_client │ │ │ ├── Entity.java │ │ │ ├── Book.java │ │ │ ├── EntityMapper.java │ │ │ └── BookClient.java │ │ │ └── singletons │ │ │ ├── container_concurrency │ │ │ └── CarStorage.java │ │ │ ├── bean_concurrency │ │ │ └── CarStorage.java │ │ │ └── application_startup │ │ │ └── CarStorage.java │ │ ├── webapp │ │ └── WEB-INF │ │ │ └── beans.xml │ │ └── resources │ │ └── META-INF │ │ └── persistence.xml │ └── pom.xml ├── Chapter07 ├── systemtest │ ├── src │ │ ├── main │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── cars │ │ │ │ ├── Color.java │ │ │ │ ├── EngineType.java │ │ │ │ └── Car.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── example │ │ │ └── cars │ │ │ ├── AssemblyLine.java │ │ │ └── CarCreationTest.java │ └── deployment │ │ └── assembly-line.yaml ├── cars-persistence │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── cars │ │ │ │ └── entity │ │ │ │ ├── PartType.java │ │ │ │ └── CarPart.java │ │ ├── webapp │ │ │ └── WEB-INF │ │ │ │ └── beans.xml │ │ └── resources │ │ │ └── META-INF │ │ │ └── persistence.xml │ │ └── test │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── cars │ │ │ └── entity │ │ │ └── CarPartIT.java │ │ └── resources │ │ └── META-INF │ │ └── persistence.xml ├── cars │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── cars │ │ │ │ ├── entity │ │ │ │ ├── Color.java │ │ │ │ ├── EngineType.java │ │ │ │ ├── Specification.java │ │ │ │ └── Car.java │ │ │ │ ├── control │ │ │ │ ├── Automation.java │ │ │ │ ├── AssemblyLine.java │ │ │ │ ├── empty │ │ │ │ │ └── CarFactory.java │ │ │ │ └── CarFactory.java │ │ │ │ └── boundary │ │ │ │ └── CarManufacturer.java │ │ ├── webapp │ │ │ └── WEB-INF │ │ │ │ └── beans.xml │ │ └── resources │ │ │ └── META-INF │ │ │ └── persistence.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── example │ │ └── cars │ │ ├── boundary │ │ ├── component_tests │ │ │ ├── package-info.java │ │ │ └── CarManufacturerTest.java │ │ ├── CarManufacturerComponent.java │ │ ├── ManufactureCarTest.java │ │ ├── CarManufacturerTest.java │ │ └── ManufactureCarIT.java │ │ ├── control │ │ └── CarFactoryComponent.java │ │ └── CarAssert.java ├── cars-mockito │ ├── src │ │ ├── main │ │ │ ├── java │ │ │ │ └── com │ │ │ │ │ └── example │ │ │ │ │ └── cars │ │ │ │ │ ├── entity │ │ │ │ │ ├── Color.java │ │ │ │ │ ├── EngineType.java │ │ │ │ │ ├── Car.java │ │ │ │ │ └── Specification.java │ │ │ │ │ ├── control │ │ │ │ │ ├── Automation.java │ │ │ │ │ ├── AssemblyLine.java │ │ │ │ │ └── CarFactory.java │ │ │ │ │ └── boundary │ │ │ │ │ └── CarManufacturer.java │ │ │ └── webapp │ │ │ │ └── WEB-INF │ │ │ │ └── beans.xml │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── example │ │ │ └── cars │ │ │ └── boundary │ │ │ ├── CarManufacturerTest.java │ │ │ └── ManufactureCarTest.java │ └── pom.xml ├── cars-cdi-unit │ ├── src │ │ ├── main │ │ │ ├── java │ │ │ │ └── com │ │ │ │ │ └── example │ │ │ │ │ └── cars │ │ │ │ │ ├── entity │ │ │ │ │ ├── Color.java │ │ │ │ │ ├── EngineType.java │ │ │ │ │ ├── Car.java │ │ │ │ │ └── Specification.java │ │ │ │ │ ├── control │ │ │ │ │ ├── Automation.java │ │ │ │ │ ├── AssemblyLine.java │ │ │ │ │ └── CarFactory.java │ │ │ │ │ └── boundary │ │ │ │ │ └── CarManufacturer.java │ │ │ └── webapp │ │ │ │ └── WEB-INF │ │ │ │ └── beans.xml │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── example │ │ │ └── cars │ │ │ └── boundary │ │ │ └── ManufactureCarIT.java │ └── pom.xml └── performance │ ├── src │ └── test │ │ └── scala │ │ └── com │ │ └── example │ │ └── cars │ │ └── CarCreationSimulation.scala │ └── pom.xml ├── Chapter09 └── cars │ ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── cars │ │ │ ├── entity │ │ │ ├── Color.java │ │ │ ├── EngineType.java │ │ │ ├── CarCreated.java │ │ │ ├── Car.java │ │ │ └── Specification.java │ │ │ ├── control │ │ │ ├── ErrorLogger.java │ │ │ ├── CarFactory.java │ │ │ ├── LoggerExposer.java │ │ │ └── ManufacturingStatistics.java │ │ │ ├── MetricsCollectorFilter.java │ │ │ └── boundary │ │ │ └── CarManufacturer.java │ │ └── webapp │ │ └── WEB-INF │ │ └── beans.xml │ └── pom.xml ├── Chapter01 ├── cars-gradle │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ └── gradle-wrapper.properties │ └── src │ │ └── main │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── cars │ │ │ ├── HelloResource.java │ │ │ └── JAXRSConfiguration.java │ │ └── webapp │ │ └── WEB-INF │ │ └── beans.xml └── cars-maven │ ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── cars │ │ │ ├── HelloResource.java │ │ │ └── JAXRSConfiguration.java │ │ └── webapp │ │ └── WEB-INF │ │ └── beans.xml │ └── pom.xml ├── Chapter08 ├── restaurant │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── restaurant │ │ │ │ ├── orders │ │ │ │ ├── entity │ │ │ │ │ ├── MealSpecification.java │ │ │ │ │ ├── OrderState.java │ │ │ │ │ ├── OrderInfo.java │ │ │ │ │ └── MealOrder.java │ │ │ │ ├── boundary │ │ │ │ │ ├── OrderService.java │ │ │ │ │ ├── OrdersQueryResource.java │ │ │ │ │ ├── OrderUpdateConsumer.java │ │ │ │ │ ├── OrdersCommandResource.java │ │ │ │ │ └── OrderEventHandler.java │ │ │ │ └── control │ │ │ │ │ └── MealOrders.java │ │ │ │ ├── JAXRSConfiguration.java │ │ │ │ └── events │ │ │ │ ├── entity │ │ │ │ ├── MealEvent.java │ │ │ │ ├── OrderCancelled.java │ │ │ │ ├── OrderStarted.java │ │ │ │ ├── MealPrepared.java │ │ │ │ ├── MealDelivered.java │ │ │ │ ├── MealPreparationStarted.java │ │ │ │ ├── MealFailedNoIngredients.java │ │ │ │ └── OrderPlaced.java │ │ │ │ └── control │ │ │ │ ├── EventProducer.java │ │ │ │ └── EventConsumer.java │ │ │ └── webapp │ │ │ └── WEB-INF │ │ │ └── beans.xml │ └── pom.xml └── resilience │ ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── resilience │ │ │ ├── JAXRSConfiguration.java │ │ │ ├── boundary │ │ │ ├── UserStore.java │ │ │ └── UsersResource.java │ │ │ ├── RejectedExecutionHandler.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ └── control │ │ │ ├── CircuitBreaker.java │ │ │ └── CustomExecutorConfiguration.java │ │ └── webapp │ │ └── WEB-INF │ │ └── beans.xml │ └── pom.xml ├── Chapter10 └── security │ ├── src │ └── main │ │ ├── webapp │ │ └── WEB-INF │ │ │ ├── beans.xml │ │ │ └── web.xml │ │ └── java │ │ └── com │ │ └── example │ │ └── security │ │ ├── SecurityConfig.java │ │ ├── CompanyProcesses.java │ │ ├── TestIdentityStore.java │ │ └── TestAuthenticationMechanism.java │ └── pom.xml ├── .gitattributes ├── .gitignore └── LICENSE /Chapter05/hello-cloud/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM jboss/wildfly:10.0.0.Final 2 | 3 | COPY target/hello-cloud.war /opt/jboss/wildfly/standalone/deployments/ 4 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/entity/GermanCar.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.entity; 2 | 3 | public interface GermanCar { 4 | } 5 | -------------------------------------------------------------------------------- /Chapter07/systemtest/src/main/java/com/example/cars/Color.java: -------------------------------------------------------------------------------- 1 | package com.example.cars; 2 | 3 | public enum Color { 4 | 5 | BLACK, RED, GREY 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter07/cars-persistence/src/main/java/com/example/cars/entity/PartType.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public enum PartType { 4 | CHASSIS 5 | } 6 | -------------------------------------------------------------------------------- /Chapter07/cars/src/main/java/com/example/cars/entity/Color.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public enum Color { 4 | 5 | BLACK, RED, GREY 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/java/com/example/cars/entity/Color.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public enum Color { 4 | 5 | BLACK, RED, GREY 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cdi_producers/CarFactory.java: -------------------------------------------------------------------------------- 1 | package com.example.cdi_producers; 2 | 3 | public class CarFactory { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/configuration/entity/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.configuration.entity; 2 | 3 | public class Car { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cross_cutting/entity/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.cross_cutting.entity; 2 | 3 | public class Car { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/entity/LogBook.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.entity; 2 | 3 | public class LogBook { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/src/main/java/com/example/cars/entity/Color.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public enum Color { 4 | 5 | BLACK, RED, GREY 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/jaxrs_resource/User.java: -------------------------------------------------------------------------------- 1 | package com.example.async.jaxrs_resource; 2 | 3 | public class User { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/entity/BMWCar.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.entity; 2 | 3 | @BMW 4 | public class BMWCar implements GermanCar { 5 | } 6 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/entity/Color.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.entity; 2 | 3 | public enum Color { 4 | 5 | BLACK, RED, GREY 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/http_client/entity/OrderId.java: -------------------------------------------------------------------------------- 1 | package com.example.http_client.entity; 2 | 3 | public class OrderId { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter07/cars-cdi-unit/src/main/java/com/example/cars/entity/Color.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public enum Color { 4 | 5 | BLACK, RED, GREY 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter07/systemtest/src/main/java/com/example/cars/EngineType.java: -------------------------------------------------------------------------------- 1 | package com.example.cars; 2 | 3 | public enum EngineType { 4 | 5 | DIESEL, PETROL, ELECTRIC 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/cdi_events/entity/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.async.cdi_events.entity; 2 | 3 | public class Car { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/http_client/entity/BeanType.java: -------------------------------------------------------------------------------- 1 | package com.example.http_client.entity; 2 | 3 | public class BeanType { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/http_client/entity/Purchase.java: -------------------------------------------------------------------------------- 1 | package com.example.http_client.entity; 2 | 3 | public class Purchase { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter07/cars/src/main/java/com/example/cars/entity/EngineType.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public enum EngineType { 4 | 5 | DIESEL, PETROL, ELECTRIC 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/java/com/example/cars/entity/EngineType.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public enum EngineType { 4 | 5 | DIESEL, PETROL, ELECTRIC 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cdi_events/entity/Specification.java: -------------------------------------------------------------------------------- 1 | package com.example.cdi_events.entity; 2 | 3 | public class Specification { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/entity/PorscheCar.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.entity; 2 | 3 | @Porsche 4 | public class PorscheCar implements GermanCar { 5 | } 6 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/configuration/entity/Specification.java: -------------------------------------------------------------------------------- 1 | package com.example.configuration.entity; 2 | 3 | public class Specification { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/core_components/entity/Color.java: -------------------------------------------------------------------------------- 1 | package com.example.core_components.entity; 2 | 3 | public enum Color { 4 | 5 | BLACK, RED, GREY 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cross_cutting/entity/Specification.java: -------------------------------------------------------------------------------- 1 | package com.example.cross_cutting.entity; 2 | 3 | public class Specification { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/decorators/cdi/CoffeeMaker.java: -------------------------------------------------------------------------------- 1 | package com.example.decorators.cdi; 2 | 3 | public interface CoffeeMaker { 4 | 5 | void makeCoffee(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter07/cars-cdi-unit/src/main/java/com/example/cars/entity/EngineType.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public enum EngineType { 4 | 5 | DIESEL, PETROL, ELECTRIC 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/src/main/java/com/example/cars/entity/EngineType.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public enum EngineType { 4 | 5 | DIESEL, PETROL, ELECTRIC 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/entity/EngineType.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.entity; 2 | 3 | public enum EngineType { 4 | 5 | DIESEL, PETROL, ELECTRIC 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms/cars/entity/EngineType.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms.cars.entity; 2 | 3 | public enum EngineType { 4 | 5 | DIESEL, PETROL, ELECTRIC 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms/cars/entity/SeatShape.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms.cars.entity; 2 | 3 | public enum SeatShape { 4 | 5 | NORMAL, COMFORT, SPORT 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/cdi_events/entity/Specification.java: -------------------------------------------------------------------------------- 1 | package com.example.async.cdi_events.entity; 2 | 3 | public class Specification { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter01/cars-gradle/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'war' 3 | } 4 | 5 | repositories { 6 | mavenCentral() 7 | } 8 | 9 | dependencies { 10 | providedCompile 'javax:javaee-api:8.0' 11 | } 12 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/core_components/entity/EngineType.java: -------------------------------------------------------------------------------- 1 | package com.example.core_components.entity; 2 | 3 | public enum EngineType { 4 | 5 | DIESEL, PETROL, ELECTRIC 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms/cars/entity/SeatMaterial.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms.cars.entity; 2 | 3 | public enum SeatMaterial { 4 | 5 | VINYL, FAUX_LEATHER, LEATHER 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/orders/entity/MealSpecification.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.orders.entity; 2 | 3 | public class MealSpecification { 4 | 5 | // ... 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/facades/Coffee.java: -------------------------------------------------------------------------------- 1 | package com.example.facades; 2 | 3 | public interface Coffee { 4 | 5 | double getCaffeine(); 6 | 7 | double getCalories(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/orders/entity/OrderState.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.orders.entity; 2 | 3 | public enum OrderState { 4 | 5 | PLACED, STARTED, DELIVERED, CANCELLED 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/decorators/plain/Coffee.java: -------------------------------------------------------------------------------- 1 | package com.example.decorators.plain; 2 | 3 | public interface Coffee { 4 | 5 | double getCaffeine(); 6 | 7 | double getCalories(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/entity/ElectricBMWCar.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.entity; 2 | 3 | import javax.enterprise.inject.Specializes; 4 | 5 | @Specializes 6 | public class ElectricBMWCar extends BMWCar { 7 | } 8 | -------------------------------------------------------------------------------- /Chapter07/cars/src/main/java/com/example/cars/control/Automation.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | public class Automation { 4 | 5 | public boolean isAutomated() { 6 | // ... 7 | return true; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Chapter07/cars-cdi-unit/src/main/java/com/example/cars/control/Automation.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | public class Automation { 4 | 5 | public boolean isAutomated() { 6 | // ... 7 | return true; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/src/main/java/com/example/cars/control/Automation.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | public class Automation { 4 | 5 | public boolean isAutomated() { 6 | // ... 7 | return true; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/java/com/example/cars/control/ErrorLogger.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | public class ErrorLogger { 4 | 5 | public void fatal(Throwable throwable) { 6 | throwable.printStackTrace(); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/jaxrs_resource/UserStore.java: -------------------------------------------------------------------------------- 1 | package com.example.async.jaxrs_resource; 2 | 3 | public class UserStore { 4 | 5 | public long create(User user) { 6 | // ... 7 | return 1; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/decorators/cdi/CoffeeConsumer.java: -------------------------------------------------------------------------------- 1 | package com.example.decorators.cdi; 2 | 3 | import javax.inject.Inject; 4 | 5 | public class CoffeeConsumer { 6 | 7 | @Inject 8 | CoffeeMaker coffeeMaker; 9 | 10 | // ... 11 | } 12 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/http_client/entity/CoffeeOrder.java: -------------------------------------------------------------------------------- 1 | package com.example.http_client.entity; 2 | 3 | public class CoffeeOrder { 4 | 5 | private OrderId id; 6 | 7 | public OrderId getId() { 8 | return id; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter05/hello-cloud/deployment/config-map/hello-cloud-config.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | kind: ConfigMap 3 | apiVersion: v1 4 | metadata: 5 | name: hello-cloud-config 6 | data: 7 | application.properties: | 8 | hello.greeting=Hello from Kubernetes 9 | hello.name=Java EE 10 | --- -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/decorators/cdi/FilterCoffeeMaker.java: -------------------------------------------------------------------------------- 1 | package com.example.decorators.cdi; 2 | 3 | public class FilterCoffeeMaker implements CoffeeMaker { 4 | 5 | @Override 6 | public void makeCoffee() { 7 | // brew coffee 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/control/injection/GermanCarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.control.injection; 2 | 3 | import com.example.factories.entity.GermanCar; 4 | 5 | public interface GermanCarManufacturer { 6 | GermanCar manufactureCar(); 7 | } 8 | -------------------------------------------------------------------------------- /Chapter07/cars/src/main/java/com/example/cars/control/AssemblyLine.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | import com.example.cars.entity.Specification; 4 | 5 | public class AssemblyLine { 6 | 7 | public void assemble(Specification specification) { 8 | // ... 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter01/cars-gradle/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Sep 17 09:27:09 CEST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.0.2-bin.zip 7 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/strategies/cdi/GreetingStrategy.java: -------------------------------------------------------------------------------- 1 | package com.example.strategies.cdi; 2 | 3 | import java.time.LocalTime; 4 | 5 | public interface GreetingStrategy { 6 | 7 | boolean isAppropriate(LocalTime localTime); 8 | 9 | String greet(String name); 10 | 11 | } 12 | 13 | -------------------------------------------------------------------------------- /Chapter07/cars-cdi-unit/src/main/java/com/example/cars/control/AssemblyLine.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | import com.example.cars.entity.Specification; 4 | 5 | public class AssemblyLine { 6 | 7 | public void assemble(Specification specification) { 8 | // ... 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/src/main/java/com/example/cars/control/AssemblyLine.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | import com.example.cars.entity.Specification; 4 | 5 | public class AssemblyLine { 6 | 7 | public void assemble(Specification specification) { 8 | // ... 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jsonb/Organization.java: -------------------------------------------------------------------------------- 1 | package com.example.jsonb; 2 | 3 | public class Organization { 4 | 5 | private long id; 6 | 7 | public long getId() { 8 | return id; 9 | } 10 | 11 | public void setId(long id) { 12 | this.id = id; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Chapter01/cars-gradle/src/main/java/com/example/cars/HelloResource.java: -------------------------------------------------------------------------------- 1 | package com.example.cars; 2 | 3 | import javax.ws.rs.GET; 4 | import javax.ws.rs.Path; 5 | 6 | @Path("hello") 7 | public class HelloResource { 8 | 9 | @GET 10 | public String hello() { 11 | return "Hello World"; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Chapter01/cars-maven/src/main/java/com/example/cars/HelloResource.java: -------------------------------------------------------------------------------- 1 | package com.example.cars; 2 | 3 | import javax.ws.rs.GET; 4 | import javax.ws.rs.Path; 5 | 6 | @Path("hello") 7 | public class HelloResource { 8 | 9 | @GET 10 | public String hello() { 11 | return "Hello World"; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/sse/JAXRSConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.example.sse; 2 | 3 | import javax.ws.rs.ApplicationPath; 4 | import javax.ws.rs.core.Application; 5 | 6 | @ApplicationPath("resources") 7 | public class JAXRSConfiguration extends Application { 8 | 9 | // nothing to configure 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter01/cars-maven/src/main/java/com/example/cars/JAXRSConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.example.cars; 2 | 3 | import javax.ws.rs.ApplicationPath; 4 | import javax.ws.rs.core.Application; 5 | 6 | @ApplicationPath("resources") 7 | public class JAXRSConfiguration extends Application { 8 | 9 | // nothing to configure 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs/JAXRSConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs; 2 | 3 | import javax.ws.rs.ApplicationPath; 4 | import javax.ws.rs.core.Application; 5 | 6 | @ApplicationPath("resources") 7 | public class JAXRSConfiguration extends Application { 8 | 9 | // nothing to configure 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter07/cars/src/test/java/com/example/cars/boundary/component_tests/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * This package solely exists to avoid naming conflicts. 3 | * The component tests usually reside directly in the corresponding package, 4 | * that is {@link com.example.cars.boundary}. 5 | */ 6 | package com.example.cars.boundary.component_tests; -------------------------------------------------------------------------------- /Chapter01/cars-gradle/src/main/java/com/example/cars/JAXRSConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.example.cars; 2 | 3 | import javax.ws.rs.ApplicationPath; 4 | import javax.ws.rs.core.Application; 5 | 6 | @ApplicationPath("resources") 7 | public class JAXRSConfiguration extends Application { 8 | 9 | // nothing to configure 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter05/hello-cloud/src/main/java/com/example/hello_cloud/JAXRSConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.example.hello_cloud; 2 | 3 | import javax.ws.rs.ApplicationPath; 4 | import javax.ws.rs.core.Application; 5 | 6 | @ApplicationPath("resources") 7 | public class JAXRSConfiguration extends Application { 8 | 9 | // nothing to configure 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/resilience/src/main/java/com/example/resilience/JAXRSConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.example.resilience; 2 | 3 | import javax.ws.rs.ApplicationPath; 4 | import javax.ws.rs.core.Application; 5 | 6 | @ApplicationPath("resources") 7 | public class JAXRSConfiguration extends Application { 8 | 9 | // nothing to configure 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/JAXRSConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant; 2 | 3 | import javax.ws.rs.ApplicationPath; 4 | import javax.ws.rs.core.Application; 5 | 6 | @ApplicationPath("resources") 7 | public class JAXRSConfiguration extends Application { 8 | 9 | // nothing to configure 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jsonb/OrganizationStore.java: -------------------------------------------------------------------------------- 1 | package com.example.jsonb; 2 | 3 | public class OrganizationStore { 4 | 5 | public Organization getOrganization(long id) { 6 | Organization organization = new Organization(); 7 | organization.setId(id); 8 | return organization; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter07/cars/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs_hypermedia/JAXRSConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs_hypermedia; 2 | 3 | import javax.ws.rs.ApplicationPath; 4 | import javax.ws.rs.core.Application; 5 | 6 | @ApplicationPath("resources") 7 | public class JAXRSConfiguration extends Application { 8 | 9 | // nothing to configure 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Chapter01/cars-gradle/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter01/cars-maven/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter05/hello-cloud/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter07/cars-cdi-unit/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter08/resilience/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/java/com/example/cars/control/CarFactory.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | import com.example.cars.entity.Car; 4 | import com.example.cars.entity.Specification; 5 | 6 | public class CarFactory { 7 | 8 | public Car createCar(Specification spec) { 9 | // ... 10 | return new Car(spec); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/ejb_exceptions/GreetingException.java: -------------------------------------------------------------------------------- 1 | package com.example.ejb_exceptions; 2 | 3 | import javax.ejb.ApplicationException; 4 | 5 | @ApplicationException 6 | public class GreetingException extends RuntimeException { 7 | 8 | public GreetingException(String message) { 9 | super(message); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Chapter07/cars-persistence/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter10/security/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/decorators/plain/BlackCoffee.java: -------------------------------------------------------------------------------- 1 | package com.example.decorators.plain; 2 | 3 | public class BlackCoffee implements Coffee { 4 | 5 | @Override 6 | public double getCaffeine() { 7 | return 100.0; 8 | } 9 | 10 | @Override 11 | public double getCalories() { 12 | return 0; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms_datasources/CarManagement.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms_datasources; 2 | 3 | import javax.inject.Inject; 4 | import javax.persistence.EntityManager; 5 | 6 | public class CarManagement { 7 | 8 | @Inject 9 | @VehicleDB 10 | EntityManager entityManager; 11 | 12 | // replaceEngine 13 | // ... 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/sse/entity/DomainEvent.java: -------------------------------------------------------------------------------- 1 | package com.example.sse.entity; 2 | 3 | public class DomainEvent { 4 | 5 | private final String contents; 6 | 7 | public DomainEvent(String contents) { 8 | this.contents = contents; 9 | } 10 | 11 | public String getContents() { 12 | return contents; 13 | } 14 | 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/java/com/example/cars/control/LoggerExposer.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | import javax.enterprise.inject.Produces; 4 | import java.util.function.Consumer; 5 | 6 | public class LoggerExposer { 7 | 8 | @Produces 9 | public Consumer fatalErrorConsumer() { 10 | return Throwable::printStackTrace; 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/jaxrs_client/Forecast.java: -------------------------------------------------------------------------------- 1 | package com.example.async.jaxrs_client; 2 | 3 | public class Forecast { 4 | 5 | private String forecast; 6 | 7 | public String getForecast() { 8 | return forecast; 9 | } 10 | 11 | public void setForecast(String forecast) { 12 | this.forecast = forecast; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cdi_events/control/CarFactory.java: -------------------------------------------------------------------------------- 1 | package com.example.cdi_events.control; 2 | 3 | import com.example.cdi_events.entity.Car; 4 | import com.example.cdi_events.entity.Specification; 5 | 6 | public class CarFactory { 7 | 8 | public Car createCar(Specification spec) { 9 | // ... 10 | return new Car(spec); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/decorators/plain/MilkCoffee.java: -------------------------------------------------------------------------------- 1 | package com.example.decorators.plain; 2 | 3 | public class MilkCoffee extends CoffeeGarnish { 4 | 5 | protected MilkCoffee(Coffee coffee) { 6 | super(coffee); 7 | } 8 | 9 | @Override 10 | public double getCalories() { 11 | return super.getCalories() + 20.0; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/decorators/plain/CreamCoffee.java: -------------------------------------------------------------------------------- 1 | package com.example.decorators.plain; 2 | 3 | public class CreamCoffee extends CoffeeGarnish { 4 | 5 | protected CreamCoffee(Coffee coffee) { 6 | super(coffee); 7 | } 8 | 9 | @Override 10 | public double getCalories() { 11 | return super.getCalories() + 100.0; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/decorators/plain/SugarCoffee.java: -------------------------------------------------------------------------------- 1 | package com.example.decorators.plain; 2 | 3 | public class SugarCoffee extends CoffeeGarnish { 4 | 5 | protected SugarCoffee(Coffee coffee) { 6 | super(coffee); 7 | } 8 | 9 | @Override 10 | public double getCalories() { 11 | return super.getCalories() + 30.0; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/entity/BMW.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.entity; 2 | 3 | import javax.inject.Qualifier; 4 | import java.lang.annotation.Documented; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | 8 | @Qualifier 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | public @interface BMW { 12 | } 13 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cross_cutting/control/ProcessTracker.java: -------------------------------------------------------------------------------- 1 | package com.example.cross_cutting.control; 2 | 3 | public class ProcessTracker { 4 | 5 | public void track(Category category) { 6 | // ... 7 | System.out.println("tracking: " + category); 8 | } 9 | 10 | public enum Category { 11 | MANUFACTURER, UNUSED 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/entity/Porsche.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.entity; 2 | 3 | import javax.inject.Qualifier; 4 | import java.lang.annotation.Documented; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | 8 | @Qualifier 9 | @Documented 10 | @Retention(RetentionPolicy.RUNTIME) 11 | public @interface Porsche { 12 | } 13 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/cdi_events/control/CarFactory.java: -------------------------------------------------------------------------------- 1 | package com.example.async.cdi_events.control; 2 | 3 | import com.example.async.cdi_events.entity.Car; 4 | import com.example.async.cdi_events.entity.Specification; 5 | 6 | public class CarFactory { 7 | 8 | public Car createCar(Specification spec) { 9 | // ... 10 | return new Car(); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms_datasources/OrderDB.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms_datasources; 2 | 3 | import javax.inject.Qualifier; 4 | import java.lang.annotation.Documented; 5 | import java.lang.annotation.Retention; 6 | 7 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 8 | 9 | @Qualifier 10 | @Documented 11 | @Retention(RUNTIME) 12 | public @interface OrderDB { 13 | } 14 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/strategies/cdi/Greeter.java: -------------------------------------------------------------------------------- 1 | package com.example.strategies.cdi; 2 | 3 | import javax.inject.Inject; 4 | import java.util.function.Function; 5 | 6 | public class Greeter { 7 | 8 | @Inject 9 | Function greetingStrategy; 10 | 11 | public String greet(String name) { 12 | return greetingStrategy.apply(name); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cdi_producers/CarFactoryProducer.java: -------------------------------------------------------------------------------- 1 | package com.example.cdi_producers; 2 | 3 | import javax.enterprise.inject.Produces; 4 | 5 | public class CarFactoryProducer { 6 | 7 | @Produces 8 | public CarFactory exposeCarFactory() { 9 | CarFactory factory = new CarFactory(); 10 | // use custom logic 11 | return factory; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms_datasources/VehicleDB.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms_datasources; 2 | 3 | import javax.inject.Qualifier; 4 | import java.lang.annotation.Documented; 5 | import java.lang.annotation.Retention; 6 | 7 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 8 | 9 | @Qualifier 10 | @Documented 11 | @Retention(RUNTIME) 12 | public @interface VehicleDB { 13 | } 14 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/core_components/control/CarFactory.java: -------------------------------------------------------------------------------- 1 | package com.example.core_components.control; 2 | 3 | 4 | import com.example.core_components.entity.Car; 5 | import com.example.core_components.entity.Specification; 6 | 7 | public class CarFactory { 8 | 9 | public Car createCar(Specification spec) { 10 | // ... 11 | return new Car(spec); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/java/com/example/cars/entity/CarCreated.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public class CarCreated { 4 | 5 | private final Specification specification; 6 | 7 | public CarCreated(Specification specification) { 8 | this.specification = specification; 9 | } 10 | 11 | public Specification getSpecification() { 12 | return specification; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/orders/entity/OrderInfo.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.orders.entity; 2 | 3 | import java.util.UUID; 4 | 5 | public class OrderInfo { 6 | 7 | private final UUID orderId; 8 | 9 | public OrderInfo(UUID orderId) { 10 | this.orderId = orderId; 11 | } 12 | 13 | public UUID getOrderId() { 14 | return orderId; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cdi_events/entity/CarCreated.java: -------------------------------------------------------------------------------- 1 | package com.example.cdi_events.entity; 2 | 3 | public class CarCreated { 4 | 5 | private final Specification specification; 6 | 7 | public CarCreated(Specification specification) { 8 | this.specification = specification; 9 | } 10 | 11 | public Specification getSpecification() { 12 | return specification; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/cdi_events/control/CreatedCarListener.java: -------------------------------------------------------------------------------- 1 | package com.example.async.cdi_events.control; 2 | 3 | import com.example.async.cdi_events.entity.CarCreated; 4 | 5 | import javax.enterprise.event.ObservesAsync; 6 | 7 | public class CreatedCarListener { 8 | 9 | public void onCarCreated(@ObservesAsync CarCreated event) { 10 | // handle event asynchronously 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/cdi_events/entity/CarCreated.java: -------------------------------------------------------------------------------- 1 | package com.example.async.cdi_events.entity; 2 | 3 | public class CarCreated { 4 | 5 | private final Specification specification; 6 | 7 | public CarCreated(Specification specification) { 8 | this.specification = specification; 9 | } 10 | 11 | public Specification getSpecification() { 12 | return specification; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter08/resilience/src/main/java/com/example/resilience/boundary/UserStore.java: -------------------------------------------------------------------------------- 1 | package com.example.resilience.boundary; 2 | 3 | import javax.ejb.Stateless; 4 | import javax.xml.registry.infomodel.User; 5 | import java.util.Collections; 6 | import java.util.List; 7 | 8 | @Stateless 9 | public class UserStore { 10 | 11 | public List getUsers() { 12 | // ... 13 | return Collections.emptyList(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Chapter05/hello-cloud/src/main/java/com/example/hello_cloud/boundary/HelloResource.java: -------------------------------------------------------------------------------- 1 | package com.example.hello_cloud.boundary; 2 | 3 | import javax.inject.Inject; 4 | import javax.ws.rs.GET; 5 | import javax.ws.rs.Path; 6 | 7 | @Path("hello") 8 | public class HelloResource { 9 | 10 | @Inject 11 | HelloGreeter greeter; 12 | 13 | @GET 14 | public String hello() { 15 | return greeter.processGreeting(); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/ejb/Calculator.java: -------------------------------------------------------------------------------- 1 | package com.example.async.ejb; 2 | 3 | import javax.ejb.Asynchronous; 4 | import javax.ejb.Stateless; 5 | import java.util.Random; 6 | 7 | @Stateless 8 | @Asynchronous 9 | public class Calculator { 10 | 11 | public double calculatePi(long decimalPlaces) { 12 | // this may run for a long time 13 | // ... 14 | return new Random().nextDouble(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/timed_execution/ejb/PeriodicJob.java: -------------------------------------------------------------------------------- 1 | package com.example.timed_execution.ejb; 2 | 3 | import javax.ejb.Schedule; 4 | import javax.ejb.Singleton; 5 | import javax.ejb.Startup; 6 | 7 | @Singleton 8 | @Startup 9 | public class PeriodicJob { 10 | 11 | @Schedule(minute = "*/10", hour = "*", persistent = false) 12 | public void executeJob() { 13 | // this is executed every 10 minutes 14 | } 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/boundary/CarEnthusiast.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.boundary; 2 | 3 | import com.example.factories.control.injection.GermanCarManufacturer; 4 | import com.example.factories.entity.BMW; 5 | 6 | import javax.ejb.Stateless; 7 | import javax.inject.Inject; 8 | 9 | @Stateless 10 | public class CarEnthusiast { 11 | 12 | @Inject 13 | @BMW 14 | GermanCarManufacturer carManufacturer; 15 | 16 | // ... 17 | } 18 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/strategies/cdi/EveningGreetingStrategy.java: -------------------------------------------------------------------------------- 1 | package com.example.strategies.cdi; 2 | 3 | import java.time.LocalTime; 4 | 5 | public class EveningGreetingStrategy implements GreetingStrategy { 6 | 7 | @Override 8 | public boolean isAppropriate(LocalTime localTime) { 9 | return false; 10 | } 11 | 12 | @Override 13 | public String greet(String name) { 14 | return "Good evening, " + name; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/strategies/cdi/MorningGreetingStrategy.java: -------------------------------------------------------------------------------- 1 | package com.example.strategies.cdi; 2 | 3 | import java.time.LocalTime; 4 | 5 | public class MorningGreetingStrategy implements GreetingStrategy { 6 | 7 | @Override 8 | public boolean isAppropriate(LocalTime localTime) { 9 | return false; 10 | } 11 | 12 | @Override 13 | public String greet(String name) { 14 | return "Good morning, " + name; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Chapter07/cars-persistence/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/control/injection/BMWCarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.control.injection; 2 | 3 | import com.example.factories.entity.BMW; 4 | import com.example.factories.entity.BMWCar; 5 | import com.example.factories.entity.GermanCar; 6 | 7 | @BMW 8 | public class BMWCarManufacturer implements GermanCarManufacturer { 9 | 10 | @Override 11 | public GermanCar manufactureCar() { 12 | return new BMWCar(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/strategies/cdi/AfternoonGreetingStrategy.java: -------------------------------------------------------------------------------- 1 | package com.example.strategies.cdi; 2 | 3 | import java.time.LocalTime; 4 | 5 | public class AfternoonGreetingStrategy implements GreetingStrategy { 6 | 7 | @Override 8 | public boolean isAppropriate(LocalTime localTime) { 9 | return false; 10 | } 11 | 12 | @Override 13 | public String greet(String name) { 14 | return "Good afternoon, " + name; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/websockets/ChatHandler.java: -------------------------------------------------------------------------------- 1 | package com.example.websockets; 2 | 3 | import javax.enterprise.context.ApplicationScoped; 4 | 5 | @ApplicationScoped 6 | public class ChatHandler { 7 | 8 | public void initiateChat() { 9 | // ... 10 | } 11 | 12 | public void store(ChatMessage message) { 13 | System.out.println("message stored: " + message); 14 | } 15 | 16 | public void closeChat() { 17 | // ... 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cdi_events/control/CreatedCarListener.java: -------------------------------------------------------------------------------- 1 | package com.example.cdi_events.control; 2 | 3 | import com.example.cdi_events.entity.CarCreated; 4 | import com.example.cdi_events.entity.Specification; 5 | 6 | import javax.enterprise.event.Observes; 7 | 8 | public class CreatedCarListener { 9 | 10 | public void onCarCreated(@Observes CarCreated event) { 11 | Specification spec = event.getSpecification(); 12 | // handle event 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/configuration/control/Config.java: -------------------------------------------------------------------------------- 1 | package com.example.configuration.control; 2 | 3 | import javax.enterprise.util.Nonbinding; 4 | import javax.inject.Qualifier; 5 | import java.lang.annotation.Documented; 6 | import java.lang.annotation.Retention; 7 | 8 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 9 | 10 | @Qualifier 11 | @Documented 12 | @Retention(RUNTIME) 13 | public @interface Config { 14 | 15 | @Nonbinding 16 | String value(); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/control/injection/PorscheCarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.control.injection; 2 | 3 | import com.example.factories.entity.GermanCar; 4 | import com.example.factories.entity.Porsche; 5 | import com.example.factories.entity.PorscheCar; 6 | 7 | @Porsche 8 | public class PorscheCarManufacturer implements GermanCarManufacturer { 9 | 10 | @Override 11 | public GermanCar manufactureCar() { 12 | return new PorscheCar(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter05/hello-cloud/src/main/java/com/example/hello_cloud/control/Config.java: -------------------------------------------------------------------------------- 1 | package com.example.hello_cloud.control; 2 | 3 | import javax.enterprise.util.Nonbinding; 4 | import javax.inject.Qualifier; 5 | import java.lang.annotation.Documented; 6 | import java.lang.annotation.Retention; 7 | 8 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 9 | 10 | @Qualifier 11 | @Documented 12 | @Retention(RUNTIME) 13 | public @interface Config { 14 | 15 | @Nonbinding 16 | String value(); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/control/producer/BMWCarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.control.producer; 2 | 3 | import com.example.factories.entity.BMW; 4 | import com.example.factories.entity.BMWCar; 5 | import com.example.factories.entity.GermanCar; 6 | 7 | import javax.enterprise.inject.Produces; 8 | 9 | public class BMWCarManufacturer { 10 | 11 | @Produces 12 | @BMW 13 | public GermanCar manufactureCar() { 14 | return new BMWCar(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Chapter07/cars/src/main/java/com/example/cars/control/empty/CarFactory.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control.empty; 2 | 3 | import com.example.cars.entity.Car; 4 | import com.example.cars.entity.Specification; 5 | 6 | public class CarFactory extends com.example.cars.control.CarFactory { 7 | 8 | @Override 9 | public Car createCar(Specification spec) { 10 | // example without dependencies, overrides CarFactory to make test example work 11 | // ... 12 | return new Car(spec); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/facades/Barista.java: -------------------------------------------------------------------------------- 1 | package com.example.facades; 2 | 3 | import javax.ejb.Stateless; 4 | import javax.inject.Inject; 5 | 6 | @Stateless 7 | public class Barista { 8 | 9 | @Inject 10 | BaristaCoffeeShop coffeeShop; 11 | 12 | Coffee makeCoffee() { 13 | // check temperature & humidity 14 | // calculate amount of beans & machine settings 15 | // fetch & grind beans 16 | // operate espresso machine 17 | 18 | return null; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/decorators/plain/CoffeeGarnish.java: -------------------------------------------------------------------------------- 1 | package com.example.decorators.plain; 2 | 3 | public class CoffeeGarnish implements Coffee { 4 | 5 | private final Coffee coffee; 6 | 7 | protected CoffeeGarnish(Coffee coffee) { 8 | this.coffee = coffee; 9 | } 10 | 11 | @Override 12 | public double getCaffeine() { 13 | return coffee.getCaffeine(); 14 | } 15 | 16 | @Override 17 | public double getCalories() { 18 | return coffee.getCalories(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs_hypermedia/boundary/BookStore.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs_hypermedia.boundary; 2 | 3 | import com.example.jaxrs_hypermedia.entity.Book; 4 | 5 | import java.util.List; 6 | 7 | import static java.util.Arrays.asList; 8 | 9 | public class BookStore { 10 | 11 | public List getBooks() { 12 | return asList(new Book()); 13 | } 14 | 15 | public Book getBook(long id) { 16 | Book book = new Book(); 17 | book.setId(id); 18 | return book; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/websockets/ChatMessage.java: -------------------------------------------------------------------------------- 1 | package com.example.websockets; 2 | 3 | public class ChatMessage { 4 | 5 | private final String contents; 6 | 7 | public ChatMessage(String contents) { 8 | this.contents = contents; 9 | } 10 | 11 | public String getContents() { 12 | return contents; 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return "ChatMessage{" + 18 | "contents='" + contents + '\'' + 19 | '}'; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/java/com/example/cars/entity/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public class Car { 4 | 5 | private Specification specification; 6 | 7 | private Car() { 8 | } 9 | 10 | public Car(Specification specification) { 11 | this.specification = specification; 12 | } 13 | 14 | public Specification getSpecification() { 15 | return specification; 16 | } 17 | 18 | public void setSpecification(Specification specification) { 19 | this.specification = specification; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Chapter07/cars/src/main/java/com/example/cars/control/CarFactory.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | import com.example.cars.entity.Car; 4 | import com.example.cars.entity.Specification; 5 | 6 | import javax.inject.Inject; 7 | 8 | public class CarFactory { 9 | 10 | @Inject 11 | Automation automation; 12 | 13 | @Inject 14 | AssemblyLine assemblyLine; 15 | 16 | public Car createCar(Specification spec) { 17 | assemblyLine.assemble(spec); 18 | automation.isAutomated(); 19 | 20 | return new Car(spec); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/java/com/example/cars/entity/Specification.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public class Specification { 4 | 5 | private EngineType engine; 6 | private Color color; 7 | 8 | public EngineType getEngine() { 9 | return engine; 10 | } 11 | 12 | public void setEngine(EngineType engine) { 13 | this.engine = engine; 14 | } 15 | 16 | public Color getColor() { 17 | return color; 18 | } 19 | 20 | public void setColor(Color color) { 21 | this.color = color; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cdi_events/entity/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.cdi_events.entity; 2 | 3 | public class Car { 4 | 5 | private Specification specification; 6 | 7 | private Car() { 8 | } 9 | 10 | public Car(Specification specification) { 11 | this.specification = specification; 12 | } 13 | 14 | public Specification getSpecification() { 15 | return specification; 16 | } 17 | 18 | public void setSpecification(Specification specification) { 19 | this.specification = specification; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Chapter05/hello-cloud/src/main/java/com/example/hello_cloud/boundary/HelloGreeter.java: -------------------------------------------------------------------------------- 1 | package com.example.hello_cloud.boundary; 2 | 3 | import com.example.hello_cloud.control.Config; 4 | 5 | import javax.inject.Inject; 6 | import javax.ws.rs.GET; 7 | 8 | public class HelloGreeter { 9 | 10 | @Inject 11 | @Config("hello.greeting") 12 | String greeting; 13 | 14 | @Inject 15 | @Config("hello.name") 16 | String greetingName; 17 | 18 | @GET 19 | public String processGreeting() { 20 | return greeting + ", " + greetingName; 21 | } 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Chapter07/cars-cdi-unit/src/main/java/com/example/cars/control/CarFactory.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | import com.example.cars.entity.Car; 4 | import com.example.cars.entity.Specification; 5 | 6 | import javax.inject.Inject; 7 | 8 | public class CarFactory { 9 | 10 | @Inject 11 | Automation automation; 12 | 13 | @Inject 14 | AssemblyLine assemblyLine; 15 | 16 | public Car createCar(Specification spec) { 17 | assemblyLine.assemble(spec); 18 | automation.isAutomated(); 19 | 20 | return new Car(spec); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/src/main/java/com/example/cars/control/CarFactory.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | import com.example.cars.entity.Car; 4 | import com.example.cars.entity.Specification; 5 | 6 | import javax.inject.Inject; 7 | 8 | public class CarFactory { 9 | 10 | @Inject 11 | Automation automation; 12 | 13 | @Inject 14 | AssemblyLine assemblyLine; 15 | 16 | public Car createCar(Specification spec) { 17 | assemblyLine.assemble(spec); 18 | automation.isAutomated(); 19 | 20 | return new Car(spec); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms_datasources/EntityManagerExposer.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms_datasources; 2 | 3 | import javax.enterprise.inject.Produces; 4 | import javax.persistence.EntityManager; 5 | import javax.persistence.PersistenceContext; 6 | 7 | public class EntityManagerExposer { 8 | 9 | @Produces 10 | @VehicleDB 11 | @PersistenceContext(unitName = "vehicle") 12 | private EntityManager vehicleEntityManager; 13 | 14 | @Produces 15 | @OrderDB 16 | @PersistenceContext(unitName = "order") 17 | private EntityManager orderEntityManager; 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/events/entity/MealEvent.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.events.entity; 2 | 3 | import java.time.Instant; 4 | import java.util.Objects; 5 | 6 | public abstract class MealEvent { 7 | 8 | private final Instant instant; 9 | 10 | protected MealEvent() { 11 | instant = Instant.now(); 12 | } 13 | 14 | protected MealEvent(Instant instant) { 15 | Objects.requireNonNull(instant); 16 | this.instant = instant; 17 | } 18 | 19 | public Instant getInstant() { 20 | return instant; 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | com.example.decorators.cdi.CountingCoffeeMaker 8 | 9 | 10 | com.example.cross_cutting.control.TrackingInterceptor 11 | 12 | 13 | -------------------------------------------------------------------------------- /Chapter08/resilience/src/main/java/com/example/resilience/RejectedExecutionHandler.java: -------------------------------------------------------------------------------- 1 | package com.example.resilience; 2 | 3 | import javax.ws.rs.core.Response; 4 | import javax.ws.rs.ext.ExceptionMapper; 5 | import javax.ws.rs.ext.Provider; 6 | import java.util.concurrent.RejectedExecutionException; 7 | 8 | @Provider 9 | public class RejectedExecutionHandler implements ExceptionMapper { 10 | 11 | @Override 12 | public Response toResponse(RejectedExecutionException exception) { 13 | return Response.status(Response.Status.SERVICE_UNAVAILABLE).build(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/events/entity/OrderCancelled.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.events.entity; 2 | 3 | import java.util.UUID; 4 | 5 | public class OrderCancelled extends MealEvent { 6 | 7 | private final UUID orderId; 8 | private final String reason; 9 | 10 | public OrderCancelled(UUID orderId, String reason) { 11 | this.orderId = orderId; 12 | this.reason = reason; 13 | } 14 | 15 | public UUID getOrderId() { 16 | return orderId; 17 | } 18 | 19 | public String getReason() { 20 | return reason; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/events/entity/OrderStarted.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.events.entity; 2 | 3 | import java.time.Instant; 4 | import java.util.UUID; 5 | 6 | public class OrderStarted extends MealEvent { 7 | 8 | private final UUID orderId; 9 | 10 | public OrderStarted(UUID orderId) { 11 | this.orderId = orderId; 12 | } 13 | 14 | public OrderStarted(UUID orderId, Instant instant) { 15 | super(instant); 16 | this.orderId = orderId; 17 | } 18 | 19 | public UUID getOrderId() { 20 | return orderId; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/events/entity/MealPrepared.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.events.entity; 2 | 3 | import java.time.Instant; 4 | import java.util.UUID; 5 | 6 | public class MealPrepared extends MealEvent { 7 | 8 | private final UUID orderId; 9 | 10 | public MealPrepared(UUID orderId) { 11 | this.orderId = orderId; 12 | } 13 | 14 | public MealPrepared(UUID orderId, Instant instant) { 15 | super(instant); 16 | this.orderId = orderId; 17 | } 18 | 19 | public UUID getOrderId() { 20 | return orderId; 21 | } 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Chapter07/systemtest/deployment/assembly-line.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | kind: Service 3 | apiVersion: v1 4 | metadata: 5 | name: assembly-line 6 | spec: 7 | selector: 8 | app: assembly-line 9 | ports: 10 | - port: 8080 11 | --- 12 | kind: Deployment 13 | apiVersion: apps/v1beta1 14 | metadata: 15 | name: assembly-line 16 | spec: 17 | replicas: 1 18 | template: 19 | metadata: 20 | labels: 21 | app: assembly-line 22 | spec: 23 | containers: 24 | - name: assembly-line 25 | image: docker.example.com/wiremock:2.6 26 | imagePullPolicy: IfNotPresent 27 | restartPolicy: Always 28 | --- -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/events/entity/MealDelivered.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.events.entity; 2 | 3 | import java.time.Instant; 4 | import java.util.UUID; 5 | 6 | public class MealDelivered extends MealEvent { 7 | 8 | private final UUID orderId; 9 | 10 | public MealDelivered(UUID orderId) { 11 | this.orderId = orderId; 12 | } 13 | 14 | public MealDelivered(UUID orderId, Instant instant) { 15 | super(instant); 16 | this.orderId = orderId; 17 | } 18 | 19 | public UUID getOrderId() { 20 | return orderId; 21 | } 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cross_cutting/boundary/CarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.cross_cutting.boundary; 2 | 3 | import com.example.cross_cutting.control.ProcessTracker; 4 | import com.example.cross_cutting.control.Tracked; 5 | import com.example.cross_cutting.entity.Car; 6 | import com.example.cross_cutting.entity.Specification; 7 | 8 | import javax.ejb.Stateless; 9 | 10 | @Stateless 11 | public class CarManufacturer { 12 | 13 | @Tracked(ProcessTracker.Category.MANUFACTURER) 14 | public Car manufactureCar(Specification spec) { 15 | // ... 16 | return new Car(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/http_client/control/FailureToNullInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.example.http_client.control; 2 | 3 | import javax.interceptor.AroundInvoke; 4 | import javax.interceptor.Interceptor; 5 | import javax.interceptor.InvocationContext; 6 | 7 | @Interceptor 8 | public class FailureToNullInterceptor { 9 | 10 | @AroundInvoke 11 | public Object aroundInvoke(InvocationContext context) { 12 | try { 13 | return context.proceed(); 14 | } catch (Exception e) { 15 | // record exception 16 | // ... 17 | return null; 18 | } 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/events/entity/MealPreparationStarted.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.events.entity; 2 | 3 | import java.time.Instant; 4 | import java.util.UUID; 5 | 6 | public class MealPreparationStarted extends MealEvent { 7 | 8 | private final UUID orderId; 9 | 10 | public MealPreparationStarted(UUID orderId) { 11 | this.orderId = orderId; 12 | } 13 | 14 | public MealPreparationStarted(UUID orderId, Instant instant) { 15 | super(instant); 16 | this.orderId = orderId; 17 | } 18 | 19 | public UUID getOrderId() { 20 | return orderId; 21 | } 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/events/entity/MealFailedNoIngredients.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.events.entity; 2 | 3 | import java.time.Instant; 4 | import java.util.UUID; 5 | 6 | public class MealFailedNoIngredients extends MealEvent { 7 | 8 | private final UUID orderId; 9 | 10 | public MealFailedNoIngredients(UUID orderId) { 11 | this.orderId = orderId; 12 | } 13 | 14 | public MealFailedNoIngredients(UUID orderId, Instant instant) { 15 | super(instant); 16 | this.orderId = orderId; 17 | } 18 | 19 | public UUID getOrderId() { 20 | return orderId; 21 | } 22 | 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Chapter07/cars/src/test/java/com/example/cars/control/CarFactoryComponent.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | import com.example.cars.entity.Specification; 4 | 5 | import static org.mockito.Mockito.*; 6 | 7 | public class CarFactoryComponent extends CarFactory { 8 | 9 | public CarFactoryComponent() { 10 | automation = mock(Automation.class); 11 | assemblyLine = mock(AssemblyLine.class); 12 | when(automation.isAutomated()).thenReturn(true); 13 | } 14 | 15 | public void verifyCarCreation(Specification spec) { 16 | verify(assemblyLine).assemble(spec); 17 | verify(automation).isAutomated(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/configuration/boundary/CarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.configuration.boundary; 2 | 3 | import com.example.configuration.control.Config; 4 | import com.example.configuration.entity.Car; 5 | import com.example.configuration.entity.Specification; 6 | 7 | import javax.ejb.Stateless; 8 | import javax.inject.Inject; 9 | 10 | @Stateless 11 | public class CarManufacturer { 12 | 13 | @Inject 14 | @Config("car.default.color") 15 | String defaultColor; 16 | 17 | public Car manufactureCar(Specification spec) { 18 | // use defaultColor 19 | 20 | // ... 21 | return new Car(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/strategies/cdi/GreetingStrategyExposer.java: -------------------------------------------------------------------------------- 1 | package com.example.strategies.cdi; 2 | 3 | import javax.enterprise.inject.Produces; 4 | import java.util.function.Function; 5 | 6 | public class GreetingStrategyExposer { 7 | 8 | private Function formalGreeting = name -> "Dear " + name; 9 | private Function informalGreeting = name -> "Hey " + name; 10 | 11 | @Produces 12 | public Function exposeStrategy() { 13 | // select a strategy 14 | Function strategy = formalGreeting; 15 | 16 | // ... 17 | return strategy; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/websockets/ChatMessageEncoder.java: -------------------------------------------------------------------------------- 1 | package com.example.websockets; 2 | 3 | import javax.websocket.EncodeException; 4 | import javax.websocket.Encoder; 5 | import javax.websocket.EndpointConfig; 6 | 7 | public class ChatMessageEncoder implements Encoder.Text { 8 | 9 | @Override 10 | public String encode(ChatMessage object) throws EncodeException { 11 | return object.getContents(); 12 | } 13 | 14 | @Override 15 | public void init(EndpointConfig config) { 16 | // nothing to do 17 | } 18 | 19 | @Override 20 | public void destroy() { 21 | // nothing to do 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/hypermedia_client/Entity.java: -------------------------------------------------------------------------------- 1 | package com.example.hypermedia_client; 2 | 3 | import javax.json.JsonObject; 4 | import java.net.URI; 5 | 6 | public class Entity { 7 | 8 | private URI uri; 9 | private JsonObject properties; 10 | 11 | // actions, links, etc. 12 | 13 | public URI getUri() { 14 | return uri; 15 | } 16 | 17 | public void setUri(URI uri) { 18 | this.uri = uri; 19 | } 20 | 21 | public JsonObject getProperties() { 22 | return properties; 23 | } 24 | 25 | public void setProperties(JsonObject properties) { 26 | this.properties = properties; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/sse/control/EventProducer.java: -------------------------------------------------------------------------------- 1 | package com.example.sse.control; 2 | 3 | import com.example.sse.entity.DomainEvent; 4 | 5 | import javax.ejb.Schedule; 6 | import javax.ejb.Singleton; 7 | import javax.ejb.Startup; 8 | import javax.enterprise.event.Event; 9 | import javax.inject.Inject; 10 | import java.time.Instant; 11 | 12 | @Startup 13 | @Singleton 14 | public class EventProducer { 15 | 16 | @Inject 17 | Event domainEvents; 18 | 19 | @Schedule(second = "*/10", minute = "*", hour = "*") 20 | public void produceEvent() { 21 | domainEvents.fire(new DomainEvent("Hello, " + Instant.now())); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/core_components/control/CarStorage.java: -------------------------------------------------------------------------------- 1 | package com.example.core_components.control; 2 | 3 | import com.example.core_components.entity.Car; 4 | 5 | import javax.ejb.Lock; 6 | import javax.ejb.LockType; 7 | import javax.ejb.Singleton; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | @Singleton 12 | public class CarStorage { 13 | 14 | private final Map cars = new HashMap<>(); 15 | 16 | @Lock 17 | public void store(Car car) { 18 | cars.put(car.getId(), car); 19 | } 20 | 21 | @Lock(LockType.READ) 22 | public Car retrieve(String id) { 23 | return cars.get(id); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs/boundary/UserStore.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs.boundary; 2 | 3 | import com.example.jaxrs.entity.User; 4 | 5 | import java.util.List; 6 | 7 | import static java.util.Arrays.asList; 8 | 9 | public class UserStore { 10 | 11 | public List getUsers() { 12 | return asList(new User(1L, "duke")); 13 | } 14 | 15 | public long create(User user) { 16 | // ... 17 | return 1L; 18 | } 19 | 20 | public User getUser(long id) { 21 | return new User(id, "duke"); 22 | } 23 | 24 | public boolean isNameTaken(String string) { 25 | // ... 26 | return false; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs/control/ValidUserValidator.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs.control; 2 | 3 | import com.example.jaxrs.entity.ValidUser; 4 | 5 | import javax.json.JsonObject; 6 | import javax.validation.ConstraintValidator; 7 | import javax.validation.ConstraintValidatorContext; 8 | 9 | public class ValidUserValidator implements ConstraintValidator { 10 | 11 | public void initialize(ValidUser constraint) { 12 | // nothing to do 13 | } 14 | 15 | public boolean isValid(JsonObject json, ConstraintValidatorContext context) { 16 | // ... 17 | return json.getString("name", null) != null; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/events/entity/OrderPlaced.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.events.entity; 2 | 3 | import com.example.restaurant.orders.entity.OrderInfo; 4 | 5 | import java.time.Instant; 6 | 7 | public class OrderPlaced extends MealEvent { 8 | 9 | private final OrderInfo orderInfo; 10 | 11 | public OrderPlaced(OrderInfo orderInfo) { 12 | this.orderInfo = orderInfo; 13 | } 14 | 15 | public OrderPlaced(OrderInfo orderInfo, Instant instant) { 16 | super(instant); 17 | this.orderInfo = orderInfo; 18 | } 19 | 20 | public OrderInfo getOrderInfo() { 21 | return orderInfo; 22 | } 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/singletons/container_concurrency/CarStorage.java: -------------------------------------------------------------------------------- 1 | package com.example.singletons.container_concurrency; 2 | 3 | import com.example.core_components.entity.Car; 4 | 5 | import javax.ejb.Lock; 6 | import javax.ejb.LockType; 7 | import javax.ejb.Singleton; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | @Singleton 12 | public class CarStorage { 13 | 14 | private final Map cars = new HashMap<>(); 15 | 16 | @Lock 17 | public void store(Car car) { 18 | cars.put(car.getId(), car); 19 | } 20 | 21 | @Lock(LockType.READ) 22 | public Car retrieve(String id) { 23 | return cars.get(id); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Chapter07/cars/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/timed_execution/mses/Periodic.java: -------------------------------------------------------------------------------- 1 | package com.example.timed_execution.mses; 2 | 3 | import javax.annotation.Resource; 4 | import javax.enterprise.concurrent.ManagedScheduledExecutorService; 5 | import javax.enterprise.context.ApplicationScoped; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | @ApplicationScoped 9 | public class Periodic { 10 | 11 | @Resource 12 | ManagedScheduledExecutorService mses; 13 | 14 | public void startAsyncJobs() { 15 | mses.schedule(this::execute, 1, TimeUnit.SECONDS); 16 | mses.scheduleAtFixedRate(this::execute, 6, 1, TimeUnit.SECONDS); 17 | } 18 | 19 | private void execute() { 20 | // ... 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Chapter08/resilience/src/main/java/com/example/resilience/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.resilience.entity; 2 | 3 | public class User { 4 | 5 | private long id; 6 | private String name; 7 | private String email; 8 | 9 | public long getId() { 10 | return id; 11 | } 12 | 13 | public void setId(long id) { 14 | this.id = id; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | 25 | public String getEmail() { 26 | return email; 27 | } 28 | 29 | public void setEmail(String email) { 30 | this.email = email; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Chapter10/security/src/main/java/com/example/security/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.security; 2 | 3 | import javax.security.enterprise.authentication.mechanism.http.BasicAuthenticationMechanismDefinition; 4 | import javax.security.enterprise.identitystore.DatabaseIdentityStoreDefinition; 5 | import javax.security.enterprise.identitystore.IdentityStore; 6 | 7 | @BasicAuthenticationMechanismDefinition(realmName = "car-realm") 8 | @DatabaseIdentityStoreDefinition( 9 | dataSourceLookup = "java:comp/UserDS", 10 | callerQuery = "select password from users where name = ?", 11 | useFor = IdentityStore.ValidationType.VALIDATE 12 | ) 13 | public class SecurityConfig { 14 | 15 | // nothing to configure 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms/cars/control/CarManagement.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms.cars.control; 2 | 3 | import com.example.rdbms.cars.entity.Car; 4 | import com.example.rdbms.cars.entity.Engine; 5 | 6 | import javax.persistence.EntityManager; 7 | import javax.persistence.PersistenceContext; 8 | 9 | public class CarManagement { 10 | 11 | @PersistenceContext 12 | EntityManager entityManager; 13 | 14 | public void replaceEngine(long carIdentifier, Engine engine) { 15 | Car car = entityManager.find(Car.class, carIdentifier); 16 | car.replaceEngine(engine); 17 | // merge operation is applied on the car and all cascading relations 18 | entityManager.merge(car); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | jdbc/VehicleDB 8 | 9 | 10 | jdbc/OrderDB 11 | 12 | -------------------------------------------------------------------------------- /Chapter07/cars/src/test/java/com/example/cars/boundary/CarManufacturerComponent.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary; 2 | 3 | import com.example.cars.control.CarFactoryComponent; 4 | import com.example.cars.entity.Car; 5 | import org.mockito.Mockito; 6 | 7 | import javax.persistence.EntityManager; 8 | 9 | import static org.mockito.Mockito.verify; 10 | 11 | public class CarManufacturerComponent extends CarManufacturer { 12 | 13 | public CarManufacturerComponent(CarFactoryComponent carFactoryComponent) { 14 | entityManager = Mockito.mock(EntityManager.class); 15 | carFactory = carFactoryComponent; 16 | } 17 | 18 | public void verifyManufacture(Car car) { 19 | verify(entityManager).merge(car); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/singletons/bean_concurrency/CarStorage.java: -------------------------------------------------------------------------------- 1 | package com.example.singletons.bean_concurrency; 2 | 3 | import com.example.core_components.entity.Car; 4 | 5 | import javax.ejb.ConcurrencyManagement; 6 | import javax.ejb.ConcurrencyManagementType; 7 | import javax.ejb.Singleton; 8 | import java.util.Map; 9 | import java.util.concurrent.ConcurrentHashMap; 10 | 11 | @Singleton 12 | @ConcurrencyManagement(ConcurrencyManagementType.BEAN) 13 | public class CarStorage { 14 | 15 | private final Map cars = new ConcurrentHashMap<>(); 16 | 17 | public void store(Car car) { 18 | cars.put(car.getId(), car); 19 | } 20 | 21 | public Car retrieve(String id) { 22 | return cars.get(id); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/decorators/cdi/CountingCoffeeMaker.java: -------------------------------------------------------------------------------- 1 | package com.example.decorators.cdi; 2 | 3 | import javax.decorator.Decorator; 4 | import javax.decorator.Delegate; 5 | import javax.enterprise.inject.Any; 6 | import javax.inject.Inject; 7 | 8 | @Decorator 9 | public class CountingCoffeeMaker implements CoffeeMaker { 10 | 11 | private static final int MAX_COFFEES = 3; 12 | private int count; 13 | 14 | @Inject 15 | @Any 16 | @Delegate 17 | CoffeeMaker coffeeMaker; 18 | 19 | @Override 20 | public void makeCoffee() { 21 | if (count >= MAX_COFFEES) 22 | throw new IllegalStateException("Reached maximum coffee limit."); 23 | count++; 24 | 25 | coffeeMaker.makeCoffee(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Chapter07/cars/src/main/java/com/example/cars/boundary/CarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary; 2 | 3 | import com.example.cars.control.CarFactory; 4 | import com.example.cars.entity.Car; 5 | import com.example.cars.entity.Specification; 6 | 7 | import javax.ejb.Stateless; 8 | import javax.inject.Inject; 9 | import javax.persistence.EntityManager; 10 | import javax.persistence.PersistenceContext; 11 | 12 | @Stateless 13 | public class CarManufacturer { 14 | 15 | @Inject 16 | CarFactory carFactory; 17 | 18 | @PersistenceContext 19 | EntityManager entityManager; 20 | 21 | public Car manufactureCar(Specification spec) { 22 | Car car = carFactory.createCar(spec); 23 | entityManager.merge(car); 24 | return car; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/core_components/boundary/CarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.core_components.boundary; 2 | 3 | import com.example.core_components.control.CarFactory; 4 | import com.example.core_components.control.CarStorage; 5 | import com.example.core_components.entity.Car; 6 | import com.example.core_components.entity.Specification; 7 | 8 | import javax.ejb.Stateless; 9 | import javax.inject.Inject; 10 | 11 | @Stateless 12 | public class CarManufacturer { 13 | 14 | @Inject 15 | CarFactory carFactory; 16 | 17 | @Inject 18 | CarStorage carStorage; 19 | 20 | public Car manufactureCar(Specification spec) { 21 | Car car = carFactory.createCar(spec); 22 | carStorage.store(car); 23 | return car; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cross_cutting/control/Tracked.java: -------------------------------------------------------------------------------- 1 | package com.example.cross_cutting.control; 2 | 3 | import javax.enterprise.util.Nonbinding; 4 | import javax.interceptor.InterceptorBinding; 5 | import java.lang.annotation.Documented; 6 | import java.lang.annotation.Inherited; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.Target; 9 | 10 | import static java.lang.annotation.ElementType.METHOD; 11 | import static java.lang.annotation.ElementType.TYPE; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @InterceptorBinding 15 | @Inherited 16 | @Documented 17 | @Target({TYPE, METHOD}) 18 | @Retention(RUNTIME) 19 | public @interface Tracked { 20 | 21 | @Nonbinding 22 | ProcessTracker.Category value(); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs/control/UserNameNotTakenValidator.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs.control; 2 | 3 | import com.example.jaxrs.boundary.UserStore; 4 | import com.example.jaxrs.entity.UserNameNotTaken; 5 | 6 | import javax.inject.Inject; 7 | import javax.validation.ConstraintValidator; 8 | import javax.validation.ConstraintValidatorContext; 9 | 10 | public class UserNameNotTakenValidator implements ConstraintValidator { 11 | 12 | @Inject 13 | UserStore userStore; 14 | 15 | public void initialize(UserNameNotTaken constraint) { 16 | // nothing to do 17 | } 18 | 19 | public boolean isValid(String string, ConstraintValidatorContext context) { 20 | return !userStore.isNameTaken(string); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/strategies/plain/Greeter.java: -------------------------------------------------------------------------------- 1 | package com.example.strategies.plain; 2 | 3 | import java.util.function.Function; 4 | 5 | public class Greeter { 6 | 7 | private Function strategy; 8 | 9 | String greet(String name) { 10 | return strategy.apply(name) + ", my name is Duke"; 11 | } 12 | 13 | public static void main(String[] args) { 14 | Greeter greeter = new Greeter(); 15 | 16 | Function formalGreeting = name -> "Dear " + name; 17 | Function informalGreeting = name -> "Hey " + name; 18 | 19 | greeter.strategy = formalGreeting; 20 | String greeting = greeter.greet("Java"); 21 | 22 | System.out.println(greeting); 23 | } 24 | 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Chapter07/cars-cdi-unit/src/main/java/com/example/cars/boundary/CarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary; 2 | 3 | import com.example.cars.control.CarFactory; 4 | import com.example.cars.entity.Car; 5 | import com.example.cars.entity.Specification; 6 | 7 | import javax.ejb.Stateless; 8 | import javax.inject.Inject; 9 | import javax.persistence.EntityManager; 10 | import javax.persistence.PersistenceContext; 11 | 12 | @Stateless 13 | public class CarManufacturer { 14 | 15 | @Inject 16 | CarFactory carFactory; 17 | 18 | @PersistenceContext 19 | EntityManager entityManager; 20 | 21 | public Car manufactureCar(Specification spec) { 22 | Car car = carFactory.createCar(spec); 23 | entityManager.merge(car); 24 | return car; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/src/main/java/com/example/cars/boundary/CarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary; 2 | 3 | import com.example.cars.control.CarFactory; 4 | import com.example.cars.entity.Car; 5 | import com.example.cars.entity.Specification; 6 | 7 | import javax.ejb.Stateless; 8 | import javax.inject.Inject; 9 | import javax.persistence.EntityManager; 10 | import javax.persistence.PersistenceContext; 11 | 12 | @Stateless 13 | public class CarManufacturer { 14 | 15 | @Inject 16 | CarFactory carFactory; 17 | 18 | @PersistenceContext 19 | EntityManager entityManager; 20 | 21 | public Car manufactureCar(Specification spec) { 22 | Car car = carFactory.createCar(spec); 23 | entityManager.merge(car); 24 | return car; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs_hypermedia/boundary/programmatic_builder/EntityBuilder.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs_hypermedia.boundary.programmatic_builder; 2 | 3 | import com.example.jaxrs_hypermedia.entity.Book; 4 | 5 | import javax.json.Json; 6 | import javax.json.JsonObject; 7 | import java.net.URI; 8 | 9 | public class EntityBuilder { 10 | 11 | public JsonObject buildForBook(Book book, URI selfUri) { 12 | return Json.createObjectBuilder() 13 | .add("name", book.getName()) 14 | .add("author", book.getName()) 15 | .add("isbn", book.getName()) 16 | .add("_links", Json.createObjectBuilder() 17 | .add("self", selfUri.toString())) 18 | .build(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/websockets/ChatMessageDecoder.java: -------------------------------------------------------------------------------- 1 | package com.example.websockets; 2 | 3 | import javax.websocket.DecodeException; 4 | import javax.websocket.Decoder; 5 | import javax.websocket.EndpointConfig; 6 | 7 | public class ChatMessageDecoder implements Decoder.Text { 8 | 9 | @Override 10 | public ChatMessage decode(String string) throws DecodeException { 11 | return new ChatMessage(string); 12 | } 13 | 14 | @Override 15 | public boolean willDecode(String string) { 16 | return true; 17 | } 18 | 19 | @Override 20 | public void init(EndpointConfig config) { 21 | // nothing to do 22 | } 23 | 24 | @Override 25 | public void destroy() { 26 | // nothing to do 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Chapter10/security/src/main/java/com/example/security/CompanyProcesses.java: -------------------------------------------------------------------------------- 1 | package com.example.security; 2 | 3 | import javax.ejb.Stateless; 4 | import javax.inject.Inject; 5 | import javax.security.enterprise.SecurityContext; 6 | 7 | @Stateless 8 | public class CompanyProcesses { 9 | 10 | @Inject 11 | SecurityContext securityContext; 12 | 13 | public void executeProcess() { 14 | executeUserProcess(); 15 | if (securityContext.isCallerInRole("admin")) { 16 | String name = securityContext.getCallerPrincipal().getName(); 17 | executeAdminProcess(name); 18 | } 19 | } 20 | 21 | private void executeUserProcess() { 22 | // ... 23 | } 24 | 25 | private void executeAdminProcess(String name) { 26 | // ... 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cdi_events/boundary/CarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.cdi_events.boundary; 2 | 3 | import com.example.cdi_events.control.CarFactory; 4 | import com.example.cdi_events.entity.Car; 5 | import com.example.cdi_events.entity.CarCreated; 6 | import com.example.cdi_events.entity.Specification; 7 | 8 | import javax.ejb.Stateless; 9 | import javax.enterprise.event.Event; 10 | import javax.inject.Inject; 11 | 12 | @Stateless 13 | public class CarManufacturer { 14 | 15 | @Inject 16 | CarFactory carFactory; 17 | 18 | @Inject 19 | Event carCreated; 20 | 21 | public Car manufactureCar(Specification spec) { 22 | Car car = carFactory.createCar(spec); 23 | carCreated.fire(new CarCreated(spec)); 24 | return car; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Chapter10/security/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | Protected pages 10 | /management 11 | 12 | 13 | admin 14 | 15 | 16 | 17 | 18 | admin 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/strategies/cdi/GreetingStrategySelector.java: -------------------------------------------------------------------------------- 1 | package com.example.strategies.cdi; 2 | 3 | import javax.enterprise.inject.Any; 4 | import javax.enterprise.inject.Instance; 5 | import javax.enterprise.inject.Produces; 6 | import javax.inject.Inject; 7 | import java.time.LocalTime; 8 | import java.util.function.Function; 9 | 10 | public class GreetingStrategySelector { 11 | 12 | @Inject 13 | @Any 14 | Instance strategies; 15 | 16 | @Produces 17 | public Function exposeStrategy() { 18 | for (GreetingStrategy strategy : strategies) { 19 | if (strategy.isAppropriate(LocalTime.now())) 20 | return strategy::greet; 21 | } 22 | throw new IllegalStateException("Couldn't find an appropriate greeting"); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/cdi_events/boundary/CarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.async.cdi_events.boundary; 2 | 3 | import com.example.async.cdi_events.control.CarFactory; 4 | import com.example.async.cdi_events.entity.Car; 5 | import com.example.async.cdi_events.entity.CarCreated; 6 | import com.example.async.cdi_events.entity.Specification; 7 | 8 | import javax.ejb.Stateless; 9 | import javax.enterprise.event.Event; 10 | import javax.inject.Inject; 11 | 12 | @Stateless 13 | public class CarManufacturer { 14 | 15 | @Inject 16 | CarFactory carFactory; 17 | 18 | @Inject 19 | Event carCreated; 20 | 21 | public Car manufactureCar(Specification spec) { 22 | Car car = carFactory.createCar(spec); 23 | carCreated.fireAsync(new CarCreated(spec)); 24 | return car; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Chapter07/systemtest/src/main/java/com/example/cars/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.cars; 2 | 3 | import javax.json.bind.annotation.JsonbProperty; 4 | 5 | public class Car { 6 | 7 | @JsonbProperty("identifier") 8 | private String id; 9 | @JsonbProperty("engine-type") 10 | private EngineType engine; 11 | private Color color; 12 | 13 | public String getId() { 14 | return id; 15 | } 16 | 17 | public void setId(String id) { 18 | this.id = id; 19 | } 20 | 21 | public EngineType getEngine() { 22 | return engine; 23 | } 24 | 25 | public void setEngine(EngineType engine) { 26 | this.engine = engine; 27 | } 28 | 29 | public Color getColor() { 30 | return color; 31 | } 32 | 33 | public void setColor(Color color) { 34 | this.color = color; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs/ValidationExceptionMapper.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs; 2 | 3 | import javax.validation.ConstraintViolationException; 4 | import javax.ws.rs.core.Response; 5 | import javax.ws.rs.ext.ExceptionMapper; 6 | import javax.ws.rs.ext.Provider; 7 | 8 | @Provider 9 | public class ValidationExceptionMapper implements ExceptionMapper { 10 | 11 | @Override 12 | public Response toResponse(ConstraintViolationException exception) { 13 | Response.ResponseBuilder builder = Response.status(Response.Status.BAD_REQUEST); 14 | 15 | exception.getConstraintViolations() 16 | .forEach(v -> { 17 | // ... 18 | builder.header("Error-Description", "..."); 19 | }); 20 | return builder.build(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Chapter08/resilience/src/main/java/com/example/resilience/control/CircuitBreaker.java: -------------------------------------------------------------------------------- 1 | package com.example.resilience.control; 2 | 3 | import javax.interceptor.AroundInvoke; 4 | import javax.interceptor.Interceptor; 5 | import javax.interceptor.InvocationContext; 6 | 7 | @Interceptor 8 | public class CircuitBreaker { 9 | 10 | // ... 11 | 12 | @AroundInvoke 13 | public Object aroundInvoke(InvocationContext context) { 14 | // close circuit after recovery time 15 | 16 | // if circuit is open 17 | // return null; 18 | 19 | try { 20 | return context.proceed(); 21 | } catch (Exception e) { 22 | 23 | // record exception 24 | // increase failure counter 25 | // open circuit if failure exceeds threshold 26 | 27 | return null; 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs/entity/ValidUser.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs.entity; 2 | 3 | import com.example.jaxrs.control.ValidUserValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Constraint(validatedBy = ValidUserValidator.class) 15 | @Documented 16 | @Retention(RUNTIME) 17 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 18 | public @interface ValidUser { 19 | 20 | String message() default ""; 21 | 22 | Class[] groups() default {}; 23 | 24 | Class[] payload() default {}; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/executor_service/Calculator.java: -------------------------------------------------------------------------------- 1 | package com.example.async.executor_service; 2 | 3 | import javax.annotation.Resource; 4 | import javax.ejb.Stateless; 5 | import javax.enterprise.concurrent.ManagedExecutorService; 6 | import java.util.Random; 7 | import java.util.concurrent.CompletableFuture; 8 | 9 | @Stateless 10 | public class Calculator { 11 | 12 | @Resource 13 | ManagedExecutorService mes; 14 | 15 | public CompletableFuture calculateRandomPi(int maxDecimalPlaces) { 16 | return CompletableFuture.supplyAsync(() -> new Random().nextInt(maxDecimalPlaces) + 1, mes) 17 | .thenApply(this::calculatePi); 18 | } 19 | 20 | private double calculatePi(long decimalPlaces) { 21 | // this may run for a long time 22 | // ... 23 | return new Random().nextDouble(); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs/entity/UserNameNotTaken.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs.entity; 2 | 3 | import com.example.jaxrs.control.UserNameNotTakenValidator; 4 | 5 | import javax.validation.Constraint; 6 | import javax.validation.Payload; 7 | import java.lang.annotation.Documented; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import static java.lang.annotation.ElementType.*; 12 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 13 | 14 | @Constraint(validatedBy = UserNameNotTakenValidator.class) 15 | @Documented 16 | @Retention(RUNTIME) 17 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 18 | public @interface UserNameNotTaken { 19 | 20 | String message() default ""; 21 | 22 | Class[] groups() default {}; 23 | 24 | Class[] payload() default {}; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/websockets/ChatServer.java: -------------------------------------------------------------------------------- 1 | package com.example.websockets; 2 | 3 | import javax.inject.Inject; 4 | import javax.websocket.OnClose; 5 | import javax.websocket.OnMessage; 6 | import javax.websocket.OnOpen; 7 | import javax.websocket.Session; 8 | import javax.websocket.server.ServerEndpoint; 9 | 10 | @ServerEndpoint(value = "/chat", decoders = ChatMessageDecoder.class, encoders = ChatMessageEncoder.class) 11 | public class ChatServer { 12 | 13 | @Inject 14 | ChatHandler chatHandler; 15 | 16 | @OnOpen 17 | public void openSession(Session session) { 18 | // ... 19 | } 20 | 21 | @OnMessage 22 | public void onMessage(ChatMessage message, Session session) { 23 | chatHandler.store(message); 24 | } 25 | 26 | @OnClose 27 | public void closeSession(Session session) { 28 | // ... 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/factories/entity/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.factories.entity; 2 | 3 | public class Car { 4 | 5 | private String id; 6 | private EngineType engine; 7 | private Color color; 8 | 9 | public LogBook createDriverLog() { 10 | // create logbook statement 11 | 12 | // ... 13 | return new LogBook(); 14 | } 15 | 16 | public String getId() { 17 | return id; 18 | } 19 | 20 | public void setId(String id) { 21 | this.id = id; 22 | } 23 | 24 | public EngineType getEngine() { 25 | return engine; 26 | } 27 | 28 | public void setEngine(EngineType engine) { 29 | this.engine = engine; 30 | } 31 | 32 | public Color getColor() { 33 | return color; 34 | } 35 | 36 | public void setColor(Color color) { 37 | this.color = color; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jsonb/OrganizationTypeAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.jsonb; 2 | 3 | import javax.inject.Inject; 4 | import javax.json.bind.adapter.JsonbAdapter; 5 | 6 | public class OrganizationTypeAdapter implements JsonbAdapter { 7 | 8 | @Inject 9 | OrganizationStore organizationStore; 10 | 11 | @Override 12 | public String adaptToJson(Organization organization) { 13 | return String.valueOf(organization.getId()); 14 | } 15 | 16 | @Override 17 | public Organization adaptFromJson(String string) { 18 | long id = Long.parseLong(string); 19 | Organization organization = organizationStore.getOrganization(id); 20 | 21 | if (organization == null) 22 | throw new IllegalArgumentException("Could not find organization for ID " + string); 23 | 24 | return organization; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Chapter08/resilience/src/main/java/com/example/resilience/control/CustomExecutorConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.example.resilience.control; 2 | 3 | import com.airhacks.porcupine.configuration.control.ExecutorConfigurator; 4 | import com.airhacks.porcupine.execution.control.ExecutorConfiguration; 5 | 6 | import javax.enterprise.inject.Specializes; 7 | 8 | @Specializes 9 | public class CustomExecutorConfiguration extends ExecutorConfigurator { 10 | 11 | @Override 12 | public ExecutorConfiguration defaultConfigurator() { 13 | return super.defaultConfigurator(); 14 | } 15 | 16 | @Override 17 | public ExecutorConfiguration forPipeline(String name) { 18 | if ("custom-name".equals(name)) { 19 | return new ExecutorConfiguration.Builder(). 20 | abortPolicy(). 21 | build(); 22 | } 23 | return super.forPipeline(name); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms/cars/entity/Engine.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms.cars.entity; 2 | 3 | import javax.persistence.*; 4 | 5 | @Entity 6 | @Table(name = "engines") 7 | public class Engine { 8 | 9 | @Id 10 | @GeneratedValue 11 | private long id; 12 | 13 | @Enumerated(EnumType.STRING) 14 | @Basic(optional = false) 15 | private EngineType type; 16 | 17 | private double ccm; 18 | 19 | public long getId() { 20 | return id; 21 | } 22 | 23 | public void setId(long id) { 24 | this.id = id; 25 | } 26 | 27 | public EngineType getType() { 28 | return type; 29 | } 30 | 31 | public void setType(EngineType type) { 32 | this.type = type; 33 | } 34 | 35 | public double getCcm() { 36 | return ccm; 37 | } 38 | 39 | public void setCcm(double ccm) { 40 | this.ccm = ccm; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms/persons/entity/Person.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms.persons.entity; 2 | 3 | import javax.persistence.*; 4 | 5 | @Entity 6 | @Table(name = "persons") 7 | public class Person { 8 | 9 | @Id 10 | @GeneratedValue 11 | private long id; 12 | 13 | @Basic(optional = false) 14 | private String name; 15 | 16 | @Embedded 17 | private Address address; 18 | 19 | public long getId() { 20 | return id; 21 | } 22 | 23 | public void setId(long id) { 24 | this.id = id; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public Address getAddress() { 36 | return address; 37 | } 38 | 39 | public void setAddress(Address address) { 40 | this.address = address; 41 | } 42 | 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Chapter10/security/src/main/java/com/example/security/TestIdentityStore.java: -------------------------------------------------------------------------------- 1 | package com.example.security; 2 | 3 | import javax.enterprise.context.ApplicationScoped; 4 | import javax.security.enterprise.credential.UsernamePasswordCredential; 5 | import javax.security.enterprise.identitystore.CredentialValidationResult; 6 | import javax.security.enterprise.identitystore.IdentityStore; 7 | 8 | import static java.util.Collections.singleton; 9 | 10 | @ApplicationScoped 11 | public class TestIdentityStore implements IdentityStore { 12 | 13 | public CredentialValidationResult validate(UsernamePasswordCredential usernamePasswordCredential) { 14 | 15 | // validate 16 | // ... 17 | if (usernamePasswordCredential.compareTo("duke", "helloWorld")) { 18 | return new CredentialValidationResult("duke", singleton("admin")); 19 | } 20 | 21 | return CredentialValidationResult.INVALID_RESULT; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/hypermedia_client/Book.java: -------------------------------------------------------------------------------- 1 | package com.example.hypermedia_client; 2 | 3 | import java.net.URI; 4 | 5 | public class Book { 6 | 7 | private URI uri; 8 | private String name; 9 | private String author; 10 | private String isbn; 11 | 12 | public URI getUri() { 13 | return uri; 14 | } 15 | 16 | public void setUri(URI uri) { 17 | this.uri = uri; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public String getAuthor() { 29 | return author; 30 | } 31 | 32 | public void setAuthor(String author) { 33 | this.author = author; 34 | } 35 | 36 | public String getIsbn() { 37 | return isbn; 38 | } 39 | 40 | public void setIsbn(String isbn) { 41 | this.isbn = isbn; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms/persons/control/PersonAdministration.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms.persons.control; 2 | 3 | import com.example.rdbms.persons.entity.Address; 4 | import com.example.rdbms.persons.entity.Person; 5 | 6 | import javax.ejb.Stateless; 7 | import javax.persistence.EntityManager; 8 | import javax.persistence.PersistenceContext; 9 | 10 | @Stateless 11 | public class PersonAdministration { 12 | 13 | @PersistenceContext 14 | EntityManager entityManager; 15 | 16 | public void createPerson(Person person) { 17 | entityManager.persist(person); 18 | } 19 | 20 | public void updateAddress(long personId, Address newAddress) { 21 | Person person = entityManager.find(Person.class, personId); 22 | 23 | if (person == null) 24 | throw new IllegalArgumentException("Could not find person with ID " + personId); 25 | 26 | person.setAddress(newAddress); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Chapter07/cars-persistence/src/test/java/com/example/cars/entity/CarPartIT.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import javax.persistence.EntityManager; 7 | import javax.persistence.EntityTransaction; 8 | import javax.persistence.Persistence; 9 | 10 | public class CarPartIT { 11 | 12 | private EntityManager entityManager; 13 | private EntityTransaction transaction; 14 | 15 | @Before 16 | public void setUp() { 17 | entityManager = Persistence.createEntityManagerFactory("it").createEntityManager(); 18 | transaction = entityManager.getTransaction(); 19 | } 20 | 21 | @Test 22 | public void test() { 23 | transaction.begin(); 24 | 25 | CarPart part = new CarPart(); 26 | part.setOrder("123"); 27 | part.setType(PartType.CHASSIS); 28 | entityManager.merge(part); 29 | 30 | transaction.commit(); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/sse/control/SseClient.java: -------------------------------------------------------------------------------- 1 | package com.example.sse.control; 2 | 3 | import javax.ws.rs.client.ClientBuilder; 4 | import javax.ws.rs.client.WebTarget; 5 | import javax.ws.rs.sse.SseEventSource; 6 | import java.util.function.Consumer; 7 | 8 | public class SseClient { 9 | 10 | private final WebTarget target = ClientBuilder.newClient().target("..."); 11 | private SseEventSource eventSource; 12 | 13 | public void connect(Consumer dataConsumer) { 14 | eventSource = SseEventSource.target(target).build(); 15 | 16 | eventSource.register( 17 | item -> dataConsumer.accept(item.readData()), 18 | Throwable::printStackTrace, 19 | () -> System.out.println("completed")); 20 | 21 | eventSource.open(); 22 | } 23 | 24 | public void disconnect() { 25 | if (eventSource != null) 26 | eventSource.close(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Chapter07/cars-persistence/src/test/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | com.example.cars.entity.CarPart 8 | 9 | true 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/java/com/example/cars/control/ManufacturingStatistics.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.control; 2 | 3 | import com.example.cars.entity.Specification; 4 | import io.prometheus.client.Counter; 5 | 6 | import javax.annotation.PostConstruct; 7 | import javax.enterprise.context.ApplicationScoped; 8 | import javax.enterprise.event.Observes; 9 | import javax.enterprise.event.TransactionPhase; 10 | 11 | @ApplicationScoped 12 | public class ManufacturingStatistics { 13 | 14 | private Counter createdCars; 15 | 16 | @PostConstruct 17 | private void initMetrics() { 18 | createdCars = Counter.build("cars_manufactured_total", 19 | "Total number of manufactured cars") 20 | .labelNames("color", "engine") 21 | .register(); 22 | } 23 | 24 | public void carCreated(@Observes(during = TransactionPhase.AFTER_SUCCESS) Specification spec) { 25 | createdCars.labels(spec.getColor().name(), spec.getEngine().name()).inc(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms/cars/entity/Seat.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms.cars.entity; 2 | 3 | import javax.persistence.*; 4 | 5 | @Entity 6 | @Table(name = "seats") 7 | public class Seat { 8 | 9 | @Id 10 | @GeneratedValue 11 | private long id; 12 | 13 | @Basic(optional = false) 14 | @Enumerated(EnumType.STRING) 15 | private SeatMaterial material; 16 | 17 | @Basic(optional = false) 18 | @Enumerated(EnumType.STRING) 19 | private SeatShape shape; 20 | 21 | public long getId() { 22 | return id; 23 | } 24 | 25 | public void setId(long id) { 26 | this.id = id; 27 | } 28 | 29 | public SeatMaterial getMaterial() { 30 | return material; 31 | } 32 | 33 | public void setMaterial(SeatMaterial material) { 34 | this.material = material; 35 | } 36 | 37 | public SeatShape getShape() { 38 | return shape; 39 | } 40 | 41 | public void setShape(SeatShape shape) { 42 | this.shape = shape; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Chapter07/cars-cdi-unit/src/main/java/com/example/cars/entity/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public class Car { 4 | 5 | private Specification specification; 6 | 7 | private Car() { 8 | } 9 | 10 | public Car(Specification specification) { 11 | this.specification = specification; 12 | } 13 | 14 | public Specification getSpecification() { 15 | return specification; 16 | } 17 | 18 | public void setSpecification(Specification specification) { 19 | this.specification = specification; 20 | } 21 | 22 | @Override 23 | public boolean equals(Object o) { 24 | if (this == o) return true; 25 | if (o == null || getClass() != o.getClass()) return false; 26 | 27 | Car car = (Car) o; 28 | 29 | return specification != null ? specification.equals(car.specification) : car.specification == null; 30 | } 31 | 32 | @Override 33 | public int hashCode() { 34 | return specification != null ? specification.hashCode() : 0; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/src/main/java/com/example/cars/entity/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public class Car { 4 | 5 | private Specification specification; 6 | 7 | private Car() { 8 | } 9 | 10 | public Car(Specification specification) { 11 | this.specification = specification; 12 | } 13 | 14 | public Specification getSpecification() { 15 | return specification; 16 | } 17 | 18 | public void setSpecification(Specification specification) { 19 | this.specification = specification; 20 | } 21 | 22 | @Override 23 | public boolean equals(Object o) { 24 | if (this == o) return true; 25 | if (o == null || getClass() != o.getClass()) return false; 26 | 27 | Car car = (Car) o; 28 | 29 | return specification != null ? specification.equals(car.specification) : car.specification == null; 30 | } 31 | 32 | @Override 33 | public int hashCode() { 34 | return specification != null ? specification.hashCode() : 0; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/hypermedia_client/EntityMapper.java: -------------------------------------------------------------------------------- 1 | package com.example.hypermedia_client; 2 | 3 | import javax.json.Json; 4 | import javax.json.JsonObject; 5 | import java.net.URI; 6 | 7 | public class EntityMapper { 8 | 9 | public Entity encodeBook(Book book) { 10 | // ... 11 | Entity entity = new Entity(); 12 | entity.setUri(book.getUri()); 13 | entity.setProperties(Json.createObjectBuilder() 14 | .add("name", book.getName()) 15 | .add("author", book.getAuthor()) 16 | .add("isbn", book.getIsbn()) 17 | .build() 18 | ); 19 | return entity; 20 | } 21 | 22 | public Book decodeBook(URI uri, JsonObject properties) { 23 | // ... 24 | Book book = new Book(); 25 | book.setUri(uri); 26 | book.setName(properties.getString("name")); 27 | book.setAuthor(properties.getString("author")); 28 | book.setIsbn(properties.getString("isbn")); 29 | 30 | return book; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Chapter07/cars-persistence/src/main/java/com/example/cars/entity/CarPart.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | import javax.persistence.*; 4 | 5 | import static javax.persistence.EnumType.STRING; 6 | 7 | @Entity 8 | @Table(name = "car_parts") 9 | public class CarPart { 10 | 11 | @Id 12 | @GeneratedValue 13 | private long id; 14 | 15 | @Basic(optional = false) 16 | // without this annotation our test example will fail 17 | @Column(name = "part_order") 18 | private String order; 19 | 20 | @Enumerated(STRING) 21 | @Basic(optional = false) 22 | private PartType type; 23 | 24 | public long getId() { 25 | return id; 26 | } 27 | 28 | public void setId(long id) { 29 | this.id = id; 30 | } 31 | 32 | public String getOrder() { 33 | return order; 34 | } 35 | 36 | public void setOrder(String order) { 37 | this.order = order; 38 | } 39 | 40 | public PartType getType() { 41 | return type; 42 | } 43 | 44 | public void setType(PartType type) { 45 | this.type = type; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Chapter02/example/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.example 6 | example 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | 11 | 12 | javax 13 | javaee-api 14 | 8.0 15 | provided 16 | 17 | 18 | 19 | 20 | example 21 | 22 | 23 | 24 | 1.8 25 | 1.8 26 | false 27 | UTF-8 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/singletons/application_startup/CarStorage.java: -------------------------------------------------------------------------------- 1 | package com.example.singletons.application_startup; 2 | 3 | import com.example.core_components.entity.Car; 4 | 5 | import javax.annotation.PostConstruct; 6 | import javax.annotation.PreDestroy; 7 | import javax.ejb.ConcurrencyManagement; 8 | import javax.ejb.ConcurrencyManagementType; 9 | import javax.ejb.Singleton; 10 | import javax.ejb.Startup; 11 | import java.util.Map; 12 | import java.util.concurrent.ConcurrentHashMap; 13 | 14 | @Singleton 15 | @Startup 16 | @ConcurrencyManagement(ConcurrencyManagementType.BEAN) 17 | public class CarStorage { 18 | 19 | private final Map cars = new ConcurrentHashMap<>(); 20 | 21 | @PostConstruct 22 | private void loadStorage() { 23 | // load contents from file 24 | } 25 | 26 | public void store(Car car) { 27 | cars.put(car.getId(), car); 28 | } 29 | 30 | public Car retrieve(String id) { 31 | return cars.get(id); 32 | } 33 | 34 | @PreDestroy 35 | public void writeStorage() { 36 | // write contents to file 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Chapter07/performance/src/test/scala/com/example/cars/CarCreationSimulation.scala: -------------------------------------------------------------------------------- 1 | package com.example.cars 2 | 3 | import io.gatling.core.Predef._ 4 | import io.gatling.core.structure.ScenarioBuilder 5 | import io.gatling.http.Predef._ 6 | import io.gatling.http.protocol.HttpProtocolBuilder 7 | 8 | import scala.concurrent.duration._ 9 | 10 | class CarCreationSimulation extends Simulation { 11 | 12 | val httpConf: HttpProtocolBuilder = http 13 | .baseURL("http://test.car-manufacture.example.com/car-manufacture/resources") 14 | .acceptHeader("*/*") 15 | 16 | val scn: ScenarioBuilder = scenario("create_car") 17 | .exec(http("request_1") 18 | .get("/cars")) 19 | .exec(http("request_1") 20 | .post("/cars") 21 | .body(StringBody("""{"id": "X123A234", "color": "RED", "engine": "DIESEL"}""")).asJSON 22 | .check(header("Location").saveAs("locationHeader"))) 23 | .exec(http("request_1") 24 | .get("${locationHeader}")) 25 | 26 | pause(1 second) 27 | 28 | setUp( 29 | scn.inject(rampUsersPerSec(10).to(20).during(10 seconds)) 30 | ).protocols(httpConf) 31 | .constantPauses 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Chapter10/security/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.example 6 | security 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | 11 | 12 | javax 13 | javaee-api 14 | 8.0 15 | provided 16 | 17 | 18 | 19 | 20 | security 21 | 22 | 23 | 24 | 1.8 25 | 1.8 26 | false 27 | UTF-8 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter05/hello-cloud/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.example 6 | hello-cloud 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | 11 | 12 | javax 13 | javaee-api 14 | 8.0 15 | provided 16 | 17 | 18 | 19 | 20 | hello-cloud 21 | 22 | 23 | 24 | 1.8 25 | 1.8 26 | false 27 | UTF-8 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms/cars/entity/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms.cars.entity; 2 | 3 | import javax.persistence.*; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | @Entity 8 | @Table(name = "cars") 9 | public class Car { 10 | 11 | @Id 12 | @GeneratedValue 13 | private long id; 14 | 15 | @OneToOne(optional = false, cascade = CascadeType.ALL) 16 | private Engine engine; 17 | 18 | @OneToMany(cascade = CascadeType.ALL) 19 | private Set seats = new HashSet<>(); 20 | 21 | public long getId() { 22 | return id; 23 | } 24 | 25 | public void setId(long id) { 26 | this.id = id; 27 | } 28 | 29 | public Engine getEngine() { 30 | return engine; 31 | } 32 | 33 | public void setEngine(Engine engine) { 34 | this.engine = engine; 35 | } 36 | 37 | public Set getSeats() { 38 | return seats; 39 | } 40 | 41 | public void setSeats(Set seats) { 42 | this.seats = seats; 43 | } 44 | 45 | public void replaceEngine(Engine engine) { 46 | setEngine(engine); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Chapter07/cars/src/main/java/com/example/cars/entity/Specification.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public class Specification { 4 | 5 | private EngineType engine; 6 | private Color color; 7 | 8 | public EngineType getEngine() { 9 | return engine; 10 | } 11 | 12 | public void setEngine(EngineType engine) { 13 | this.engine = engine; 14 | } 15 | 16 | public Color getColor() { 17 | return color; 18 | } 19 | 20 | public void setColor(Color color) { 21 | this.color = color; 22 | } 23 | 24 | @Override 25 | public boolean equals(Object o) { 26 | if (this == o) return true; 27 | if (o == null || getClass() != o.getClass()) return false; 28 | 29 | Specification that = (Specification) o; 30 | 31 | if (engine != that.engine) return false; 32 | return color == that.color; 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | int result = engine != null ? engine.hashCode() : 0; 38 | result = 31 * result + (color != null ? color.hashCode() : 0); 39 | return result; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Chapter01/cars-maven/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.example.cars 6 | car-manufacture 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | 11 | 12 | javax 13 | javaee-api 14 | 8.0 15 | provided 16 | 17 | 18 | 19 | 20 | car-manufacture 21 | 22 | 23 | 24 | 1.8 25 | 1.8 26 | false 27 | UTF-8 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/src/main/java/com/example/cars/entity/Specification.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public class Specification { 4 | 5 | private EngineType engine; 6 | private Color color; 7 | 8 | public EngineType getEngine() { 9 | return engine; 10 | } 11 | 12 | public void setEngine(EngineType engine) { 13 | this.engine = engine; 14 | } 15 | 16 | public Color getColor() { 17 | return color; 18 | } 19 | 20 | public void setColor(Color color) { 21 | this.color = color; 22 | } 23 | 24 | @Override 25 | public boolean equals(Object o) { 26 | if (this == o) return true; 27 | if (o == null || getClass() != o.getClass()) return false; 28 | 29 | Specification that = (Specification) o; 30 | 31 | if (engine != that.engine) return false; 32 | return color == that.color; 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | int result = engine != null ? engine.hashCode() : 0; 38 | result = 31 * result + (color != null ? color.hashCode() : 0); 39 | return result; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Chapter07/cars-cdi-unit/src/main/java/com/example/cars/entity/Specification.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | public class Specification { 4 | 5 | private EngineType engine; 6 | private Color color; 7 | 8 | public EngineType getEngine() { 9 | return engine; 10 | } 11 | 12 | public void setEngine(EngineType engine) { 13 | this.engine = engine; 14 | } 15 | 16 | public Color getColor() { 17 | return color; 18 | } 19 | 20 | public void setColor(Color color) { 21 | this.color = color; 22 | } 23 | 24 | @Override 25 | public boolean equals(Object o) { 26 | if (this == o) return true; 27 | if (o == null || getClass() != o.getClass()) return false; 28 | 29 | Specification that = (Specification) o; 30 | 31 | if (engine != that.engine) return false; 32 | return color == that.color; 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | int result = engine != null ? engine.hashCode() : 0; 38 | result = 31 * result + (color != null ? color.hashCode() : 0); 39 | return result; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Chapter05/hello-cloud/deployment/hello-cloud.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | kind: Service 3 | apiVersion: v1 4 | metadata: 5 | name: hello-cloud 6 | spec: 7 | selector: 8 | app: hello-cloud 9 | ports: 10 | - port: 8080 11 | --- 12 | kind: Deployment 13 | apiVersion: apps/v1beta1 14 | metadata: 15 | name: hello-cloud 16 | spec: 17 | replicas: 1 18 | template: 19 | metadata: 20 | labels: 21 | app: hello-cloud 22 | spec: 23 | containers: 24 | - name: hello-cloud 25 | image: docker.example.com/hello-cloud:1 26 | imagePullPolicy: IfNotPresent 27 | livenessProbe: 28 | httpGet: 29 | path: / 30 | port: 8080 31 | readinessProbe: 32 | httpGet: 33 | path: /hello-cloud/resources/hello 34 | port: 8080 35 | restartPolicy: Always 36 | --- 37 | kind: Ingress 38 | apiVersion: extensions/v1beta1 39 | metadata: 40 | name: hello-cloud 41 | spec: 42 | rules: 43 | - host: hello.example.com 44 | http: 45 | paths: 46 | - path: / 47 | backend: 48 | serviceName: hello-cloud 49 | servicePort: 8080 50 | --- -------------------------------------------------------------------------------- /Chapter05/hello-cloud/src/main/java/com/example/hello_cloud/control/HelloCloudProcessor.java: -------------------------------------------------------------------------------- 1 | package com.example.hello_cloud.control; 2 | 3 | import javax.annotation.PostConstruct; 4 | import javax.annotation.PreDestroy; 5 | import javax.enterprise.context.ApplicationScoped; 6 | import javax.ws.rs.client.Client; 7 | import javax.ws.rs.client.ClientBuilder; 8 | import javax.ws.rs.client.WebTarget; 9 | import java.util.concurrent.TimeUnit; 10 | 11 | @ApplicationScoped 12 | public class HelloCloudProcessor { 13 | 14 | private Client client; 15 | private WebTarget target; 16 | 17 | @PostConstruct 18 | private void initClient() { 19 | client = ClientBuilder.newBuilder() 20 | .connectTimeout(100, TimeUnit.MILLISECONDS) 21 | .readTimeout(2, TimeUnit.SECONDS) 22 | .build(); 23 | target = client.target("http://cloud-processor:8080/processor/resources/hello"); 24 | } 25 | 26 | public String processGreeting() { 27 | return target.request().get(String.class); 28 | } 29 | 30 | @PreDestroy 31 | public void closeClient() { 32 | client.close(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/core_components/entity/Specification.java: -------------------------------------------------------------------------------- 1 | package com.example.core_components.entity; 2 | 3 | public class Specification { 4 | 5 | private EngineType engine; 6 | private Color color; 7 | 8 | public EngineType getEngine() { 9 | return engine; 10 | } 11 | 12 | public void setEngine(EngineType engine) { 13 | this.engine = engine; 14 | } 15 | 16 | public Color getColor() { 17 | return color; 18 | } 19 | 20 | public void setColor(Color color) { 21 | this.color = color; 22 | } 23 | 24 | @Override 25 | public boolean equals(Object o) { 26 | if (this == o) return true; 27 | if (o == null || getClass() != o.getClass()) return false; 28 | 29 | Specification that = (Specification) o; 30 | 31 | if (engine != that.engine) return false; 32 | return color == that.color; 33 | } 34 | 35 | @Override 36 | public int hashCode() { 37 | int result = engine != null ? engine.hashCode() : 0; 38 | result = 31 * result + (color != null ? color.hashCode() : 0); 39 | return result; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter07/performance/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.example.cars 6 | performance 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | 11 | 12 | io.gatling.highcharts 13 | gatling-charts-highcharts 14 | 2.2.4 15 | test 16 | 17 | 18 | 19 | 20 | performance 21 | 22 | 23 | 24 | 1.8 25 | 1.8 26 | false 27 | UTF-8 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter07/cars/src/test/java/com/example/cars/boundary/component_tests/CarManufacturerTest.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary.component_tests; 2 | 3 | import com.example.cars.boundary.CarManufacturerComponent; 4 | import com.example.cars.control.CarFactoryComponent; 5 | import com.example.cars.entity.Car; 6 | import com.example.cars.entity.Specification; 7 | import org.assertj.core.api.AssertionsForClassTypes; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | public class CarManufacturerTest { 12 | 13 | private CarManufacturerComponent carManufacture; 14 | private CarFactoryComponent carFactory; 15 | 16 | @Before 17 | public void setUp() { 18 | carFactory = new CarFactoryComponent(); 19 | carManufacture = new CarManufacturerComponent(carFactory); 20 | } 21 | 22 | @Test 23 | public void test() { 24 | Specification spec = new Specification(); 25 | Car expected = new Car(spec); 26 | AssertionsForClassTypes.assertThat(carManufacture.manufactureCar(spec)).isEqualTo(expected); 27 | 28 | carManufacture.verifyManufacture(expected); 29 | carFactory.verifyCarCreation(spec); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jsonb/Employee.java: -------------------------------------------------------------------------------- 1 | package com.example.jsonb; 2 | 3 | import javax.json.bind.annotation.JsonbTransient; 4 | import javax.json.bind.annotation.JsonbTypeAdapter; 5 | 6 | public class Employee { 7 | 8 | @JsonbTransient 9 | private long id; 10 | private String name; 11 | private String email; 12 | 13 | @JsonbTypeAdapter(value = OrganizationTypeAdapter.class) 14 | private Organization organization; 15 | 16 | public long getId() { 17 | return id; 18 | } 19 | 20 | public void setId(long id) { 21 | this.id = id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | public String getEmail() { 33 | return email; 34 | } 35 | 36 | public void setEmail(String email) { 37 | this.email = email; 38 | } 39 | 40 | public Organization getOrganization() { 41 | return organization; 42 | } 43 | 44 | public void setOrganization(Organization organization) { 45 | this.organization = organization; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Chapter07/systemtest/src/test/java/com/example/cars/AssemblyLine.java: -------------------------------------------------------------------------------- 1 | package com.example.cars; 2 | 3 | import com.github.tomakehurst.wiremock.matching.StringValuePattern; 4 | 5 | import static com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder.okForJson; 6 | import static com.github.tomakehurst.wiremock.client.WireMock.*; 7 | import static java.util.Collections.singletonMap; 8 | 9 | public class AssemblyLine { 10 | 11 | public void initBehavior() { 12 | configureFor("http://test.assembly.example.com", 80); 13 | resetAllRequests(); 14 | 15 | stubFor(get(urlPathMatching("/assembly-line/processes/[0-9A-Z]+")) 16 | .willReturn(okForJson(singletonMap("status", "IN_PROGRESS")))); 17 | 18 | stubFor(post(urlPathMatching("/assembly-line/processes")).willReturn(status(202))); 19 | } 20 | 21 | public void verifyInstructions(String id) { 22 | verify(postRequestedFor(urlEqualTo("/assembly-line/processes/" + id)) 23 | .withRequestBody(processRequestBody())); 24 | } 25 | 26 | private StringValuePattern processRequestBody() { 27 | // ... 28 | return null; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/orders/boundary/OrderService.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.orders.boundary; 2 | 3 | import com.example.restaurant.events.control.EventProducer; 4 | import com.example.restaurant.events.entity.MealDelivered; 5 | import com.example.restaurant.events.entity.OrderCancelled; 6 | import com.example.restaurant.events.entity.OrderPlaced; 7 | import com.example.restaurant.events.entity.OrderStarted; 8 | import com.example.restaurant.orders.entity.OrderInfo; 9 | 10 | import javax.inject.Inject; 11 | import java.util.UUID; 12 | 13 | public class OrderService { 14 | 15 | @Inject 16 | EventProducer eventProducer; 17 | 18 | public void orderMeal(OrderInfo orderInfo) { 19 | eventProducer.publish(new OrderPlaced(orderInfo)); 20 | } 21 | 22 | void cancelOrder(UUID orderId, String reason) { 23 | eventProducer.publish(new OrderCancelled(orderId, reason)); 24 | } 25 | 26 | void startOrder(UUID orderId) { 27 | eventProducer.publish(new OrderStarted(orderId)); 28 | } 29 | 30 | void deliverMeal(UUID orderId) { 31 | eventProducer.publish(new MealDelivered(orderId)); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter07/cars-cdi-unit/src/test/java/com/example/cars/boundary/ManufactureCarIT.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary; 2 | 3 | import com.example.cars.entity.Car; 4 | import com.example.cars.entity.Specification; 5 | import org.jglue.cdiunit.CdiRunner; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.mockito.Mock; 10 | 11 | import javax.inject.Inject; 12 | import javax.persistence.EntityManager; 13 | 14 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 15 | import static org.mockito.Mockito.verify; 16 | 17 | @RunWith(CdiRunner.class) 18 | public class ManufactureCarIT { 19 | 20 | @Inject 21 | CarManufacturer carManufacturer; 22 | 23 | @Mock 24 | EntityManager entityManager; 25 | 26 | @Before 27 | public void setUp() { 28 | carManufacturer.entityManager = entityManager; 29 | } 30 | 31 | @Test 32 | public void test() { 33 | Specification spec = new Specification(); 34 | Car expected = new Car(spec); 35 | assertThat(carManufacturer.manufactureCar(spec)).isEqualTo(expected); 36 | 37 | verify(entityManager).merge(expected); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/orders/boundary/OrdersQueryResource.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.orders.boundary; 2 | 3 | import com.example.restaurant.orders.control.MealOrders; 4 | import com.example.restaurant.orders.entity.MealOrder; 5 | 6 | import javax.inject.Inject; 7 | import javax.json.Json; 8 | import javax.json.JsonObject; 9 | import javax.ws.rs.GET; 10 | import javax.ws.rs.NotFoundException; 11 | import javax.ws.rs.Path; 12 | import javax.ws.rs.PathParam; 13 | import java.util.UUID; 14 | 15 | @Path("orders") 16 | public class OrdersQueryResource { 17 | 18 | @Inject 19 | MealOrders mealOrders; 20 | 21 | @GET 22 | @Path("{id}") 23 | public JsonObject getOrder(@PathParam("id") UUID orderId) { 24 | MealOrder order = mealOrders.get(orderId); 25 | 26 | if (order == null) 27 | throw new NotFoundException(); 28 | 29 | return Json.createObjectBuilder() 30 | .add("id", order.getOrderId()) 31 | .add("status", order.getState().name()) 32 | // ... 33 | .add("specification", order.getSpecification().toString()) 34 | .build(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs.entity; 2 | 3 | import javax.json.bind.annotation.JsonbProperty; 4 | import javax.json.bind.annotation.JsonbTransient; 5 | import javax.validation.constraints.Email; 6 | import javax.validation.constraints.NotBlank; 7 | 8 | public class User { 9 | 10 | @JsonbTransient 11 | private long id; 12 | 13 | @NotBlank 14 | @UserNameNotTaken 15 | @JsonbProperty("username") 16 | private String name; 17 | 18 | @Email 19 | private String email; 20 | 21 | public User(long id, String name) { 22 | this.id = id; 23 | this.name = name; 24 | } 25 | 26 | public User() { 27 | } 28 | 29 | public long getId() { 30 | return id; 31 | } 32 | 33 | public void setId(long id) { 34 | this.id = id; 35 | } 36 | 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public String getEmail() { 46 | return email; 47 | } 48 | 49 | public void setEmail(String email) { 50 | this.email = email; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Chapter08/resilience/src/main/java/com/example/resilience/boundary/UsersResource.java: -------------------------------------------------------------------------------- 1 | package com.example.resilience.boundary; 2 | 3 | import com.airhacks.porcupine.execution.boundary.Dedicated; 4 | 5 | import javax.inject.Inject; 6 | import javax.ws.rs.GET; 7 | import javax.ws.rs.Path; 8 | import javax.ws.rs.Produces; 9 | import javax.ws.rs.core.MediaType; 10 | import javax.ws.rs.core.Response; 11 | import javax.xml.registry.infomodel.User; 12 | import java.util.List; 13 | import java.util.concurrent.CompletableFuture; 14 | import java.util.concurrent.CompletionStage; 15 | import java.util.concurrent.ExecutorService; 16 | 17 | @Path("users") 18 | @Produces(MediaType.APPLICATION_JSON) 19 | public class UsersResource { 20 | 21 | @Inject 22 | @Dedicated("custom-name") 23 | ExecutorService executorService; 24 | 25 | @Inject 26 | UserStore userStore; 27 | 28 | @GET 29 | public CompletionStage get() { 30 | return CompletableFuture.supplyAsync(this::getUsers, executorService) 31 | .thenApply(s -> Response.ok(s).build()); 32 | } 33 | 34 | private List getUsers() { 35 | // ... 36 | return userStore.getUsers(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/jaxrs_resource/completion_stage/UsersResource.java: -------------------------------------------------------------------------------- 1 | package com.example.async.jaxrs_resource.completion_stage; 2 | 3 | import com.example.async.jaxrs_resource.User; 4 | import com.example.async.jaxrs_resource.UserStore; 5 | 6 | import javax.annotation.Resource; 7 | import javax.enterprise.concurrent.ManagedExecutorService; 8 | import javax.inject.Inject; 9 | import javax.ws.rs.Consumes; 10 | import javax.ws.rs.POST; 11 | import javax.ws.rs.Path; 12 | import javax.ws.rs.core.MediaType; 13 | import javax.ws.rs.core.Response; 14 | import java.util.concurrent.CompletableFuture; 15 | import java.util.concurrent.CompletionStage; 16 | 17 | @Path("users") 18 | @Consumes(MediaType.APPLICATION_JSON) 19 | public class UsersResource { 20 | 21 | @Resource 22 | ManagedExecutorService mes; 23 | 24 | @Inject 25 | UserStore userStore; 26 | 27 | @POST 28 | public CompletionStage createUserAsync(User user) { 29 | return CompletableFuture.supplyAsync(() -> createUser(user), mes); 30 | } 31 | 32 | private Response createUser(User user) { 33 | userStore.create(user); 34 | 35 | return Response.accepted().build(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/orders/entity/MealOrder.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.orders.entity; 2 | 3 | import javax.persistence.*; 4 | 5 | @Entity 6 | @Table(name = "meal_orders") 7 | public class MealOrder { 8 | 9 | @Id 10 | private String orderId; 11 | 12 | @Embedded 13 | private MealSpecification specification; 14 | 15 | @Enumerated(EnumType.STRING) 16 | private OrderState state; 17 | 18 | private MealOrder() { 19 | } 20 | 21 | public MealOrder(OrderInfo orderInfo) { 22 | orderId = orderInfo.getOrderId().toString(); 23 | state = OrderState.PLACED; 24 | 25 | // define specifications 26 | } 27 | 28 | public void start() { 29 | state = OrderState.STARTED; 30 | } 31 | 32 | public void deliver() { 33 | state = OrderState.DELIVERED; 34 | } 35 | 36 | public void cancel() { 37 | state = OrderState.CANCELLED; 38 | } 39 | 40 | public String getOrderId() { 41 | return orderId; 42 | } 43 | 44 | public MealSpecification getSpecification() { 45 | return specification; 46 | } 47 | 48 | public OrderState getState() { 49 | return state; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/rdbms/persons/entity/Address.java: -------------------------------------------------------------------------------- 1 | package com.example.rdbms.persons.entity; 2 | 3 | import javax.persistence.Basic; 4 | import javax.persistence.Embeddable; 5 | 6 | @Embeddable 7 | public class Address { 8 | 9 | @Basic(optional = false) 10 | private String streetName; 11 | 12 | @Basic(optional = false) 13 | private String postalCode; 14 | 15 | @Basic(optional = false) 16 | private String city; 17 | 18 | public Address() { 19 | } 20 | 21 | public Address(String streetName, String postalCode, String city) { 22 | this.streetName = streetName; 23 | this.postalCode = postalCode; 24 | this.city = city; 25 | } 26 | 27 | public String getStreetName() { 28 | return streetName; 29 | } 30 | 31 | public void setStreetName(String streetName) { 32 | this.streetName = streetName; 33 | } 34 | 35 | public String getPostalCode() { 36 | return postalCode; 37 | } 38 | 39 | public void setPostalCode(String postalCode) { 40 | this.postalCode = postalCode; 41 | } 42 | 43 | public String getCity() { 44 | return city; 45 | } 46 | 47 | public void setCity(String city) { 48 | this.city = city; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Chapter07/cars/src/main/java/com/example/cars/entity/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.entity; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.Id; 5 | import javax.persistence.Transient; 6 | 7 | // proper persistence is not part of this example 8 | @Entity 9 | public class Car { 10 | 11 | @Id 12 | private long id; 13 | 14 | @Transient 15 | private Specification specification; 16 | 17 | private Car() { 18 | } 19 | 20 | public Car(Specification specification) { 21 | this.specification = specification; 22 | } 23 | 24 | public Specification getSpecification() { 25 | return specification; 26 | } 27 | 28 | public void setSpecification(Specification specification) { 29 | this.specification = specification; 30 | } 31 | 32 | @Override 33 | public boolean equals(Object o) { 34 | if (this == o) return true; 35 | if (o == null || getClass() != o.getClass()) return false; 36 | 37 | Car car = (Car) o; 38 | 39 | return specification != null ? specification.equals(car.specification) : car.specification == null; 40 | } 41 | 42 | @Override 43 | public int hashCode() { 44 | return specification != null ? specification.hashCode() : 0; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/configuration/control/ConfigurationExposer.java: -------------------------------------------------------------------------------- 1 | package com.example.configuration.control; 2 | 3 | import javax.annotation.PostConstruct; 4 | import javax.enterprise.context.ApplicationScoped; 5 | import javax.enterprise.inject.Produces; 6 | import javax.enterprise.inject.spi.InjectionPoint; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.util.Properties; 10 | 11 | @ApplicationScoped 12 | public class ConfigurationExposer { 13 | 14 | private final Properties properties = new Properties(); 15 | 16 | @PostConstruct 17 | private void initProperties() { 18 | try (final InputStream inputStream = ConfigurationExposer.class.getResourceAsStream("/application.properties")) { 19 | properties.load(inputStream); 20 | } catch (IOException e) { 21 | throw new IllegalStateException("Could not init configuration", e); 22 | } 23 | } 24 | 25 | @Produces 26 | @Config("") 27 | public String exposeConfig(InjectionPoint injectionPoint) { 28 | final Config config = injectionPoint.getAnnotated().getAnnotation(Config.class); 29 | if (config != null) 30 | return properties.getProperty(config.value()); 31 | return null; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter05/hello-cloud/src/main/java/com/example/hello_cloud/control/ConfigurationExposer.java: -------------------------------------------------------------------------------- 1 | package com.example.hello_cloud.control; 2 | 3 | import javax.annotation.PostConstruct; 4 | import javax.enterprise.context.ApplicationScoped; 5 | import javax.enterprise.inject.Produces; 6 | import javax.enterprise.inject.spi.InjectionPoint; 7 | import java.io.FileInputStream; 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | import java.util.Properties; 11 | 12 | @ApplicationScoped 13 | public class ConfigurationExposer { 14 | 15 | private final Properties properties = new Properties(); 16 | 17 | @PostConstruct 18 | private void initProperties() { 19 | try (final InputStream inputStream = new FileInputStream("/opt/config/application.properties")) { 20 | properties.load(inputStream); 21 | } catch (IOException e) { 22 | throw new IllegalStateException("Could not init configuration", e); 23 | } 24 | } 25 | 26 | @Produces 27 | @Config("") 28 | public String exposeConfig(InjectionPoint injectionPoint) { 29 | final Config config = injectionPoint.getAnnotated().getAnnotation(Config.class); 30 | if (config != null) 31 | return properties.getProperty(config.value()); 32 | return null; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/orders/boundary/OrderUpdateConsumer.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.orders.boundary; 2 | 3 | import com.example.restaurant.events.control.EventConsumer; 4 | import com.example.restaurant.events.entity.MealEvent; 5 | 6 | import javax.annotation.PostConstruct; 7 | import javax.annotation.PreDestroy; 8 | import javax.annotation.Resource; 9 | import javax.ejb.Singleton; 10 | import javax.ejb.Startup; 11 | import javax.enterprise.concurrent.ManagedExecutorService; 12 | import javax.enterprise.event.Event; 13 | import javax.inject.Inject; 14 | import java.util.Properties; 15 | 16 | @Startup 17 | @Singleton 18 | public class OrderUpdateConsumer { 19 | 20 | private EventConsumer eventConsumer; 21 | 22 | @Resource 23 | ManagedExecutorService mes; 24 | 25 | @Inject 26 | Properties kafkaProperties; 27 | 28 | @Inject 29 | Event events; 30 | 31 | @PostConstruct 32 | private void init() { 33 | String orders = kafkaProperties.getProperty("orders.topic"); 34 | 35 | eventConsumer = new EventConsumer(kafkaProperties, ev -> events.fire(ev), orders); 36 | 37 | mes.execute(eventConsumer); 38 | } 39 | 40 | @PreDestroy 41 | public void close() { 42 | eventConsumer.stop(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Chapter07/cars/src/test/java/com/example/cars/boundary/ManufactureCarTest.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary; 2 | 3 | import com.example.cars.control.empty.CarFactory; 4 | import com.example.cars.entity.Car; 5 | import com.example.cars.entity.Specification; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import javax.persistence.EntityManager; 10 | 11 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 12 | import static org.mockito.ArgumentMatchers.any; 13 | import static org.mockito.Mockito.*; 14 | 15 | /** 16 | * Component test using plain instantiation. 17 | */ 18 | public class ManufactureCarTest { 19 | 20 | private CarManufacturer carManufacturer; 21 | 22 | @Before 23 | public void setUp() { 24 | carManufacturer = new CarManufacturer(); 25 | carManufacturer.carFactory = new CarFactory(); 26 | carManufacturer.entityManager = mock(EntityManager.class); 27 | } 28 | 29 | @Test 30 | public void test() { 31 | when(carManufacturer.entityManager.merge(any())).then(a -> a.getArgument(0)); 32 | 33 | Specification spec = new Specification(); 34 | Car expected = new Car(spec); 35 | 36 | assertThat(carManufacturer.manufactureCar(spec)).isEqualTo(expected); 37 | verify(carManufacturer.entityManager).merge(any(Car.class)); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /Chapter07/cars/src/test/java/com/example/cars/boundary/CarManufacturerTest.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary; 2 | 3 | import com.example.cars.control.CarFactory; 4 | import com.example.cars.entity.Car; 5 | import com.example.cars.entity.Specification; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import javax.persistence.EntityManager; 10 | 11 | import static org.assertj.core.api.Assertions.assertThat; 12 | import static org.mockito.ArgumentMatchers.any; 13 | import static org.mockito.Mockito.*; 14 | 15 | public class CarManufacturerTest { 16 | 17 | private CarManufacturer testObject; 18 | 19 | @Before 20 | public void setUp() { 21 | testObject = new CarManufacturer(); 22 | testObject.carFactory = mock(CarFactory.class); 23 | testObject.entityManager = mock(EntityManager.class); 24 | } 25 | 26 | @Test 27 | public void test() { 28 | Specification spec = new Specification(); 29 | Car car = new Car(spec); 30 | 31 | when(testObject.entityManager.merge(any())).then(a -> a.getArgument(0)); 32 | when(testObject.carFactory.createCar(any())).thenReturn(car); 33 | 34 | assertThat(testObject.manufactureCar(spec)).isEqualTo(car); 35 | 36 | verify(testObject.carFactory).createCar(spec); 37 | verify(testObject.entityManager).merge(car); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /Chapter08/resilience/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.example 6 | resilience 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | 11 | 12 | javax 13 | javaee-api 14 | 8.0 15 | provided 16 | 17 | 18 | com.airhacks 19 | porcupine 20 | 0.0.4 21 | provided 22 | 23 | 24 | 25 | 26 | resilience 27 | 28 | 29 | 30 | 1.8 31 | 1.8 32 | false 33 | UTF-8 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Chapter08/restaurant/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.example 6 | restaurant 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | 11 | 12 | javax 13 | javaee-api 14 | 8.0 15 | provided 16 | 17 | 18 | org.apache.kafka 19 | kafka-clients 20 | 0.11.0.0 21 | provided 22 | 23 | 24 | 25 | 26 | restaurant 27 | 28 | 29 | 30 | 1.8 31 | 1.8 32 | false 33 | UTF-8 34 | 35 | 36 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/orders/boundary/OrdersCommandResource.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.orders.boundary; 2 | 3 | import com.example.restaurant.orders.entity.OrderInfo; 4 | 5 | import javax.inject.Inject; 6 | import javax.json.JsonObject; 7 | import javax.ws.rs.POST; 8 | import javax.ws.rs.Path; 9 | import javax.ws.rs.core.Context; 10 | import javax.ws.rs.core.HttpHeaders; 11 | import javax.ws.rs.core.Response; 12 | import javax.ws.rs.core.UriInfo; 13 | import java.net.URI; 14 | import java.util.UUID; 15 | 16 | @Path("orders") 17 | public class OrdersCommandResource { 18 | 19 | @Inject 20 | OrderService orderService; 21 | 22 | @Context 23 | UriInfo uriInfo; 24 | 25 | @POST 26 | public Response orderCoffee(JsonObject order) { 27 | OrderInfo orderInfo = createOrderInfo(order); 28 | orderService.orderMeal(orderInfo); 29 | 30 | URI uri = uriInfo.getBaseUriBuilder() 31 | .path(OrdersQueryResource.class) 32 | .path(OrdersQueryResource.class, "getOrder") 33 | .build(orderInfo.getOrderId()); 34 | 35 | return Response.accepted().header(HttpHeaders.LOCATION, uri).build(); 36 | } 37 | 38 | private OrderInfo createOrderInfo(JsonObject order) { 39 | // ... 40 | return new OrderInfo(UUID.randomUUID()); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/hypermedia_client/BookClient.java: -------------------------------------------------------------------------------- 1 | package com.example.hypermedia_client; 2 | 3 | import javax.inject.Inject; 4 | import javax.json.Json; 5 | import javax.json.JsonObject; 6 | import javax.json.JsonObjectBuilder; 7 | import java.net.URI; 8 | 9 | public class BookClient { 10 | 11 | @Inject 12 | EntityMapper entityMapper; 13 | 14 | public Book retrieveBook(URI uri) { 15 | Entity book = retrieveEntity(uri); 16 | return entityMapper.decodeBook(uri, book.getProperties()); 17 | } 18 | 19 | public void addToCart(Book book, int quantity) { 20 | Entity bookEntity = retrieveEntity(book.getUri()); 21 | 22 | JsonObjectBuilder properties = Json.createObjectBuilder(); 23 | properties.add("quantity", quantity); 24 | 25 | Entity entity = entityMapper.encodeBook(book); 26 | entity.getProperties().forEach(properties::add); 27 | 28 | performAction(bookEntity, "add-to-cart", properties.build()); 29 | } 30 | 31 | private Entity retrieveEntity(URI uri) { 32 | // access URI and map response to entity 33 | // ... 34 | return null; 35 | } 36 | 37 | private void performAction(Entity entity, String actionName, JsonObject properties) { 38 | // follow action of entity using specific action and properties 39 | // ... 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Chapter05/hello-cloud/deployment/config-map/hello-cloud.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | kind: Service 3 | apiVersion: v1 4 | metadata: 5 | name: hello-cloud 6 | spec: 7 | selector: 8 | app: hello-cloud 9 | ports: 10 | - port: 8080 11 | --- 12 | kind: Deployment 13 | apiVersion: apps/v1beta1 14 | metadata: 15 | name: hello-cloud 16 | spec: 17 | replicas: 1 18 | template: 19 | metadata: 20 | labels: 21 | app: hello-cloud 22 | spec: 23 | containers: 24 | - name: hello-cloud 25 | image: docker.example.com/hello-cloud:1 26 | imagePullPolicy: IfNotPresent 27 | volumeMounts: 28 | - name: config-volume 29 | mountPath: /opt/config 30 | livenessProbe: 31 | httpGet: 32 | path: / 33 | port: 8080 34 | readinessProbe: 35 | httpGet: 36 | path: /hello-cloud/resources/hello 37 | port: 8080 38 | volumes: 39 | - name: config-volume 40 | configMap: 41 | name: hello-cloud-config 42 | restartPolicy: Always 43 | --- 44 | kind: Ingress 45 | apiVersion: extensions/v1beta1 46 | metadata: 47 | name: hello-cloud 48 | spec: 49 | rules: 50 | - host: hello.example.com 51 | http: 52 | paths: 53 | - path: / 54 | backend: 55 | serviceName: hello-cloud 56 | servicePort: 8080 57 | --- -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs_hypermedia/entity/Book.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs_hypermedia.entity; 2 | 3 | import javax.json.bind.annotation.JsonbProperty; 4 | import javax.json.bind.annotation.JsonbTransient; 5 | import java.net.URI; 6 | import java.util.Map; 7 | 8 | public class Book { 9 | 10 | @JsonbTransient 11 | private long id; 12 | 13 | private String name; 14 | private String author; 15 | private String isbn; 16 | 17 | @JsonbProperty("_links") 18 | private Map links; 19 | 20 | public long getId() { 21 | return id; 22 | } 23 | 24 | public void setId(long id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public String getAuthor() { 37 | return author; 38 | } 39 | 40 | public void setAuthor(String author) { 41 | this.author = author; 42 | } 43 | 44 | public String getIsbn() { 45 | return isbn; 46 | } 47 | 48 | public void setIsbn(String isbn) { 49 | this.isbn = isbn; 50 | } 51 | 52 | public Map getLinks() { 53 | return links; 54 | } 55 | 56 | public void setLinks(Map links) { 57 | this.links = links; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs/boundary/UsersResource.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs.boundary; 2 | 3 | import com.example.jaxrs.entity.User; 4 | 5 | import javax.inject.Inject; 6 | import javax.validation.Valid; 7 | import javax.validation.constraints.NotNull; 8 | import javax.ws.rs.*; 9 | import javax.ws.rs.core.Context; 10 | import javax.ws.rs.core.MediaType; 11 | import javax.ws.rs.core.Response; 12 | import javax.ws.rs.core.UriInfo; 13 | import java.net.URI; 14 | import java.util.List; 15 | 16 | @Path("users") 17 | @Produces(MediaType.APPLICATION_JSON) 18 | @Consumes(MediaType.APPLICATION_JSON) 19 | public class UsersResource { 20 | 21 | @Inject 22 | UserStore userStore; 23 | 24 | @Context 25 | UriInfo uriInfo; 26 | 27 | @GET 28 | public List getUsers() { 29 | return userStore.getUsers(); 30 | } 31 | 32 | @GET 33 | @Path("{id}") 34 | public User getUser(@PathParam("id") long id) { 35 | return userStore.getUser(id); 36 | } 37 | 38 | @POST 39 | public Response createUser(@Valid @NotNull User user) { 40 | long id = userStore.create(user); 41 | 42 | URI userUri = uriInfo.getBaseUriBuilder() 43 | .path(UsersResource.class) 44 | .path(UsersResource.class, "getUser") 45 | .build(id); 46 | 47 | return Response.created(userUri).build(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/core_components/entity/Car.java: -------------------------------------------------------------------------------- 1 | package com.example.core_components.entity; 2 | 3 | public class Car { 4 | 5 | private String id; 6 | private Specification specification; 7 | 8 | private Car() { 9 | } 10 | 11 | public Car(Specification specification) { 12 | this.specification = specification; 13 | } 14 | 15 | public String getId() { 16 | return id; 17 | } 18 | 19 | public void setId(String id) { 20 | this.id = id; 21 | } 22 | 23 | public Specification getSpecification() { 24 | return specification; 25 | } 26 | 27 | public void setSpecification(Specification specification) { 28 | this.specification = specification; 29 | } 30 | 31 | @Override 32 | public boolean equals(Object o) { 33 | if (this == o) return true; 34 | if (o == null || getClass() != o.getClass()) return false; 35 | 36 | Car car = (Car) o; 37 | 38 | if (id != null ? !id.equals(car.id) : car.id != null) return false; 39 | return specification != null ? specification.equals(car.specification) : car.specification == null; 40 | } 41 | 42 | @Override 43 | public int hashCode() { 44 | int result = id != null ? id.hashCode() : 0; 45 | result = 31 * result + (specification != null ? specification.hashCode() : 0); 46 | return result; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/java/com/example/cars/MetricsCollectorFilter.java: -------------------------------------------------------------------------------- 1 | package com.example.cars; 2 | 3 | import io.prometheus.client.Histogram; 4 | 5 | import javax.servlet.*; 6 | import javax.servlet.annotation.WebFilter; 7 | import javax.servlet.http.HttpServletRequest; 8 | import java.io.IOException; 9 | 10 | @WebFilter(urlPatterns = "/*") 11 | public class MetricsCollectorFilter implements Filter { 12 | 13 | private Histogram requestDuration; 14 | 15 | @Override 16 | public void init(FilterConfig filterConfig) throws ServletException { 17 | requestDuration = Histogram.build("request_duration_seconds", "Duration of HTTP requests in seconds") 18 | .buckets(0.1, 0.4, 1.0) 19 | .labelNames("request_uri") 20 | .register(); 21 | } 22 | 23 | public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 24 | throws IOException, ServletException { 25 | if (!(req instanceof HttpServletRequest)) { 26 | chain.doFilter(req, res); 27 | return; 28 | } 29 | 30 | String url = ((HttpServletRequest) req).getRequestURI(); 31 | try (Histogram.Timer ignored = requestDuration.labels(url).startTimer()) { 32 | chain.doFilter(req, res); 33 | } 34 | } 35 | 36 | @Override 37 | public void destroy() { 38 | // nothing to do 39 | } 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Chapter09/cars/src/main/java/com/example/cars/boundary/CarManufacturer.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary; 2 | 3 | import com.example.cars.control.CarFactory; 4 | import com.example.cars.entity.Car; 5 | import com.example.cars.entity.CarCreated; 6 | import com.example.cars.entity.Specification; 7 | import io.opentracing.ActiveSpan; 8 | import io.opentracing.Tracer; 9 | 10 | import javax.ejb.Stateless; 11 | import javax.enterprise.event.Event; 12 | import javax.inject.Inject; 13 | import javax.persistence.EntityManager; 14 | import javax.persistence.PersistenceContext; 15 | 16 | @Stateless 17 | public class CarManufacturer { 18 | 19 | @Inject 20 | CarFactory carFactory; 21 | 22 | @Inject 23 | Event carCreated; 24 | 25 | @PersistenceContext 26 | EntityManager entityManager; 27 | 28 | @Inject 29 | Tracer tracer; 30 | 31 | public Car manufactureCar(Specification spec) { 32 | try (ActiveSpan span = tracer.buildSpan("createCar") 33 | .withTag("color", spec.getColor().toString()) 34 | .withTag("engine", spec.getEngine().toString()) 35 | .startActive()) { 36 | 37 | Car car = carFactory.createCar(spec); 38 | entityManager.merge(car); 39 | carCreated.fire(new CarCreated(spec)); 40 | 41 | span.log("Car successfully created"); 42 | 43 | return car; 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/jaxrs_resource/async_response/UsersResource.java: -------------------------------------------------------------------------------- 1 | package com.example.async.jaxrs_resource.async_response; 2 | 3 | import com.example.async.jaxrs_resource.User; 4 | import com.example.async.jaxrs_resource.UserStore; 5 | 6 | import javax.annotation.Resource; 7 | import javax.enterprise.concurrent.ManagedExecutorService; 8 | import javax.inject.Inject; 9 | import javax.ws.rs.Consumes; 10 | import javax.ws.rs.POST; 11 | import javax.ws.rs.Path; 12 | import javax.ws.rs.container.AsyncResponse; 13 | import javax.ws.rs.container.Suspended; 14 | import javax.ws.rs.core.MediaType; 15 | import javax.ws.rs.core.Response; 16 | import java.util.concurrent.TimeUnit; 17 | 18 | @Path("users") 19 | @Consumes(MediaType.APPLICATION_JSON) 20 | public class UsersResource { 21 | 22 | @Resource 23 | ManagedExecutorService mes; 24 | 25 | @Inject 26 | UserStore userStore; 27 | 28 | @POST 29 | public void createUserAsync(User user, @Suspended AsyncResponse response) { 30 | 31 | response.setTimeout(5, TimeUnit.SECONDS); 32 | response.setTimeoutHandler(r -> 33 | r.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE))); 34 | 35 | mes.submit(() -> response.resume(createUser(user))); 36 | } 37 | 38 | private Response createUser(User user) { 39 | userStore.create(user); 40 | 41 | return Response.accepted().build(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/cross_cutting/control/TrackingInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.example.cross_cutting.control; 2 | 3 | import javax.annotation.Priority; 4 | import javax.inject.Inject; 5 | import javax.interceptor.AroundInvoke; 6 | import javax.interceptor.Interceptor; 7 | import javax.interceptor.InvocationContext; 8 | import java.lang.reflect.AnnotatedElement; 9 | import java.lang.reflect.Method; 10 | import java.util.function.Function; 11 | 12 | @Tracked(ProcessTracker.Category.UNUSED) 13 | @Interceptor 14 | @Priority(Interceptor.Priority.APPLICATION) 15 | public class TrackingInterceptor { 16 | 17 | @Inject 18 | ProcessTracker processTracker; 19 | 20 | @AroundInvoke 21 | public Object aroundInvoke(InvocationContext context) throws Exception { 22 | Tracked tracked = resolveAnnotation(context); 23 | 24 | if (tracked != null) { 25 | ProcessTracker.Category category = tracked.value(); 26 | processTracker.track(category); 27 | } 28 | 29 | return context.proceed(); 30 | } 31 | 32 | private Tracked resolveAnnotation(InvocationContext context) { 33 | Function extractor = c -> c.getAnnotation(Tracked.class); 34 | Method method = context.getMethod(); 35 | 36 | Tracked tracked = extractor.apply(method); 37 | return tracked != null ? tracked : extractor.apply(method.getDeclaringClass()); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/src/test/java/com/example/cars/boundary/CarManufacturerTest.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary; 2 | 3 | import com.example.cars.control.CarFactory; 4 | import com.example.cars.entity.Car; 5 | import com.example.cars.entity.Specification; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.mockito.InjectMocks; 9 | import org.mockito.Mock; 10 | import org.mockito.junit.MockitoJUnitRunner; 11 | 12 | import javax.persistence.EntityManager; 13 | 14 | import static org.assertj.core.api.Assertions.assertThat; 15 | import static org.mockito.ArgumentMatchers.any; 16 | import static org.mockito.Mockito.verify; 17 | import static org.mockito.Mockito.when; 18 | 19 | @RunWith(MockitoJUnitRunner.class) 20 | public class CarManufacturerTest { 21 | 22 | @InjectMocks 23 | private CarManufacturer testObject; 24 | 25 | @Mock 26 | private CarFactory carFactory; 27 | 28 | @Mock 29 | private EntityManager entityManager; 30 | 31 | @Test 32 | public void test() { 33 | Specification spec = new Specification(); 34 | Car car = new Car(spec); 35 | 36 | when(entityManager.merge(any())).then(a -> a.getArgument(0)); 37 | when(carFactory.createCar(any())).thenReturn(car); 38 | 39 | assertThat(testObject.manufactureCar(spec)).isEqualTo(car); 40 | 41 | verify(carFactory).createCar(spec); 42 | verify(entityManager).merge(car); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/facades/BaristaCoffeeShop.java: -------------------------------------------------------------------------------- 1 | package com.example.facades; 2 | 3 | public class BaristaCoffeeShop { 4 | 5 | private BeanStore beanStore; 6 | private Grinder grinder; 7 | private EspressoMachine espressoMachine; 8 | private Scale scale; 9 | private Thermometer thermometer; 10 | private Hygrometer hygrometer; 11 | 12 | GroundBeans grindBeans(Beans beans, double weight) { 13 | // ... 14 | return null; 15 | } 16 | 17 | Beans fetchBeans(BeanType type) { 18 | // ... 19 | return null; 20 | } 21 | 22 | double getTemperature() { 23 | // ... 24 | return 0.0; 25 | } 26 | 27 | double getHumidity() { 28 | // ... 29 | return 0.0; 30 | } 31 | 32 | Coffee makeEspresso(GroundBeans beans, Settings settings) { 33 | // ... 34 | return null; 35 | } 36 | 37 | // ... 38 | // required classes 39 | 40 | private class BeanStore { 41 | } 42 | 43 | private class Grinder { 44 | } 45 | 46 | private class EspressoMachine { 47 | } 48 | 49 | private class Scale { 50 | } 51 | 52 | private class Thermometer { 53 | } 54 | 55 | private class Hygrometer { 56 | } 57 | 58 | private class GroundBeans { 59 | } 60 | 61 | private class Beans { 62 | } 63 | 64 | private class BeanType { 65 | } 66 | 67 | private class Coffee { 68 | } 69 | 70 | private class Settings { 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs_hypermedia/boundary/declarative/BooksResource.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs_hypermedia.boundary.declarative; 2 | 3 | import com.example.jaxrs_hypermedia.entity.Book; 4 | import com.example.jaxrs_hypermedia.boundary.BookStore; 5 | 6 | import javax.inject.Inject; 7 | import javax.ws.rs.GET; 8 | import javax.ws.rs.Path; 9 | import javax.ws.rs.PathParam; 10 | import javax.ws.rs.Produces; 11 | import javax.ws.rs.core.Context; 12 | import javax.ws.rs.core.MediaType; 13 | import javax.ws.rs.core.UriInfo; 14 | import java.net.URI; 15 | import java.util.List; 16 | 17 | @Path("books") 18 | @Produces(MediaType.APPLICATION_JSON) 19 | public class BooksResource { 20 | 21 | @Inject 22 | BookStore bookStore; 23 | 24 | @Context 25 | UriInfo uriInfo; 26 | 27 | @GET 28 | public List getBooks() { 29 | List books = bookStore.getBooks(); 30 | books.forEach(this::addLinks); 31 | return books; 32 | } 33 | 34 | @GET 35 | @Path("{id}") 36 | public Book getBook(@PathParam("id") long id) { 37 | Book book = bookStore.getBook(id); 38 | addLinks(book); 39 | return book; 40 | } 41 | 42 | private void addLinks(Book book) { 43 | URI selfUri = uriInfo.getBaseUriBuilder() 44 | .path(BooksResource.class) 45 | .path(BooksResource.class, "getBook") 46 | .build(book.getId()); 47 | 48 | book.getLinks().put("self", selfUri); 49 | // other links 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/events/control/EventProducer.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.events.control; 2 | 3 | import com.example.restaurant.events.entity.MealEvent; 4 | import org.apache.kafka.clients.producer.KafkaProducer; 5 | import org.apache.kafka.clients.producer.Producer; 6 | import org.apache.kafka.clients.producer.ProducerRecord; 7 | import org.apache.kafka.common.KafkaException; 8 | import org.apache.kafka.common.errors.ProducerFencedException; 9 | 10 | import javax.annotation.PostConstruct; 11 | import javax.annotation.PreDestroy; 12 | import javax.enterprise.context.ApplicationScoped; 13 | import javax.inject.Inject; 14 | import java.util.Properties; 15 | 16 | @ApplicationScoped 17 | public class EventProducer { 18 | 19 | private Producer producer; 20 | private String topic; 21 | 22 | @Inject 23 | Properties kafkaProperties; 24 | 25 | @PostConstruct 26 | private void init() { 27 | producer = new KafkaProducer<>(kafkaProperties); 28 | topic = kafkaProperties.getProperty("topics.order"); 29 | producer.initTransactions(); 30 | } 31 | 32 | public void publish(MealEvent event) { 33 | ProducerRecord record = new ProducerRecord<>(topic, event); 34 | try { 35 | producer.beginTransaction(); 36 | producer.send(record); 37 | producer.commitTransaction(); 38 | } catch (ProducerFencedException e) { 39 | producer.close(); 40 | } catch (KafkaException e) { 41 | producer.abortTransaction(); 42 | } 43 | } 44 | 45 | @PreDestroy 46 | public void close() { 47 | producer.close(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs/boundary/jsonp/UsersResource.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs.boundary.jsonp; 2 | 3 | import com.example.jaxrs.boundary.UserStore; 4 | import com.example.jaxrs.entity.User; 5 | import com.example.jaxrs.entity.ValidUser; 6 | 7 | import javax.inject.Inject; 8 | import javax.json.JsonObject; 9 | import javax.validation.Valid; 10 | import javax.ws.rs.*; 11 | import javax.ws.rs.core.Context; 12 | import javax.ws.rs.core.MediaType; 13 | import javax.ws.rs.core.Response; 14 | import javax.ws.rs.core.UriInfo; 15 | import java.net.URI; 16 | import java.util.List; 17 | 18 | @Path("users") 19 | @Produces(MediaType.APPLICATION_JSON) 20 | @Consumes(MediaType.APPLICATION_JSON) 21 | public class UsersResource { 22 | 23 | @Inject 24 | UserStore userStore; 25 | 26 | @Context 27 | UriInfo uriInfo; 28 | 29 | @GET 30 | public List getUsers() { 31 | return userStore.getUsers(); 32 | } 33 | 34 | @GET 35 | @Path("{id}") 36 | public User getUser(@PathParam("id") long id) { 37 | return userStore.getUser(id); 38 | } 39 | 40 | @POST 41 | public Response createUser(@Valid @ValidUser JsonObject object) { 42 | User user = readUser(object); 43 | long id = userStore.create(user); 44 | 45 | URI userUri = uriInfo.getBaseUriBuilder() 46 | .path(UsersResource.class) 47 | .path(UsersResource.class, "getUser") 48 | .build(id); 49 | 50 | return Response.created(userUri).build(); 51 | } 52 | 53 | private User readUser(JsonObject object) { 54 | User user = new User(); 55 | user.setName(object.getString("name")); 56 | return user; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/src/test/java/com/example/cars/boundary/ManufactureCarTest.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary; 2 | 3 | import com.example.cars.control.AssemblyLine; 4 | import com.example.cars.control.Automation; 5 | import com.example.cars.control.CarFactory; 6 | import com.example.cars.entity.Car; 7 | import com.example.cars.entity.Specification; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.mockito.InjectMocks; 12 | import org.mockito.Mock; 13 | import org.mockito.junit.MockitoJUnitRunner; 14 | 15 | import javax.persistence.EntityManager; 16 | 17 | import static org.assertj.core.api.Assertions.assertThat; 18 | import static org.mockito.ArgumentMatchers.any; 19 | import static org.mockito.Mockito.verify; 20 | 21 | @RunWith(MockitoJUnitRunner.class) 22 | public class ManufactureCarTest { 23 | 24 | @InjectMocks 25 | private CarManufacturer carManufacturer; 26 | 27 | @InjectMocks 28 | private CarFactory carFactory; 29 | 30 | @Mock 31 | private EntityManager entityManager; 32 | 33 | @Mock 34 | private AssemblyLine assemblyLine; 35 | 36 | @Mock 37 | private Automation automation; 38 | 39 | @Before 40 | public void setUp() { 41 | carManufacturer.carFactory = carFactory; 42 | 43 | // setup mock behavior such as ... 44 | // when(assemblyLine.assemble()).thenReturn(...); 45 | } 46 | 47 | @Test 48 | public void test() { 49 | Specification spec = new Specification(); 50 | Car expected = new Car(spec); 51 | 52 | assertThat(carManufacturer.manufactureCar(spec)).isEqualTo(expected); 53 | verify(carManufacturer.entityManager).merge(any(Car.class)); 54 | } 55 | 56 | } 57 | 58 | -------------------------------------------------------------------------------- /Chapter07/cars/src/test/java/com/example/cars/boundary/ManufactureCarIT.java: -------------------------------------------------------------------------------- 1 | package com.example.cars.boundary; 2 | 3 | import com.example.cars.control.AssemblyLine; 4 | import com.example.cars.control.Automation; 5 | import com.example.cars.control.CarFactory; 6 | import com.example.cars.entity.Car; 7 | import com.example.cars.entity.Specification; 8 | import org.jboss.arquillian.container.test.api.Deployment; 9 | import org.jboss.arquillian.junit.Arquillian; 10 | import org.jboss.shrinkwrap.api.ShrinkWrap; 11 | import org.jboss.shrinkwrap.api.asset.EmptyAsset; 12 | import org.jboss.shrinkwrap.api.spec.WebArchive; 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | 16 | import javax.inject.Inject; 17 | 18 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 19 | 20 | @RunWith(Arquillian.class) 21 | public class ManufactureCarIT { 22 | 23 | @Inject 24 | CarManufacturer carManufacturer; 25 | 26 | @Deployment 27 | public static WebArchive createDeployment() { 28 | return ShrinkWrap.create(WebArchive.class) 29 | .addClasses(CarManufacturer.class) 30 | .addClasses(CarFactory.class, AssemblyLine.class, Automation.class) 31 | .addClasses(Car.class, Specification.class) 32 | .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") 33 | // required for this Arquillian example, since EntityManager is being used 34 | .addAsResource("META-INF/persistence.xml"); 35 | } 36 | 37 | @Test 38 | public void test() { 39 | Specification spec = new Specification(); 40 | Car expected = new Car(spec); 41 | assertThat(carManufacturer.manufactureCar(spec)).isEqualTo(expected); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/orders/control/MealOrders.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.orders.control; 2 | 3 | import com.example.restaurant.events.entity.MealDelivered; 4 | import com.example.restaurant.events.entity.OrderCancelled; 5 | import com.example.restaurant.events.entity.OrderPlaced; 6 | import com.example.restaurant.events.entity.OrderStarted; 7 | import com.example.restaurant.orders.entity.MealOrder; 8 | 9 | import javax.ejb.Stateless; 10 | import javax.enterprise.event.Observes; 11 | import javax.persistence.EntityManager; 12 | import javax.persistence.PersistenceContext; 13 | import java.util.UUID; 14 | import java.util.function.Consumer; 15 | 16 | @Stateless 17 | public class MealOrders { 18 | 19 | @PersistenceContext 20 | EntityManager entityManager; 21 | 22 | public MealOrder get(UUID orderId) { 23 | return entityManager.find(MealOrder.class, orderId.toString()); 24 | } 25 | 26 | public void apply(@Observes OrderPlaced event) { 27 | MealOrder order = new MealOrder(event.getOrderInfo()); 28 | entityManager.persist(order); 29 | } 30 | 31 | public void apply(@Observes OrderStarted event) { 32 | apply(event.getOrderId(), MealOrder::start); 33 | } 34 | 35 | public void apply(@Observes OrderCancelled event) { 36 | apply(event.getOrderId(), MealOrder::cancel); 37 | } 38 | 39 | public void apply(@Observes MealDelivered event) { 40 | apply(event.getOrderId(), MealOrder::deliver); 41 | } 42 | 43 | private void apply(UUID orderId, Consumer consumer) { 44 | MealOrder order = entityManager.find(MealOrder.class, orderId.toString()); 45 | if (order != null) 46 | consumer.accept(order); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Chapter10/security/src/main/java/com/example/security/TestAuthenticationMechanism.java: -------------------------------------------------------------------------------- 1 | package com.example.security; 2 | 3 | import javax.enterprise.context.ApplicationScoped; 4 | import javax.inject.Inject; 5 | import javax.security.enterprise.AuthenticationException; 6 | import javax.security.enterprise.AuthenticationStatus; 7 | import javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism; 8 | import javax.security.enterprise.authentication.mechanism.http.HttpMessageContext; 9 | import javax.security.enterprise.credential.UsernamePasswordCredential; 10 | import javax.security.enterprise.identitystore.CredentialValidationResult; 11 | import javax.security.enterprise.identitystore.IdentityStoreHandler; 12 | import javax.servlet.http.HttpServletRequest; 13 | import javax.servlet.http.HttpServletResponse; 14 | 15 | @ApplicationScoped 16 | public class TestAuthenticationMechanism implements HttpAuthenticationMechanism { 17 | 18 | @Inject 19 | private IdentityStoreHandler identityStoreHandler; 20 | 21 | @Override 22 | public AuthenticationStatus validateRequest(HttpServletRequest request, HttpServletResponse response, 23 | HttpMessageContext httpMessageContext) throws AuthenticationException { 24 | 25 | // ... 26 | String name = request.getParameter("name"); 27 | String password = request.getParameter("password"); 28 | 29 | if (name != null && password != null) { 30 | CredentialValidationResult result = identityStoreHandler.validate(new UsernamePasswordCredential(name, password)); 31 | 32 | return httpMessageContext.notifyContainerAboutLogin(result); 33 | } 34 | 35 | return httpMessageContext.doNothing(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Chapter07/cars-mockito/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.example.cars 6 | car-manufacture 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | 11 | 12 | javax 13 | javaee-api 14 | 8.0 15 | provided 16 | 17 | 18 | junit 19 | junit 20 | 4.12 21 | test 22 | 23 | 24 | org.mockito 25 | mockito-core 26 | 2.8.47 27 | test 28 | 29 | 30 | org.assertj 31 | assertj-core 32 | 3.8.0 33 | test 34 | 35 | 36 | 37 | 38 | car-manufacture 39 | 40 | 41 | 42 | 1.8 43 | 1.8 44 | false 45 | UTF-8 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/http_client/control/CoffeePurchaser.java: -------------------------------------------------------------------------------- 1 | package com.example.http_client.control; 2 | 3 | import com.example.http_client.entity.BeanType; 4 | import com.example.http_client.entity.CoffeeOrder; 5 | import com.example.http_client.entity.OrderId; 6 | import com.example.http_client.entity.Purchase; 7 | 8 | import javax.annotation.PostConstruct; 9 | import javax.annotation.PreDestroy; 10 | import javax.enterprise.context.ApplicationScoped; 11 | import javax.interceptor.Interceptors; 12 | import javax.ws.rs.client.Client; 13 | import javax.ws.rs.client.ClientBuilder; 14 | import javax.ws.rs.client.Entity; 15 | import javax.ws.rs.client.WebTarget; 16 | import javax.ws.rs.core.MediaType; 17 | import java.util.concurrent.TimeUnit; 18 | 19 | @ApplicationScoped 20 | public class CoffeePurchaser { 21 | 22 | private Client client; 23 | private WebTarget target; 24 | 25 | @PostConstruct 26 | private void initClient() { 27 | client = ClientBuilder.newBuilder() 28 | .connectTimeout(100, TimeUnit.MILLISECONDS) 29 | .readTimeout(2, TimeUnit.SECONDS) 30 | .build(); 31 | target = client.target("http://coffee.example.com/coffee/purchases/"); 32 | } 33 | 34 | @Interceptors(FailureToNullInterceptor.class) 35 | public OrderId purchaseBeans(BeanType type) { 36 | // construct purchase payload from type 37 | // ... 38 | Purchase purchase = new Purchase(); 39 | 40 | CoffeeOrder coffeeOrder = target 41 | .request(MediaType.APPLICATION_JSON_TYPE) 42 | .post(Entity.json(purchase)) 43 | .readEntity(CoffeeOrder.class); 44 | 45 | return coffeeOrder.getId(); 46 | } 47 | 48 | @PreDestroy 49 | public void closeClient() { 50 | client.close(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Chapter09/cars/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.example.cars 6 | car-manufacture 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | 11 | 12 | javax 13 | javaee-api 14 | 8.0 15 | provided 16 | 17 | 18 | io.prometheus 19 | simpleclient 20 | 0.0.26 21 | provided 22 | 23 | 24 | io.prometheus 25 | simpleclient_common 26 | 0.0.26 27 | provided 28 | 29 | 30 | io.opentracing 31 | opentracing-api 32 | 0.30.0 33 | provided 34 | 35 | 36 | 37 | 38 | car-manufacture 39 | 40 | 41 | 42 | 1.8 43 | 1.8 44 | false 45 | UTF-8 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/events/control/EventConsumer.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.events.control; 2 | 3 | import com.example.restaurant.events.entity.MealEvent; 4 | import org.apache.kafka.clients.consumer.ConsumerRecord; 5 | import org.apache.kafka.clients.consumer.ConsumerRecords; 6 | import org.apache.kafka.clients.consumer.KafkaConsumer; 7 | import org.apache.kafka.common.errors.WakeupException; 8 | 9 | import java.util.Properties; 10 | import java.util.concurrent.atomic.AtomicBoolean; 11 | import java.util.function.Consumer; 12 | 13 | import static java.util.Arrays.asList; 14 | 15 | public class EventConsumer implements Runnable { 16 | 17 | private final KafkaConsumer consumer; 18 | private final Consumer eventConsumer; 19 | private final AtomicBoolean closed = new AtomicBoolean(); 20 | 21 | public EventConsumer(Properties kafkaProperties, Consumer eventConsumer, String... topics) { 22 | this.eventConsumer = eventConsumer; 23 | consumer = new KafkaConsumer<>(kafkaProperties); 24 | consumer.subscribe(asList(topics)); 25 | } 26 | 27 | @Override 28 | public void run() { 29 | try { 30 | while (!closed.get()) { 31 | consume(); 32 | } 33 | } catch (WakeupException e) { 34 | // will wakeup for closing 35 | } finally { 36 | consumer.close(); 37 | } 38 | } 39 | 40 | private void consume() { 41 | ConsumerRecords records = consumer.poll(Long.MAX_VALUE); 42 | for (ConsumerRecord record : records) { 43 | eventConsumer.accept(record.value()); 44 | } 45 | consumer.commitSync(); 46 | } 47 | 48 | public void stop() { 49 | closed.set(true); 50 | consumer.wakeup(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs_hypermedia/boundary/programmatic/BooksResource.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs_hypermedia.boundary.programmatic; 2 | 3 | import com.example.jaxrs_hypermedia.entity.Book; 4 | import com.example.jaxrs_hypermedia.boundary.BookStore; 5 | 6 | import javax.inject.Inject; 7 | import javax.json.Json; 8 | import javax.json.JsonArray; 9 | import javax.json.JsonObject; 10 | import javax.json.stream.JsonCollectors; 11 | import javax.ws.rs.GET; 12 | import javax.ws.rs.Path; 13 | import javax.ws.rs.PathParam; 14 | import javax.ws.rs.core.Context; 15 | import javax.ws.rs.core.UriInfo; 16 | import java.net.URI; 17 | 18 | @Path("books") 19 | public class BooksResource { 20 | 21 | @Inject 22 | BookStore bookStore; 23 | 24 | @Context 25 | UriInfo uriInfo; 26 | 27 | @GET 28 | public JsonArray getBooks() { 29 | return bookStore.getBooks().stream() 30 | .map(this::buildBookJson) 31 | .collect(JsonCollectors.toJsonArray()); 32 | } 33 | 34 | @GET 35 | @Path("{id}") 36 | public JsonObject getBook(@PathParam("id") long id) { 37 | Book book = bookStore.getBook(id); 38 | return buildBookJson(book); 39 | } 40 | 41 | private JsonObject buildBookJson(Book book) { 42 | URI selfUri = uriInfo.getBaseUriBuilder() 43 | .path(BooksResource.class) 44 | .path(BooksResource.class, "getBook") 45 | .build(book.getId()); 46 | 47 | // ... 48 | URI authorUri = null; 49 | 50 | return Json.createObjectBuilder() 51 | .add("name", book.getName()) 52 | .add("author", book.getAuthor()) 53 | .add("isbn", book.getIsbn()) 54 | .add("_links", Json.createObjectBuilder() 55 | .add("self", selfUri.toString()) 56 | .add("author", authorUri.toString())) 57 | .build(); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Chapter07/systemtest/src/test/java/com/example/cars/CarCreationTest.java: -------------------------------------------------------------------------------- 1 | package com.example.cars; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import java.util.List; 7 | 8 | import static org.assertj.core.api.Assertions.assertThat; 9 | import static org.junit.Assert.fail; 10 | 11 | public class CarCreationTest { 12 | 13 | private CarManufacturer carManufacturer; 14 | private AssemblyLine assemblyLine; 15 | 16 | @Before 17 | public void setUp() { 18 | carManufacturer = new CarManufacturer(); 19 | assemblyLine = new AssemblyLine(); 20 | 21 | carManufacturer.verifyRunning(); 22 | assemblyLine.initBehavior(); 23 | } 24 | 25 | @Test 26 | public void testCarCreation() { 27 | String id = "X123A345"; 28 | EngineType engine = EngineType.DIESEL; 29 | Color color = Color.RED; 30 | 31 | verifyCarNotExistent(id); 32 | 33 | String carId = carManufacturer.createCar(id, engine, color); 34 | assertThat(carId).isEqualTo(id); 35 | 36 | verifyCar(id, engine, color); 37 | 38 | verifyCarExistent(id); 39 | 40 | assemblyLine.verifyInstructions(id); 41 | } 42 | 43 | private void verifyCarExistent(String id) { 44 | List cars = carManufacturer.getCarList(); 45 | if (cars.stream().noneMatch(c -> c.getId().equals(id))) 46 | fail("Car with id '" + id + "' not existent"); 47 | } 48 | 49 | private void verifyCarNotExistent(String id) { 50 | List cars = carManufacturer.getCarList(); 51 | if (cars.stream().anyMatch(c -> c.getId().equals(id))) 52 | fail("Car with id '" + id + "' existed before"); 53 | } 54 | 55 | private void verifyCar(String carId, EngineType engine, Color color) { 56 | Car car = carManufacturer.getCar(carId); 57 | assertThat(car.getEngine()).isEqualTo(engine); 58 | assertThat(car.getColor()).isEqualTo(color); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Chapter07/cars/src/test/java/com/example/cars/CarAssert.java: -------------------------------------------------------------------------------- 1 | package com.example.cars; 2 | 3 | import com.example.cars.entity.Car; 4 | import com.example.cars.entity.Color; 5 | import com.example.cars.entity.EngineType; 6 | import com.example.cars.entity.Specification; 7 | import org.assertj.core.api.AbstractAssert; 8 | 9 | import java.util.Objects; 10 | 11 | public class CarAssert extends AbstractAssert { 12 | 13 | public CarAssert(Car actual) { 14 | super(actual, CarAssert.class); 15 | } 16 | 17 | public static CarAssert assertThat(Car actual) { 18 | return new CarAssert(actual); 19 | } 20 | 21 | public CarAssert hasEngine(EngineType type) { 22 | isNotNull(); 23 | 24 | if (!Objects.equals(actual.getSpecification().getEngine(), type)) { 25 | failWithMessage("Expected car's engine to be <%s> but was <%s>", type, actual.getSpecification().getEngine()); 26 | } 27 | return this; 28 | } 29 | 30 | public CarAssert satisfies(Specification spec) { 31 | isNotNull(); 32 | 33 | if (!Objects.equals(actual.getSpecification(), spec)) { 34 | failWithMessage("Expected car to match <%s> but was <%s>", spec, actual.getSpecification()); 35 | } 36 | return this; 37 | } 38 | 39 | public CarAssert hasColor(Color color) { 40 | isNotNull(); 41 | 42 | if (!Objects.equals(actual.getSpecification().getColor(), color)) { 43 | failWithMessage("Expected car's color to be <%s> but was <%s>", color, actual.getSpecification().getColor()); 44 | } 45 | return this; 46 | } 47 | 48 | public CarAssert isEnvironmentalFriendly() { 49 | isNotNull(); 50 | 51 | if (actual.getSpecification().getEngine() != EngineType.ELECTRIC) { 52 | failWithMessage("Expected car with environmental friendly engine but was <%s>", actual.getSpecification().getEngine()); 53 | } 54 | return this; 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/jaxrs_hypermedia/boundary/programmatic_builder/BooksResource.java: -------------------------------------------------------------------------------- 1 | package com.example.jaxrs_hypermedia.boundary.programmatic_builder; 2 | 3 | import com.example.jaxrs_hypermedia.entity.Book; 4 | import com.example.jaxrs_hypermedia.boundary.BookStore; 5 | 6 | import javax.inject.Inject; 7 | import javax.json.JsonArray; 8 | import javax.json.JsonObject; 9 | import javax.json.stream.JsonCollectors; 10 | import javax.ws.rs.GET; 11 | import javax.ws.rs.Path; 12 | import javax.ws.rs.PathParam; 13 | import javax.ws.rs.core.Context; 14 | import javax.ws.rs.core.UriInfo; 15 | import java.net.URI; 16 | 17 | @Path("books") 18 | public class BooksResource { 19 | 20 | @Inject 21 | BookStore bookStore; 22 | 23 | @Context 24 | UriInfo uriInfo; 25 | 26 | @Inject 27 | EntityBuilder entityBuilder; 28 | 29 | @GET 30 | public JsonArray getBooks() { 31 | return bookStore.getBooks().stream() 32 | .map((Book book) -> { 33 | URI selfUri = uriInfo.getBaseUriBuilder() 34 | .path(BooksResource.class) 35 | .path(BooksResource.class, "getBook") 36 | .build(book.getId()); 37 | return entityBuilder.buildForBook(book, selfUri); 38 | }) 39 | .collect(JsonCollectors.toJsonArray()); 40 | } 41 | 42 | @GET 43 | @Path("{id}") 44 | public JsonObject getBook(@PathParam("id") long id) { 45 | Book book = bookStore.getBook(id); 46 | return buildBookJson(book); 47 | } 48 | 49 | private JsonObject buildBookJson(Book book) { 50 | URI selfUri = uriInfo.getBaseUriBuilder() 51 | .path(BooksResource.class) 52 | .path(BooksResource.class, "getBook") 53 | .build(book.getId()); 54 | 55 | // ... 56 | URI authorUri = null; 57 | 58 | return entityBuilder.buildForBook(book, selfUri); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Chapter07/cars-cdi-unit/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.example.cars 6 | car-manufacture 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | 11 | 12 | javax 13 | javaee-api 14 | 8.0 15 | provided 16 | 17 | 18 | junit 19 | junit 20 | 4.12 21 | test 22 | 23 | 24 | org.mockito 25 | mockito-core 26 | 2.8.47 27 | test 28 | 29 | 30 | org.assertj 31 | assertj-core 32 | 3.8.0 33 | test 34 | 35 | 36 | org.jglue.cdi-unit 37 | cdi-unit 38 | 3.1.2 39 | test 40 | 41 | 42 | 43 | 44 | car-manufacture 45 | 46 | 47 | 48 | 1.8 49 | 1.8 50 | false 51 | UTF-8 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Chapter08/restaurant/src/main/java/com/example/restaurant/orders/boundary/OrderEventHandler.java: -------------------------------------------------------------------------------- 1 | package com.example.restaurant.orders.boundary; 2 | 3 | import com.example.restaurant.events.control.EventConsumer; 4 | import com.example.restaurant.events.entity.MealEvent; 5 | import com.example.restaurant.events.entity.MealFailedNoIngredients; 6 | import com.example.restaurant.events.entity.MealPreparationStarted; 7 | import com.example.restaurant.events.entity.MealPrepared; 8 | 9 | import javax.annotation.PostConstruct; 10 | import javax.annotation.PreDestroy; 11 | import javax.annotation.Resource; 12 | import javax.ejb.Singleton; 13 | import javax.ejb.Startup; 14 | import javax.enterprise.concurrent.ManagedExecutorService; 15 | import javax.enterprise.event.Event; 16 | import javax.enterprise.event.Observes; 17 | import javax.inject.Inject; 18 | import java.util.Properties; 19 | 20 | @Startup 21 | @Singleton 22 | public class OrderEventHandler { 23 | 24 | private EventConsumer eventConsumer; 25 | 26 | @Resource 27 | ManagedExecutorService mes; 28 | 29 | @Inject 30 | Properties kafkaProperties; 31 | 32 | @Inject 33 | Event events; 34 | 35 | @Inject 36 | OrderService orderService; 37 | 38 | public void handle(@Observes MealPreparationStarted event) { 39 | orderService.startOrder(event.getOrderId()); 40 | } 41 | 42 | public void handle(@Observes MealFailedNoIngredients event) { 43 | orderService.cancelOrder(event.getOrderId(), "Insufficient ingredients"); 44 | } 45 | 46 | public void handle(@Observes MealPrepared event) { 47 | orderService.deliverMeal(event.getOrderId()); 48 | } 49 | 50 | @PostConstruct 51 | private void init() { 52 | kafkaProperties.put("group.id", "order-handler"); 53 | String chef = kafkaProperties.getProperty("chef.topic"); 54 | 55 | eventConsumer = new EventConsumer(kafkaProperties, ev -> events.fire(ev), chef); 56 | 57 | mes.execute(eventConsumer); 58 | } 59 | 60 | @PreDestroy 61 | public void close() { 62 | eventConsumer.stop(); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /Chapter02/example/src/main/java/com/example/async/jaxrs_client/plain/WeatherForecast.java: -------------------------------------------------------------------------------- 1 | package com.example.async.jaxrs_client.plain; 2 | 3 | import com.example.async.jaxrs_client.Forecast; 4 | 5 | import javax.annotation.PostConstruct; 6 | import javax.annotation.PreDestroy; 7 | import javax.annotation.Resource; 8 | import javax.enterprise.concurrent.ManagedExecutorService; 9 | import javax.enterprise.context.ApplicationScoped; 10 | import javax.ws.rs.client.Client; 11 | import javax.ws.rs.client.ClientBuilder; 12 | import javax.ws.rs.client.WebTarget; 13 | import javax.ws.rs.core.MediaType; 14 | import java.util.List; 15 | import java.util.concurrent.CompletableFuture; 16 | import java.util.stream.Collectors; 17 | 18 | import static java.util.Arrays.asList; 19 | 20 | @ApplicationScoped 21 | public class WeatherForecast { 22 | 23 | private Client client; 24 | private List targets; 25 | 26 | @Resource 27 | ManagedExecutorService mes; 28 | 29 | @PostConstruct 30 | private void initClient() { 31 | client = ClientBuilder.newClient(); 32 | targets = asList( 33 | client.target("..."), 34 | client.target("...") 35 | ); 36 | } 37 | 38 | public Forecast getAverageForecast() { 39 | return invokeTargetsAsync() 40 | .stream() 41 | .map(CompletableFuture::join) 42 | .reduce(this::calculateAverage) 43 | .orElseThrow(() -> new IllegalStateException("No weather service available")); 44 | } 45 | 46 | private List> invokeTargetsAsync() { 47 | return targets.stream() 48 | .map(t -> CompletableFuture.supplyAsync(() -> t 49 | .request(MediaType.APPLICATION_JSON_TYPE) 50 | .get(Forecast.class), mes)) 51 | .collect(Collectors.toList()); 52 | } 53 | 54 | private Forecast calculateAverage(Forecast first, Forecast second) { 55 | // ... 56 | return first; 57 | } 58 | 59 | @PreDestroy 60 | public void closeClient() { 61 | client.close(); 62 | } 63 | 64 | } 65 | --------------------------------------------------------------------------------