-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathTrexSQLInstanceManager.java
More file actions
117 lines (100 loc) · 3.62 KB
/
TrexSQLInstanceManager.java
File metadata and controls
117 lines (100 loc) · 3.62 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
package org.ohdsi.webapi.trexsql;
import org.trex.Trexsql;
import jakarta.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
@Component
@ConditionalOnProperty(name = "trexsql.enabled", havingValue = "true", matchIfMissing = false)
public class TrexSQLInstanceManager {
private static final Logger log = LoggerFactory.getLogger(TrexSQLInstanceManager.class);
private final TrexSQLConfig config;
private volatile boolean initialized = false;
private volatile boolean initFailed = false;
private final ReentrantLock initLock = new ReentrantLock();
public TrexSQLInstanceManager(TrexSQLConfig config) {
this.config = config;
}
public void ensureInitialized() {
if (!config.isEnabled()) {
throw new IllegalStateException("TrexSQL is not enabled");
}
if (initFailed) {
return;
}
if (!initialized) {
initLock.lock();
try {
if (!initialized && !initFailed) {
log.info("Initializing TrexSQL instance");
try {
Trexsql.init(buildConfig());
initialized = true;
log.info("TrexSQL instance initialized successfully");
} catch (Exception | Error e) {
log.error("Failed to initialize TrexSQL: {}. TrexSQL features will be unavailable.", e.getMessage());
initFailed = true;
}
}
} finally {
initLock.unlock();
}
}
}
public boolean isAvailable() {
if (!config.isEnabled() || !initialized) {
return false;
}
try {
return Trexsql.isRunning();
} catch (Exception e) {
log.warn("Error checking TrexSQL status: {}", e.getMessage());
return false;
}
}
public boolean isAttached(String databaseCode) {
if (!initialized) {
return false;
}
try {
return Trexsql.isAttached(databaseCode);
} catch (Exception e) {
log.warn("Error checking if database {} is attached: {}", databaseCode, e.getMessage());
return false;
}
}
private Map<String, Object> buildConfig() {
Map<String, Object> initConfig = new HashMap<>();
if (config.getExtensionsPath() != null && !config.getExtensionsPath().isEmpty()) {
initConfig.put("extensions-path", config.getExtensionsPath());
}
if (config.getCachePath() != null && !config.getCachePath().isEmpty()) {
initConfig.put("cache-path", config.getCachePath());
}
initConfig.put("allow-unsigned-extensions", true);
return initConfig;
}
@PreDestroy
public void shutdown() {
initLock.lock();
try {
if (initialized) {
log.info("Shutting down TrexSQL instance");
try {
Trexsql.shutdown();
log.info("TrexSQL instance shut down successfully");
} catch (Exception e) {
log.error("Error shutting down TrexSQL instance: {}", e.getMessage(), e);
} finally {
initialized = false;
}
}
} finally {
initLock.unlock();
}
}
}