ServerCacheFactory.java
8.32 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package it.softecspa.fileproxy.services;
import it.softecspa.database.dbconnect.ConnectionManager;
import it.softecspa.database.dbconnect.NoRecordFoundException;
import it.softecspa.fileproxy.DatabaseBalancer;
import it.softecspa.fileproxy.DatabaseBalancer.Balancer;
import it.softecspa.fileproxy.cache.CacheContainer;
import it.softecspa.fileproxy.cache.CacheElement;
import it.softecspa.fileproxy.db.ConfigProperties;
import it.softecspa.fileproxy.db.criterias.ConfigPropertiesCriteria;
import it.softecspa.kahuna.lang.XString;
import it.softecspa.kahuna.util.Properties;
import java.sql.SQLException;
import java.util.Set;
import org.apache.log4j.Logger;
/**
* Contiene lo stato dei servizi e dei domini attivi *
* @author m.veroni
* @param <T>
*/
public class ServerCacheFactory {
private static ServerCacheFactory instance;
private Logger log = Logger.getLogger(getClass());
// Contenitore delle strutture in cache
private CacheContainer cacheContainer;
public ListContainer list;
// Parametri di configurazione su database - T001_CONFIG_PROPERTIES
private Properties dbProperties;
/**
* Classe fittizia di rapporto
*/
public class ListContainer {
/* FIXME esempio
public Collection<Instance> ofInstances(boolean stage) {
try {
SortedMap<String, Instance> container = cacheContainer.getContainer(CacheKey.INSTANCE_ID, getBalancer(stage));
return container.values();
} catch (CacheException e) {
log.error("Cache container problem: "+e.toString());
return null;
}
}
*/
}
private ServerCacheFactory() {
super();
synchronized (ServerCacheFactory.class) {
if (instance == null) {
instance = this;
//
log.info(ServerCacheFactory.class.getSimpleName()+" instance is starting!");
instance.reloadAll();
}
}
}
public static ServerCacheFactory getInstance() {
synchronized (ServerCacheFactory.class) {
if (instance == null) new ServerCacheFactory();
}
return instance;
}
/**
* Carica la struttura contenente le abilitazioni al servizio
*/
private synchronized void reload(boolean stage) {
try {
Balancer balancer = getBalancer(stage);
if (!DatabaseBalancer.getInstance().isActive()) {
// non posso ancare avanti se non è presente un database
return;
}
if (stage) {
loadProperties();
}
/* Esempio
cacheContainer.createContainer(new KmTypeRemoteCache(balancer));
cacheContainer.createContainer(new KmTypeDeviceCache(balancer));
*/
/*
} catch (CacheException e) {
log.fatal(e.toString(),e);
*/} catch (Exception e) {
log.fatal("Unhandled exception",e);
}
}
public synchronized void reloadAll() {
if (cacheContainer==null) {
if (log.isDebugEnabled()) log.debug("Create instance of " + CacheContainer.class + " service");
cacheContainer = CacheContainer.getInstance();
list = new ListContainer();
}
reloadOnStage();
reloadOnMaster();
}
public synchronized void reloadOnStage() {
// Estraggo i dati dal database STAGE
log.info("Reload cache on STAGE database");
reload(true);
}
public synchronized void reloadOnMaster() {
// Estraggo i dati dal database MASTER
log.info("Reload cache on MASTER database");
reload(false);
}
private Balancer getBalancer(boolean stage) {
return (stage?Balancer.STAGE:Balancer.MASTER);
}
/**
* Estrazione parametri di configurazione dal database (MASTER)
* Nota: su STAGE è una vista su MASTER
*/
private void loadProperties() {
ConnectionManager cm = DatabaseBalancer.getInstance().get(Balancer.STAGE);
try {
log.info("Load 'Properties' configuration list on STAGE");
ConfigPropertiesCriteria criteria = new ConfigPropertiesCriteria(cm);
ConfigProperties[] lista = criteria.select();
if (dbProperties!=null) {
dbProperties.clear();
} else {
dbProperties = new Properties();
}
for (ConfigProperties elemento : lista) {
dbProperties.put(elemento.getKey(), elemento.getValue());
}
log.info("Loaded configuration from database");
} catch (NoRecordFoundException e) {
log.warn("No properties available on STAGE database, verify configuration");
} catch (SQLException e) {
log.fatal("Problem reading database",e);
dbProperties = null;
}
}
/**
* Properties estratte dal database (MASTER)
* @return
*/
public Properties getDbProperties() {
// Controllo inserito per la gestione dei problemi di connettività
if (dbProperties==null) loadProperties();
return dbProperties;
}
public enum PropertiesKey {
CLUSTER_PASSWORD ("cluster_password")
, CLUSTER_USERNAME ("cluster_username")
, MASTER_SERVER_URL ("master.server.url")
, MASTER_HEARTBEAT_COUNT ("master.heartbeat.count")
, MASTER_SERVER_USERNAME ("master.server.username")
, MASTER_SERVER_PASSWORD ("master.server.password")
// Url pubblico del semaforo
//, URL_SEMAFORO ("url.semaforo")
, CONSOLE_URL ("url.console")
// ...
, HEARTBEAT_DELAY ("haertbeat.delay")
, TRIAL_DEFAULT_TYPE ("trial.default.type")
, TRIAL_DEFAULT_VALUE ("trial.default.value")
, DAY_ALERT ("day_alert")
// Root di filesystem per salvare le risorse
, RESOURCE_ROOT_MASTER ("resource.root")
, RESOURCE_ROOT_STAGE ("resource.root.stage")
//, RESOURCE_THUMB_WIDTH ("resource.thumb.width")
//, RESOURCE_THUMB_HEIGHT ("resource.thumb.height")
// Root di filesystem per salvare i pacchetti da scaricare
//, DOWNLOAD_ROOT ("download.root")
//, DOWNLOAD_URL ("download.url")
/*
// Email per sincronizzazione
, SYNCHRO_EMAIL_ERROR ("synchro.mail.error")
, SYNCHRO_EMAIL ("synchro.mail")
, SYNCHRO_LOCK_CLIENT ("synchro.lock.client")
*/
/*
// Connettività verso My-DesktopMate
, MYDM_URL ("mydesktopmate.url")
, MYDM_USERNAME ("mydesktopmate.username")
, MYDM_PASSWORD ("mydesktopmate.password")
*/
// Abilitazione/disabilitazione servizi
//, DISABLE_CHECK_HISTORY ("disable.check.history")
, LANGUAGE_DISABLE_LIST ("language.disable.list")
/*
, SHARE_URL ("share.url")
, MAINTENANCE_ENABLE ("maintenance.enable")
, PROVISIONING_ALERT ("provisioning.alert")
, DAILY_REPORT_FLUSH_LINE ("daily.report.flush.line")
, DAILY_REPORT_FLUSH_HYML ("daily.report.flush.html")
*/
;
private String value;
private PropertiesKey(String value) {
this.value = value;
}
public String toString() {
return value;
}
}
public enum CacheKey {
KM_TYPE_REMOTE
, KM_TYPE_DEVICE
;
}
/**
* @deprecated
* Restituisce la ROOT delle risorse
* @param stagemode
* @return
*/
public String getResourceRoot(boolean stagemode) {
return getDbProperties().get(stagemode?PropertiesKey.RESOURCE_ROOT_STAGE.toString():PropertiesKey.RESOURCE_ROOT_MASTER.toString());
}
public String getResourceRoot() {
return getDbProperties().get(PropertiesKey.RESOURCE_ROOT_MASTER.toString());
}
public Set<String> getCachedList(boolean stagemode) {
return cacheContainer.getCachedTables()[getBalancer(stagemode).index()].keySet();
}
public boolean isCached(String tablename, boolean stagemode) {
if (XString.isBlankNull(tablename)) return true;
return cacheContainer.getCachedTables()[getBalancer(stagemode).index()].containsKey(tablename);
}
public synchronized void reloadAll(String tablename) {
reloadOnStage(tablename);
reloadOnMaster(tablename);
}
public void reloadOnStage(String tablename) {
if (XString.isBlankNull(tablename)) {
reloadOnStage();
return;
}
if (!isCached(tablename, true)) return;
for (CacheElement<?,?> element : cacheContainer.getCachedTables()[getBalancer(true).index()].get(tablename)) {
element.flush();
}
}
public void reloadOnMaster(String tablename) {
if (XString.isBlankNull(tablename)) {
reloadOnMaster();
return;
}
if (!isCached(tablename, false)) return;
for (CacheElement<?,?> element : cacheContainer.getCachedTables()[getBalancer(false).index()].get(tablename)) {
element.flush();
}
}
}