CacheElement.java
4.22 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package it.softecspa.fileproxy.cache;
import it.softecspa.PortalSettings;
import it.softecspa.database.dbconnect.SQLTimeoutException;
import it.softecspa.fileproxy.DatabaseBalancer.Balancer;
import it.softecspa.fileproxy.services.ServerCacheFactory.CacheKey;
import it.softecspa.fileproxy.services.common.EnterpriseLog;
import java.sql.SQLException;
import java.util.Collection;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.log4j.Logger;
public abstract class CacheElement<K, V> {
protected Logger log = Logger.getLogger(getClass());
// Flag che indicase la cache è stata caricata
private boolean loaded;
// Contenitore delle coppie chiave/valore
private SortedMap<K,V> container;
// Nome della cache che la identifica univocamente
private CacheKey uniqueName;
// --------------------------------
private Balancer balancer;
public CacheElement(CacheKey name, Balancer balancer) {
this.uniqueName = name;
this.balancer = balancer;
this.container = new TreeMap<K, V>();
}
/**
* La cache è spirata, deve essere ricaricata
* @return
*/
public boolean isExpired() {
return !loaded;
}
/**
* La cache non è valida, non è caricata
* @return
*/
public boolean isLoaded() {
return loaded;
}
/**
* Restituisce il container
* @return
*/
protected SortedMap<K, V> getContainer() {
return container;
}
/*
private void setContainer(Hashtable<K, V> values) {
this.container = values;
}
*/
/**
* Esegue il reload della cache
*/
public synchronized void load() {
flush();
// Eseguo la chiamata vera e propria per i caricamento
/*
* 2013-05-07, m.veroni - Riscontrato errore che impedisce il reload in caso di errore di caricamento
*/
try {
log.info("Load '"+getUniqueName()+"' cache container from "+balancer.toString()+" database");
loaded = load(container, balancer);
} catch (CacheException e) {
loaded = false;
// SQLTimeoutException, nessun invio di mail
if ((e.getCause()!=null && e.getCause() instanceof SQLTimeoutException)) {
log.fatal("SQLTimeoutException loading cache (DB TIMEOUT):" + e.getCause());
return;
}
// SQLException, invio della mail
if ((e.getCause()!=null && e.getCause() instanceof SQLException)) {
EnterpriseLog elog = new EnterpriseLog(e.getCause().getMessage(), e);
elog.write();
elog.sendEmail(PortalSettings.MAIL_SQL_ERROR_ENABLE, "Error loading cache table");
return;
}
log.fatal("Error in table cache: "+e.toString(),e);
EnterpriseLog elog = new EnterpriseLog(e.getCause().getMessage(), e);
elog.write();
elog.sendEmail(PortalSettings.MAIL_SQL_ERROR_ENABLE, "Error loading cache table");
}
}
/**
* Esegue il caricamento della cache a partire dal database
* In caso di esito positivo è restituito TRUE
* In caso di errore è restituito un FALSE
* @param container
* @param balancer
* @return
*/
protected abstract boolean load(SortedMap<K, V> container, Balancer balancer) throws CacheException;
protected abstract String[] getTables();
/**
* Pulisce il contenitore e lo obbliga ad un successivo reload
*/
public synchronized void flush() {
if (log.isDebugEnabled()) log.debug("Flush '"+getUniqueName()+"' cache container ("+balancer.name()+" balance)");
container.clear();
loaded = false;
}
public Balancer getBalancer() {
return balancer;
}
public CacheKey getUniqueName() {
return uniqueName;
}
/**
* Restituisce i valori di un altro container
* @param uniqueName
* @param balancer
* @return
* @throws CacheException
*/
protected Collection<V> getOtherContainerValues(CacheKey uniqueName, Balancer balancer) throws CacheException {
CacheContainer cacheContainer = CacheContainer.getInstance();
SortedMap<?, V> other = cacheContainer.getContainer(uniqueName, balancer);
if (other==null) return null;
// Faccio una doppia verifica nel caso di errori di caricamento
CacheElement<?,V> cacheElement = cacheContainer.getCacheElement(uniqueName, balancer);
if (cacheElement.isExpired()) return null;
//
return other.values();
}
}