From 8d5386be38db25a2a41c3bf6c876adee21ca26cc Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Fri, 19 Sep 2014 08:26:02 +0000
Subject: [PATCH] #396 fix - Fixed more SonarQube issues
---
securis/src/main/java/net/curisit/securis/utils/CacheTTL.java | 184 +++++++++++++++++++++++----------------------
1 files changed, 93 insertions(+), 91 deletions(-)
diff --git a/securis/src/main/java/net/curisit/securis/utils/CacheTTL.java b/securis/src/main/java/net/curisit/securis/utils/CacheTTL.java
index 01b5c9b..f69cf25 100644
--- a/securis/src/main/java/net/curisit/securis/utils/CacheTTL.java
+++ b/securis/src/main/java/net/curisit/securis/utils/CacheTTL.java
@@ -13,120 +13,122 @@
import org.apache.logging.log4j.LogManager;
/**
- * Cache implementation with TTL (time To Live) The objects are removed from cache when TTL is reached.
+ * Cache implementation with TTL (time To Live) The objects are removed from
+ * cache when TTL is reached.
*
* @author roberto <roberto.sanchez@curisit.net>
*/
@Singleton
public class CacheTTL {
- private static final Logger LOG = LogManager.getLogger(CacheTTL.class);
+ private static final Logger LOG = LogManager.getLogger(CacheTTL.class);
- /**
- * Period before token expires, set in seconds.
- */
- private static int DEFAULT_CACHE_DURATION = 24 * 60 * 60;
+ /**
+ * Period before token expires, set in seconds.
+ */
+ private static int DEFAULT_CACHE_DURATION = 24 * 60 * 60;
- private Map<String, CachedObject> data = new HashMap<>();
+ private Map<String, CachedObject> data = new HashMap<>();
- private Thread cleaningThread = null;
+ private Thread cleaningThread = null;
- @Inject
- public CacheTTL() {
- cleaningThread = new Thread(new Runnable() {
+ @Inject
+ public CacheTTL() {
+ cleaningThread = new Thread(new Runnable() {
- @Override
- public void run() {
- while (CacheTTL.this.data != null) {
- try {
- // We check for expired object every 60 seconds
- Thread.sleep(60 * 1000);
- } catch (InterruptedException e) {
- LOG.error("Exiting from Cache Thread");
- data.clear();
- return;
- }
- // LOG.info("Cheking expired objects " + new Date());
- Date now = new Date();
- List<String> keysToRemove = new ArrayList<>();
- for (String key : CacheTTL.this.data.keySet()) {
- CachedObject co = CacheTTL.this.data.get(key);
- if (now.after(co.getExpireAt())) {
- keysToRemove.add(key);
- }
- }
- for (String key : keysToRemove) {
- // If we try to remove directly in the previous loop an exception is thrown java.util.ConcurrentModificationException
- CacheTTL.this.data.remove(key);
- }
- }
- }
- });
- cleaningThread.start();
- }
+ @Override
+ public void run() {
+ while (CacheTTL.this.data != null) {
+ try {
+ // We check for expired object every 60 seconds
+ Thread.sleep(60 * 1000);
+ } catch (InterruptedException e) {
+ LOG.error("Exiting from Cache Thread");
+ data.clear();
+ return;
+ }
+ Date now = new Date();
+ List<String> keysToRemove = new ArrayList<>();
+ for (String key : CacheTTL.this.data.keySet()) {
+ CachedObject co = CacheTTL.this.data.get(key);
+ if (now.after(co.getExpireAt())) {
+ keysToRemove.add(key);
+ }
+ }
+ for (String key : keysToRemove) {
+ // If we try to remove directly in the previous loop an
+ // exception is thrown
+ // java.util.ConcurrentModificationException
+ CacheTTL.this.data.remove(key);
+ }
+ }
+ }
+ });
+ cleaningThread.start();
+ }
- /**
- *
- * @param key
- * @param obj
- * @param ttl
- * Time To Live in seconds
- */
- public void set(String key, Object obj, int ttl) {
- Date expirationDate = new Date(new Date().getTime() + ttl * 1000);
- data.put(key, new CachedObject(expirationDate, obj));
- }
+ /**
+ *
+ * @param key
+ * @param obj
+ * @param ttl
+ * Time To Live in seconds
+ */
+ public void set(String key, Object obj, int ttl) {
+ Date expirationDate = new Date(new Date().getTime() + ttl * 1000);
+ data.put(key, new CachedObject(expirationDate, obj));
+ }
- public void set(String key, Object obj) {
- set(key, obj, DEFAULT_CACHE_DURATION);
- }
+ public void set(String key, Object obj) {
+ set(key, obj, DEFAULT_CACHE_DURATION);
+ }
- public Object get(String key) {
- CachedObject co = data.get(key);
- return co == null ? null : co.getObject();
- }
+ public Object get(String key) {
+ CachedObject co = data.get(key);
+ return co == null ? null : co.getObject();
+ }
- public <T> T get(String key, Class<T> type) {
- CachedObject co = data.get(key);
- return co == null ? null : co.getObject(type);
- }
+ public <T> T get(String key, Class<T> type) {
+ CachedObject co = data.get(key);
+ return co == null ? null : co.getObject(type);
+ }
- public <T> T remove(String key, Class<T> type) {
- CachedObject co = data.remove(key);
- return co == null ? null : co.getObject(type);
- }
+ public <T> T remove(String key, Class<T> type) {
+ CachedObject co = data.remove(key);
+ return co == null ? null : co.getObject(type);
+ }
- public Object remove(String key) {
- CachedObject co = data.remove(key);
- return co == null ? null : co.getObject();
- }
+ public Object remove(String key) {
+ CachedObject co = data.remove(key);
+ return co == null ? null : co.getObject();
+ }
- public void clear() {
- data.clear();
- }
+ public void clear() {
+ data.clear();
+ }
- private class CachedObject {
- Date expireAt;
- Object object;
+ private class CachedObject {
+ Date expireAt;
+ Object object;
- public CachedObject(Date date, Object obj) {
- expireAt = date;
- object = obj;
- }
+ public CachedObject(Date date, Object obj) {
+ expireAt = date;
+ object = obj;
+ }
- public Date getExpireAt() {
- return expireAt;
- }
+ public Date getExpireAt() {
+ return expireAt;
+ }
- public Object getObject() {
- return object;
- }
+ public Object getObject() {
+ return object;
+ }
- @SuppressWarnings("unchecked")
- public <T> T getObject(Class<T> type) {
- return (T) object;
- }
+ @SuppressWarnings("unchecked")
+ public <T> T getObject(Class<T> type) {
+ return (T) object;
+ }
- }
+ }
}
--
Gitblit v1.3.2