├── .gitattributes ├── .gitignore ├── Chapter01 ├── 10_when.kts ├── 11_NullType.kts ├── 11a_NullCheck.kts ├── 12_LambdaExpressions_HelloWorld.kts ├── 12a_LambdaExpressions.kts ├── 12b_LambdaExpressions.kts ├── 13_ExtensionFunctons.kts ├── 14_Class.kts ├── 14a_PrimaryConstructor.kts ├── 14b_PrimaryConstructor.kts ├── 14c_SecondaryConstructor.kts ├── 15a_StaticMethods.kts ├── 15b_StaticMethods.kts ├── 15c_StaticMethods.kts ├── 16_Coroutines.kts ├── 3_valnVar.kts ├── 3a_TypeInference.kts ├── 4_StringTemplate.kts ├── 4a_StringTemplate.kts ├── 4b_MultilineString.kts ├── 5_Expressions.kts ├── 6_Function.kts ├── 6a_Function.kts ├── 6b_Function.kts ├── 6c_Function.kts ├── 6d_Function.kts ├── 6e_DefaultArguments.kts ├── 6f_NamedArguments.kts ├── 6g_NamedArguments.kts ├── 7a_VarArgs.kts ├── 7b_Spread.kts ├── 7c_Spread.kts ├── 8a_ForLoop.kts ├── 8b_ForLoop_Until.kts ├── 8c_ForLoop_Step.kts ├── 8d_ForLoop_downTo.kts ├── 8e1_ForLoop_downTo.kts ├── 8e2_ForLoop_downTo_Step.kts ├── 8e_ForLoop_downTo_Step.kts ├── 9_IteratingOverList.kts ├── 9a_IteratingOverListIndex.kts ├── 9b_IteratingOverListIndex.kts ├── 9c_IteratingOverList.kts ├── HelloWorld.jar ├── HelloWorld.kt └── HelloWorld.kts ├── Chapter02 ├── Intermixing Java and Kotlin │ ├── myJavaApp │ │ ├── pom.xml │ │ └── src │ │ │ ├── main │ │ │ └── java │ │ │ │ └── org │ │ │ │ └── rao │ │ │ │ └── kotlin │ │ │ │ ├── App.java │ │ │ │ ├── HelloWorld.kt │ │ │ │ ├── ImmutabilityTest.kt │ │ │ │ ├── InlineLambda.kt │ │ │ │ ├── LambdaEvenNumbers.kt │ │ │ │ ├── NewApp.kt │ │ │ │ ├── NullabilityTest.kt │ │ │ │ ├── Test.kt │ │ │ │ └── UserUtil.java │ │ │ └── test │ │ │ └── java │ │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── HelloWorldTest.kt │ └── myKotlinApp │ │ ├── pom.xml │ │ └── src │ │ ├── main │ │ └── kotlin │ │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── intro │ │ │ ├── DeviceApp.java │ │ │ └── UserApp.kt │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── intro │ │ └── HelloTest.kt ├── MyApp-Eclipse │ └── src │ │ └── PrimeNumber.kt ├── MyApp │ └── src │ │ └── Factorial.kt └── coroutines │ ├── pom.xml │ └── src │ ├── main │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── intro │ │ ├── 16_coroutines.kt │ │ ├── 17_async_coroutines.kt │ │ ├── 18_async_coroutines.kt │ │ ├── Hello.kt │ │ └── coroutines.kt │ └── test │ └── kotlin │ └── org │ └── rao │ └── kotlin │ └── intro │ └── HelloTest.kt ├── Chapter03 ├── BeanValidation │ ├── pom.xml │ └── src │ │ ├── main │ │ └── kotlin │ │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── javaee │ │ │ └── Person.kt │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── javaee │ │ └── PersonTest.kt ├── ServletExample-EJB │ ├── pom.xml │ └── src │ │ └── main │ │ ├── java │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── javaee │ │ │ └── servletexample │ │ │ ├── AuthController.kt │ │ │ └── StatelessEjb.kt │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp └── ServletExample │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── org │ │ └── rao │ │ └── kotlin │ │ └── javaee │ │ └── servletexample │ │ └── AuthController.kt │ └── webapp │ ├── WEB-INF │ └── web.xml │ └── index.jsp ├── Chapter04 ├── CDI-Examples │ ├── pom.xml │ └── src │ │ ├── main │ │ └── kotlin │ │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── cdi │ │ │ ├── App.kt │ │ │ ├── IdGenerator.kt │ │ │ ├── Identity.kt │ │ │ ├── IdentityCreator.kt │ │ │ ├── IdentityRepository.java │ │ │ ├── InputData.kt │ │ │ ├── NativeEnglishSpeaker.kt │ │ │ ├── Person.kt │ │ │ ├── PersonCreationEvent.kt │ │ │ ├── PersonCreationEventListener.kt │ │ │ ├── PreferredLanguage.kt │ │ │ ├── PreferredLanguageProducer.kt │ │ │ ├── PreferredLanguageProducer1.kt │ │ │ └── resource │ │ │ ├── Da.java │ │ │ └── Dao.kt │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── cdi │ │ └── HelloTest.kt ├── CDI-Interceptors │ ├── pom.xml │ └── src │ │ ├── main │ │ └── kotlin │ │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── cdi │ │ │ ├── App.kt │ │ │ ├── IdGenerator.kt │ │ │ ├── IdentityCreator.kt │ │ │ ├── IdentityRepository.kt │ │ │ ├── InputData.kt │ │ │ ├── NativeEnglishSpeaker.kt │ │ │ ├── Person.kt │ │ │ ├── PersonCreationEvent.kt │ │ │ ├── PersonCreationEventListener.kt │ │ │ ├── PreferredLanguage.kt │ │ │ ├── PreferredLanguageProducer.kt │ │ │ ├── PreferredLanguageProducer1.kt │ │ │ ├── Resource.kt │ │ │ ├── identity │ │ │ ├── Da.kt │ │ │ ├── Da1.kt │ │ │ ├── Dao.kt │ │ │ └── Dao1.kt │ │ │ └── interceptors │ │ │ ├── AuditInterceptor.kt │ │ │ ├── AuditProcessor.kt │ │ │ ├── Auditable.kt │ │ │ └── Auditor.kt │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── cdi │ │ └── HelloTest.kt └── JSF-App │ ├── pom.xml │ └── src │ └── main │ ├── java │ └── org │ │ └── rao │ │ └── kotlin │ │ └── jsf │ │ └── Person.kt │ └── webapp │ ├── WEB-INF │ └── web.xml │ ├── index.html │ ├── index.jsp │ └── index.xhtml ├── Chapter05 ├── JPA-Examples-mapping-relations │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── kotlin │ │ │ ├── Controller │ │ │ │ └── App.kt │ │ │ └── org │ │ │ │ └── rao │ │ │ │ └── kotlin │ │ │ │ └── jpa │ │ │ │ └── entity │ │ │ │ ├── Address.kt │ │ │ │ ├── ContactDetails.kt │ │ │ │ ├── IdGenerator.kt │ │ │ │ ├── Identity.kt │ │ │ │ ├── IdentityCreator.kt │ │ │ │ ├── InputData.kt │ │ │ │ ├── Person.kt │ │ │ │ ├── PersonName.kt │ │ │ │ ├── PreferredLanguage.kt │ │ │ │ └── Queries.kt │ │ └── resources │ │ │ └── META-INF │ │ │ └── persistence.xml │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── jpa │ │ └── HelloTest.kt ├── JPA-Examples │ ├── pom.xml │ └── src │ │ ├── main │ │ └── kotlin │ │ │ ├── Controller │ │ │ └── App.kt │ │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── jpa │ │ │ └── entity │ │ │ ├── Address.kt │ │ │ ├── IdGenerator.kt │ │ │ ├── Identity.kt │ │ │ ├── IdentityCreator.kt │ │ │ ├── InputData.kt │ │ │ ├── Person.kt │ │ │ ├── PersonName.kt │ │ │ ├── PreferredLanguage.kt │ │ │ └── Queries.kt │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── jpa │ │ └── HelloTest.kt ├── JPA-simple-project │ ├── META-INF │ │ └── application.xml │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── kotlin │ │ │ └── org │ │ │ │ └── rao │ │ │ │ └── kotlin │ │ │ │ ├── App.kt │ │ │ │ ├── dao │ │ │ │ ├── PersonDao.kt │ │ │ │ └── PersonDaoImpl.kt │ │ │ │ ├── entity │ │ │ │ ├── Address.kt │ │ │ │ ├── ContactDetails.kt │ │ │ │ ├── Person.kt │ │ │ │ ├── PersonName.kt │ │ │ │ ├── PreferredLanguage.kt │ │ │ │ └── Queries.kt │ │ │ │ └── service │ │ │ │ ├── PersonService.kt │ │ │ │ └── PersonServiceImpl.kt │ │ └── resources │ │ │ └── META-INF │ │ │ ├── beans.xml │ │ │ └── persistence.xml │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── jpa │ │ └── PersonTest.kt └── docker │ ├── docker-compose.yml │ └── postgres │ └── Dockerfile ├── Chapter06 ├── Point-To-Point-Messaging-MessageType-Acknowledgement │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── kotlin │ │ │ └── org │ │ │ │ └── rao │ │ │ │ └── kotlin │ │ │ │ └── messaging │ │ │ │ ├── Consumer.kt │ │ │ │ ├── InitialContextProducer.kt │ │ │ │ └── Producer.kt │ │ └── resources │ │ │ └── META-INF │ │ │ └── beans.xml │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── messaging │ │ ├── ConsumerTest.kt │ │ └── ProducerTest.kt ├── Point-To-Point-Messaging-MessageType-Transactions │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── kotlin │ │ │ └── org │ │ │ │ └── rao │ │ │ │ └── kotlin │ │ │ │ └── messaging │ │ │ │ ├── Consumer.kt │ │ │ │ ├── InitialContextProducer.kt │ │ │ │ └── Producer.kt │ │ └── resources │ │ │ └── META-INF │ │ │ └── beans.xml │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── messaging │ │ ├── ConsumerTest.kt │ │ └── ProducerTest.kt ├── Point-To-Point-Messaging-MessageType │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── kotlin │ │ │ └── org │ │ │ │ └── rao │ │ │ │ └── kotlin │ │ │ │ └── messaging │ │ │ │ ├── Consumer.kt │ │ │ │ ├── InitialContextProducer.kt │ │ │ │ └── Producer.kt │ │ └── resources │ │ │ └── META-INF │ │ │ └── beans.xml │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── messaging │ │ ├── ConsumerTest.kt │ │ └── ProducerTest.kt ├── Point-To-Point-Messaging │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── kotlin │ │ │ └── org │ │ │ │ └── rao │ │ │ │ └── kotlin │ │ │ │ └── messaging │ │ │ │ ├── Consumer.kt │ │ │ │ ├── InitialContextProducer.kt │ │ │ │ └── Producer.kt │ │ └── resources │ │ │ └── META-INF │ │ │ └── beans.xml │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── messaging │ │ ├── ConsumerTest.kt │ │ └── ProducerTest.kt ├── Publish-Subscribe-Acknowledgement │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── kotlin │ │ │ └── org │ │ │ │ └── rao │ │ │ │ └── kotlin │ │ │ │ └── messaging │ │ │ │ ├── InitialContextProducer.kt │ │ │ │ ├── Publisher.kt │ │ │ │ └── Subscriber.kt │ │ └── resources │ │ │ └── META-INF │ │ │ └── beans.xml │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── messaging │ │ ├── PublisherTest.kt │ │ └── SubscriberTest.kt ├── Publish-Subscribe-Transactions │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── kotlin │ │ │ └── org │ │ │ │ └── rao │ │ │ │ └── kotlin │ │ │ │ └── messaging │ │ │ │ ├── InitialContextProducer.kt │ │ │ │ ├── Publisher.kt │ │ │ │ └── Subscriber.kt │ │ └── resources │ │ │ └── META-INF │ │ │ └── beans.xml │ │ └── test │ │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── messaging │ │ ├── PublisherTest.kt │ │ └── SubscriberTest.kt └── Publish-Subscribe │ ├── pom.xml │ └── src │ ├── main │ ├── kotlin │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── messaging │ │ │ ├── InitialContextProducer.kt │ │ │ ├── Publisher.kt │ │ │ └── Subscriber.kt │ └── resources │ │ └── META-INF │ │ └── beans.xml │ └── test │ └── kotlin │ └── org │ └── rao │ └── kotlin │ └── messaging │ ├── PublisherTest.kt │ └── SubscriberTest.kt ├── Chapter07 ├── IdentityApp │ ├── pom.xml │ └── src │ │ └── main │ │ ├── kotlin │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── rest │ │ │ └── organization │ │ │ ├── context │ │ │ └── Initailizer.kt │ │ │ ├── controller │ │ │ └── OrganizationConroller.kt │ │ │ ├── dao │ │ │ └── OrganizationDao.kt │ │ │ ├── entity │ │ │ ├── OrganizationEntity.kt │ │ │ └── Queries.kt │ │ │ ├── model │ │ │ ├── OrganizationRequest.kt │ │ │ └── OrganizationResponse.kt │ │ │ └── service │ │ │ └── OrganizaionService.kt │ │ ├── resources │ │ └── META-INF │ │ │ ├── beans.xml │ │ │ └── persistence.xml │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp └── RestExample │ ├── pom.xml │ └── src │ └── main │ ├── kotlin │ └── org │ │ └── rao │ │ └── kotlin │ │ └── rest │ │ └── Controller.kt │ ├── resources │ └── META-INF │ │ └── beans.xml │ └── webapp │ └── WEB-INF │ └── web.xml ├── Chapter08 ├── 01_IdentityStore Example │ ├── pom.xml │ └── src │ │ └── main │ │ ├── kotlin │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── rest │ │ │ ├── ApplicationConfig.kt │ │ │ ├── Controller.kt │ │ │ └── Filter.java │ │ ├── resources │ │ └── META-INF │ │ │ └── beans.xml │ │ └── webapp │ │ └── WEB-INF │ │ └── web.xml ├── 02_SecureIdentityApp │ ├── pom.xml │ └── src │ │ └── main │ │ ├── kotlin │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── rest │ │ │ └── organization │ │ │ ├── context │ │ │ └── Initailizer.kt │ │ │ ├── controller │ │ │ └── OrganizationConroller.kt │ │ │ ├── dao │ │ │ └── OrganizationDao.kt │ │ │ ├── entity │ │ │ └── OrganizationEntity.kt │ │ │ ├── filter │ │ │ ├── AuthException.kt │ │ │ ├── AuthFilter.kt │ │ │ ├── CallBackHandlerImpl.java │ │ │ ├── MessageInfoImpl.java │ │ │ ├── MyAuthMechanism.kt │ │ │ └── MyIdentityStore.kt │ │ │ ├── model │ │ │ ├── OrganizationRequest.kt │ │ │ └── OrganizationResponse.kt │ │ │ └── service │ │ │ └── OrganizaionService.kt │ │ ├── resources │ │ └── META-INF │ │ │ ├── beans.xml │ │ │ └── persistence.xml │ │ └── webapp │ │ ├── WEB-INF │ │ └── web.xml │ │ └── index.jsp └── 03_JWT_Example │ ├── pom.xml │ └── src │ └── main │ ├── kotlin │ └── org │ │ └── rao │ │ └── kotlin │ │ └── rest │ │ └── security │ │ └── organization │ │ ├── context │ │ └── Initailizer.kt │ │ ├── controller │ │ ├── LoginController.kt │ │ └── OrganizationConroller.kt │ │ ├── dao │ │ └── OrganizationDao.kt │ │ ├── entity │ │ └── OrganizationEntity.kt │ │ ├── filter │ │ ├── AuthException.kt │ │ ├── AuthFilter.kt │ │ ├── CallBackHandlerImpl.java │ │ ├── MessageInfoImpl.java │ │ ├── MyAuthMechanism.kt │ │ └── MyIdentityStore.kt │ │ ├── model │ │ ├── OrganizationRequest.kt │ │ └── OrganizationResponse.kt │ │ ├── service │ │ └── OrganizaionService.kt │ │ └── util │ │ └── JWTUtil.kt │ ├── resources │ └── META-INF │ │ ├── beans.xml │ │ └── persistence.xml │ └── webapp │ └── WEB-INF │ └── web.xml ├── Chapter09 └── MicroServices │ ├── AuthenticationService │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── kotlin │ │ │ └── org │ │ │ │ └── rao │ │ │ │ └── kotlin │ │ │ │ └── microservices │ │ │ │ └── authentication │ │ │ │ ├── auth │ │ │ │ ├── CallBackHandlerImpl.kt │ │ │ │ ├── CustomAuthMechanism.kt │ │ │ │ └── MessageInfoImpl.kt │ │ │ │ ├── controller │ │ │ │ └── AuthenticationController.kt │ │ │ │ ├── jwt │ │ │ │ └── JWTUtil.kt │ │ │ │ └── store │ │ │ │ └── MyIdentityStore.kt │ │ └── webapp │ │ │ └── WEB-INF │ │ │ └── web.xml │ │ └── test │ │ └── kotlin │ │ └── MyTest.java │ ├── IdentityService │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── kotlin │ │ │ └── org │ │ │ │ └── rao │ │ │ │ └── kotlin │ │ │ │ ├── filter │ │ │ │ ├── AuthFilter.kt │ │ │ │ ├── JWTVerifier.kt │ │ │ │ └── JerseyClientPost.kt │ │ │ │ └── microservices │ │ │ │ └── identity │ │ │ │ ├── exception │ │ │ │ ├── IdentityAlreadyExistsException.kt │ │ │ │ ├── IdentityException.kt │ │ │ │ └── IdentityNotFoundException.kt │ │ │ │ ├── organization │ │ │ │ ├── controller │ │ │ │ │ └── OrganizationController.kt │ │ │ │ ├── dao │ │ │ │ │ ├── OrganizationDao.kt │ │ │ │ │ └── OrganizationDaoImpl.kt │ │ │ │ ├── entity │ │ │ │ │ └── OrganizationEntity.kt │ │ │ │ ├── model │ │ │ │ │ └── Organization.kt │ │ │ │ └── service │ │ │ │ │ ├── OrganizationService.kt │ │ │ │ │ └── OrganizationServiceImpl.kt │ │ │ │ ├── person │ │ │ │ ├── controller │ │ │ │ │ └── PersonController.kt │ │ │ │ ├── dao │ │ │ │ │ ├── PersonDao.kt │ │ │ │ │ └── PersonDaoImpl.kt │ │ │ │ ├── entity │ │ │ │ │ ├── AddressEntity.kt │ │ │ │ │ ├── ContactDetailsEntity.kt │ │ │ │ │ ├── PersonEntity.kt │ │ │ │ │ ├── PersonNameEntity.kt │ │ │ │ │ ├── PreferredLanguage.kt │ │ │ │ │ └── Queries.kt │ │ │ │ ├── model │ │ │ │ │ ├── Address.kt │ │ │ │ │ ├── ContactDetails.kt │ │ │ │ │ ├── Person.kt │ │ │ │ │ ├── PersonName.kt │ │ │ │ │ └── PreferredLanguage.kt │ │ │ │ └── service │ │ │ │ │ ├── PersonService.kt │ │ │ │ │ └── PersonServiceImpl.kt │ │ │ │ ├── response │ │ │ │ ├── ErrorResponse.kt │ │ │ │ └── GenericResponse.kt │ │ │ │ └── util │ │ │ │ ├── Constants.kt │ │ │ │ └── ResponseUtil.kt │ │ ├── resources │ │ │ └── META-INF │ │ │ │ ├── beans.xml │ │ │ │ └── persistence.xml │ │ └── webapp │ │ │ └── WEB-INF │ │ │ └── web.xml │ │ └── test │ │ ├── kotlin │ │ └── org │ │ │ └── rao │ │ │ └── kotlin │ │ │ └── microservices │ │ │ └── identity │ │ │ ├── organization │ │ │ ├── GetOrgIntTest.kt │ │ │ ├── GetOrgIntTestRefactoredCode.kt │ │ │ ├── MainTest.kt │ │ │ └── TestUtil.kt │ │ │ └── person │ │ │ └── CreatePersonTest.kt │ │ └── resources │ │ └── mockito-extensions │ │ └── org.mockito.plugins.MockMaker │ └── pom.xml ├── Chapter10 ├── GarbageCollection │ ├── pom.xml │ └── src │ │ └── main │ │ └── kotlin │ │ ├── Example.kt │ │ └── Person.kt ├── MemoryLeak │ ├── pom.xml │ └── src │ │ └── main │ │ └── java │ │ ├── HelloWorld.kt │ │ └── MemoryLeakDemo.kt └── flight_recording_180151MemoryLeakDemo1080_jmx.jfr ├── Chapter11 └── DesignPatterns │ ├── pom.xml │ └── src │ ├── main │ └── kotlin │ │ └── org │ │ └── rao │ │ └── kotlin │ │ └── designpatterns │ │ ├── builder │ │ └── model │ │ │ ├── Person.kt │ │ │ ├── PersonBuilder.kt │ │ │ └── Test.kt │ │ ├── chain │ │ ├── ChainOfResponsibilityDemo.kt │ │ ├── handler │ │ │ ├── AbstractAuditHandler.kt │ │ │ ├── Constants.kt │ │ │ ├── DefaultAuditHandler.kt │ │ │ ├── DeviceAuditHandler.kt │ │ │ ├── GroupAuditHandler.kt │ │ │ ├── Handler.kt │ │ │ ├── OrganizationAuditHandler.kt │ │ │ └── PersonAuditHandler.kt │ │ └── model │ │ │ ├── audit │ │ │ ├── AuditMessage.kt │ │ │ ├── AuditService.kt │ │ │ ├── AuditServiceImpl.kt │ │ │ ├── AuditUtil.kt │ │ │ ├── Event.kt │ │ │ ├── ParticipantObject.kt │ │ │ └── Request.kt │ │ │ └── identity │ │ │ ├── Device.kt │ │ │ ├── Group.kt │ │ │ ├── Identity.kt │ │ │ ├── IdentityType.kt │ │ │ ├── Organization.kt │ │ │ └── Person.kt │ │ ├── decorator │ │ ├── DecoratorDemo.kt │ │ ├── component │ │ │ └── Window.kt │ │ ├── concretedecorator │ │ │ ├── color │ │ │ │ ├── BrightSilver.kt │ │ │ │ ├── PebbleGray.kt │ │ │ │ └── Red.kt │ │ │ ├── material │ │ │ │ ├── Aluminum.kt │ │ │ │ ├── Clad.kt │ │ │ │ ├── Glass.kt │ │ │ │ ├── Vinyl.kt │ │ │ │ └── Wood.kt │ │ │ └── shape │ │ │ │ ├── Arched.kt │ │ │ │ └── Round.kt │ │ └── decorator │ │ │ └── WindowDecorator.kt │ │ ├── factory │ │ ├── usecase1 │ │ │ ├── HumiditySensor.kt │ │ │ ├── OpticalSensor.kt │ │ │ ├── Sensor.kt │ │ │ ├── SensorClient.kt │ │ │ ├── SensorFactory.kt │ │ │ └── TemparatureSensor.kt │ │ ├── usecase11 │ │ │ ├── HumiditySensor.kt │ │ │ ├── OpticalSensor.kt │ │ │ ├── Sensor.kt │ │ │ ├── SensorClient.kt │ │ │ ├── SensorFactory.kt │ │ │ ├── TemparatureSensor.kt │ │ │ └── enum │ │ │ │ └── SensorType.kt │ │ └── usecase2 │ │ │ ├── HumiditySensor.kt │ │ │ ├── OpticalSensor.kt │ │ │ ├── Sensor.kt │ │ │ ├── SensorClient.kt │ │ │ ├── SensorFactory.kt │ │ │ ├── TemparatureSensor.kt │ │ │ └── enum │ │ │ └── SensorType.kt │ │ ├── observer │ │ ├── DAXTradeObserver.kt │ │ ├── LupinTradeObserver.kt │ │ ├── Observer.kt │ │ ├── SampleData.kt │ │ ├── Stock.kt │ │ ├── StockData.kt │ │ ├── Subject.kt │ │ ├── Test.kt │ │ └── TradeEnum.java │ │ └── singleton │ │ ├── Singleton.kt │ │ ├── multithread │ │ └── Singleton.java │ │ └── staticinitialization │ │ └── Singleton.java │ └── test │ └── kotlin │ └── org │ └── rao │ └── kotlin │ └── designpatterns │ ├── factory │ └── SensorClient.java │ └── singleton │ └── SingletonTest.kt └── README.md /.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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /Chapter01/10_when.kts: -------------------------------------------------------------------------------- 1 | fun processInput(input: Any) { 2 | when(input) { 3 | 10 -> println("It is 10") 4 | 98,99 -> println("It is 98 or 99") 5 | in 101 .. 120 -> println("More than 100") 6 | is String -> println("This is ${input} of length ${input.length}") 7 | else -> println("Not known") 8 | } 9 | } 10 | processInput(10) 11 | processInput(98) 12 | processInput(99) 13 | processInput(102) 14 | processInput("hey there") 15 | processInput(Thread()) 16 | -------------------------------------------------------------------------------- /Chapter01/11_NullType.kts: -------------------------------------------------------------------------------- 1 | fun checkInput (data: String) : String? { 2 | if(data == "Web") 3 | return "Web development" 4 | return null 5 | } 6 | println(checkInput ("Web")) 7 | println(checkInput ("Android")) 8 | -------------------------------------------------------------------------------- /Chapter01/11a_NullCheck.kts: -------------------------------------------------------------------------------- 1 | fun checkInput (data: String) : String? { 2 | if(data == "Web") 3 | return "Web development" 4 | return null 5 | } 6 | var response = checkInput ("iOS") 7 | println(response) 8 | response ?.let { 9 | println("got non null value") 10 | } ?: run { 11 | println("got null") 12 | } 13 | -------------------------------------------------------------------------------- /Chapter01/12_LambdaExpressions_HelloWorld.kts: -------------------------------------------------------------------------------- 1 | val greetingLambda = { println("Hello")} 2 | 3 | greetingLambda.invoke() 4 | -------------------------------------------------------------------------------- /Chapter01/12a_LambdaExpressions.kts: -------------------------------------------------------------------------------- 1 | val greetingLambda = { user: String -> println("Hello ${user}")} 2 | greetingLambda.invoke("Tom") 3 | -------------------------------------------------------------------------------- /Chapter01/12b_LambdaExpressions.kts: -------------------------------------------------------------------------------- 1 | listOf(0,1,2,3,4,5,6,7,8,9) 2 | .filter{ e -> e % 2 == 0} 3 | .forEach{ e -> println(e)} 4 | -------------------------------------------------------------------------------- /Chapter01/13_ExtensionFunctons.kts: -------------------------------------------------------------------------------- 1 | fun String.myExtentedFunction() = toUpperCase() 2 | 3 | println("Kotlin".myExtentedFunction()) 4 | -------------------------------------------------------------------------------- /Chapter01/14_Class.kts: -------------------------------------------------------------------------------- 1 | class User { 2 | var name = "George" 3 | } 4 | val user = User() 5 | println(user.name) 6 | -------------------------------------------------------------------------------- /Chapter01/14a_PrimaryConstructor.kts: -------------------------------------------------------------------------------- 1 | class User(var firstName: String, var lastName: String) { 2 | 3 | } 4 | val user = User("Norman", "Lewis") 5 | println("First Name= ${user.firstName}") 6 | println("Last Name= ${user.lastName}") 7 | -------------------------------------------------------------------------------- /Chapter01/14b_PrimaryConstructor.kts: -------------------------------------------------------------------------------- 1 | class User(var firstName: String, val id: String) { 2 | 3 | } 4 | val user = User("Norman", "myId") 5 | println("First Name = ${user.firstName}") 6 | println("User Id = ${user.id}") 7 | -------------------------------------------------------------------------------- /Chapter01/14c_SecondaryConstructor.kts: -------------------------------------------------------------------------------- 1 | class AuditData { 2 | var message = "" 3 | var locale = "" 4 | constructor(msg: String) { 5 | message = msg 6 | } 7 | constructor(msg: String, localeInfo: String) { 8 | message = msg 9 | locale = localeInfo 10 | } 11 | } 12 | var audit = AuditData("some message") 13 | println("Message ${audit.message}") 14 | 15 | audit = AuditData("audit message", "en-US") 16 | println("Message ${audit.message}") 17 | println("Locale ${audit.locale}") 18 | -------------------------------------------------------------------------------- /Chapter01/15a_StaticMethods.kts: -------------------------------------------------------------------------------- 1 | class Person { 2 | companion object { 3 | fun foo(){ 4 | println("Static method foo() is invoked") 5 | } 6 | } 7 | } 8 | Person.foo() 9 | -------------------------------------------------------------------------------- /Chapter01/15b_StaticMethods.kts: -------------------------------------------------------------------------------- 1 | class Person { 2 | companion object { 3 | fun foo(){ 4 | println("Static method foo() is invoked") 5 | } 6 | } 7 | } 8 | Person.foo() 9 | -------------------------------------------------------------------------------- /Chapter01/15c_StaticMethods.kts: -------------------------------------------------------------------------------- 1 | class Person { 2 | companion object Util { 3 | fun foo(){ 4 | println("Static method foo() is invoked") 5 | } 6 | } 7 | } 8 | Person.Util.foo() 9 | -------------------------------------------------------------------------------- /Chapter01/16_Coroutines.kts: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | println("Kotlin Start") 3 | launch(CommonPool) { 4 | delay(2000) 5 | println("Kotlin Inside") 6 | } 7 | println("Kotlin End") 8 | } 9 | -------------------------------------------------------------------------------- /Chapter01/3_valnVar.kts: -------------------------------------------------------------------------------- 1 | val number = 10 2 | println(number) 3 | println() 4 | var anotherNumber = 15 5 | println(anotherNumber) 6 | 7 | anotherNumber = "20" 8 | println(anotherNumber) 9 | -------------------------------------------------------------------------------- /Chapter01/3a_TypeInference.kts: -------------------------------------------------------------------------------- 1 | val message = "Hi there" 2 | println(message.javaClass) -------------------------------------------------------------------------------- /Chapter01/4_StringTemplate.kts: -------------------------------------------------------------------------------- 1 | val name = "Atrium" 2 | println("Hello $name") 3 | -------------------------------------------------------------------------------- /Chapter01/4a_StringTemplate.kts: -------------------------------------------------------------------------------- 1 | val name = "Atrium" 2 | println("Hello ${name}") 3 | -------------------------------------------------------------------------------- /Chapter01/4b_MultilineString.kts: -------------------------------------------------------------------------------- 1 | val name = "Atrium" 2 | val message = """This is an example of 3 | ^multiline String $name 4 | """ 5 | println(message.trimMargin("^")) 6 | -------------------------------------------------------------------------------- /Chapter01/5_Expressions.kts: -------------------------------------------------------------------------------- 1 | val number = 5 2 | val evenOrOdd = if(number % 2 == 0) "is even number" else "is odd number" 3 | println("$number $evenOrOdd") 4 | -------------------------------------------------------------------------------- /Chapter01/6_Function.kts: -------------------------------------------------------------------------------- 1 | fun welcome(name: String, msg:String) : Unit { 2 | println("$msg $name") 3 | } 4 | welcome("Bob", "hello") 5 | -------------------------------------------------------------------------------- /Chapter01/6a_Function.kts: -------------------------------------------------------------------------------- 1 | fun welcome(name: String, msg:String) = 2 | "$msg $name" 3 | println(welcome("Bob", "hello")) 4 | -------------------------------------------------------------------------------- /Chapter01/6b_Function.kts: -------------------------------------------------------------------------------- 1 | fun welcome(name: String, msg:String) : String { 2 | return "$msg $name" 3 | } 4 | println(welcome("Bob", "hello")) 5 | -------------------------------------------------------------------------------- /Chapter01/6c_Function.kts: -------------------------------------------------------------------------------- 1 | fun welcome(name: String, msg:String) : String { 2 | return "$msg $name" 3 | } 4 | println(welcome("Bob", "hello")) 5 | -------------------------------------------------------------------------------- /Chapter01/6d_Function.kts: -------------------------------------------------------------------------------- 1 | fun welcome(name: String, msg:String) { 2 | println("$msg $name") 3 | } 4 | welcome("Bob", "hello") 5 | -------------------------------------------------------------------------------- /Chapter01/6e_DefaultArguments.kts: -------------------------------------------------------------------------------- 1 | fun welcome(name: String, msg:String = "Hey") { 2 | println("$msg $name") 3 | } 4 | welcome("Bob", "hello") 5 | welcome("Berry") 6 | -------------------------------------------------------------------------------- /Chapter01/6f_NamedArguments.kts: -------------------------------------------------------------------------------- 1 | fun welcome(name: String, msg:String = "Hey") { 2 | println("$msg $name") 3 | } 4 | welcome(msg = "How are you", name = "Mark") 5 | -------------------------------------------------------------------------------- /Chapter01/6g_NamedArguments.kts: -------------------------------------------------------------------------------- 1 | fun welcome(name: String, msg:String = "Hey") { 2 | println("$msg $name") 3 | } 4 | welcome("Vijay", msg = "Hi") 5 | -------------------------------------------------------------------------------- /Chapter01/7a_VarArgs.kts: -------------------------------------------------------------------------------- 1 | 2 | fun findMaxNumber(vararg numbers : Int) = 3 | numbers.reduce { max , e -> if(max > e) max else e } 4 | 5 | println(findMaxNumber(1,2,3,4)) 6 | -------------------------------------------------------------------------------- /Chapter01/7b_Spread.kts: -------------------------------------------------------------------------------- 1 | 2 | fun findMaxNumber(vararg numbers : Int) = 3 | numbers.reduce { max , e -> if(max > e) max else e } 4 | 5 | val numberArray = intArrayOf(25,50,75,100) 6 | println(findMaxNumber(*numberArray)) 7 | -------------------------------------------------------------------------------- /Chapter01/7c_Spread.kts: -------------------------------------------------------------------------------- 1 | 2 | fun findMaxNumber(vararg numbers : Int) = 3 | numbers.reduce { max , e -> if(max > e) max else e } 4 | 5 | val numberArray = intArrayOf(25,50,75,100) 6 | println(findMaxNumber(4,5,*numberArray,200, 10)) 7 | -------------------------------------------------------------------------------- /Chapter01/8a_ForLoop.kts: -------------------------------------------------------------------------------- 1 | for(num in 1 .. 5){ 2 | println(num) 3 | } 4 | -------------------------------------------------------------------------------- /Chapter01/8b_ForLoop_Until.kts: -------------------------------------------------------------------------------- 1 | for(num in 1 until 5){ 2 | println(num) 3 | } 4 | -------------------------------------------------------------------------------- /Chapter01/8c_ForLoop_Step.kts: -------------------------------------------------------------------------------- 1 | for(num in 1 .. 10 step 2){ 2 | println(num) 3 | } 4 | -------------------------------------------------------------------------------- /Chapter01/8d_ForLoop_downTo.kts: -------------------------------------------------------------------------------- 1 | for(num in 25 downTo 20){ 2 | println(num) 3 | } 4 | -------------------------------------------------------------------------------- /Chapter01/8e1_ForLoop_downTo.kts: -------------------------------------------------------------------------------- 1 | for(num in 25 .. 20){ 2 | println(num) 3 | } 4 | -------------------------------------------------------------------------------- /Chapter01/8e2_ForLoop_downTo_Step.kts: -------------------------------------------------------------------------------- 1 | for(num in 25 downTo 15 step -2){ 2 | println(num) 3 | } 4 | -------------------------------------------------------------------------------- /Chapter01/8e_ForLoop_downTo_Step.kts: -------------------------------------------------------------------------------- 1 | for(num in 25 downTo 15 step 2){ 2 | println(num) 3 | } 4 | -------------------------------------------------------------------------------- /Chapter01/9_IteratingOverList.kts: -------------------------------------------------------------------------------- 1 | val names = listOf("Mark", "Tina", "Williams") 2 | for(name in names) { 3 | println(name) 4 | } 5 | -------------------------------------------------------------------------------- /Chapter01/9a_IteratingOverListIndex.kts: -------------------------------------------------------------------------------- 1 | val names = listOf("Mark", "Tina", "Williams") 2 | for(index in names.indices) { 3 | println(index) 4 | } 5 | -------------------------------------------------------------------------------- /Chapter01/9b_IteratingOverListIndex.kts: -------------------------------------------------------------------------------- 1 | val names = listOf("Mark", "Tina", "Joseph") 2 | for(index in names.indices) { 3 | println("$index") 4 | } 5 | -------------------------------------------------------------------------------- /Chapter01/9c_IteratingOverList.kts: -------------------------------------------------------------------------------- 1 | val names = listOf("Mark", "Tina", "Joseph") 2 | for(index in names.indices) { 3 | println("$index: ${names.get(index)}") 4 | } 5 | -------------------------------------------------------------------------------- /Chapter01/HelloWorld.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Kotlin-for-Enterprise-Applications-using-Java-EE/9da34da228af17dab9e3e25c4d406483387e8a87/Chapter01/HelloWorld.jar -------------------------------------------------------------------------------- /Chapter01/HelloWorld.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | println("Hello ${args[0]}") 3 | } 4 | -------------------------------------------------------------------------------- /Chapter01/HelloWorld.kts: -------------------------------------------------------------------------------- 1 | println("Hello World from Script") 2 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myJavaApp/src/main/java/org/rao/kotlin/App.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin; 2 | 3 | public class App { 4 | public static void main (String[] args) { 5 | 6 | System.out.println ("Hello World from Java!"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myJavaApp/src/main/java/org/rao/kotlin/HelloWorld.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin 2 | 3 | class HelloWorld { 4 | fun hello(): String { 5 | return "hello" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myJavaApp/src/main/java/org/rao/kotlin/ImmutabilityTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin 2 | 3 | /* 4 | 5 | object ImmutabilityTest { 6 | @JvmStatic 7 | fun main(args: Array) { 8 | val msg = "Hello" 9 | msg = "hi" 10 | println(msg) 11 | } 12 | } 13 | */ 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myJavaApp/src/main/java/org/rao/kotlin/InlineLambda.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin 2 | object InlineLambda { 3 | val greetingLambda = { println("Greet from inline lambda") } 4 | @JvmStatic 5 | fun main(args: Array) { 6 | greetingLambda() 7 | } 8 | } 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myJavaApp/src/main/java/org/rao/kotlin/LambdaEvenNumbers.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin 2 | 3 | object LambdaEvenNumbers { 4 | // val greetingLambda = { println(" dsfsd Greet from inline lambda") } 5 | @JvmStatic 6 | fun main(args: Array) { 7 | listOf(0,1,2,3,4,5,6,7,8,9) 8 | .filter{ e -> e % 2 == 0} 9 | .forEach{ e -> println(e)} 10 | } 11 | } 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myJavaApp/src/main/java/org/rao/kotlin/NewApp.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin 2 | object NewApp { 3 | @JvmStatic 4 | fun main(args: Array) { 5 | println("Hello World from Kotlin!") 6 | } 7 | } 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myJavaApp/src/main/java/org/rao/kotlin/NullabilityTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin 2 | object NullabilityTest { 3 | @JvmStatic 4 | fun main(args: Array) { 5 | var userList:ArrayList ? = null 6 | var user = userList?.get(0) 7 | 8 | user ?.let { 9 | println("got the details of user") 10 | } ?: run { 11 | println("there is no user") 12 | } 13 | } 14 | } 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myJavaApp/src/main/java/org/rao/kotlin/Test.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin 2 | 3 | fun main(vararg args:String) { 4 | println("Hello, world!") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myJavaApp/src/main/java/org/rao/kotlin/UserUtil.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin; 2 | 3 | public class UserUtil 4 | { 5 | } 6 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myJavaApp/src/test/java/org/rao/kotlin/HelloWorldTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import org.mockito.Mock 6 | 7 | import org.mockito.Mockito.mock 8 | import org.mockito.Mockito.`when` 9 | 10 | class HelloWorldTest { 11 | 12 | internal var helloWorld = mock(HelloWorld::class.java) 13 | 14 | @Test 15 | fun testHello() { 16 | val mockMessage = "hello" 17 | `when`(helloWorld.hello()).thenReturn(mockMessage) 18 | 19 | val message = helloWorld.hello() 20 | 21 | Assert.assertNotNull(message) 22 | Assert.assertEquals("hello", message) 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myKotlinApp/src/main/kotlin/org/rao/kotlin/intro/DeviceApp.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.intro; 2 | 3 | class Imperative 4 | { 5 | 6 | public static void main(String[] args) 7 | { 8 | List names = 9 | Arrays.asList(“Tina”, “Tom”, ”George”); String firstNameWith3LettersLong = “”; 10 | boolean found; for (int i = 0; i < names.size(); i++) 11 | { 12 | if (names[i].length() == 3) 13 | { 14 | firstNameWith3LettersLong = name[i]; 15 | found = true; 16 | break; 17 | } 18 | } 19 | if (found) 20 | { 21 | System.out.println(firstNameWith3LettersLong); 22 | } 23 | else 24 | { 25 | System.out.println(“No results”); 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myKotlinApp/src/main/kotlin/org/rao/kotlin/intro/UserApp.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.intro 2 | 3 | fun main(args: Array) { 4 | println("Hello from Kotlin") 5 | } 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Chapter02/Intermixing Java and Kotlin/myKotlinApp/src/test/kotlin/org/rao/kotlin/intro/HelloTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.intro 2 | 3 | import org.junit.Test 4 | import kotlin.test.assertEquals 5 | 6 | class HelloTest { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Chapter02/MyApp-Eclipse/src/PrimeNumber.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | System.out.println(isPrime(2)); 3 | } 4 | private fun isPrime(number:Int): Boolean{ 5 | for(i in 2..number /2) { 6 | if(number % i == 0){ 7 | return true 8 | } 9 | } 10 | return false; 11 | } -------------------------------------------------------------------------------- /Chapter02/MyApp/src/Factorial.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | 3 | val number = 5 4 | var factorial: Int = 1 5 | for (i in 1..number) { 6 | factorial *= i 7 | } 8 | println("Factorial of $number = $factorial") 9 | } 10 | -------------------------------------------------------------------------------- /Chapter02/coroutines/pom.xml: -------------------------------------------------------------------------------- 1 | So far we discussed creating and running the coroutines. Note that all the coroutines that we created so far executes in a serial fashion. In the example 16_coroutine where we created two coroutines game1() and game2() are executed serially in the main thread. -------------------------------------------------------------------------------- /Chapter02/coroutines/src/main/kotlin/org/rao/kotlin/intro/16_coroutines.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.intro 2 | 3 | import kotlinx.coroutines.experimental.CommonPool 4 | import kotlinx.coroutines.experimental.delay 5 | import kotlinx.coroutines.experimental.launch 6 | 7 | private suspend fun game1(): String { 8 | delay(1000) 9 | return "game1" 10 | } 11 | private suspend fun game2(): String { 12 | delay(2000) 13 | return "game2" 14 | } 15 | 16 | fun main(args: Array) { 17 | println("Inside main") 18 | 19 | launch(CommonPool) { 20 | val one = game1() 21 | val two = game2() 22 | println("Game ----- " + one) 23 | println("Game ----- " + two) 24 | } 25 | Thread.sleep(4000) 26 | } 27 | -------------------------------------------------------------------------------- /Chapter02/coroutines/src/main/kotlin/org/rao/kotlin/intro/17_async_coroutines.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.intro 2 | 3 | import kotlinx.coroutines.experimental.CommonPool 4 | import kotlinx.coroutines.experimental.async 5 | import kotlinx.coroutines.experimental.delay 6 | import kotlinx.coroutines.experimental.launch 7 | 8 | private suspend fun game1(): String { 9 | delay(1000) 10 | return "game1" 11 | } 12 | private suspend fun game2(): String { 13 | delay(2000) 14 | return "game2" 15 | } 16 | 17 | fun main(args: Array) { 18 | println("Inside main") 19 | 20 | launch(CommonPool) { 21 | val one = async(CommonPool){ 22 | game1() 23 | } 24 | val two = async(CommonPool){ 25 | game2() 26 | } 27 | println("Game ----- " + one.await()+": "+two.await()) 28 | } 29 | Thread.sleep(4000) 30 | } 31 | -------------------------------------------------------------------------------- /Chapter02/coroutines/src/main/kotlin/org/rao/kotlin/intro/18_async_coroutines.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.intro 2 | 3 | import kotlinx.coroutines.experimental.CommonPool 4 | import kotlinx.coroutines.experimental.async 5 | import kotlinx.coroutines.experimental.launch 6 | 7 | fun main(args: Array) { 8 | println("Inside main") 9 | 10 | launch(CommonPool) { 11 | val deferred = (1..1_000_000) 12 | .map { n -> async { n } 13 | } 14 | var sum = deferred.sumBy { it.await() } 15 | println("sum ----- " + sum) 16 | } 17 | Thread.sleep(4000) 18 | } 19 | -------------------------------------------------------------------------------- /Chapter02/coroutines/src/main/kotlin/org/rao/kotlin/intro/Hello.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.intro 2 | 3 | fun main(args: Array) { 4 | println("Hello, World") 5 | } 6 | 7 | -------------------------------------------------------------------------------- /Chapter02/coroutines/src/main/kotlin/org/rao/kotlin/intro/coroutines.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.intro 2 | 3 | import kotlinx.coroutines.experimental.launch 4 | 5 | fun main(args: Array){ 6 | println("Inside main") 7 | 8 | 9 | launch{ 10 | println("Coroutine in execution") 11 | } 12 | 13 | Thread.sleep(2000) 14 | println("main completed") 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Chapter02/coroutines/src/test/kotlin/org/rao/kotlin/intro/HelloTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.intro 2 | 3 | import org.junit.Test 4 | import kotlin.test.assertEquals 5 | 6 | class HelloTest { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Chapter03/BeanValidation/src/main/kotlin/org/rao/kotlin/javaee/Person.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.javaee 2 | 3 | import javax.validation.constraints.Email 4 | import javax.validation.constraints.NotBlank 5 | import javax.validation.constraints.NotNull 6 | 7 | class Person(@field:NotBlank private val name: String, 8 | @field:Email private val emailId: String, 9 | @field:NotNull private val preferredLanguage: String) 10 | 11 | -------------------------------------------------------------------------------- /Chapter03/BeanValidation/src/test/kotlin/org/rao/kotlin/javaee/PersonTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.javaee 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.validation.Validation 6 | import javax.validation.Validator 7 | 8 | class PersonTest { 9 | 10 | @Test 11 | fun validUser() { 12 | val person = Person( 13 | "Raghavendra Rao", 14 | "kraghavendrarao1@gmail.com", 15 | "en-us") 16 | 17 | val validationErrors = PersonTest.validator!! 18 | .validate(person) 19 | Assert.assertTrue(validationErrors.isEmpty()) 20 | } 21 | 22 | @Test 23 | fun invalidName() { 24 | val person = Person( 25 | "", 26 | "kraghavendrarao1@gmail.com", 27 | "en-us") 28 | 29 | val validationErrors = PersonTest.validator!! 30 | .validate(person) 31 | Assert.assertEquals(1, validationErrors.size.toLong()) 32 | } 33 | 34 | @Test 35 | fun invalidEmailId() { 36 | val person = Person( 37 | "Raghavendra Rao", 38 | "kraghavendrarao1", 39 | "en-us") 40 | 41 | val validationErrors = PersonTest.validator!! 42 | .validate(person) 43 | Assert.assertEquals(1, validationErrors.size.toLong()) 44 | } 45 | 46 | companion object { 47 | private val validator : Validator = Validation.buildDefaultValidatorFactory() 48 | .validator 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Chapter03/ServletExample-EJB/src/main/java/org/rao/kotlin/javaee/servletexample/AuthController.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.javaee.servletexample 2 | 3 | import javax.inject.Inject 4 | import javax.servlet.annotation.WebServlet 5 | import javax.servlet.http.HttpServlet 6 | import javax.servlet.http.HttpServletRequest 7 | import javax.servlet.http.HttpServletResponse 8 | 9 | @WebServlet(name = "home", value = "/home") 10 | class HomeController @Inject constructor( 11 | private val statelessEjb: StatelessEjb): HttpServlet() { 12 | 13 | //@Inject 14 | // private var statelessEjb: StatelessEjb? = null 15 | 16 | // private var statelessEjb: StatelessEjb = StatelessEjb() 17 | 18 | override fun doPost(request: HttpServletRequest, response: HttpServletResponse) { 19 | response.writer.write("

${statelessEjb.hello(request.getParameter("loginId"))}

") 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Chapter03/ServletExample-EJB/src/main/java/org/rao/kotlin/javaee/servletexample/StatelessEjb.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.javaee.servletexample 2 | 3 | import javax.ejb.Stateless 4 | 5 | 6 | @Stateless 7 | open class StatelessEjb() { 8 | //constructor(StatelessEjb) 9 | fun hello(name: String) : String { 10 | return "Hello $name" 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Chapter03/ServletExample-EJB/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /Chapter03/ServletExample/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 4.0.0 5 | 6 | org.rao.kotlin.javaee 7 | servlet-example 8 | 1.0-SNAPSHOT 9 | war 10 | 11 | ServletExample 12 | 13 | 14 | UTF-8 15 | 1.2.41 16 | 4.12 17 | 8.0 18 | 19 | 20 | 21 | 22 | javax 23 | javaee-api 24 | ${javax.javaee-api.version} 25 | 26 | 27 | org.jetbrains.kotlin 28 | kotlin-runtime 29 | ${org.jetbrains.kotlin.kotlin-runtime} 30 | 31 | 32 | org.jetbrains.kotlin 33 | kotlin-runtime 34 | ${org.jetbrains.kotlin.kotlin-runtime} 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Chapter03/ServletExample/src/main/java/org/rao/kotlin/javaee/servletexample/AuthController.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.javaee.servletexample 2 | 3 | import javax.servlet.annotation.WebServlet 4 | import javax.servlet.http.HttpServlet 5 | import javax.servlet.http.HttpServletRequest 6 | import javax.servlet.http.HttpServletResponse 7 | 8 | @WebServlet(name = "home", value = "/home") 9 | class HomeController : HttpServlet() { 10 | override fun doPost(request: HttpServletRequest, response: HttpServletResponse) { 11 | response.writer.write("

Welcome ${request.getParameter("loginId")}

") 12 | } 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /Chapter03/ServletExample/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/App.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import javax.ejb.Stateless 4 | import javax.enterprise.event.Event 5 | import javax.inject.Inject 6 | 7 | @Stateless 8 | class App { 9 | @Inject 10 | private lateinit var identityCreator: IdentityCreator 11 | @Inject 12 | private lateinit var identityRepository: IdentityRepository 13 | @Inject 14 | private lateinit var personCreationEvent: Event 15 | 16 | 17 | fun createResource(inputData: InputData): Identity { 18 | val person = identityCreator.createPerson(inputData) 19 | identityRepository.store(person) 20 | PersonCreationEvent(person.identifier) 21 | personCreationEvent.fire(PersonCreationEvent(person.identifier)) 22 | return person 23 | 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/IdGenerator.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | class IdGenerator 4 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/Identity.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import javax.inject.Inject 4 | 5 | open class Identity { 6 | @Inject 7 | private lateinit var idGenerator: IdGenerator 8 | } 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/IdentityCreator.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import javax.enterprise.context.Dependent 4 | import javax.inject.Inject 5 | 6 | @Dependent 7 | class IdentityCreator { 8 | @Inject 9 | @NativeEnglishSpeaker 10 | private lateinit var defaultPreferredLanguage: PreferredLanguage 11 | 12 | 13 | fun createPerson(inputData: InputData): Person { 14 | val person = Person() 15 | person.preferredLanguage = if (inputData.preferredLanguage == null) 16 | defaultPreferredLanguage 17 | else 18 | inputData.preferredLanguage 19 | return person 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/IdentityRepository.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi; 2 | 3 | import java.util.UUID; 4 | 5 | public class IdentityRepository 6 | { 7 | public void store(Person person) { 8 | person.setIdentifier(UUID.randomUUID()); 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/InputData.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | class InputData { 4 | var preferredLanguage: PreferredLanguage? = null 5 | } 6 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/NativeEnglishSpeaker.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 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 | @Retention(RetentionPolicy.RUNTIME) 10 | @Documented 11 | annotation class NativeEnglishSpeaker 12 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/Person.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import java.util.UUID 4 | 5 | class Person : Identity() { 6 | var preferredLanguage: PreferredLanguage? = null 7 | lateinit var identifier: UUID 8 | } 9 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/PersonCreationEvent.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import java.util.UUID 4 | 5 | 6 | class PersonCreationEvent(val identifier: UUID) 7 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/PersonCreationEventListener.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import javax.enterprise.event.Observes 4 | 5 | class PersonCreationEventListener { 6 | fun onPersonCreation(@Observes event: PersonCreationEvent) { 7 | println("new person created with id " + event.identifier) 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/PreferredLanguage.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | enum class PreferredLanguage (val preferredLanguage: String) { 4 | EN_US("en-us"), EN_UK("en-gb") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/PreferredLanguageProducer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import javax.enterprise.inject.Produces 4 | 5 | class PreferredLanguageProducer { 6 | 7 | @Produces 8 | @NativeEnglishSpeaker 9 | fun exposeDefaultPreferredLanguage(): PreferredLanguage { 10 | return PreferredLanguage.EN_US 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/PreferredLanguageProducer1.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import javax.enterprise.inject.Produces 4 | 5 | class PreferredLanguageProducer1 { 6 | @Produces 7 | private lateinit var defaultPreferredLanguage: PreferredLanguage 8 | 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/resource/Da.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi.resource; 2 | 3 | import javax.annotation.Resource; 4 | import javax.sql.DataSource; 5 | 6 | public class Da { 7 | private DataSource dataSource; 8 | 9 | @Resource(name ="myDataBase") 10 | private void setDataSource(javax.sql.DataSource dataSource) { 11 | this.dataSource = dataSource; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/main/kotlin/org/rao/kotlin/cdi/resource/Dao.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi.resource 2 | 3 | import javax.annotation.Resource 4 | 5 | class Dao { 6 | @Resource(name ="myDataSource") 7 | private lateinit var dataSource: javax.sql.DataSource 8 | } 9 | -------------------------------------------------------------------------------- /Chapter04/CDI-Examples/src/test/kotlin/org/rao/kotlin/cdi/HelloTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import org.junit.Test 4 | import kotlin.test.assertEquals 5 | 6 | class HelloTest { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/App.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import org.rao.kotlin.cdi.interceptors.Auditable 4 | import org.rao.kotlin.cdi.interceptors.Auditor 5 | import javax.ejb.Stateless 6 | import javax.inject.Inject 7 | 8 | @Stateless 9 | class App { 10 | @Inject 11 | private lateinit var identityCreator: IdentityCreator 12 | @Inject 13 | private lateinit var identityRepository: IdentityRepository 14 | 15 | @Auditable(Auditor.AuditType.CREATE) 16 | fun createResource(inputData: InputData): Resource { 17 | val person = identityCreator.createPerson(inputData) 18 | identityRepository.store(person) 19 | return person 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/IdGenerator.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | class IdGenerator 4 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/IdentityCreator.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import javax.enterprise.context.Dependent 4 | import javax.inject.Inject 5 | 6 | @Dependent 7 | class IdentityCreator { 8 | @Inject 9 | @NativeEnglishSpeaker 10 | private lateinit var defaultPreferredLanguage: PreferredLanguage 11 | 12 | 13 | fun createPerson(inputData: InputData): Person { 14 | val person = Person() 15 | person.preferredLanguage = if (inputData.preferredLanguage == null) 16 | defaultPreferredLanguage 17 | else 18 | inputData.preferredLanguage 19 | return person 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/IdentityRepository.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import java.util.UUID 4 | 5 | class IdentityRepository { 6 | fun store(person: Person) { 7 | person.identifier = UUID.randomUUID() 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/InputData.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | class InputData { 4 | var preferredLanguage: PreferredLanguage? = null 5 | } 6 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/NativeEnglishSpeaker.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 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 | @Retention(RetentionPolicy.RUNTIME) 10 | @Documented 11 | annotation class NativeEnglishSpeaker 12 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/Person.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import java.util.UUID 4 | 5 | class Person : Resource() { 6 | var preferredLanguage: PreferredLanguage? = null 7 | lateinit var identifier: UUID 8 | } 9 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/PersonCreationEvent.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import java.util.UUID 4 | 5 | 6 | class PersonCreationEvent(val identifier: UUID) 7 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/PersonCreationEventListener.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import javax.enterprise.event.Observes 4 | 5 | class PersonCreationEventListener { 6 | fun onPersonCreation(@Observes event: PersonCreationEvent) { 7 | println("new person created with id " + event.identifier) 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/PreferredLanguage.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | enum class PreferredLanguage (val preferredLanguage: String) { 4 | EN_US("en-us"), EN_UK("en-gb") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/PreferredLanguageProducer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import javax.enterprise.inject.Produces 4 | 5 | class PreferredLanguageProducer { 6 | 7 | @Produces 8 | @NativeEnglishSpeaker 9 | fun exposeDefaultPreferredLanguage(): PreferredLanguage { 10 | return PreferredLanguage.EN_US 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/PreferredLanguageProducer1.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import javax.enterprise.inject.Produces 4 | 5 | class PreferredLanguageProducer1 { 6 | @Produces 7 | private lateinit var defaultPreferredLanguage: PreferredLanguage 8 | 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/Resource.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import javax.inject.Inject 4 | 5 | open class Resource { 6 | @Inject 7 | private lateinit var idGenerator: IdGenerator 8 | } 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/identity/Da.kt: -------------------------------------------------------------------------------- 1 | import org.rao.kotlin.cdi.interceptors.Auditor 2 | 3 | class AuditProcessor { 4 | fun track(type: Auditor.AuditType) { 5 | println("Audit message type$type") 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/identity/Da1.kt: -------------------------------------------------------------------------------- 1 | import javax.interceptor.InterceptorBinding 2 | import java.lang.annotation.Documented 3 | import java.lang.annotation.Retention 4 | import java.lang.annotation.RetentionPolicy 5 | 6 | import org.rao.kotlin.cdi.interceptors.Auditor 7 | 8 | @InterceptorBinding 9 | @Retention(RetentionPolicy.RUNTIME) 10 | @Documented 11 | annotation class Auditable(val value: Auditor.AuditType) 12 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/identity/Dao.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi.identity 2 | 3 | import javax.annotation.Resource 4 | 5 | class Dao { 6 | @Resource(name ="myDataSource") 7 | private lateinit var dataSource: javax.sql.DataSource 8 | } 9 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/identity/Dao1.kt: -------------------------------------------------------------------------------- 1 | var Greeter = Greet { name - >"hello $name" } 2 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/interceptors/AuditInterceptor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi.interceptors 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 | 9 | 10 | @Interceptor 11 | @Auditable(Auditor.AuditType.DELETE) 12 | @Priority(Interceptor.Priority.APPLICATION) 13 | class AuditInterceptor { 14 | @Inject 15 | private lateinit var auditor: Auditor 16 | 17 | @Inject 18 | private lateinit var auditProcessor: AuditProcessor 19 | 20 | 21 | @AroundInvoke 22 | fun handle(context: InvocationContext) { 23 | auditor.audit("message") 24 | auditProcessor 25 | .processMessage(context.method.getAnnotation(Auditable::class.java).value) 26 | context.proceed() 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/interceptors/AuditProcessor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi.interceptors 2 | 3 | class AuditProcessor { 4 | fun processMessage(type: Auditor.AuditType) { 5 | println("Audit message type $type") 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/interceptors/Auditable.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi.interceptors 2 | 3 | import java.lang.annotation.Documented 4 | import java.lang.annotation.Retention 5 | import java.lang.annotation.RetentionPolicy 6 | import javax.interceptor.InterceptorBinding 7 | 8 | 9 | @InterceptorBinding 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Documented 12 | annotation class Auditable(val value: Auditor.AuditType) 13 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/main/kotlin/org/rao/kotlin/cdi/interceptors/Auditor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi.interceptors 2 | 3 | open class Auditor { 4 | fun audit(message: String) { 5 | println(message) 6 | } 7 | 8 | enum class AuditType{ 9 | CREATE, READ, UPDATE, DELETE 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Chapter04/CDI-Interceptors/src/test/kotlin/org/rao/kotlin/cdi/HelloTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.cdi 2 | 3 | import org.junit.Test 4 | import kotlin.test.assertEquals 5 | 6 | class HelloTest { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Chapter04/JSF-App/src/main/java/org/rao/kotlin/jsf/Person.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jsf 2 | 3 | import javax.faces.bean.ManagedBean 4 | 5 | @ManagedBean(name = "person") 6 | class Person { 7 | 8 | val name: String 9 | get() = "Mathur" 10 | } 11 | -------------------------------------------------------------------------------- /Chapter04/JSF-App/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | 9 | index.jsf 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter04/JSF-App/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | hello 2 | -------------------------------------------------------------------------------- /Chapter04/JSF-App/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter04/JSF-App/src/main/webapp/index.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 8 | 9 | 10 | 11 | 12 | 13 |

14 | Welcome #{person.getName()} 15 |

16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/kotlin/Controller/App.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa 2 | 3 | import javax.ejb.Stateless 4 | import javax.inject.Inject 5 | import javax.persistence.EntityManager 6 | import javax.persistence.PersistenceContext 7 | 8 | import org.rao.kotlin.jpa.entity.Identity 9 | import org.rao.kotlin.jpa.entity.IdentityCreator 10 | import org.rao.kotlin.jpa.entity.InputData 11 | import org.rao.kotlin.jpa.entity.Person 12 | import org.rao.kotlin.jpa.entity.Queries 13 | 14 | @Stateless 15 | class App { 16 | @Inject 17 | private lateinit var identityCreator: IdentityCreator 18 | @PersistenceContext(unitName = "production") 19 | private lateinit var entityManager: EntityManager 20 | 21 | 22 | fun createIdentity(inputData: InputData): Person { 23 | val person = identityCreator.createPerson(inputData) 24 | entityManager.persist(person) 25 | return person 26 | } 27 | 28 | fun findAllPerson(): List { 29 | 30 | return entityManager!!.createNamedQuery(Queries.FIND_ALL_PERSON, 31 | Person::class.java).resultList 32 | } 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/kotlin/org/rao/kotlin/jpa/entity/Address.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | import javax.persistence.Embeddable 4 | 5 | @Embeddable 6 | class Address { 7 | lateinit var street: String 8 | lateinit var city: String 9 | lateinit var state: String 10 | lateinit var country: String 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/kotlin/org/rao/kotlin/jpa/entity/ContactDetails.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | import java.util.* 4 | import javax.persistence.* 5 | 6 | @Table(name = "contacts") 7 | @Entity 8 | class ContactDetails { 9 | @Id 10 | private lateinit var id: Integer 11 | lateinit var number: String 12 | 13 | @Enumerated(EnumType.STRING) 14 | private var type: ContactType? = null 15 | 16 | 17 | @ManyToOne(cascade = arrayOf(CascadeType.ALL), fetch = FetchType.LAZY) 18 | lateinit var person: Person 19 | 20 | 21 | } 22 | 23 | enum class ContactType(val contactType: String) { 24 | WORK("work"), MOBILE("mobile"), HOME("home") 25 | } 26 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/kotlin/org/rao/kotlin/jpa/entity/IdGenerator.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | class IdGenerator 4 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/kotlin/org/rao/kotlin/jpa/entity/Identity.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | import javax.inject.Inject 4 | 5 | open class Identity { 6 | @Inject 7 | private lateinit var idGenerator: IdGenerator 8 | } 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/kotlin/org/rao/kotlin/jpa/entity/IdentityCreator.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | import javax.enterprise.context.Dependent 4 | import javax.inject.Inject 5 | 6 | @Dependent 7 | class IdentityCreator { 8 | @Inject 9 | private lateinit var defaultPreferredLanguage: PreferredLanguage 10 | 11 | 12 | fun createPerson(inputData: InputData): Person { 13 | val person = Person() 14 | person.preferredLanguage = if (inputData.preferredLanguage == null) 15 | defaultPreferredLanguage 16 | else 17 | inputData.preferredLanguage 18 | return person 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/kotlin/org/rao/kotlin/jpa/entity/InputData.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | class InputData { 4 | var preferredLanguage: PreferredLanguage? = null 5 | } 6 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/kotlin/org/rao/kotlin/jpa/entity/Person.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | import org.rao.kotlin.jpa.entity.Queries.Companion.FIND_ALL_PERSON 4 | import java.util.UUID 5 | import javax.persistence.* 6 | 7 | 8 | @Table(name = "person") 9 | @Entity 10 | @NamedQuery(name = FIND_ALL_PERSON, query = "select p from Person p") 11 | class Person : Identity() { 12 | @Id 13 | lateinit var identifier: UUID 14 | lateinit var name: PersonName 15 | lateinit var loginId: String 16 | 17 | @Enumerated(EnumType.STRING) 18 | var preferredLanguage: PreferredLanguage? = null 19 | 20 | @OneToMany(cascade = arrayOf(CascadeType.ALL), fetch = FetchType.LAZY) 21 | @JoinColumn(name = "PERSON_ID", nullable = false) 22 | lateinit var contact: List 23 | @Embedded 24 | lateinit var address: Address 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/kotlin/org/rao/kotlin/jpa/entity/PersonName.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | class PersonName { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/kotlin/org/rao/kotlin/jpa/entity/PreferredLanguage.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | enum class PreferredLanguage (val preferredLanguage: String) { 4 | EN_US("en-us"), EN_UK("en-gb") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/kotlin/org/rao/kotlin/jpa/entity/Queries.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | class Queries{ 4 | companion object { 5 | const val FIND_ALL_PERSON: String = "Person.findAll" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | jdbc/CustomDS 5 | 6 | 7 | 8 | jdbc/CustomDS 9 | 10 | 11 | 12 | jdbc/CustomDS 13 | 14 | 15 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples-mapping-relations/src/test/kotlin/org/rao/kotlin/jpa/HelloTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa 2 | 3 | import org.junit.Test 4 | import kotlin.test.assertEquals 5 | 6 | class HelloTest { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples/src/main/kotlin/Controller/App.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa 2 | 3 | import org.rao.kotlin.jpa.entity.* 4 | import javax.ejb.Stateless 5 | import javax.inject.Inject 6 | import javax.persistence.EntityManager 7 | import javax.persistence.PersistenceContext 8 | 9 | 10 | 11 | @Stateless 12 | class App { 13 | @Inject 14 | private lateinit var identityCreator: IdentityCreator 15 | @PersistenceContext 16 | private lateinit var entityManager: EntityManager 17 | 18 | 19 | fun createIdentity(inputData: InputData): Person { 20 | val person = identityCreator.createPerson(inputData) 21 | entityManager.persist(person) 22 | return person 23 | } 24 | 25 | fun findAllPerson(): List { 26 | 27 | return entityManager!!.createNamedQuery(Queries.FIND_ALL_PERSON, 28 | Person::class.java).resultList 29 | } 30 | 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples/src/main/kotlin/org/rao/kotlin/jpa/entity/Address.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | 4 | class Address { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples/src/main/kotlin/org/rao/kotlin/jpa/entity/IdGenerator.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | class IdGenerator 4 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples/src/main/kotlin/org/rao/kotlin/jpa/entity/Identity.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | import javax.inject.Inject 4 | 5 | open class Identity { 6 | @Inject 7 | private lateinit var idGenerator: IdGenerator 8 | } 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples/src/main/kotlin/org/rao/kotlin/jpa/entity/IdentityCreator.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | import javax.enterprise.context.Dependent 4 | import javax.inject.Inject 5 | 6 | @Dependent 7 | class IdentityCreator { 8 | @Inject 9 | private lateinit var defaultPreferredLanguage: PreferredLanguage 10 | 11 | 12 | fun createPerson(inputData: InputData): Person { 13 | val person = Person() 14 | person.preferredLanguage = if (inputData.preferredLanguage == null) 15 | defaultPreferredLanguage 16 | else 17 | inputData.preferredLanguage 18 | return person 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples/src/main/kotlin/org/rao/kotlin/jpa/entity/InputData.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | class InputData { 4 | var preferredLanguage: PreferredLanguage? = null 5 | } 6 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples/src/main/kotlin/org/rao/kotlin/jpa/entity/Person.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | import org.rao.kotlin.jpa.entity.Queries.Companion.FIND_ALL_PERSON 4 | import java.util.UUID 5 | import javax.persistence.Entity 6 | import javax.persistence.Id 7 | import javax.persistence.NamedQuery 8 | import javax.persistence.Table 9 | 10 | 11 | 12 | 13 | @Table 14 | @Entity 15 | @NamedQuery(name = FIND_ALL_PERSON, query = "select p from Person p") 16 | class Person : Identity() { 17 | @Id 18 | lateinit var identifier: UUID 19 | lateinit var name: PersonName 20 | lateinit var loginId: String 21 | lateinit var address: Address 22 | var preferredLanguage: PreferredLanguage? = null 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples/src/main/kotlin/org/rao/kotlin/jpa/entity/PersonName.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | class PersonName { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples/src/main/kotlin/org/rao/kotlin/jpa/entity/PreferredLanguage.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | enum class PreferredLanguage (val preferredLanguage: String) { 4 | EN_US("en-us"), EN_UK("en-gb") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples/src/main/kotlin/org/rao/kotlin/jpa/entity/Queries.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa.entity 2 | 3 | class Queries{ 4 | companion object { 5 | val FIND_ALL_PERSON: String = "Person.findAll" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Chapter05/JPA-Examples/src/test/kotlin/org/rao/kotlin/jpa/HelloTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa 2 | 3 | import org.junit.Test 4 | import kotlin.test.assertEquals 5 | 6 | class HelloTest { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/META-INF/application.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/kotlin/org/rao/kotlin/App.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin 2 | 3 | import java.util.ArrayList 4 | import java.util.Random 5 | import org.rao.kotlin.jpa.PersonDaoImpl 6 | import org.rao.kotlin.entity.Address 7 | import org.rao.kotlin.entity.ContactDetails 8 | import org.rao.kotlin.entity.ContactType 9 | import org.rao.kotlin.service.PersonServiceImpl 10 | import org.rao.kotlin.entity.Person 11 | import org.rao.kotlin.entity.PreferredLanguage 12 | import javax.inject.Inject 13 | 14 | object App { 15 | @Inject 16 | lateinit var personServiceImpl: PersonServiceImpl 17 | 18 | @JvmStatic 19 | fun main(args: Array) { 20 | 21 | val createPersonRequest = Person("myLoginId" + Random().nextInt()) 22 | val contactDetails = ContactDetails() 23 | 24 | contactDetails.number = "1234567871" 25 | contactDetails.type = ContactType.WORK 26 | val contactDetailsList = ArrayList() 27 | contactDetailsList.add(contactDetails) 28 | createPersonRequest.contact = contactDetailsList 29 | 30 | val address = Address() 31 | address.street = "a" 32 | address.city = "Bengaluru" 33 | address.state = "Karnataka" 34 | address.country = "India" 35 | 36 | createPersonRequest.address = address 37 | createPersonRequest.name = "myName" 38 | createPersonRequest.preferredLanguage = PreferredLanguage.EN_US 39 | val person = personServiceImpl.createPerson(createPersonRequest) 40 | println("Data created " + person.identifier) 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/kotlin/org/rao/kotlin/dao/PersonDao.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.dao 2 | 3 | import org.rao.kotlin.entity.Person 4 | 5 | interface PersonDao { 6 | fun createPerson(person: Person): Person 7 | } 8 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/kotlin/org/rao/kotlin/dao/PersonDaoImpl.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.jpa 2 | 3 | import org.rao.kotlin.dao.PersonDao 4 | import javax.ejb.Stateless 5 | import javax.persistence.EntityManager 6 | import javax.persistence.PersistenceContext 7 | 8 | import org.rao.kotlin.entity.Person 9 | import org.rao.kotlin.entity.Queries 10 | 11 | import javax.persistence.Persistence 12 | 13 | @Stateless 14 | class PersonDaoImpl : PersonDao { 15 | @PersistenceContext(unitName = "prod") 16 | private var entityManager: EntityManager = Persistence.createEntityManagerFactory("prod") 17 | .createEntityManager() 18 | 19 | override 20 | fun createPerson(person: Person): Person { 21 | // val person = identityCreator.person(person) 22 | entityManager.transaction.begin() 23 | entityManager.persist(person) 24 | entityManager.transaction.commit() 25 | return person 26 | } 27 | 28 | fun findAllPerson(): List { 29 | 30 | return entityManager!!.createNamedQuery(Queries.FIND_ALL_PERSON, 31 | Person::class.java).resultList 32 | } 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/kotlin/org/rao/kotlin/entity/Address.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.entity 2 | 3 | import javax.persistence.Embeddable 4 | 5 | @Embeddable 6 | class Address { 7 | lateinit var street: String 8 | lateinit var city: String 9 | lateinit var state: String 10 | lateinit var country: String 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/kotlin/org/rao/kotlin/entity/ContactDetails.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.entity 2 | 3 | import java.util.* 4 | import javax.annotation.Generated 5 | import javax.persistence.* 6 | 7 | @Table(name = "contacts") 8 | @Entity 9 | class ContactDetails { 10 | @Id 11 | @GeneratedValue(strategy = GenerationType.IDENTITY) 12 | lateinit var id: String 13 | lateinit var number: String 14 | 15 | @Enumerated(EnumType.STRING) 16 | var type: ContactType? = null 17 | 18 | } 19 | 20 | enum class ContactType(val contactType: String) { 21 | WORK("work"), MOBILE("mobile"), HOME("home") 22 | } 23 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/kotlin/org/rao/kotlin/entity/Person.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.entity 2 | 3 | 4 | import javax.persistence.CascadeType 5 | import javax.persistence.Embedded 6 | import javax.persistence.Entity 7 | import javax.persistence.EnumType 8 | import javax.persistence.Enumerated 9 | import javax.persistence.FetchType 10 | import javax.persistence.Id 11 | import javax.persistence.JoinColumn 12 | import javax.persistence.NamedQuery 13 | import javax.persistence.OneToMany 14 | import javax.persistence.Table 15 | import org.rao.kotlin.entity.Queries.Companion.FIND_ALL_PERSON 16 | 17 | 18 | @Table(name = "person") 19 | @Entity 20 | @NamedQuery(name = FIND_ALL_PERSON, query = "select p from Person p") 21 | data class Person(val loginId: String) { 22 | @Id 23 | lateinit var identifier: String 24 | lateinit var name: String 25 | 26 | @Enumerated(EnumType.STRING) 27 | var preferredLanguage: PreferredLanguage? = null 28 | 29 | @OneToMany(cascade = arrayOf(CascadeType.ALL), fetch = FetchType.LAZY) 30 | @JoinColumn(name = "PERSON_ID", nullable = false, referencedColumnName = "identifier") 31 | lateinit var contact: List 32 | @Embedded 33 | lateinit var address: Address 34 | } 35 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/kotlin/org/rao/kotlin/entity/PersonName.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.entity 2 | 3 | import java.io.Serializable 4 | 5 | class PersonName : Serializable { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/kotlin/org/rao/kotlin/entity/PreferredLanguage.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.entity 2 | 3 | enum class PreferredLanguage(val preferredLanguage: String) { 4 | EN_US("en-us"), EN_UK("en-gb") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/kotlin/org/rao/kotlin/entity/Queries.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.entity 2 | 3 | class Queries { 4 | companion object { 5 | const val FIND_ALL_PERSON: String = "Person.findAll" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/kotlin/org/rao/kotlin/service/PersonService.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.service 2 | 3 | import org.rao.kotlin.entity.Person 4 | 5 | interface PersonService { 6 | fun createPerson(createPerson: Person): Person 7 | } 8 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/kotlin/org/rao/kotlin/service/PersonServiceImpl.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.service 2 | 3 | import org.rao.kotlin.dao.PersonDao 4 | import org.rao.kotlin.entity.Person 5 | import org.rao.kotlin.entity.PreferredLanguage 6 | import java.util.UUID 7 | 8 | import javax.enterprise.context.Dependent 9 | import javax.inject.Inject 10 | 11 | 12 | @Dependent 13 | class PersonServiceImpl : PersonService { 14 | @Inject 15 | private lateinit var personDao: PersonDao 16 | 17 | @Inject 18 | private lateinit var defaultPreferredLanguage: PreferredLanguage 19 | 20 | override 21 | fun createPerson(createPerson: Person): Person { 22 | var person = createPerson 23 | 24 | person.preferredLanguage = if (createPerson.preferredLanguage == null) 25 | defaultPreferredLanguage 26 | else 27 | person.preferredLanguage 28 | person.identifier = UUID.randomUUID().toString() 29 | return personDao.createPerson(person) 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter05/JPA-simple-project/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | org.eclipse.persistence.jpa.PersistenceProvider 7 | org.rao.kotlin.service.PersonServiceImpl 8 | org.rao.kotlin.entity.Person 9 | org.rao.kotlin.entity.ContactDetails 10 | org.rao.kotlin.entity.Address 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter05/docker/docker-compose.yml: -------------------------------------------------------------------------------- 1 | 2 | version: "3.5" 3 | services: 4 | docker: 5 | image: docker 6 | build: ./postgres 7 | ports: 8 | - "5432:5432" 9 | environment: 10 | - POSTGRES_USER=test 11 | - POSTGRES_PASSWORD=test 12 | - POSTGRES_DB=test 13 | healthcheck: 14 | test: ["CMD-SHELL", "pg_isready -U postgres"] 15 | interval: 30s 16 | timeout: 30s 17 | retries: 3 18 | 19 | -------------------------------------------------------------------------------- /Chapter05/docker/postgres/Dockerfile: -------------------------------------------------------------------------------- 1 | # Extend Postgres to be used as an OpenIDM repository. 2 | # See https://hub.docker.com/_/postgres/ 3 | # 4 | # Copyright (c) 2016-2017 ForgeRock AS. Use of this source code is subject to the 5 | # Common Development and Distribution License (CDDL) that can be found in the LICENSE file 6 | # 7 | 8 | FROM postgres:9.6.1 9 | #COPY docker-entrypoint-initdb.d /docker-entrypoint-initdb.d 10 | EXPOSE 5432 11 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Acknowledgement/src/main/kotlin/org/rao/kotlin/messaging/Consumer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.* 5 | import javax.naming.InitialContext 6 | 7 | class Consumer { 8 | @Inject 9 | internal var initialContext: InitialContext? = null 10 | 11 | fun receiveMessage(): Message { 12 | 13 | val queue = initialContext!!.lookup("jms/PointToPointQueue") as Queue 14 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 15 | val jmsConsumer = connectionFactory 16 | .createContext(JMSContext.DUPS_OK_ACKNOWLEDGE) 17 | .createConsumer(queue) 18 | 19 | val textMessage = jmsConsumer.receive() 20 | jmsConsumer.receive().acknowledge() 21 | // println("Message received $message") 22 | return textMessage 23 | } 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Acknowledgement/src/main/kotlin/org/rao/kotlin/messaging/InitialContextProducer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.enterprise.inject.Produces 4 | import javax.naming.InitialContext 5 | import javax.naming.NamingException 6 | 7 | 8 | class InitialContextProducer { 9 | @Produces 10 | @Throws(NamingException::class) 11 | fun initialContext(): InitialContext { 12 | return InitialContext() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Acknowledgement/src/main/kotlin/org/rao/kotlin/messaging/Producer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.ConnectionFactory 5 | import javax.jms.Queue 6 | import javax.naming.InitialContext 7 | import javax.naming.NamingException 8 | 9 | class Producer { 10 | @Inject 11 | internal var initialContext: InitialContext? = null 12 | 13 | fun sendMessage(message: String): String { 14 | try { 15 | val queue = initialContext!!.lookup("jms/PointToPointQueue") as Queue 16 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 17 | val jmsContext = connectionFactory.createContext() 18 | val textMessage = jmsContext.createTextMessage(message) 19 | 20 | jmsContext.createProducer() 21 | .send(queue, textMessage) 22 | // println("Message sent") 23 | return "Message sent" 24 | }catch ( e: NamingException){ 25 | println("unable to load a resource "+e.message) 26 | return "Unable to deliver a message" 27 | } 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Acknowledgement/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Acknowledgement/src/test/kotlin/org/rao/kotlin/messaging/ConsumerTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.enterprise.inject.se.SeContainerInitializer 6 | import javax.jms.Message 7 | import javax.jms.TextMessage 8 | 9 | class ConsumerTest { 10 | 11 | @Test 12 | fun receiveMessageTest() { 13 | 14 | val seContainerInitializer = SeContainerInitializer.newInstance() 15 | val consumer = seContainerInitializer.initialize() 16 | .select(Consumer::class.java) 17 | .get() 18 | Assert.assertNotNull(consumer) 19 | val message: Message = consumer.receiveMessage() 20 | if (message is TextMessage) 21 | println("message received ---- ${message.text}") 22 | Assert.assertNotNull(message) 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Acknowledgement/src/test/kotlin/org/rao/kotlin/messaging/ProducerTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.enterprise.inject.se.SeContainerInitializer 6 | 7 | class ProducerTest { 8 | 9 | @Test 10 | fun sendMessageTest() { 11 | 12 | val seContainerInitializer = SeContainerInitializer.newInstance() 13 | val producer = seContainerInitializer.initialize() 14 | .select(Producer::class.java) 15 | .get() 16 | 17 | Assert.assertNotNull(producer) 18 | val message = producer.sendMessage("TEST MESSAGE") 19 | Assert.assertEquals("Message sent", message) 20 | 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Transactions/src/main/kotlin/org/rao/kotlin/messaging/Consumer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.* 5 | import javax.naming.InitialContext 6 | 7 | class Consumer { 8 | @Inject 9 | internal var initialContext: InitialContext? = null 10 | 11 | fun receiveMessage(): Message { 12 | 13 | val queue = initialContext!!.lookup("jms/PointToPointQueue") as Queue 14 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 15 | //val textMessage = connectionFactory 16 | val jmsContext = connectionFactory.createContext(JMSContext.CLIENT_ACKNOWLEDGE) 17 | val textMessage = jmsContext.createConsumer(queue) 18 | .receive() 19 | jmsContext.commit() 20 | return textMessage 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Transactions/src/main/kotlin/org/rao/kotlin/messaging/InitialContextProducer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.enterprise.inject.Produces 4 | import javax.naming.InitialContext 5 | import javax.naming.NamingException 6 | 7 | 8 | class InitialContextProducer { 9 | @Produces 10 | @Throws(NamingException::class) 11 | fun initialContext(): InitialContext { 12 | return InitialContext() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Transactions/src/main/kotlin/org/rao/kotlin/messaging/Producer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.enterprise.context.RequestScoped 4 | import javax.inject.Inject 5 | import javax.jms.ConnectionFactory 6 | import javax.jms.JMSContext 7 | import javax.jms.Queue 8 | import javax.naming.InitialContext 9 | import javax.naming.NamingException 10 | 11 | class Producer { 12 | @Inject 13 | internal var initialContext: InitialContext? = null 14 | 15 | fun sendMessage(message: String): String { 16 | try { 17 | val queue = initialContext!!.lookup("jms/PointToPointQueue") as Queue 18 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 19 | val jmsContext = connectionFactory.createContext(JMSContext.SESSION_TRANSACTED) 20 | val textMessage = jmsContext.createTextMessage(message) 21 | jmsContext.createProducer() 22 | .send(queue, textMessage) 23 | jmsContext.commit() 24 | println("Message sent") 25 | return "Message sent" 26 | } catch (e: NamingException) { 27 | println("unable to load a resource " + e.message) 28 | return "Unable to deliver a message" 29 | } 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Transactions/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Transactions/src/test/kotlin/org/rao/kotlin/messaging/ConsumerTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.enterprise.inject.se.SeContainerInitializer 6 | import javax.jms.Message 7 | import javax.jms.TextMessage 8 | 9 | class ConsumerTest { 10 | 11 | @Test 12 | fun receiveMessageTest() { 13 | 14 | val seContainerInitializer = SeContainerInitializer.newInstance() 15 | val consumer = seContainerInitializer.initialize() 16 | .select(Consumer::class.java) 17 | .get() 18 | Assert.assertNotNull(consumer) 19 | 20 | val message = consumer.receiveMessage() 21 | if (message is TextMessage) 22 | println("message received ---- ${message.text}") 23 | 24 | Assert.assertNotNull(message) 25 | 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType-Transactions/src/test/kotlin/org/rao/kotlin/messaging/ProducerTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.enterprise.inject.se.SeContainerInitializer 6 | 7 | class ProducerTest { 8 | 9 | @Test 10 | fun sendMessageTest() { 11 | 12 | val seContainerInitializer = SeContainerInitializer.newInstance() 13 | val producer = seContainerInitializer.initialize() 14 | .select(Producer::class.java) 15 | .get() 16 | 17 | Assert.assertNotNull(producer) 18 | 19 | val message = producer.sendMessage("Test Message") 20 | 21 | Assert.assertEquals("Message sent", message) 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType/src/main/kotlin/org/rao/kotlin/messaging/Consumer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.ConnectionFactory 5 | import javax.jms.Message 6 | import javax.jms.Queue 7 | import javax.jms.TextMessage 8 | import javax.naming.InitialContext 9 | 10 | class Consumer { 11 | @Inject 12 | internal var initialContext: InitialContext? = null 13 | 14 | fun receiveMessage(): Message { 15 | 16 | val queue = initialContext!!.lookup("jms/PointToPointQueue") as Queue 17 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 18 | val textMessage = connectionFactory 19 | .createContext() 20 | .createConsumer(queue) 21 | .receive() 22 | // println("Message received $message") 23 | return textMessage 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType/src/main/kotlin/org/rao/kotlin/messaging/InitialContextProducer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.enterprise.inject.Produces 4 | import javax.naming.InitialContext 5 | import javax.naming.NamingException 6 | 7 | 8 | class InitialContextProducer { 9 | @Produces 10 | @Throws(NamingException::class) 11 | fun initialContext(): InitialContext { 12 | return InitialContext() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType/src/main/kotlin/org/rao/kotlin/messaging/Producer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.ConnectionFactory 5 | import javax.jms.Queue 6 | import javax.naming.InitialContext 7 | import javax.naming.NamingException 8 | 9 | class Producer { 10 | @Inject 11 | internal var initialContext: InitialContext? = null 12 | 13 | fun sendMessage(message: String): String { 14 | try { 15 | val queue = initialContext!!.lookup("jms/PointToPointQueue") as Queue 16 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 17 | val jmsContext = connectionFactory.createContext() 18 | val textMessage = jmsContext.createTextMessage(message) 19 | 20 | jmsContext.createProducer() 21 | .send(queue, textMessage) 22 | // println("Message sent") 23 | return "Message sent" 24 | }catch ( e: NamingException){ 25 | println("unable to load a resource "+e.message) 26 | return "Unable to deliver a message" 27 | } 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType/src/test/kotlin/org/rao/kotlin/messaging/ConsumerTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.enterprise.inject.se.SeContainerInitializer 6 | 7 | class ConsumerTest { 8 | 9 | @Test 10 | fun receiveMessageTest() { 11 | 12 | val seContainerInitializer = SeContainerInitializer.newInstance() 13 | val consumer = seContainerInitializer.initialize() 14 | .select(Consumer::class.java) 15 | .get() 16 | Assert.assertNotNull(consumer) 17 | val message = consumer.receiveMessage() 18 | Assert.assertNotNull(message) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging-MessageType/src/test/kotlin/org/rao/kotlin/messaging/ProducerTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.enterprise.inject.se.SeContainerInitializer 6 | 7 | class ProducerTest { 8 | 9 | @Test 10 | fun sendMessageTest() { 11 | 12 | val seContainerInitializer = SeContainerInitializer.newInstance() 13 | val producer = seContainerInitializer.initialize() 14 | .select(Producer::class.java) 15 | .get() 16 | 17 | Assert.assertNotNull(producer) 18 | val message = producer.sendMessage("TESTMESSAGE") 19 | Assert.assertEquals("Message sent", message) 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging/src/main/kotlin/org/rao/kotlin/messaging/Consumer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.ConnectionFactory 5 | import javax.jms.Queue 6 | import javax.naming.InitialContext 7 | 8 | class Consumer { 9 | @Inject 10 | internal var initialContext: InitialContext? = null 11 | 12 | fun receiveMessage(): String { 13 | 14 | val queue = initialContext!!.lookup("jms/PointToPointQueue") as Queue 15 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 16 | val message = connectionFactory 17 | .createContext() 18 | .createConsumer(queue) 19 | .receiveBody(String::class.java) 20 | 21 | // println("Message received $message") 22 | return message 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging/src/main/kotlin/org/rao/kotlin/messaging/InitialContextProducer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.enterprise.inject.Produces 4 | import javax.naming.InitialContext 5 | import javax.naming.NamingException 6 | 7 | 8 | class InitialContextProducer { 9 | @Produces 10 | @Throws(NamingException::class) 11 | fun initialContext(): InitialContext { 12 | return InitialContext() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging/src/main/kotlin/org/rao/kotlin/messaging/Producer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.ConnectionFactory 5 | import javax.jms.Queue 6 | import javax.naming.InitialContext 7 | import javax.naming.NamingException 8 | 9 | class Producer { 10 | @Inject 11 | internal var initialContext: InitialContext? = null 12 | 13 | fun sendMessage(message: String): String { 14 | try { 15 | val queue = initialContext!!.lookup("jms/PointToPointQueue") as Queue 16 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 17 | connectionFactory.createContext() 18 | .createProducer() 19 | .send(queue, message) 20 | // println("Message sent") 21 | return "Message sent" 22 | }catch ( e: NamingException){ 23 | println("unable to load a resource "+e.message) 24 | return "Unable to deliver a message" 25 | } 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging/src/test/kotlin/org/rao/kotlin/messaging/ConsumerTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.enterprise.inject.se.SeContainerInitializer 6 | 7 | class ConsumerTest { 8 | 9 | @Test 10 | fun receiveMessageTest() { 11 | 12 | val seContainerInitializer = SeContainerInitializer.newInstance() 13 | val consumer = seContainerInitializer.initialize() 14 | .select(Consumer::class.java) 15 | .get() 16 | Assert.assertNotNull(consumer) 17 | val message = consumer.receiveMessage() 18 | Assert.assertNotNull(message) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter06/Point-To-Point-Messaging/src/test/kotlin/org/rao/kotlin/messaging/ProducerTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.enterprise.inject.se.SeContainerInitializer 6 | 7 | class ProducerTest { 8 | 9 | @Test 10 | fun sendMessageTest() { 11 | 12 | val seContainerInitializer = SeContainerInitializer.newInstance() 13 | val producer = seContainerInitializer.initialize() 14 | .select(Producer::class.java) 15 | .get() 16 | 17 | Assert.assertNotNull(producer) 18 | val message = producer.sendMessage("TESTMESSAGE") 19 | Assert.assertEquals("Message sent", message) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe-Acknowledgement/src/main/kotlin/org/rao/kotlin/messaging/InitialContextProducer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.enterprise.inject.Produces 4 | import javax.naming.InitialContext 5 | import javax.naming.NamingException 6 | 7 | 8 | class InitialContextProducer { 9 | @Produces 10 | @Throws(NamingException::class) 11 | fun initialContext(): InitialContext { 12 | return InitialContext() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe-Acknowledgement/src/main/kotlin/org/rao/kotlin/messaging/Publisher.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.ConnectionFactory 5 | import javax.jms.Topic 6 | import javax.naming.InitialContext 7 | 8 | class Publisher { 9 | @Inject 10 | internal var initialContext: InitialContext? = null 11 | 12 | fun publishMessage(message:String) { 13 | val topic = initialContext!!.lookup("jms/Topic") as Topic 14 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 15 | 16 | connectionFactory.createContext() 17 | .createProducer() 18 | .send(topic, message) 19 | println("Message sent") 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe-Acknowledgement/src/main/kotlin/org/rao/kotlin/messaging/Subscriber.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.ConnectionFactory 5 | import javax.jms.JMSContext 6 | import javax.jms.Topic 7 | import javax.naming.InitialContext 8 | import javax.naming.NamingException 9 | 10 | class Subscriber { 11 | @Inject 12 | internal var initialContext: InitialContext? = null 13 | 14 | @Throws(NamingException::class) 15 | fun listenToMessage(): String? { 16 | val topic = initialContext!!.lookup("jms/Topic") as Topic 17 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 18 | // println("inside getting message") 19 | 20 | var messageResponse = connectionFactory.createContext(JMSContext.DUPS_OK_ACKNOWLEDGE) 21 | .createConsumer(topic) 22 | .receiveBody(String::class.java) 23 | // println("Yay--- " + msgResponse!!) 24 | return messageResponse 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe-Acknowledgement/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe-Acknowledgement/src/test/kotlin/org/rao/kotlin/messaging/PublisherTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.enterprise.inject.se.SeContainerInitializer 6 | 7 | 8 | class PublisherTest { 9 | 10 | @Test 11 | fun testPublishMessage() { 12 | val seContainerInitializer = SeContainerInitializer.newInstance() 13 | val seContainer = seContainerInitializer.initialize() 14 | val publisherInstance = seContainer.select(Publisher::class.java) 15 | 16 | val publisher = publisherInstance.get() 17 | Assert.assertNotNull(publisher) 18 | val message = "Test messgage for topic" 19 | publisher.publishMessage(message) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe-Transactions/src/main/kotlin/org/rao/kotlin/messaging/InitialContextProducer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.enterprise.inject.Produces 4 | import javax.naming.InitialContext 5 | import javax.naming.NamingException 6 | 7 | 8 | class InitialContextProducer { 9 | @Produces 10 | @Throws(NamingException::class) 11 | fun initialContext(): InitialContext { 12 | return InitialContext() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe-Transactions/src/main/kotlin/org/rao/kotlin/messaging/Publisher.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.ConnectionFactory 5 | import javax.jms.JMSContext 6 | import javax.jms.Topic 7 | import javax.naming.InitialContext 8 | 9 | class Publisher { 10 | @Inject 11 | internal var initialContext: InitialContext? = null 12 | 13 | fun publishMessage(message:String) { 14 | val topic = initialContext!!.lookup("jms/Topic") as Topic 15 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 16 | 17 | val jmsContext = connectionFactory.createContext(JMSContext.SESSION_TRANSACTED) 18 | jmsContext 19 | .createProducer() 20 | .send(topic, message) 21 | jmsContext.commit() 22 | println("Message sent") 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe-Transactions/src/main/kotlin/org/rao/kotlin/messaging/Subscriber.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.ConnectionFactory 5 | import javax.jms.JMSContext 6 | import javax.jms.Topic 7 | import javax.naming.InitialContext 8 | import javax.naming.NamingException 9 | 10 | class Subscriber { 11 | @Inject 12 | internal var initialContext: InitialContext? = null 13 | 14 | @Throws(NamingException::class) 15 | fun listenToMessage(): String? { 16 | val topic = initialContext!!.lookup("jms/Topic") as Topic 17 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 18 | // println("inside getting message") 19 | 20 | val jmsContext = connectionFactory.createContext(JMSContext.SESSION_TRANSACTED) 21 | var messageResponse = jmsContext.createConsumer(topic) 22 | .receiveBody(String::class.java) 23 | 24 | println("Yay--- " + messageResponse!!) 25 | return messageResponse 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe-Transactions/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe-Transactions/src/test/kotlin/org/rao/kotlin/messaging/PublisherTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.enterprise.inject.se.SeContainerInitializer 6 | 7 | 8 | class PublisherTest { 9 | 10 | @Test 11 | fun testPublishMessage() { 12 | val seContainerInitializer = SeContainerInitializer.newInstance() 13 | val seContainer = seContainerInitializer.initialize() 14 | val publisherInstance = seContainer.select(Publisher::class.java) 15 | 16 | val publisher = publisherInstance.get() 17 | Assert.assertNotNull(publisher) 18 | val message = "Test messgage for topic" 19 | publisher.publishMessage(message) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe/src/main/kotlin/org/rao/kotlin/messaging/InitialContextProducer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.enterprise.inject.Produces 4 | import javax.naming.InitialContext 5 | import javax.naming.NamingException 6 | 7 | 8 | class InitialContextProducer { 9 | @Produces 10 | @Throws(NamingException::class) 11 | fun initialContext(): InitialContext { 12 | return InitialContext() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe/src/main/kotlin/org/rao/kotlin/messaging/Publisher.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.ConnectionFactory 5 | import javax.jms.Topic 6 | import javax.naming.InitialContext 7 | 8 | class Publisher { 9 | @Inject 10 | internal var initialContext: InitialContext? = null 11 | 12 | fun publishMessage(message:String) { 13 | val topic = initialContext!!.lookup("jms/Topic") as Topic 14 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 15 | 16 | connectionFactory.createContext() 17 | .createProducer() 18 | .send(topic, message) 19 | println("Message sent") 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe/src/main/kotlin/org/rao/kotlin/messaging/Subscriber.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import javax.inject.Inject 4 | import javax.jms.ConnectionFactory 5 | import javax.jms.Topic 6 | import javax.naming.InitialContext 7 | import javax.naming.NamingException 8 | 9 | class Subscriber { 10 | @Inject 11 | internal var initialContext: InitialContext? = null 12 | 13 | @Throws(NamingException::class) 14 | fun listenToMessage(): String? { 15 | val topic = initialContext!!.lookup("jms/Topic") as Topic 16 | val connectionFactory = initialContext!!.lookup("jms/__defaultConnectionFactory") as ConnectionFactory 17 | // println("inside getting message") 18 | 19 | var messageResponse = connectionFactory.createContext() 20 | .createConsumer(topic) 21 | .receiveBody(String::class.java) 22 | // println("Yay--- " + msgResponse!!) 23 | return messageResponse 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter06/Publish-Subscribe/src/test/kotlin/org/rao/kotlin/messaging/PublisherTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.messaging 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | import javax.enterprise.inject.se.SeContainerInitializer 6 | 7 | 8 | class PublisherTest { 9 | 10 | @Test 11 | fun testPublishMessage() { 12 | val seContainerInitializer = SeContainerInitializer.newInstance() 13 | val seContainer = seContainerInitializer.initialize() 14 | val publisherInstance = seContainer.select(Publisher::class.java) 15 | 16 | val publisher = publisherInstance.get() 17 | Assert.assertNotNull(publisher) 18 | val message = "Test messgage for topic" 19 | publisher.publishMessage(message) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/context/Initailizer.kt: -------------------------------------------------------------------------------- 1 | /* 2 | package org.rao.kotlin.rest.organization.context 3 | 4 | import org.rao.kotlin.rest.organization.service.OrganizaionService 5 | import javax.enterprise.context.ApplicationScoped 6 | import javax.enterprise.inject.Produces 7 | import javax.inject.Singleton 8 | 9 | import javax.naming.NamingException 10 | 11 | @ApplicationScoped 12 | public class Initializer { 13 | @Produces 14 | public fun organizaionService(): OrganizaionService { 15 | return OrganizaionService() 16 | } 17 | } 18 | */ 19 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/controller/OrganizationConroller.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.controller 2 | 3 | import org.rao.kotlin.rest.organization.model.OrganizationRequest 4 | import org.rao.kotlin.rest.organization.model.OrganizationResponse 5 | import org.rao.kotlin.rest.organization.service.OrganizaionService 6 | //import org.rao.kotlin.rest.organization.service.OrganizaionService 7 | import java.net.URI 8 | import javax.inject.Inject 9 | import javax.ws.rs.* 10 | 11 | import javax.ws.rs.core.MediaType 12 | import javax.ws.rs.core.Response 13 | 14 | 15 | @Path("/organization") 16 | public class OrganizationController { 17 | @Inject 18 | internal var organizationService: OrganizaionService? = null 19 | 20 | 21 | @POST 22 | @Consumes(MediaType.APPLICATION_JSON) 23 | fun createOrganization(organizationRequest: OrganizationRequest): Response { 24 | 25 | val response = "Created : ${organizationRequest.orgName}" 26 | var orgId = organizationService!!.createOrganization(organizationRequest) 27 | 28 | return Response.status(201) 29 | .location(URI(orgId)) 30 | .entity(response).build() 31 | 32 | } 33 | 34 | @Path("/{orgId}") 35 | @GET 36 | @Produces(MediaType.APPLICATION_JSON) 37 | fun retrieveOrganization(@PathParam("orgId") orgId: String): Response { 38 | var organizationResponse: OrganizationResponse = organizationService!! 39 | .retrieveOrganization(orgId) 40 | return Response.status(200) 41 | .entity(organizationResponse).build() 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/dao/OrganizationDao.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.dao 2 | 3 | import org.rao.kotlin.rest.organization.entity.OrganizationEntity 4 | import javax.ejb.Stateless 5 | import javax.persistence.EntityManager 6 | import javax.persistence.Persistence 7 | import javax.persistence.PersistenceContext 8 | 9 | @Stateless 10 | class OrganizationDao { 11 | @PersistenceContext(unitName = "local") 12 | internal var entityManager: EntityManager = Persistence.createEntityManagerFactory("local") 13 | .createEntityManager() 14 | 15 | 16 | 17 | fun createOrganization(organizationEntity: OrganizationEntity): OrganizationEntity { 18 | entityManager.transaction.begin() 19 | entityManager.persist(organizationEntity) 20 | entityManager.transaction.commit() 21 | 22 | return organizationEntity 23 | } 24 | fun retrieveOrganization(orgId:String): OrganizationEntity { 25 | entityManager.transaction.begin() 26 | var organizationEntity:OrganizationEntity = entityManager.find(OrganizationEntity::class.java,orgId) 27 | entityManager.transaction.commit() 28 | 29 | return organizationEntity 30 | } 31 | 32 | 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/entity/OrganizationEntity.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.entity 2 | 3 | 4 | import org.rao.kotlin.rest.organization.entity.Queries.Companion.FIND_ALL_ORGANIZATION 5 | import javax.persistence.Entity 6 | import javax.persistence.Id 7 | import javax.persistence.NamedQuery 8 | import javax.persistence.Table 9 | 10 | 11 | @Table(name = "organization") 12 | @Entity 13 | @NamedQuery(name = FIND_ALL_ORGANIZATION, query = "select o from Organization o") 14 | open class OrganizationEntity(@Id val orgId:String) { 15 | var orgName: String?= null 16 | var description: String?= null 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/entity/Queries.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.entity 2 | 3 | class Queries { 4 | companion object { 5 | const val FIND_ALL_ORGANIZATION: String = "OrganizationEntity.findAll" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/model/OrganizationRequest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.model 2 | 3 | import java.util.* 4 | 5 | class OrganizationRequest { 6 | var orgId: String?= null 7 | var orgName: String? = null 8 | var description: String? = null 9 | 10 | override fun toString(): String { 11 | return "Organization [orgName=$orgName, description=$description]" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/model/OrganizationResponse.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.model 2 | 3 | import java.util.* 4 | 5 | class OrganizationResponse { 6 | var orgId: String?= null 7 | var orgName: String? = null 8 | var description: String? = null 9 | 10 | override fun toString(): String { 11 | return "Organization [orgId=$orgId, orgName=$orgName, description=$description]" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/service/OrganizaionService.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.service 2 | 3 | import org.rao.kotlin.rest.organization.dao.OrganizationDao 4 | import org.rao.kotlin.rest.organization.entity.OrganizationEntity 5 | import org.rao.kotlin.rest.organization.model.OrganizationRequest 6 | import org.rao.kotlin.rest.organization.model.OrganizationResponse 7 | import java.util.* 8 | import javax.annotation.ManagedBean 9 | import javax.enterprise.context.ApplicationScoped 10 | 11 | @ManagedBean 12 | @ApplicationScoped 13 | 14 | open class OrganizaionService { 15 | internal var organizationDao: OrganizationDao = OrganizationDao() 16 | 17 | fun createOrganization(organizationRequest: OrganizationRequest): String { 18 | var organizationEntity: OrganizationEntity = OrganizationEntity(UUID.randomUUID().toString()) 19 | organizationEntity.orgName = organizationRequest.orgName 20 | organizationEntity.description = organizationRequest.description 21 | 22 | var entity: OrganizationEntity = organizationDao.createOrganization(organizationEntity) 23 | return entity.orgId 24 | } 25 | 26 | fun retrieveOrganization(orgId: String): OrganizationResponse { 27 | var organizationEntity: OrganizationEntity = organizationDao.retrieveOrganization(orgId) 28 | 29 | var organizationResponse:OrganizationResponse = OrganizationResponse() 30 | organizationResponse.orgId = organizationEntity.orgId 31 | organizationResponse.orgName = organizationEntity.orgName 32 | organizationResponse.description = organizationEntity.description 33 | return organizationResponse 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 24 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | MyServlet 8 | com.sun.jersey.spi.container.servlet.ServletContainer 9 | 10 | com.sun.jersey.config.property.packages 11 | org.rao.kotlin.rest.organization.controller 12 | 13 | 14 | com.sun.jersey.api.json.POJOMappingFeature 15 | true 16 | 17 | 1 18 | 19 | 20 | 21 | MyServlet 22 | /* 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter07/IdentityApp/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: 310208614 4 | Date: 8/13/2018 5 | Time: 4:25 PM 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | 10 | 11 | $Title$ 12 | 13 | 14 | $END$ 15 | 16 | 17 | -------------------------------------------------------------------------------- /Chapter07/RestExample/src/main/kotlin/org/rao/kotlin/rest/Controller.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest 2 | 3 | import javax.ws.rs.GET 4 | import javax.ws.rs.Path 5 | import javax.ws.rs.PathParam 6 | import javax.ws.rs.core.Response 7 | import javax.ws.rs.ext.Provider 8 | 9 | @Path("/greet") 10 | public class HelloWorld { 11 | 12 | @GET 13 | fun home(): String { 14 | return "hello" 15 | } 16 | 17 | 18 | @GET 19 | @Path("/{parameter}") 20 | fun greet(@PathParam("parameter") name: String): Response { 21 | 22 | val response = "Hello : $name" 23 | 24 | return Response.status(200).entity(response).build() 25 | 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Chapter07/RestExample/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter07/RestExample/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 5 | Restful Web Application 6 | 7 | 8 | MyServlet 9 | com.sun.jersey.spi.container.servlet.ServletContainer 10 | 11 | com.sun.jersey.config.property.packages 12 | org.rao.kotlin.rest 13 | 14 | 15 | com.sun.jersey.api.json.POJOMappingFeature 16 | true 17 | 18 | 1 19 | 20 | 21 | 22 | MyServlet 23 | /* 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter08/01_IdentityStore Example/src/main/kotlin/org/rao/kotlin/rest/ApplicationConfig.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest 2 | 3 | import org.glassfish.soteria.identitystores.annotation.Credentials 4 | 5 | import javax.annotation.security.DeclareRoles 6 | import javax.ws.rs.ApplicationPath 7 | import javax.ws.rs.core.Application 8 | import java.util.HashSet 9 | import javax.enterprise.context.ApplicationScoped 10 | import javax.security.enterprise.authentication.mechanism.http.BasicAuthenticationMechanismDefinition 11 | import javax.security.enterprise.authentication.mechanism.http.CustomFormAuthenticationMechanismDefinition 12 | import javax.security.enterprise.authentication.mechanism.http.FormAuthenticationMechanismDefinition 13 | import javax.security.enterprise.authentication.mechanism.http.LoginToContinue 14 | import javax.security.enterprise.identitystore.LdapIdentityStoreDefinition 15 | 16 | @CustomFormAuthenticationMechanismDefinition( 17 | loginToContinue = LoginToContinue(loginPage = "/login.jsf")) 18 | @ApplicationScoped 19 | class ApplicationConfig : Application() { 20 | override fun getClasses(): Set> { 21 | val classes = HashSet>() 22 | classes.add(Controller::class.java) 23 | return classes 24 | } 25 | 26 | } 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Chapter08/01_IdentityStore Example/src/main/kotlin/org/rao/kotlin/rest/Controller.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest 2 | 3 | import org.glassfish.soteria.identitystores.annotation.Credentials 4 | import org.glassfish.soteria.identitystores.annotation.EmbeddedIdentityStoreDefinition 5 | import javax.security.enterprise.authentication.mechanism.http.BasicAuthenticationMechanismDefinition 6 | import javax.ws.rs.GET 7 | import javax.ws.rs.POST 8 | import javax.ws.rs.Path 9 | import javax.ws.rs.PathParam 10 | import javax.ws.rs.Produces 11 | import javax.ws.rs.core.MediaType 12 | import javax.ws.rs.core.Response 13 | import javax.ws.rs.ext.Provider 14 | 15 | @Path("/greet") 16 | public class Controller { 17 | 18 | @GET 19 | @Produces(MediaType.TEXT_PLAIN) 20 | fun home(): String { 21 | return "hello" 22 | } 23 | 24 | 25 | @GET 26 | @Path("/{parameter}") 27 | fun greet(@PathParam("parameter") name: String): Response { 28 | 29 | val response = "Hello : $name" 30 | 31 | return Response.status(200).entity(response).build() 32 | 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Chapter08/01_IdentityStore Example/src/main/kotlin/org/rao/kotlin/rest/Filter.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest; 2 | 3 | import javax.servlet.FilterChain; 4 | import javax.servlet.FilterConfig; 5 | import javax.servlet.ServletException; 6 | import javax.servlet.ServletRequest; 7 | import javax.servlet.ServletResponse; 8 | import javax.servlet.annotation.WebFilter; 9 | import java.io.IOException; 10 | 11 | @WebFilter (filterName = "Filter") public class Filter 12 | implements javax.servlet.Filter { 13 | public void destroy () { 14 | 15 | } 16 | 17 | public void doFilter ( 18 | ServletRequest req, 19 | ServletResponse resp, 20 | FilterChain chain) throws ServletException, IOException { 21 | 22 | chain.doFilter (req, resp); 23 | } 24 | 25 | public void init (FilterConfig config) throws ServletException { 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Chapter08/01_IdentityStore Example/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter08/01_IdentityStore Example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 5 | Restful Web Application 6 | 7 | 8 | MyServlet 9 | com.sun.jersey.spi.container.servlet.ServletContainer 10 | 11 | com.sun.jersey.config.property.packages 12 | org.rao.kotlin.rest 13 | 14 | 15 | com.sun.jersey.api.json.POJOMappingFeature 16 | true 17 | 18 | 1 19 | 20 | 21 | 22 | MyServlet 23 | /* 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/context/Initailizer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.context 2 | 3 | import org.rao.kotlin.rest.organization.dao.OrganizationDao 4 | import org.rao.kotlin.rest.organization.filter.CallBackHandlerImpl 5 | import org.rao.kotlin.rest.organization.filter.MyIdentityStore 6 | import org.rao.kotlin.rest.organization.service.OrganizaionService 7 | import javax.enterprise.context.ApplicationScoped 8 | import javax.enterprise.inject.Produces 9 | import javax.security.auth.callback.CallbackHandler 10 | 11 | @ApplicationScoped 12 | public class Initializer { 13 | @Produces 14 | public fun organizaionService(): OrganizaionService { 15 | return OrganizaionService() 16 | } 17 | 18 | @Produces 19 | public fun organizaionDao(): OrganizationDao { 20 | return OrganizationDao() 21 | } 22 | 23 | @Produces 24 | public fun myIdentityStore(): MyIdentityStore { 25 | return MyIdentityStore() 26 | } 27 | 28 | @Produces 29 | public fun callbackHandler(): CallbackHandler { 30 | return CallBackHandlerImpl() 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/dao/OrganizationDao.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.dao 2 | 3 | import org.rao.kotlin.rest.organization.entity.OrganizationEntity 4 | import javax.ejb.Stateless 5 | import javax.persistence.EntityManager 6 | import javax.persistence.Persistence 7 | import javax.persistence.PersistenceContext 8 | 9 | @Stateless 10 | class OrganizationDao { 11 | @PersistenceContext(unitName = "local") 12 | internal var entityManager: EntityManager = Persistence.createEntityManagerFactory("local") 13 | .createEntityManager() 14 | 15 | 16 | fun createOrganization(organizationEntity: OrganizationEntity): OrganizationEntity { 17 | entityManager.transaction.begin() 18 | entityManager.persist(organizationEntity) 19 | entityManager.transaction.commit() 20 | 21 | return organizationEntity 22 | } 23 | 24 | fun retrieveOrganization(orgId: String): OrganizationEntity { 25 | entityManager.transaction.begin() 26 | var organizationEntity: OrganizationEntity = entityManager.find(OrganizationEntity::class.java, orgId) 27 | entityManager.transaction.commit() 28 | 29 | return organizationEntity 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/entity/OrganizationEntity.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.entity 2 | 3 | 4 | import javax.persistence.Entity 5 | import javax.persistence.Id 6 | import javax.persistence.Table 7 | 8 | 9 | @Table(name = "organization") 10 | @Entity 11 | open class OrganizationEntity(@Id val orgId: String) { 12 | var orgName: String? = null 13 | var description: String? = null 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/filter/AuthException.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.filter 2 | 3 | class AuthException(s: String) : Throwable() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/filter/CallBackHandlerImpl.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.filter; 2 | 3 | import java.io.*; 4 | import javax.security.auth.callback.*; 5 | 6 | public class CallBackHandlerImpl implements CallbackHandler { 7 | public void handle (Callback[] callbacks) throws IOException, UnsupportedCallbackException { 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/filter/MyAuthMechanism.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.filter 2 | 3 | import javax.enterprise.context.ApplicationScoped 4 | import javax.inject.Inject 5 | import javax.security.enterprise.AuthenticationStatus 6 | import javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism 7 | import javax.security.enterprise.authentication.mechanism.http.HttpMessageContext 8 | import javax.security.enterprise.credential.UsernamePasswordCredential 9 | import javax.security.enterprise.identitystore.CredentialValidationResult.Status.VALID 10 | import javax.servlet.http.HttpServletRequest 11 | import javax.servlet.http.HttpServletResponse 12 | 13 | @ApplicationScoped 14 | class MyAuthMechanism : HttpAuthenticationMechanism { 15 | @Inject 16 | private var myIdentityStore: MyIdentityStore? = null 17 | 18 | override fun validateRequest(req: HttpServletRequest, 19 | res: HttpServletResponse, 20 | context: HttpMessageContext): AuthenticationStatus { 21 | val result = myIdentityStore!!.validate( 22 | UsernamePasswordCredential( 23 | req.getHeader("UserId"), 24 | req.getHeader("Password"))) 25 | return if (result.status == VALID) { 26 | AuthenticationStatus.SUCCESS 27 | } else { 28 | AuthenticationStatus.SEND_FAILURE 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/filter/MyIdentityStore.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.filter 2 | 3 | import java.util.* 4 | import java.util.Arrays.asList 5 | import javax.annotation.ManagedBean 6 | import javax.enterprise.context.ApplicationScoped 7 | import javax.security.enterprise.credential.Credential 8 | import javax.security.enterprise.credential.UsernamePasswordCredential 9 | import javax.security.enterprise.identitystore.CredentialValidationResult 10 | import javax.security.enterprise.identitystore.IdentityStore 11 | 12 | @ApplicationScoped 13 | @ManagedBean 14 | class MyIdentityStore : IdentityStore { 15 | override fun validate(userCredential: Credential): CredentialValidationResult { 16 | return if (userCredential is UsernamePasswordCredential && 17 | userCredential.compareTo("testUser", "testUserPass")) { 18 | CredentialValidationResult("admin", 19 | HashSet(asList("user"))) 20 | } else CredentialValidationResult.INVALID_RESULT 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/model/OrganizationRequest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.model 2 | 3 | class OrganizationRequest { 4 | var orgId: String? = null 5 | var orgName: String? = null 6 | var description: String? = null 7 | 8 | override fun toString(): String { 9 | return "Organization [orgName=$orgName, description=$description]" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/model/OrganizationResponse.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.model 2 | 3 | class OrganizationResponse { 4 | var orgId: String? = null 5 | var orgName: String? = null 6 | var description: String? = null 7 | 8 | override fun toString(): String { 9 | return "Organization [orgId=$orgId, orgName=$orgName, description=$description]" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/kotlin/org/rao/kotlin/rest/organization/service/OrganizaionService.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.service 2 | 3 | import org.rao.kotlin.rest.organization.dao.OrganizationDao 4 | import org.rao.kotlin.rest.organization.entity.OrganizationEntity 5 | import org.rao.kotlin.rest.organization.model.OrganizationRequest 6 | import org.rao.kotlin.rest.organization.model.OrganizationResponse 7 | import java.util.UUID 8 | 9 | import javax.annotation.ManagedBean 10 | import javax.enterprise.context.ApplicationScoped 11 | 12 | @ManagedBean 13 | @ApplicationScoped 14 | 15 | open class OrganizaionService { 16 | internal var organizationDao: OrganizationDao = OrganizationDao() 17 | 18 | fun createOrganization(organizationRequest: OrganizationRequest): String { 19 | var organizationEntity: OrganizationEntity = OrganizationEntity(UUID.randomUUID().toString()) 20 | organizationEntity.orgName = organizationRequest.orgName 21 | organizationEntity.description = organizationRequest.description 22 | 23 | var entity: OrganizationEntity = organizationDao.createOrganization(organizationEntity) 24 | return entity.orgId 25 | } 26 | 27 | fun retrieveOrganization(orgId: String): OrganizationResponse { 28 | var organizationEntity: OrganizationEntity = organizationDao.retrieveOrganization(orgId) 29 | 30 | var organizationResponse: OrganizationResponse = OrganizationResponse() 31 | organizationResponse.orgId = organizationEntity.orgId 32 | organizationResponse.orgName = organizationEntity.orgName 33 | organizationResponse.description = organizationEntity.description 34 | return organizationResponse 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | org.eclipse.persistence.jpa.PersistenceProvider 7 | org.rao.kotlin.rest.organization.service.PersonServiceImpl 8 | org.rao.kotlin.rest.organization.entity.OrganizationEntity 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | MyServlet 8 | com.sun.jersey.spi.container.servlet.ServletContainer 9 | 10 | com.sun.jersey.config.property.packages 11 | org.rao.kotlin.rest.organization.controller 12 | 13 | 14 | com.sun.jersey.api.json.POJOMappingFeature 15 | true 16 | 17 | 1 18 | 19 | 20 | 21 | MyServlet 22 | /* 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter08/02_SecureIdentityApp/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: 310208614 4 | Date: 8/13/2018 5 | Time: 4:25 PM 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | 10 | 11 | $Title$ 12 | 13 | 14 | $END$ 15 | 16 | 17 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/kotlin/org/rao/kotlin/rest/security/organization/context/Initailizer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.security.organization.context 2 | 3 | import org.rao.kotlin.rest.organization.dao.OrganizationDao 4 | import org.rao.kotlin.rest.organization.service.OrganizaionService 5 | import org.rao.kotlin.rest.security.organization.filter.CallBackHandlerImpl 6 | import org.rao.kotlin.rest.security.organization.filter.MyIdentityStore 7 | import javax.enterprise.context.ApplicationScoped 8 | import javax.enterprise.inject.Produces 9 | import javax.security.auth.callback.CallbackHandler 10 | 11 | @ApplicationScoped 12 | public class Initializer { 13 | @Produces 14 | public fun organizaionService(): OrganizaionService { 15 | return OrganizaionService() 16 | } 17 | @Produces 18 | public fun organizaionDao(): OrganizationDao { 19 | return OrganizationDao() 20 | } 21 | @Produces 22 | public fun myIdentityStore(): MyIdentityStore { 23 | return MyIdentityStore() 24 | } 25 | @Produces 26 | public fun callbackHandler(): CallbackHandler { 27 | return CallBackHandlerImpl() 28 | } 29 | 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/kotlin/org/rao/kotlin/rest/security/organization/dao/OrganizationDao.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.dao 2 | 3 | import org.rao.kotlin.rest.security.organization.entity.OrganizationEntity 4 | import javax.ejb.Stateless 5 | import javax.persistence.EntityManager 6 | import javax.persistence.Persistence 7 | import javax.persistence.PersistenceContext 8 | 9 | @Stateless 10 | class OrganizationDao { 11 | @PersistenceContext(unitName = "local") 12 | internal var entityManager: EntityManager = Persistence.createEntityManagerFactory("local") 13 | .createEntityManager() 14 | 15 | 16 | fun createOrganization(organizationEntity: OrganizationEntity): OrganizationEntity { 17 | entityManager.transaction.begin() 18 | entityManager.persist(organizationEntity) 19 | entityManager.transaction.commit() 20 | 21 | return organizationEntity 22 | } 23 | 24 | fun retrieveOrganization(orgId: String): OrganizationEntity { 25 | entityManager.transaction.begin() 26 | var organizationEntity: OrganizationEntity = entityManager.find(OrganizationEntity::class.java, orgId) 27 | entityManager.transaction.commit() 28 | 29 | return organizationEntity 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/kotlin/org/rao/kotlin/rest/security/organization/entity/OrganizationEntity.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.security.organization.entity 2 | 3 | 4 | import javax.persistence.Entity 5 | import javax.persistence.Id 6 | import javax.persistence.NamedQuery 7 | import javax.persistence.Table 8 | 9 | 10 | @Table(name = "organization") 11 | @Entity 12 | open class OrganizationEntity(@Id val orgId:String) { 13 | var orgName: String?= null 14 | var description: String?= null 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/kotlin/org/rao/kotlin/rest/security/organization/filter/AuthException.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.security.organization.filter 2 | 3 | class AuthException(s: String) : Throwable() { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/kotlin/org/rao/kotlin/rest/security/organization/filter/CallBackHandlerImpl.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.security.organization.filter; 2 | 3 | import javax.security.auth.callback.*; 4 | import java.io.*; 5 | 6 | public class CallBackHandlerImpl implements CallbackHandler { 7 | public void handle (Callback[] callbacks) throws IOException, UnsupportedCallbackException { 8 | 9 | } 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/kotlin/org/rao/kotlin/rest/security/organization/filter/MyAuthMechanism.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.security.organization.filter 2 | 3 | import javax.enterprise.context.ApplicationScoped 4 | import javax.inject.Inject 5 | import javax.security.enterprise.AuthenticationStatus 6 | import javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism 7 | import javax.security.enterprise.authentication.mechanism.http.HttpMessageContext 8 | import javax.security.enterprise.credential.UsernamePasswordCredential 9 | import javax.security.enterprise.identitystore.CredentialValidationResult.Status.VALID 10 | import javax.servlet.http.HttpServletRequest 11 | import javax.servlet.http.HttpServletResponse 12 | 13 | @ApplicationScoped 14 | class MyAuthMechanism : HttpAuthenticationMechanism { 15 | @Inject 16 | internal var myIdentityStore = MyIdentityStore() 17 | 18 | override fun validateRequest(req: HttpServletRequest, 19 | res: HttpServletResponse, 20 | context: HttpMessageContext): AuthenticationStatus { 21 | val result = myIdentityStore.validate( 22 | UsernamePasswordCredential( 23 | req.getHeader("UserId"), 24 | req.getHeader("Password"))) 25 | return if (result.status == VALID) { 26 | AuthenticationStatus.SUCCESS 27 | } else { 28 | AuthenticationStatus.SEND_FAILURE 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/kotlin/org/rao/kotlin/rest/security/organization/filter/MyIdentityStore.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.security.organization.filter 2 | 3 | import java.util.* 4 | import java.util.Arrays.asList 5 | import javax.annotation.ManagedBean 6 | import javax.enterprise.context.ApplicationScoped 7 | import javax.security.enterprise.credential.Credential 8 | import javax.security.enterprise.credential.UsernamePasswordCredential 9 | import javax.security.enterprise.identitystore.CredentialValidationResult 10 | import javax.security.enterprise.identitystore.IdentityStore 11 | 12 | @ApplicationScoped 13 | @ManagedBean 14 | class MyIdentityStore : IdentityStore { 15 | override fun validate(userCredential: Credential): CredentialValidationResult { 16 | return if (userCredential is UsernamePasswordCredential && 17 | userCredential.compareTo("testUser", "testUserPass")) { 18 | CredentialValidationResult("admin", 19 | HashSet(asList("user"))) 20 | } else CredentialValidationResult.INVALID_RESULT 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/kotlin/org/rao/kotlin/rest/security/organization/model/OrganizationRequest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.security.organization.model 2 | 3 | class OrganizationRequest { 4 | var orgId: String?= null 5 | var orgName: String? = null 6 | var description: String? = null 7 | 8 | override fun toString(): String { 9 | return "Organization [orgName=$orgName, description=$description]" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/kotlin/org/rao/kotlin/rest/security/organization/model/OrganizationResponse.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.security.organization.model 2 | 3 | class OrganizationResponse { 4 | var orgId: String?= null 5 | var orgName: String? = null 6 | var description: String? = null 7 | 8 | override fun toString(): String { 9 | return "Organization [orgId=$orgId, orgName=$orgName, description=$description]" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | org.eclipse.persistence.jpa.PersistenceProvider 7 | org.rao.kotlin.rest.organization.service.PersonServiceImpl 8 | org.rao.kotlin.rest.security.organization.entity.OrganizationEntity 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Chapter08/03_JWT_Example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | MyServlet 8 | com.sun.jersey.spi.container.servlet.ServletContainer 9 | 10 | com.sun.jersey.config.property.packages 11 | org.rao.kotlin.rest.security.organization.controller 12 | 13 | 14 | com.sun.jersey.api.json.POJOMappingFeature 15 | true 16 | 17 | 1 18 | 19 | 20 | 21 | MyServlet 22 | /* 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/AuthenticationService/src/main/kotlin/org/rao/kotlin/microservices/authentication/auth/CallBackHandlerImpl.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.authentication.auth 2 | 3 | import java.io.IOException 4 | import javax.security.auth.callback.Callback 5 | import javax.security.auth.callback.CallbackHandler 6 | import javax.security.auth.callback.UnsupportedCallbackException 7 | 8 | class CallBackHandlerImpl : CallbackHandler { 9 | @Throws(IOException::class, UnsupportedCallbackException::class) 10 | override fun handle(callbacks: Array) { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/AuthenticationService/src/main/kotlin/org/rao/kotlin/microservices/authentication/auth/CustomAuthMechanism.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.authentication.auth 2 | 3 | import com.sun.jersey.spi.inject.Inject 4 | import org.rao.kotlin.microservices.authentication.store.MyIdentityStore 5 | import javax.security.enterprise.AuthenticationStatus 6 | import javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism 7 | import javax.security.enterprise.authentication.mechanism.http.HttpMessageContext 8 | import javax.security.enterprise.credential.UsernamePasswordCredential 9 | import javax.security.enterprise.identitystore.CredentialValidationResult.Status.VALID 10 | import javax.servlet.http.HttpServletRequest 11 | import javax.servlet.http.HttpServletResponse 12 | 13 | class CustomAuthMechanism: HttpAuthenticationMechanism { 14 | @Inject 15 | internal var myIdentityStore = MyIdentityStore() 16 | 17 | override fun validateRequest(req: HttpServletRequest, 18 | res: HttpServletResponse, 19 | context: HttpMessageContext): AuthenticationStatus { 20 | val result = myIdentityStore.validate( 21 | UsernamePasswordCredential( 22 | req.getHeader("UserId"), 23 | req.getHeader("Password"))) 24 | return if (result.status == VALID) { 25 | AuthenticationStatus.SUCCESS 26 | } else { 27 | AuthenticationStatus.SEND_FAILURE 28 | } 29 | } 30 | fun test() { 31 | 32 | println("MyAuthMechanism.test method") 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/AuthenticationService/src/main/kotlin/org/rao/kotlin/microservices/authentication/store/MyIdentityStore.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.authentication.store 2 | 3 | import java.util.Arrays.asList 4 | import javax.security.enterprise.credential.Credential 5 | import javax.security.enterprise.credential.UsernamePasswordCredential 6 | import javax.security.enterprise.identitystore.CredentialValidationResult 7 | import javax.security.enterprise.identitystore.IdentityStore 8 | 9 | class MyIdentityStore:IdentityStore { 10 | fun test() { 11 | 12 | println("MyIdentityStore.test method") 13 | } 14 | 15 | override fun validate(userCredential: Credential): CredentialValidationResult { 16 | return if (userCredential is UsernamePasswordCredential && 17 | userCredential.compareTo("testUser", "testUserPass")) { 18 | CredentialValidationResult("admin", 19 | HashSet(asList("user"))) 20 | } else CredentialValidationResult.INVALID_RESULT 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/AuthenticationService/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 6 | Authentication Service 7 | 8 | 9 | auth-serlvet 10 | com.sun.jersey.spi.container.servlet.ServletContainer 11 | 12 | com.sun.jersey.config.property.packages 13 | org.rao.kotlin.microservices.authentication.controller 14 | 15 | 16 | com.sun.jersey.api.json.POJOMappingFeature 17 | true 18 | 19 | 1 20 | 21 | 22 | 23 | auth-serlvet 24 | /* 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/AuthenticationService/src/test/kotlin/MyTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | public class MyTest { 6 | @Test 7 | public void shouldAnswerWithTrue() 8 | { 9 | assertTrue( true ); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/exception/IdentityAlreadyExistsException.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.exception 2 | 3 | class IdentityAlreadyExistsException(message: String) : Exception(message) { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/exception/IdentityException.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.exception 2 | 3 | class IdentityException(message: String) : Exception(message) { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/exception/IdentityNotFoundException.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.exception 2 | 3 | class IdentityNotFoundException(message: String) : Exception(message) { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/organization/dao/OrganizationDao.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.organization.dao 2 | 3 | import org.rao.kotlin.microservices.identity.organization.entity.OrganizationEntity 4 | 5 | interface OrganizationDao { 6 | 7 | 8 | 9 | fun createOrganization(organizationEntity: OrganizationEntity): OrganizationEntity? 10 | fun retrieveOrganization(orgId: String): OrganizationEntity 11 | fun updateOrganization(orgId: String, updateRequest:OrganizationEntity): 12 | OrganizationEntity 13 | fun deleteOrganization(orgId: String) 14 | } 15 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/organization/entity/OrganizationEntity.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.organization.entity 2 | 3 | 4 | import javax.persistence.Column 5 | import javax.persistence.Entity 6 | import javax.persistence.Id 7 | import javax.persistence.Table 8 | 9 | 10 | @Table(name = "organization") 11 | @Entity 12 | open class OrganizationEntity(@Id @Column(name = "org_id") val orgId:String) { 13 | @Column(name = "org_name") var orgName: String?= null 14 | var description: String?= null 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/organization/model/Organization.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.rest.organization.model 2 | 3 | class Organization { 4 | var orgId: String?= null 5 | var orgName: String? = null 6 | var description: String? = null 7 | 8 | override fun toString(): String { 9 | return "Organization [orgName=$orgName, description=$description]" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/organization/service/OrganizationService.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.organization.service 2 | 3 | import org.rao.kotlin.rest.organization.model.Organization 4 | 5 | interface OrganizaionService { 6 | 7 | fun createOrganization(organizationRequest: Organization): String 8 | 9 | fun retrieveOrganization(orgId: String): Organization 10 | fun updateOrganization(orgId: String, updateRequest:Organization): Organization 11 | fun deleteOrganization(orgId: String) 12 | } 13 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/dao/PersonDao.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.person.dao 2 | 3 | import org.rao.kotlin.microservices.identity.person.entity.PersonEntity 4 | 5 | 6 | interface PersonDao { 7 | fun createPerson(person: PersonEntity): PersonEntity 8 | fun retrievePersonByLoginId(loginId: String): PersonEntity 9 | fun retrievePersonById(uuid: String): PersonEntity 10 | fun deletePerson(uuid: String) 11 | fun updatePerson(uuid: String, updateRequest: PersonEntity): PersonEntity 12 | } 13 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/entity/AddressEntity.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.person.entity 2 | 3 | import javax.persistence.Embeddable 4 | 5 | @Embeddable 6 | class AddressEntity { 7 | lateinit var street: String 8 | lateinit var city: String 9 | lateinit var state: String 10 | lateinit var country: String 11 | 12 | override fun toString(): String { 13 | return "AddressEntity(street='$street', city='$city', state='$state', country='$country')" 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/entity/ContactDetailsEntity.kt: -------------------------------------------------------------------------------- 1 | /* 2 | package org.rao.kotlin.microservices.identity.person.entity 3 | 4 | import javax.persistence.* 5 | 6 | @Table(name = "contacts") 7 | @Entity 8 | class ContactDetails { 9 | @Id 10 | @GeneratedValue(strategy = GenerationType.IDENTITY) 11 | lateinit var id: String 12 | lateinit var number: String 13 | 14 | @Enumerated(EnumType.STRING) 15 | var type: ContactType? = null 16 | 17 | } 18 | 19 | enum class ContactType(val contactType: String) { 20 | WORK("work"), MOBILE("mobile"), HOME("home") 21 | } 22 | */ 23 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/entity/PersonEntity.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.person.entity 2 | 3 | 4 | import org.rao.kotlin.microservices.identity.person.entity.Queries.Companion.FIND_ALL_PERSON 5 | import javax.persistence.Column 6 | import javax.persistence.Embedded 7 | import javax.persistence.Entity 8 | import javax.persistence.Id 9 | import javax.persistence.NamedQuery 10 | import javax.persistence.Table 11 | 12 | 13 | @Table(name = "person") 14 | @Entity 15 | @NamedQuery(name = FIND_ALL_PERSON, query = "select p from PersonEntity p") 16 | class PersonEntity(@Column(name = "loginid") val loginId: String) { 17 | 18 | 19 | @Id 20 | lateinit var identifier: String 21 | 22 | lateinit var name: String 23 | 24 | @Column(name = "orgid") 25 | var orgId: String ?=null 26 | 27 | var preferredLanguage: String? = null 28 | 29 | @Embedded 30 | lateinit var address: AddressEntity 31 | 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/entity/PersonNameEntity.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.person.entity 2 | 3 | import java.io.Serializable 4 | 5 | class PersonNameEntity : Serializable { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/entity/PreferredLanguage.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.person.entity 2 | 3 | enum class PreferredLanguage constructor(val preferredLanguage: String) { 4 | EN_US("en-us"), EN_UK("en-gb") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/entity/Queries.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.person.entity 2 | 3 | class Queries { 4 | companion object { 5 | const val FIND_ALL_PERSON: String = "Person.findAll" 6 | const val FIND_BY_LOGINID: String = "Person.findByLoginId" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/model/Address.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.person.model 2 | 3 | class Address { 4 | lateinit var street: String 5 | lateinit var city: String 6 | lateinit var state: String 7 | lateinit var country: String 8 | override fun toString(): String { 9 | return "Address(street='$street', city='$city', state='$state', country='$country')" 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/model/ContactDetails.kt: -------------------------------------------------------------------------------- 1 | /* 2 | package org.rao.kotlin.microservices.identity.person.model 3 | 4 | 5 | class ContactDetails { 6 | lateinit var id: String 7 | lateinit var number: String 8 | 9 | var type: ContactType? = null 10 | 11 | } 12 | 13 | enum class ContactType(val contactType: String) { 14 | WORK("work"), MOBILE("mobile"), HOME("home") 15 | } 16 | */ 17 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/model/Person.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.person.model 2 | 3 | 4 | class Person() { 5 | 6 | lateinit var identifier: String 7 | lateinit var name: String 8 | 9 | var loginId:String ?= null 10 | var orgId:String ?= null 11 | var preferredLanguage: String? = null 12 | 13 | 14 | lateinit var address: Address 15 | 16 | 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/model/PersonName.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.person.model 2 | 3 | import java.io.Serializable 4 | 5 | class PersonName : Serializable { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/model/PreferredLanguage.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.person.model 2 | 3 | public enum class PreferredLanguage constructor(val preferredLanguage: String) { 4 | EN_US("en-us"), EN_UK("en-gb") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/person/service/PersonService.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.service 2 | 3 | import org.rao.kotlin.microservices.identity.person.model.Person 4 | 5 | 6 | interface PersonService { 7 | fun createPerson(createPerson: Person): String 8 | fun retrievePersonByLoginId(loginId: String): Person 9 | fun retrievePersonById(uuid: String): Person 10 | fun deletePerson(uuid: String) 11 | fun updatePerson(uuid: String, updateRequest: Person): Person 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/response/ErrorResponse.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.response 2 | 3 | class ErrorResponse { 4 | var responseMessage: String? = null 5 | 6 | } 7 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/response/GenericResponse.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.response 2 | 3 | class GenericResponse { 4 | var responseMessage: String? = null 5 | 6 | } 7 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/util/Constants.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.util 2 | 3 | object Constants { 4 | public val IDENTITY_NOT_FOUND = "Requested Identity not found" 5 | public val IDENTITY_ALREADY_EXIST = "Identity already exists" 6 | public val INTERNAL_SERVER_ERROR_MSG = "Server unable to process the reqest or something went" + 7 | " wrong" 8 | public val IDENTITY_CREATED = "Requested Identity is created" 9 | } 10 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/kotlin/org/rao/kotlin/microservices/identity/util/ResponseUtil.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.util 2 | 3 | import org.rao.kotlin.microservices.identity.response.ErrorResponse 4 | import javax.ws.rs.core.Response 5 | 6 | object ResponseUtil { 7 | 8 | fun sendErrorResponse(errorMessage: String, statusCode:Int): Response { 9 | var errorResponse: ErrorResponse = ErrorResponse() 10 | errorResponse.responseMessage = errorMessage 11 | 12 | return Response.status(statusCode) 13 | .entity(errorResponse) 14 | .build(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | 10 | org.rao.kotlin.microservices.identity.organization.entity.OrganizationEntity 11 | 12 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 5 | Restful Web Application 6 | 7 | 8 | identity-serlvet 9 | com.sun.jersey.spi.container.servlet.ServletContainer 10 | 11 | com.sun.jersey.config.property.packages 12 | 13 | org.rao.kotlin.microservices.identity.organization.controller, 14 | org.rao.kotlin.microservices.identity.person.controller 15 | 16 | 17 | 18 | 19 | com.sun.jersey.api.json.POJOMappingFeature 20 | true 21 | 22 | 1 23 | 24 | 25 | 26 | identity-serlvet 27 | /* 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/test/kotlin/org/rao/kotlin/microservices/identity/organization/TestUtil.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.microservices.identity.organization 2 | 3 | import javax.ws.rs.core.Response 4 | import org.rao.kotlin.microservices.identity.util.Constants 5 | 6 | object TestUtil { 7 | val AUTHENTICATION_SERVICE_URL = "http://localhost:8082/authentication-service" 8 | val IDENTITY_SERVICE_URL = "http://localhost:8083/identity-service" 9 | 10 | val TOKEN_API = "/authorize/jwt/token" 11 | val ORGANIZATION_API = "/identity/organization/" 12 | 13 | 14 | fun getResponseForIdentityCreation(uuid: String): Response { 15 | return Response.status(Response.Status.CREATED) 16 | .entity(Constants.IDENTITY_CREATED) 17 | .header("location", uuid) 18 | .build() 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/IdentityService/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker: -------------------------------------------------------------------------------- 1 | mock-maker-inline 2 | -------------------------------------------------------------------------------- /Chapter09/MicroServices/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.rao.kotlin.microservices 8 | source 9 | pom 10 | 1.0-SNAPSHOT 11 | 12 | AuthenticationService 13 | IdentityService 14 | 15 | MicroServices 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter10/GarbageCollection/src/main/kotlin/Example.kt: -------------------------------------------------------------------------------- 1 | object Example { 2 | @JvmStatic 3 | fun main(args: Array) { 4 | var counter:Int = 0 5 | foo() 6 | 7 | } 8 | fun foo() { 9 | var person:Person = Person() 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter10/GarbageCollection/src/main/kotlin/Person.kt: -------------------------------------------------------------------------------- 1 | class Person { 2 | lateinit var name: String 3 | lateinit var id: String 4 | } 5 | -------------------------------------------------------------------------------- /Chapter10/MemoryLeak/src/main/java/HelloWorld.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | object HelloWorld { 4 | internal fun foo() { 5 | val userList = ArrayList() 6 | 7 | } 8 | 9 | 10 | @JvmStatic 11 | fun main(args: Array) { 12 | 13 | val obj = Any() 14 | 15 | val name: String = "test" 16 | System.gc() 17 | Runtime.getRuntime().gc() 18 | foo() 19 | } 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /Chapter10/MemoryLeak/src/main/java/MemoryLeakDemo.kt: -------------------------------------------------------------------------------- 1 | class MemoryLeakDemo(val key: String) { 2 | companion object { 3 | 4 | @JvmStatic 5 | fun main(args: Array) { 6 | 7 | try { 8 | val map = HashMap() 9 | 10 | while (true) { 11 | map.put(MemoryLeakDemo("myKey"), "testValue") 12 | 13 | println("Element added to map") 14 | } 15 | } catch (e: Exception) { 16 | e.printStackTrace() 17 | } 18 | } 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Chapter10/flight_recording_180151MemoryLeakDemo1080_jmx.jfr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Kotlin-for-Enterprise-Applications-using-Java-EE/9da34da228af17dab9e3e25c4d406483387e8a87/Chapter10/flight_recording_180151MemoryLeakDemo1080_jmx.jfr -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/builder/model/Person.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.builder.model 2 | 3 | class Person { 4 | var firstName: String? = null 5 | var lastName: String? = null 6 | var middleName: String? = null 7 | var loginId: String? = null 8 | var age: Int? = null 9 | var contactNumber: String? = null 10 | var address: String? = null 11 | 12 | constructor(firstName: String?, lastName: String?, middleName: String?, loginId: String?, age: Int?, contactNumber: String?, address: String?) { 13 | this.firstName = firstName 14 | this.lastName = lastName 15 | this.middleName = middleName 16 | this.loginId = loginId 17 | this.age = age 18 | this.contactNumber = contactNumber 19 | this.address = address 20 | } 21 | 22 | constructor(firstName: String?) { 23 | this.firstName = firstName 24 | } 25 | 26 | constructor(firstName: String?, lastName: String?) { 27 | this.firstName = firstName 28 | this.lastName = lastName 29 | } 30 | 31 | constructor(firstName: String?, lastName: String?, middleName: String?) { 32 | this.firstName = firstName 33 | this.lastName = lastName 34 | this.middleName = middleName 35 | } 36 | 37 | constructor(firstName: String?, lastName: String?, loginId: String?, contactNumber: String?) { 38 | this.firstName = firstName 39 | this.lastName = lastName 40 | this.loginId = loginId 41 | this.contactNumber = contactNumber 42 | } 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/builder/model/Test.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.builder.model 2 | 3 | object Test { 4 | @JvmStatic 5 | fun main(args: Array) { 6 | /*var person = Person("Jane", "", "", "jane@test.org", 21, "4561298421", "10, Charles " + 7 | "Street,NY")*/ 8 | var person = PersonBuilder() 9 | .withFirstName("Jane") 10 | .withLoginId("jane@test.org") 11 | .withContactNumber("4561298421") 12 | .build(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/handler/AbstractAuditHandler.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.handler 2 | 3 | import org.rao.kotlin.designpatterns.chain.model.audit.AuditServiceImpl 4 | 5 | abstract class AbstractAuditHandler : Handler { 6 | protected var auditService: AuditServiceImpl = AuditServiceImpl() 7 | protected var auditHandler: Handler? = null 8 | 9 | override fun addHandler(auditHandler: Handler) { 10 | this.auditHandler = auditHandler 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/handler/Handler.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.handler 2 | 3 | import org.rao.kotlin.designpatterns.chain.model.identity.Identity 4 | 5 | interface Handler { 6 | fun addHandler(auditHandler: Handler) 7 | fun handleRequest(identityRequest: Identity) 8 | } 9 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/audit/AuditMessage.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.audit 2 | 3 | import java.util.* 4 | 5 | class AuditMessage { 6 | var auditMessageId: String? = null 7 | var auditEventDate: Date? = null 8 | var auditMessage: String? = null 9 | var participantObject: ParticipantObject? = null 10 | var event: Event? = null 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/audit/AuditService.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.audit 2 | 3 | interface AuditService { 4 | fun audit(auditMessage: AuditMessage) 5 | } 6 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/audit/AuditServiceImpl.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.audit 2 | 3 | class AuditServiceImpl:AuditService { 4 | override fun audit(auditMessage: AuditMessage) { 5 | println() 6 | println("--------------------------- audit message --------------------------- ") 7 | System.out.println("MessageId: ${auditMessage.auditMessageId}") 8 | println("Message: ${auditMessage.auditMessage!!}") 9 | println("ParticipantObjectId: ${auditMessage.participantObject!!.participantObjectId!!}") 10 | 11 | println("ParticipantObjectName: ${auditMessage.participantObject!! 12 | .participantObjectName!!}") 13 | println("AuditEventDate: ${auditMessage.auditEventDate!!}") 14 | println("AuditEventCode: ${auditMessage.event!!.code!!}") 15 | println("AuditEventDisplayName: ${auditMessage.event!!.displayName!!}") 16 | 17 | println("--------------------------- audit message ---------------------------") 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/audit/AuditUtil.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.audit 2 | 3 | import org.rao.kotlin.designpatterns.chain.model.identity.Identity 4 | import java.time.Instant 5 | import java.util.* 6 | 7 | object AuditUtil { 8 | public fun prepareAuditMessage(message: String): AuditMessage { 9 | var auditMessage: AuditMessage = AuditMessage() 10 | 11 | auditMessage.auditMessage = message 12 | auditMessage.auditEventDate = Date.from(Instant.now()) 13 | auditMessage.auditMessageId = UUID.randomUUID().toString() 14 | 15 | return auditMessage 16 | 17 | } 18 | 19 | public fun createParticipantObject(identity: Identity): ParticipantObject { 20 | var participantObject: ParticipantObject = ParticipantObject() 21 | participantObject.participantObjectId = identity.id; 22 | participantObject.participantObjectName = identity.name 23 | participantObject.participantObjectType = identity.identityType!!.name 24 | 25 | return participantObject 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/audit/Event.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.audit 2 | 3 | class Event { 4 | var code: String? = null 5 | var displayName: String? = null 6 | } 7 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/audit/ParticipantObject.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.audit 2 | 3 | class ParticipantObject { 4 | var participantObjectId: String? = null 5 | var participantObjectName: String? = null 6 | var participantObjectType: String? = null 7 | } 8 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/audit/Request.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.audit 2 | 3 | import org.rao.kotlin.designpatterns.chain.model.identity.Identity 4 | 5 | open class Request { 6 | var identity: Identity? = null 7 | } 8 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/identity/Device.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.identity 2 | 3 | class Device : Identity() { 4 | 5 | var organizationId: String? = null 6 | 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/identity/Group.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.identity 2 | 3 | class Group : Identity() { 4 | 5 | var organizationId: String? = null 6 | 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/identity/Identity.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.identity 2 | 3 | abstract class Identity { 4 | var id: String? = null 5 | var name: String? = null 6 | var description: String? = null 7 | var identityType: IdentityType? = null 8 | var requestMethod: String? = null 9 | } 10 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/identity/IdentityType.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.identity 2 | 3 | enum class IdentityType { 4 | ORGANIZATION, GROUP, PERSON, DEVICE, TEST 5 | } 6 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/identity/Organization.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.identity 2 | 3 | class Organization : Identity() 4 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/chain/model/identity/Person.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.chain.model.identity 2 | 3 | class Person : Identity() { 4 | var organizationId: String? = null 5 | 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/DecoratorDemo.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | import org.rao.kotlin.designpatterns.decorator.concretecomponent.LargeWindow 5 | import org.rao.kotlin.designpatterns.decorator.concretedecorator.color.BrightSilver 6 | import org.rao.kotlin.designpatterns.decorator.concretedecorator.material.Glass 7 | import org.rao.kotlin.designpatterns.decorator.concretedecorator.shape.Arched 8 | 9 | object DecoratorDemo { 10 | @JvmStatic 11 | fun main(args: Array) { 12 | 13 | var window: Window = LargeWindow() 14 | window = Arched(window) 15 | window = Glass(window) 16 | window = BrightSilver(window) 17 | 18 | 19 | println("Window description: ${window.getDescription()}") 20 | println("Window price: $${window.getPrice()}") 21 | 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/component/Window.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.component 2 | 3 | interface Window { 4 | fun getDescription():String 5 | fun getPrice():Double 6 | } 7 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/concretedecorator/color/BrightSilver.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.concretedecorator.color 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | import org.rao.kotlin.designpatterns.decorator.decorator.WindowDecorator 5 | 6 | class BrightSilver(private val window: Window) : WindowDecorator(window) { 7 | internal var description: String 8 | internal var price: Double 9 | 10 | init { 11 | description = "BrightSilver color" 12 | price = 20.00 13 | } 14 | 15 | override fun getDescription(): String { 16 | var desc = super.getDescription() 17 | return "$desc, $description" 18 | } 19 | 20 | override fun getPrice(): Double { 21 | 22 | return super.getPrice() + price 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/concretedecorator/color/PebbleGray.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.concretedecorator.color 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | import org.rao.kotlin.designpatterns.decorator.decorator.WindowDecorator 5 | 6 | class PebbleGray(private val window: Window) : WindowDecorator(window) { 7 | internal var description: String 8 | internal var price: Double 9 | 10 | init { 11 | description = "PebbleGray color" 12 | price = 15.00 13 | } 14 | 15 | override fun getDescription(): String { 16 | var desc = super.getDescription() 17 | return "$desc, $description" 18 | } 19 | 20 | 21 | override fun getPrice(): Double { 22 | 23 | return super.getPrice() + price 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/concretedecorator/color/Red.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.concretedecorator.color 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | import org.rao.kotlin.designpatterns.decorator.decorator.WindowDecorator 5 | 6 | class Red(private val window: Window) : WindowDecorator(window) { 7 | internal var description: String 8 | internal var price: Double 9 | 10 | init { 11 | description = "Red color" 12 | price = 25.00 13 | } 14 | 15 | override fun getDescription(): String { 16 | var desc = super.getDescription() 17 | return "$desc, $description" 18 | } 19 | 20 | 21 | override fun getPrice(): Double { 22 | 23 | return super.getPrice() + price 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/concretedecorator/material/Aluminum.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.concretedecorator.material 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | import org.rao.kotlin.designpatterns.decorator.decorator.WindowDecorator 5 | 6 | class Aluminum(window: Window) : WindowDecorator(window) { 7 | internal var description: String 8 | internal var price: Double 9 | 10 | init { 11 | description = "Aluminum material" 12 | price = 20.00 13 | } 14 | 15 | override fun getDescription(): String { 16 | var desc = super.getDescription() 17 | return "$desc, $description" 18 | } 19 | 20 | 21 | override fun getPrice(): Double { 22 | 23 | return super.getPrice() + price 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/concretedecorator/material/Clad.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.concretedecorator.material 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | import org.rao.kotlin.designpatterns.decorator.decorator.WindowDecorator 5 | 6 | class Clad(private val window: Window) : WindowDecorator(window) { 7 | internal var description: String 8 | internal var price: Double 9 | 10 | init { 11 | description = "Clad material" 12 | price = 25.00 13 | } 14 | 15 | override fun getDescription(): String { 16 | var desc = super.getDescription() 17 | return "$desc, $description" 18 | } 19 | 20 | 21 | override fun getPrice(): Double { 22 | 23 | return super.getPrice() + price 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/concretedecorator/material/Glass.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.concretedecorator.material 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | import org.rao.kotlin.designpatterns.decorator.decorator.WindowDecorator 5 | 6 | class Glass(private val window: Window) : WindowDecorator(window) { 7 | internal var description: String 8 | internal var price: Double 9 | 10 | init { 11 | description = "Glass material" 12 | price = 10.00 13 | } 14 | 15 | override fun getDescription(): String { 16 | var desc = super.getDescription() 17 | return "$desc, $description" 18 | } 19 | 20 | 21 | override fun getPrice(): Double { 22 | 23 | return super.getPrice() + price 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/concretedecorator/material/Vinyl.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.concretedecorator.material 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | import org.rao.kotlin.designpatterns.decorator.decorator.WindowDecorator 5 | 6 | class Vinyl(private val window: Window) : WindowDecorator(window) { 7 | internal var description: String 8 | internal var price: Double = 0.toDouble() 9 | 10 | init { 11 | description = "Vinyl material" 12 | price = 30.00 13 | } 14 | 15 | override fun getDescription(): String { 16 | var desc = super.getDescription() 17 | return "$desc, $description" 18 | } 19 | 20 | 21 | override fun getPrice(): Double { 22 | 23 | return super.getPrice() + price 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/concretedecorator/material/Wood.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.concretedecorator.material 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | import org.rao.kotlin.designpatterns.decorator.decorator.WindowDecorator 5 | 6 | class Wood(private val window: Window) : WindowDecorator(window) { 7 | internal var description: String 8 | internal var price: Double 9 | 10 | init { 11 | description = "Wood material" 12 | price = 15.00 13 | } 14 | 15 | override fun getDescription(): String { 16 | var desc = super.getDescription() 17 | return "$desc, $description" 18 | } 19 | 20 | 21 | override fun getPrice(): Double { 22 | 23 | return super.getPrice() + price 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/concretedecorator/shape/Arched.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.concretedecorator.shape 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | import org.rao.kotlin.designpatterns.decorator.decorator.WindowDecorator 5 | 6 | class Arched(private val window: Window) : WindowDecorator(window) { 7 | internal var description: String 8 | internal var price: Double 9 | init { 10 | 11 | description = "Arched shape" 12 | price = 15.00 13 | } 14 | 15 | override fun getDescription(): String { 16 | var desc = super.getDescription() 17 | return "$desc, $description" 18 | } 19 | 20 | override fun getPrice(): Double { 21 | 22 | return super.getPrice() + price 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/concretedecorator/shape/Round.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.concretedecorator.shape 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | import org.rao.kotlin.designpatterns.decorator.decorator.WindowDecorator 5 | 6 | class Round(private val window: Window) : WindowDecorator(window) { 7 | internal var description: String 8 | internal var price: Double = 0.toDouble() 9 | init { 10 | description = "Round shape" 11 | price = 10.00 12 | } 13 | 14 | override fun getDescription(): String { 15 | var desc = super.getDescription() 16 | return "$desc, $description" 17 | } 18 | 19 | override fun getPrice(): Double { 20 | 21 | return super.getPrice() + price 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/decorator/decorator/WindowDecorator.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.decorator.decorator 2 | 3 | import org.rao.kotlin.designpatterns.decorator.component.Window 4 | 5 | 6 | 7 | open class WindowDecorator(private val window: Window) : Window { 8 | 9 | override fun getDescription(): String { 10 | 11 | return window.getDescription() 12 | } 13 | 14 | override fun getPrice(): Double { 15 | 16 | return window.getPrice() 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase1/HumiditySensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase1 2 | 3 | class HumiditySensor: Sensor {} 4 | 5 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase1/OpticalSensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase1 2 | 3 | class OpticalSensor: Sensor { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase1/Sensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase1 2 | 3 | interface Sensor 4 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase1/SensorClient.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase1 2 | 3 | object SensorClient { 4 | var sensor: Sensor? = null 5 | 6 | @JvmStatic 7 | fun main(args: Array) { 8 | sensor = SensorFactory.getSensor("optical") 9 | if(sensor == null){ 10 | println("requested sensor type not available yet") 11 | }else { 12 | println(sensor!!::class.simpleName) 13 | } 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase1/SensorFactory.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase1 2 | 3 | object SensorFactory { 4 | fun getSensor(sensorType:String) : Sensor?{ 5 | if (sensorType == "heat") 6 | return TemparatureSensor() 7 | else if (sensorType == "humid") 8 | return HumiditySensor() 9 | else if (sensorType == "optical") 10 | return OpticalSensor() 11 | else 12 | return null 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase1/TemparatureSensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase1 2 | 3 | class TemparatureSensor: Sensor {} 4 | 5 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase11/HumiditySensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase11 2 | 3 | class HumiditySensor: Sensor {} 4 | 5 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase11/OpticalSensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase11 2 | 3 | class OpticalSensor: Sensor { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase11/Sensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase11 2 | 3 | interface Sensor {} 4 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase11/SensorClient.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase11 2 | 3 | object SensorClient { 4 | var sensor: Sensor? = null 5 | 6 | @JvmStatic 7 | fun main(args: Array) { 8 | sensor = SensorFactory.getSensor(SensorType.OPTICAL.name) 9 | if(sensor == null){ 10 | println("requested sensor type not available yet") 11 | }else { 12 | println(sensor!!::class.simpleName) 13 | } 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase11/SensorFactory.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase11 2 | 3 | object SensorFactory { 4 | fun getSensor(sensorType:String) : Sensor?{ 5 | if (sensorType == "heat") 6 | return TemparatureSensor() 7 | else if (sensorType == "humid") 8 | return HumiditySensor() 9 | else if (sensorType == "optical") 10 | return OpticalSensor() 11 | else 12 | return null 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase11/TemparatureSensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase11 2 | 3 | class TemparatureSensor: Sensor {} 4 | 5 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase11/enum/SensorType.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase11 2 | 3 | enum class SensorType private constructor(private val type: String) { 4 | HUMIDITY("humid"), TEMPARATURE("heat"), OPTICAL("optic") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase2/HumiditySensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase2 2 | 3 | class HumiditySensor: Sensor {} 4 | 5 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase2/OpticalSensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase2 2 | 3 | class OpticalSensor: Sensor { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase2/Sensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase2 2 | 3 | interface Sensor 4 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase2/SensorClient.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase2 2 | 3 | object SensorClient { 4 | 5 | @JvmStatic 6 | fun main(args: Array) { 7 | var list = SensorFactory.getSensorList(SensorType.HUMIDITY.name,100) 8 | 9 | if(list == null){ 10 | println("requested sensors are not available yet") 11 | }else { 12 | println(list.size) 13 | println(list.get(0)::class.simpleName) 14 | } 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase2/SensorFactory.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase2 2 | 3 | 4 | object SensorFactory { 5 | fun getSensor(sensorType:String) : Sensor?{ 6 | if (sensorType == SensorType.TEMPARATURE.name) 7 | return TemparatureSensor() 8 | else if (sensorType == SensorType.HUMIDITY.name) 9 | return HumiditySensor() 10 | else if (sensorType == SensorType.OPTICAL.name) 11 | return OpticalSensor() 12 | else 13 | return null 14 | } 15 | fun getSensorList(type:String, quantity:Int): MutableList { 16 | 17 | var sensorList: ArrayList = ArrayList() 18 | println(sensorList.size) 19 | for (i in 0 until quantity) { 20 | sensorList.add(SensorFactory.getSensor(type)!!) 21 | } 22 | return sensorList 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase2/TemparatureSensor.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase2 2 | 3 | class TemparatureSensor: Sensor {} 4 | 5 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/factory/usecase2/enum/SensorType.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory.usecase2 2 | 3 | enum class SensorType private constructor(private val type: String) { 4 | HUMIDITY("humid"), TEMPARATURE("heat"), OPTICAL("optic") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/observer/DAXTradeObserver.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.observer 2 | 3 | class DAXTradeObserver internal constructor(internal var stockData: StockData) : Observer { 4 | init { 5 | stockData.registerObserver(this) 6 | 7 | } 8 | 9 | override fun update() { 10 | val price = stockData.price 11 | val symbol = stockData.symbol 12 | if (symbol.equals(TradeEnum.DAX.name, ignoreCase = true)) { 13 | println("The new price for ${TradeEnum.DAX.name}:" + price!!) 14 | } 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/observer/LupinTradeObserver.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.observer 2 | 3 | class LupinTradeObserver internal constructor(internal var stockData: StockData) : Observer { 4 | init { 5 | stockData.registerObserver(this) 6 | 7 | } 8 | 9 | override fun update() { 10 | val price = stockData.price 11 | val symbol = stockData.symbol 12 | if (symbol.equals(TradeEnum.LUPIN.name, ignoreCase = true)) { 13 | println("The new price for ${TradeEnum.LUPIN.name}: " + price!!) 14 | } 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/observer/Observer.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.observer 2 | 3 | interface Observer { 4 | fun update() 5 | } 6 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/observer/SampleData.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.observer 2 | 3 | import java.util.ArrayList 4 | import java.util.Arrays 5 | 6 | class SampleData { 7 | internal var priceList: MutableList = ArrayList() 8 | internal var companyList: MutableList = ArrayList() 9 | val latestStockData: List 10 | get() { 11 | val stockList = ArrayList() 12 | for (i in priceList.indices) { 13 | val stock = Stock() 14 | stock.price = priceList[i] 15 | stock.symbol = companyList[i] 16 | stockList.add(stock) 17 | } 18 | return stockList 19 | 20 | } 21 | 22 | init { 23 | priceList.addAll(Arrays.asList(100.05f, 105.12f, 110.03f, 120.29f, 150.25f)) 24 | companyList.addAll(Arrays.asList("DAX", "DAX", "LUPIN", "LUPIN", "DAX")) 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/observer/Stock.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.observer 2 | 3 | class Stock { 4 | var symbol: String ?= null 5 | var price: Float ?= null 6 | } 7 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/observer/StockData.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.observer 2 | 3 | import java.util.ArrayList 4 | 5 | class StockData : Subject { 6 | internal var observerList: MutableList = ArrayList() 7 | var symbol: String ?= null 8 | 9 | var price: Float? = null 10 | 11 | 12 | override fun registerObserver(observer: Observer) { 13 | observerList.add(observer) 14 | 15 | } 16 | 17 | override fun removeObserver(observer: Observer) { 18 | val i = observerList.indexOf(observer) 19 | if (i >= 0) { 20 | observerList.removeAt(i) 21 | } 22 | observerList.remove(observer) 23 | 24 | } 25 | 26 | override fun notifyObservers() { 27 | for (i in observerList.indices) { 28 | observerList[i].update() 29 | } 30 | 31 | } 32 | 33 | fun setStockData(symbol: String, price: Float?) { 34 | this.symbol = symbol 35 | this.price = price 36 | notifyObservers() 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/observer/Subject.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.observer 2 | 3 | interface Subject { 4 | 5 | fun registerObserver(observer: Observer) 6 | fun removeObserver(observer: Observer) 7 | fun notifyObservers() 8 | } 9 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/observer/Test.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.observer 2 | 3 | object Test { 4 | @Throws(InterruptedException::class) 5 | @JvmStatic 6 | fun main(args: Array) { 7 | val stockData = StockData() 8 | val daxTradeObserver = DAXTradeObserver(stockData) 9 | val lupinTradeObserver = LupinTradeObserver(stockData) 10 | 11 | val sampleData = SampleData() 12 | for (stock in sampleData.latestStockData) { 13 | 14 | stockData.setStockData(stock.symbol!!, stock.price) 15 | println("---------------------------------------") 16 | Thread.sleep(1000) 17 | } 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/observer/TradeEnum.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.observer; 2 | 3 | public enum TradeEnum { 4 | DAX, LUPIN 5 | } 6 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/singleton/Singleton.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.singleton 2 | 3 | object Singleton { 4 | 5 | fun getDetails(): String { 6 | return "this is a singleton class" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/singleton/multithread/Singleton.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.singleton.multithread; 2 | 3 | public class Singleton { 4 | private static Singleton singletonInstance; 5 | 6 | private Singleton () {} 7 | 8 | public static synchronized Singleton getInstance() { 9 | if(singletonInstance == null){ 10 | singletonInstance = new Singleton (); 11 | } 12 | return singletonInstance; 13 | } 14 | public String getDetails() { 15 | return "this is a singleton class"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/main/kotlin/org/rao/kotlin/designpatterns/singleton/staticinitialization/Singleton.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.singleton.staticinitialization; 2 | 3 | public class Singleton { 4 | private static Singleton singletonInstance = new Singleton (); 5 | 6 | private Singleton () {} 7 | 8 | public static Singleton getInstance() { 9 | return singletonInstance; 10 | } 11 | public String getDetails() { 12 | return "this is a singleton class"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/test/kotlin/org/rao/kotlin/designpatterns/factory/SensorClient.java: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.factory; 2 | 3 | public class SensorClient { 4 | } 5 | -------------------------------------------------------------------------------- /Chapter11/DesignPatterns/src/test/kotlin/org/rao/kotlin/designpatterns/singleton/SingletonTest.kt: -------------------------------------------------------------------------------- 1 | package org.rao.kotlin.designpatterns.singleton 2 | 3 | import org.junit.Assert 4 | import org.junit.Test 5 | 6 | class SingletonTest { 7 | @Test 8 | fun testSingletonObject() { 9 | 10 | var details: String = Singleton.getDetails() 11 | Assert.assertEquals ("this is a singleton class", details); 12 | 13 | } 14 | } 15 | --------------------------------------------------------------------------------