From b36a7e00cc954ecf862796915a14e7ee8dd32b4f Mon Sep 17 00:00:00 2001 From: Ante Pocedulic Date: Tue, 11 Oct 2016 17:15:13 +0200 Subject: [PATCH] Ehcache code (#734) * - created packages for each logical part of application - created validator for WebsiteUser rest API - created ValidatorEventRegister class which fixes known bug for not detecting generated events - created custom Exception Handler which creates better response messages * Code formatting * formated pom.xml replaced for loops with streams fixed bug while getting all beans * removed unnecessary code changed repository type * - added test for Spring Data REST APIs - changed bad request return code - formated code * - added source code for ehcache article - added ehcache dependency to pom.xml --- spring-all/pom.xml | 496 +++++++++--------- .../java/org/baeldung/ehcache/app/App.java | 24 + .../ehcache/calculator/SquaredCalculator.java | 24 + .../baeldung/ehcache/config/CacheHelper.java | 25 + 4 files changed, 324 insertions(+), 245 deletions(-) create mode 100755 spring-all/src/main/java/org/baeldung/ehcache/app/App.java create mode 100755 spring-all/src/main/java/org/baeldung/ehcache/calculator/SquaredCalculator.java create mode 100755 spring-all/src/main/java/org/baeldung/ehcache/config/CacheHelper.java diff --git a/spring-all/pom.xml b/spring-all/pom.xml index c70d9d75fc..003cdacc2c 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -1,295 +1,301 @@ - - 4.0.0 - com.baeldung - spring-all - 0.1-SNAPSHOT + + 4.0.0 + com.baeldung + spring-all + 0.1-SNAPSHOT - spring-all - war + spring-all + war - - org.springframework.boot - spring-boot-starter-parent - 1.3.6.RELEASE - + + org.springframework.boot + spring-boot-starter-parent + 1.3.6.RELEASE + - - - com.fasterxml.jackson.core - jackson-databind - + + + com.fasterxml.jackson.core + jackson-databind + - + - - org.springframework - spring-web - - - org.springframework - spring-webmvc - - - org.springframework - spring-orm - - - org.springframework - spring-context - + + org.springframework + spring-web + + + org.springframework + spring-webmvc + + + org.springframework + spring-orm + + + org.springframework + spring-context + - + - - org.springframework - spring-aspects - + + org.springframework + spring-aspects + - + - - org.hibernate - hibernate-core - ${hibernate.version} - - - org.javassist - javassist - - - mysql - mysql-connector-java - runtime - - - org.hsqldb - hsqldb - - - + + org.hibernate + hibernate-core + ${hibernate.version} + + + org.javassist + javassist + + + mysql + mysql-connector-java + runtime + + + org.hsqldb + hsqldb + - - org.hibernate - hibernate-validator - + - + + org.hibernate + hibernate-validator + - - javax.servlet - javax.servlet-api - provided - + - - javax.servlet - jstl - runtime - + + javax.servlet + javax.servlet-api + provided + - + + javax.servlet + jstl + runtime + - - com.google.guava - guava - ${guava.version} - - - + - - org.slf4j - slf4j-api - - - ch.qos.logback - logback-classic - - - - org.slf4j - jcl-over-slf4j - - - - org.slf4j - log4j-over-slf4j - + + com.google.guava + guava + ${guava.version} + - + - - org.springframework - spring-test - test - + + org.slf4j + slf4j-api + + + ch.qos.logback + logback-classic + + + + org.slf4j + jcl-over-slf4j + + + + org.slf4j + log4j-over-slf4j + - - junit - junit - test - + - - org.assertj - assertj-core - 3.5.1 - test - + + org.springframework + spring-test + test + - - org.hamcrest - hamcrest-core - test - - - org.hamcrest - hamcrest-library - test - + + junit + junit + test + - - org.mockito - mockito-core - test - + + org.assertj + assertj-core + 3.5.1 + test + - - org.easymock - easymock - 3.4 - test - + + org.hamcrest + hamcrest-core + test + + + org.hamcrest + hamcrest-library + test + - + + org.mockito + mockito-core + test + - + + org.easymock + easymock + 3.4 + test + + + org.ehcache + ehcache + 3.1.3 + - + - - org.springframework - spring-framework-bom - ${org.springframework.version} - pom - import - + - - org.springframework - spring-core - ${org.springframework.version} - + - + + org.springframework + spring-framework-bom + ${org.springframework.version} + pom + import + - + + org.springframework + spring-core + ${org.springframework.version} + - - spring-all - - - src/main/resources - true - - + - + - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - + + spring-all + + + src/main/resources + true + + - - org.apache.maven.plugins - maven-war-plugin - - false - - + - - org.apache.maven.plugins - maven-surefire-plugin - - - - - - - - - + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - true - - jetty8x - embedded - - - - - - - 8082 - - - - + + org.apache.maven.plugins + maven-war-plugin + + false + + - + + org.apache.maven.plugins + maven-surefire-plugin + + + + + + + + + - + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + + 8082 + + + + - - - 4.3.1.RELEASE - 4.0.4.RELEASE - 3.20.0-GA - 1.2 + - - 4.3.11.Final - 5.1.38 + - - 1.7.13 - 1.1.3 + + + 4.3.1.RELEASE + 4.0.4.RELEASE + 3.20.0-GA + 1.2 - - 5.2.2.Final + + 4.3.11.Final + 5.1.38 - - 19.0 - 3.4 + + 1.7.13 + 1.1.3 - - 1.3 - 4.12 - 1.10.19 + + 5.2.2.Final - 4.4.1 - 4.5 + + 19.0 + 3.4 - 2.9.0 + + 1.3 + 4.12 + 1.10.19 - - 3.5.1 - 2.6 - 2.19.1 - 2.7 - 1.4.18 + 4.4.1 + 4.5 - + 2.9.0 + + + 3.5.1 + 2.6 + 2.19.1 + 2.7 + 1.4.18 + + \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/ehcache/app/App.java b/spring-all/src/main/java/org/baeldung/ehcache/app/App.java new file mode 100755 index 0000000000..99370186a3 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/ehcache/app/App.java @@ -0,0 +1,24 @@ +package org.baeldung.ehcache.app; + +import org.baeldung.ehcache.calculator.SquaredCalculator; +import org.baeldung.ehcache.config.CacheHelper; + +public class App { + + public static void main(String[] args) { + + SquaredCalculator squaredCalculator = new SquaredCalculator(); + CacheHelper cacheHelper = new CacheHelper(); + + squaredCalculator.setCache(cacheHelper); + + calculate(squaredCalculator); + calculate(squaredCalculator); + } + + private static void calculate(SquaredCalculator squaredCalculator) { + for (int i = 10; i < 15; i++) { + System.out.println("Square value of " + i + " is: " + squaredCalculator.getSquareValueOfNumber(i) + "\n"); + } + } +} diff --git a/spring-all/src/main/java/org/baeldung/ehcache/calculator/SquaredCalculator.java b/spring-all/src/main/java/org/baeldung/ehcache/calculator/SquaredCalculator.java new file mode 100755 index 0000000000..25957539df --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/ehcache/calculator/SquaredCalculator.java @@ -0,0 +1,24 @@ +package org.baeldung.ehcache.calculator; + +import org.baeldung.ehcache.config.CacheHelper; + +public class SquaredCalculator { + private CacheHelper cache; + + public int getSquareValueOfNumber(int input) { + if (cache.getSquareNumberCache().containsKey(input)) { + return cache.getSquareNumberCache().get(input); + } + + System.out.println("Calculating square value of " + input + " and caching result."); + + int squaredValue = (int) Math.pow(input, 2); + cache.getSquareNumberCache().put(input, squaredValue); + + return squaredValue; + } + + public void setCache(CacheHelper cache) { + this.cache = cache; + } +} diff --git a/spring-all/src/main/java/org/baeldung/ehcache/config/CacheHelper.java b/spring-all/src/main/java/org/baeldung/ehcache/config/CacheHelper.java new file mode 100755 index 0000000000..387a57880b --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/ehcache/config/CacheHelper.java @@ -0,0 +1,25 @@ +package org.baeldung.ehcache.config; + +import org.ehcache.Cache; +import org.ehcache.CacheManager; +import org.ehcache.config.builders.CacheConfigurationBuilder; +import org.ehcache.config.builders.CacheManagerBuilder; +import org.ehcache.config.builders.ResourcePoolsBuilder; + +public class CacheHelper { + + private CacheManager cacheManager; + private Cache squareNumberCache; + + public CacheHelper() { + cacheManager = CacheManagerBuilder.newCacheManagerBuilder().withCache("squaredNumber", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, Integer.class, ResourcePoolsBuilder.heap(10))).build(); + cacheManager.init(); + + squareNumberCache = cacheManager.getCache("squaredNumber", Integer.class, Integer.class); + } + + public Cache getSquareNumberCache() { + return squareNumberCache; + } + +}