From 7a003c9287be57fe90173e4dd728ad995ed7036e Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Wed, 17 Apr 2019 15:34:48 +0200 Subject: Refactor: Move Repositories to Repository Package --- .../java/at/gv/egiz/moazs/DeliveryRepository.java | 15 ------ .../at/gv/egiz/moazs/DeliveryRequestHandler.java | 1 + .../gv/egiz/moazs/InMemoryDeliveryRepository.java | 40 -------------- .../at/gv/egiz/moazs/RedisDeliveryRepository.java | 61 --------------------- .../egiz/moazs/repository/DeliveryRepository.java | 15 ++++++ .../repository/InMemoryDeliveryRepository.java | 40 ++++++++++++++ .../moazs/repository/RedisDeliveryRepository.java | 62 ++++++++++++++++++++++ 7 files changed, 118 insertions(+), 116 deletions(-) delete mode 100644 src/main/java/at/gv/egiz/moazs/DeliveryRepository.java delete mode 100644 src/main/java/at/gv/egiz/moazs/InMemoryDeliveryRepository.java delete mode 100644 src/main/java/at/gv/egiz/moazs/RedisDeliveryRepository.java create mode 100644 src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java create mode 100644 src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java create mode 100644 src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java (limited to 'src/main/java') diff --git a/src/main/java/at/gv/egiz/moazs/DeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/DeliveryRepository.java deleted file mode 100644 index 6df2829..0000000 --- a/src/main/java/at/gv/egiz/moazs/DeliveryRepository.java +++ /dev/null @@ -1,15 +0,0 @@ -package at.gv.egiz.moazs; - -import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; -import org.springframework.stereotype.Repository; - -import java.util.Optional; - -@Repository -public interface DeliveryRepository { - - void add(DeliveryRequestType request); - - Optional getDeliveryRequest(String appDeliveryID); - -} diff --git a/src/main/java/at/gv/egiz/moazs/DeliveryRequestHandler.java b/src/main/java/at/gv/egiz/moazs/DeliveryRequestHandler.java index 7d8cb43..0dc0a15 100644 --- a/src/main/java/at/gv/egiz/moazs/DeliveryRequestHandler.java +++ b/src/main/java/at/gv/egiz/moazs/DeliveryRequestHandler.java @@ -2,6 +2,7 @@ package at.gv.egiz.moazs; import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryResponseType; +import at.gv.egiz.moazs.repository.DeliveryRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/main/java/at/gv/egiz/moazs/InMemoryDeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/InMemoryDeliveryRepository.java deleted file mode 100644 index aa1e3e9..0000000 --- a/src/main/java/at/gv/egiz/moazs/InMemoryDeliveryRepository.java +++ /dev/null @@ -1,40 +0,0 @@ -package at.gv.egiz.moazs; - -import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.annotation.Profile; -import org.springframework.stereotype.Repository; - -import java.util.Optional; -import java.util.concurrent.TimeUnit; - -@Repository -@Profile("!cluster") -public class InMemoryDeliveryRepository implements DeliveryRepository { - - private static final Logger logger = LoggerFactory.getLogger(InMemoryDeliveryRepository.class); - - private static final Cache rep = CacheBuilder.newBuilder() - .expireAfterWrite(30, TimeUnit.MINUTES) - .build(); - - public InMemoryDeliveryRepository() { - logger.info("InMemoryDeliveryRepository()"); - } - - @Override - public void add(DeliveryRequestType request) { - String key = request.getMetaData().getAppDeliveryID(); - logger.info("Save DeliveryRequest with AppDeliveryId = {} to Cache.", key); - rep.put(key, request); - } - - @Override - public Optional getDeliveryRequest(String appDeliveryID) { - DeliveryRequestType deliveryRequestType = rep.getIfPresent(appDeliveryID); - return Optional.ofNullable(deliveryRequestType); - } -} diff --git a/src/main/java/at/gv/egiz/moazs/RedisDeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/RedisDeliveryRepository.java deleted file mode 100644 index accae43..0000000 --- a/src/main/java/at/gv/egiz/moazs/RedisDeliveryRepository.java +++ /dev/null @@ -1,61 +0,0 @@ -package at.gv.egiz.moazs; - -import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; -import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.ObjectFactory; -import org.apache.tools.ant.filters.StringInputStream; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Profile; -import org.springframework.data.redis.core.HashOperations; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.stereotype.Repository; - -import javax.xml.bind.JAXBElement; -import java.util.Optional; - -@Repository -@Profile("cluster") -public class RedisDeliveryRepository implements DeliveryRepository { - - private static final Logger logger = LoggerFactory.getLogger(RedisDeliveryRepository.class); - - private static final String KEY = "MZS_DELIVERY_REQUEST"; - - private final RedisTemplate redisTemplate; - private final Marshaller marshaller; - private final HashOperations hashOperations; - private final ObjectFactory factory = new ObjectFactory(); - - @Autowired - public RedisDeliveryRepository(RedisTemplate redisTemplate, Marshaller marshaller){ - logger.info("RedisDeliveryRepository()"); - - this.redisTemplate = redisTemplate; - this.hashOperations = redisTemplate.opsForHash(); - this.marshaller = marshaller; - } - - @Override - public void add(DeliveryRequestType request) { - var hashKey = request.getMetaData().getAppDeliveryID(); - var serializedRequest = marshaller.marshallXml(factory.createDeliveryRequest(request)); - - logger.info("Save DeliveryRequest with AppDeliveryId = {} to redis.", hashKey); - hashOperations.put(KEY, hashKey, serializedRequest); - } - - @Override - public Optional getDeliveryRequest(String appDeliveryID) { - var serializedRequest = (String) hashOperations.get(KEY, appDeliveryID); - - if (serializedRequest == null) { - return Optional.empty(); - } - - var stream = new StringInputStream(serializedRequest); - var request = (JAXBElement) marshaller.unmarshallXml(stream); - return Optional.of(request.getValue()); - } - -} diff --git a/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java new file mode 100644 index 0000000..d5766b6 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/repository/DeliveryRepository.java @@ -0,0 +1,15 @@ +package at.gv.egiz.moazs.repository; + +import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface DeliveryRepository { + + void add(DeliveryRequestType request); + + Optional getDeliveryRequest(String appDeliveryID); + +} diff --git a/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java new file mode 100644 index 0000000..e5486d7 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/repository/InMemoryDeliveryRepository.java @@ -0,0 +1,40 @@ +package at.gv.egiz.moazs.repository; + +import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Repository; + +import java.util.Optional; +import java.util.concurrent.TimeUnit; + +@Repository +@Profile("!cluster") +public class InMemoryDeliveryRepository implements DeliveryRepository { + + private static final Logger logger = LoggerFactory.getLogger(InMemoryDeliveryRepository.class); + + private static final Cache rep = CacheBuilder.newBuilder() + .expireAfterWrite(30, TimeUnit.MINUTES) + .build(); + + public InMemoryDeliveryRepository() { + logger.info("InMemoryDeliveryRepository()"); + } + + @Override + public void add(DeliveryRequestType request) { + String key = request.getMetaData().getAppDeliveryID(); + logger.info("Save DeliveryRequest with AppDeliveryId = {} to Cache.", key); + rep.put(key, request); + } + + @Override + public Optional getDeliveryRequest(String appDeliveryID) { + DeliveryRequestType deliveryRequestType = rep.getIfPresent(appDeliveryID); + return Optional.ofNullable(deliveryRequestType); + } +} diff --git a/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java new file mode 100644 index 0000000..4f39f31 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/repository/RedisDeliveryRepository.java @@ -0,0 +1,62 @@ +package at.gv.egiz.moazs.repository; + +import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; +import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.ObjectFactory; +import at.gv.egiz.moazs.Marshaller; +import org.apache.tools.ant.filters.StringInputStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Repository; + +import javax.xml.bind.JAXBElement; +import java.util.Optional; + +@Repository +@Profile("cluster") +public class RedisDeliveryRepository implements DeliveryRepository { + + private static final Logger logger = LoggerFactory.getLogger(RedisDeliveryRepository.class); + + private static final String KEY = "MZS_DELIVERY_REQUEST"; + + private final RedisTemplate redisTemplate; + private final Marshaller marshaller; + private final HashOperations hashOperations; + private final ObjectFactory factory = new ObjectFactory(); + + @Autowired + public RedisDeliveryRepository(RedisTemplate redisTemplate, Marshaller marshaller){ + logger.info("RedisDeliveryRepository()"); + + this.redisTemplate = redisTemplate; + this.hashOperations = redisTemplate.opsForHash(); + this.marshaller = marshaller; + } + + @Override + public void add(DeliveryRequestType request) { + var hashKey = request.getMetaData().getAppDeliveryID(); + var serializedRequest = marshaller.marshallXml(factory.createDeliveryRequest(request)); + + logger.info("Save DeliveryRequest with AppDeliveryId = {} to redis.", hashKey); + hashOperations.put(KEY, hashKey, serializedRequest); + } + + @Override + public Optional getDeliveryRequest(String appDeliveryID) { + var serializedRequest = (String) hashOperations.get(KEY, appDeliveryID); + + if (serializedRequest == null) { + return Optional.empty(); + } + + var stream = new StringInputStream(serializedRequest); + var request = (JAXBElement) marshaller.unmarshallXml(stream); + return Optional.of(request.getValue()); + } + +} -- cgit v1.2.3