Repository navigation
Expand file tree
/
Copy pathDefaultDebugUsageController.java
More file actions
174 lines (138 loc) · 6.69 KB
/
Copy pathDefaultDebugUsageController.java
File metadata and controls
174 lines (138 loc) · 6.69 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
/*
* Copyright (c) Mirth Corporation. All rights reserved.
*
* http://www.mirthcorp.com
*
* The software in this package is published under the terms of the MPL license a copy of which has
* been included with this distribution in the LICENSE.txt file.
*/
package com.mirth.connect.server.controllers;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSessionManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.mirth.connect.client.core.ControllerException;
import com.mirth.connect.model.DebugUsage;
import com.mirth.connect.server.ExtensionLoader;
import com.mirth.connect.server.util.SqlConfig;
import com.mirth.connect.server.util.StatementLock;
/**
* The ConfigurationController provides access to the Mirth configuration.
*
*/
public class DefaultDebugUsageController extends DebugUsageController {
private static final String VACUUM_LOCK_ID = "DebugUsage.vacuumDebuggerUsageTable";
private Logger logger = LogManager.getLogger(this.getClass());
private ConfigurationController configurationController = ControllerFactory.getFactory().createConfigurationController();
// singleton pattern
private static DebugUsageController instance = null;
public DefaultDebugUsageController() {
}
public static DebugUsageController create() {
synchronized (DefaultDebugUsageController.class) {
if (instance == null) {
instance = ExtensionLoader.getInstance().getControllerInstance(DebugUsageController.class);
if (instance == null) {
instance = new DefaultDebugUsageController();
((DefaultDebugUsageController) instance).initialize();
} else {
try {
instance.getClass().getMethod("initialize").invoke(instance);
} catch (Exception e) {
LogManager.getLogger(DefaultDebugUsageController.class).error("Error calling initialize method in DefaultConfigurationController", e);
}
}
}
return instance;
}
}
public void initialize() {
}
public Map<String, Object> getDebugUsageMap(DebugUsage debugUsage) {
Map<String, Object> map = new HashMap<String, Object>();
if (debugUsage!=null) {
map.put("serverId", debugUsage.getServerId());
map.put("duppCount", debugUsage.getDuppCount());
map.put("attachBatchCount", debugUsage.getAttachBatchCount());
map.put("sourceConnectorCount", debugUsage.getSourceConnectorCount());
map.put("sourceFilterTransCount", debugUsage.getSourceFilterTransCount());
map.put("destinationFilterTransCount", debugUsage.getDestinationFilterTransCount());
map.put("destinationConnectorCount", debugUsage.getDestinationConnectorCount());
map.put("responseCount", debugUsage.getResponseCount());
map.put("invocationCount", debugUsage.getInvocationCount());
}
return map;
}
public synchronized boolean upsertDebugUsage(DebugUsage debugUsage) throws ControllerException {
StatementLock.getInstance(VACUUM_LOCK_ID).readLock();
try {
DebugUsage persistedDebugUsage = getDebugUsage(configurationController.getServerId());
// if server id record exists, update current record
if (persistedDebugUsage != null) {
logger.debug("updating debug usage statistics for serverId" + debugUsage.getServerId());
SqlConfig.getInstance().getSqlSessionManager().update("DebugUsage.updateDebugUsageStatistics", getDebugUsageMap(debugUsage));
return true;
// otherwise, insert a new record
} else {
logger.debug("inserting debug usage statistics for serverId" + debugUsage.getServerId());
SqlConfig.getInstance().getSqlSessionManager().insert("DebugUsage.insertDebugUsageStatistics", getDebugUsageMap(debugUsage));
return true;
}
} catch (PersistenceException e) {
throw new ControllerException(e);
} finally {
StatementLock.getInstance(VACUUM_LOCK_ID).readUnlock();
}
}
// returns debug usage for each server id
public DebugUsage getDebugUsage(String serverId) throws ControllerException {
logger.debug("getting debug usage for serverId: " + serverId);
if (serverId == null) {
throw new ControllerException("Error getting usage for serverId: serverId cannot be null.");
}
StatementLock.getInstance(VACUUM_LOCK_ID).readLock();
try {
DebugUsage debugUsage = new DebugUsage();
debugUsage.setServerId(serverId);
return getReadOnlySqlSessionManager().selectOne("DebugUsage.getDebugUsageStatistics", debugUsage);
} catch (PersistenceException e) {
throw new ControllerException(e);
} finally {
StatementLock.getInstance(VACUUM_LOCK_ID).readUnlock();
}
}
// returns object for all server id's in the debugger_usage table
public List<DebugUsage> getDebugUsages() throws ControllerException {
logger.debug("getting debug usage for all serverIds");
StatementLock.getInstance(VACUUM_LOCK_ID).readLock();
try {
return SqlConfig.getInstance().getReadOnlySqlSessionManager().selectList("DebugUsage.getDebugUsageStatistics", null);
} catch (PersistenceException e) {
throw new ControllerException(e);
} finally {
StatementLock.getInstance(VACUUM_LOCK_ID).readUnlock();
}
}
public int deleteDebugUsage(String serverId) throws ControllerException {
logger.debug("deleting debug usage for serverId: " + serverId);
if (serverId == null) {
throw new ControllerException("Error getting usage for serverId: serverId cannot be null.");
}
StatementLock.getInstance(VACUUM_LOCK_ID).readLock();
try {
DebugUsage debugUsage = new DebugUsage();
debugUsage.setServerId(serverId);
return SqlConfig.getInstance().getSqlSessionManager().delete("DebugUsage.deleteDebugUsageStatistics", debugUsage);
} catch (PersistenceException e) {
throw new ControllerException(e);
} finally {
StatementLock.getInstance(VACUUM_LOCK_ID).readUnlock();
}
}
protected SqlSessionManager getReadOnlySqlSessionManager() {
return SqlConfig.getInstance().getReadOnlySqlSessionManager();
}
}