-
Notifications
You must be signed in to change notification settings - Fork 8
/
CacheConnection.java
133 lines (105 loc) · 4.01 KB
/
CacheConnection.java
1
2
3
4
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.boomi.connector.caching;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.commons.codec.digest.DigestUtils;
import com.boomi.connector.api.BrowseContext;
import com.boomi.connector.api.PropertyMap;
import com.boomi.connector.util.BaseConnection;
import com.boomi.proserv.caching.CacheInstance;
/**
*
* @author anthony.rabiaza@gmail.com
*
*/
public class CacheConnection extends BaseConnection {
private static Map<String, CacheInstance> s_cacheInstance;
private static Timer s_timer;
//Every 1 minute
private static long DELAY = 1 * 60000L;
private static long PERIOD = 1 * 60000L;
//Default Inactive Period: 1 hour
private static long INACTIVE_PERIOD = 3600 * 60000L;
PropertyMap propertiesMap;
static {
TimerTask repeatedTask = new TimerTask() {
public void run() {
System.out.println("Cleaning " + new Date());
if(s_cacheInstance != null) {
for (Iterator<String> iterator = s_cacheInstance.keySet().iterator(); iterator.hasNext();) {
String currentInstanceName = iterator.next();
CacheInstance currentInstance = s_cacheInstance.get(currentInstanceName);
if(currentInstance != null && currentInstance.getLastUsedDate() != null) {
if((currentInstance.getLastUsedDate().getTime()-new Date().getTime())>=INACTIVE_PERIOD) {
currentInstance.close();
s_cacheInstance.remove(currentInstanceName);
}
}
}
}
}
};
s_timer = new Timer("CacheConnection_cleanup");
s_timer.scheduleAtFixedRate(repeatedTask, DELAY, PERIOD);
}
public CacheConnection(BrowseContext context) {
super(context);
propertiesMap = context.getConnectionProperties();
}
private CacheInstance getInstance() throws Exception {
CacheInstance cacheInstance = null;
try {
String propertiesHash = DigestUtils.sha256Hex(propertiesMap.toString());
if(s_cacheInstance!=null) {
cacheInstance = s_cacheInstance.get(propertiesHash);
} else {
synchronized(this){
s_cacheInstance = Collections.synchronizedMap(new HashMap<String, CacheInstance>());
}
}
if(cacheInstance == null || !cacheInstance.isValid()){
synchronized(this){
Properties properties = new Properties();
cacheInstance = new CacheInstance();
cacheInstance.setStandalone(false);
cacheInstance.setDynamicProcessPropertiesFilter(propertiesMap.getProperty("properties_filter"));
cacheInstance.setHashing(propertiesMap.getBooleanProperty("hashing"));
String type = propertiesMap.getProperty("type");
cacheInstance.setType(type);
properties.put(type + "." + "heap", propertiesMap.getProperty("heap"));
properties.put(type + "." + "hosts", propertiesMap.getProperty("hosts"));
properties.put(type + "." + "useSSL", propertiesMap.getBooleanProperty("useSSL").toString());
properties.put(type + "." + "user", propertiesMap.getProperty("user"));
properties.put(type + "." + "password", propertiesMap.getProperty("password"));
cacheInstance.setProperties(properties);
cacheInstance.init();
s_cacheInstance.put(propertiesHash, cacheInstance);
}
}
//TODO, put a timestamp of last used on cacheInstance
return cacheInstance;
} catch (Exception e) {
throw new Exception("Error when getting the Cache. Please Retry. Error: " + e.getMessage(), e);
}
}
public String get(String cacheName, String key) throws Exception {
return getInstance().get(cacheName, key);
}
public void upsert(String cacheName, String key, String value) throws Exception {
getInstance().set(cacheName, key, value);
}
public void delete(String cacheName) throws Exception {
getInstance().delete(cacheName);
}
public void delete(String cacheName, String key) throws Exception {
getInstance().delete(cacheName, key);
}
public String computeKey(Properties properties) throws Exception {
return getInstance().computeKey(properties);
}
}