Merge branch 'release/5.0.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / sessions / SessionsViewPart.java
1 /**
2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.view.sessions;
10
11 import java.text.DecimalFormat;
12 import java.util.Collection;
13
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
16
17 import org.eclipse.core.commands.Command;
18 import org.eclipse.core.commands.ParameterizedCommand;
19 import org.eclipse.e4.core.commands.ECommandService;
20 import org.eclipse.e4.core.commands.EHandlerService;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.swt.widgets.TableColumn;
31 import org.eclipse.swt.widgets.TableItem;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.wb.swt.SWTResourceManager;
34
35 import eu.etaxonomy.cdm.api.cache.CdmCacher;
36 import eu.etaxonomy.cdm.cache.CdmRemoteCacheManager;
37 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
38 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
39 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
40 import eu.etaxonomy.taxeditor.session.NullSession;
41 import eu.etaxonomy.taxeditor.store.CdmStore;
42 import net.sf.ehcache.Cache;
43 import net.sf.ehcache.CacheManager;
44 import net.sf.ehcache.statistics.LiveCacheStatistics;
45
46 /**
47 * @author cmathew
48 * @date 16 Feb 2015
49 *
50 */
51 public class SessionsViewPart implements ICdmEntitySessionManagerObserver {
52
53 public static final String ID = "eu.etaxonomy.taxeditor.view.sessions.SessionsViewPart"; //$NON-NLS-1$
54 public static final String OPEN_INSPECT_SESSIONS_DIALOG_ID = "eu.etaxonomy.taxeditor.store.open.InspectSessionsDialog";
55 private Table tableSessions;
56
57 private final String[] titles = { "Owner", "In Memory", "On Disk"};
58
59 private final ICdmEntitySessionManager cdmEntitySessionManager;
60 private Text txtNoOfSessions;
61 private Text txtNoOfCaches;
62 private Text txtDefaultInMemory;
63 private Text txtCdmModelInMemory;
64 private Text txtCdmModelOnDisk;
65 private Text txtDefaultOnDisk;
66
67 @Inject
68 private ECommandService commandService;
69
70 @Inject
71 private EHandlerService handlerService;
72
73 @Inject
74 public SessionsViewPart() {
75 this.cdmEntitySessionManager = CdmStore.getCurrentSessionManager();
76 if(cdmEntitySessionManager != null) {
77 cdmEntitySessionManager.addSessionObserver(this);
78 }
79 }
80
81 /**
82 * Create contents of the view part.
83 * @param parent
84 */
85 @PostConstruct
86 public void createPartControl(Composite parent) {
87 Composite container = new Composite(parent, SWT.NONE);
88 container.setLayout(new GridLayout(5, false));
89 {
90 Label lblNoOfSessions = new Label(container, SWT.NONE);
91 lblNoOfSessions.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
92 lblNoOfSessions.setAlignment(SWT.RIGHT);
93 GridData gd_lblNoOfSessions = new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1);
94 gd_lblNoOfSessions.widthHint = 115;
95 gd_lblNoOfSessions.minimumHeight = 30;
96 lblNoOfSessions.setLayoutData(gd_lblNoOfSessions);
97 lblNoOfSessions.setText("No. of Sessions : ");
98 }
99 {
100 txtNoOfSessions = new Text(container, SWT.BORDER);
101 txtNoOfSessions.setEditable(false);
102 txtNoOfSessions.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
103 GridData gd_txtNoOfSessions = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
104 gd_txtNoOfSessions.widthHint = 50;
105 txtNoOfSessions.setLayoutData(gd_txtNoOfSessions);
106 }
107 new Label(container, SWT.NONE);
108 new Label(container, SWT.NONE);
109 new Label(container, SWT.NONE);
110 {
111 Label lblNoOfCaches = new Label(container, SWT.CENTER);
112 lblNoOfCaches.setAlignment(SWT.RIGHT);
113 lblNoOfCaches.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
114 GridData gd_lblNoOfCaches = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
115 gd_lblNoOfCaches.widthHint = 150;
116 lblNoOfCaches.setLayoutData(gd_lblNoOfCaches);
117 lblNoOfCaches.setText("No. of entity caches : ");
118 }
119 {
120 txtNoOfCaches = new Text(container, SWT.BORDER);
121 txtNoOfCaches.setEditable(false);
122 GridData gd_txtNoOfCaches = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
123 gd_txtNoOfCaches.widthHint = 50;
124 txtNoOfCaches.setLayoutData(gd_txtNoOfCaches);
125 }
126 new Label(container, SWT.NONE);
127 new Label(container, SWT.NONE);
128 new Label(container, SWT.NONE);
129
130 {
131 Label lblDefaultCache = new Label(container, SWT.CENTER);
132 lblDefaultCache.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
133 lblDefaultCache.setText("Default Cache, ");
134 lblDefaultCache.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
135 lblDefaultCache.setAlignment(SWT.RIGHT);
136 }
137 {
138 Label lblDefaultInMemory = new Label(container, SWT.CENTER);
139 lblDefaultInMemory.setText("in Memory : ");
140 lblDefaultInMemory.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
141 lblDefaultInMemory.setAlignment(SWT.RIGHT);
142 lblDefaultInMemory.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
143 }
144 {
145 txtDefaultInMemory = new Text(container, SWT.BORDER);
146 txtDefaultInMemory.setEditable(false);
147 GridData gd_txtDefaultInMemory = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
148 gd_txtDefaultInMemory.widthHint = 130;
149 txtDefaultInMemory.setLayoutData(gd_txtDefaultInMemory);
150 }
151 {
152 Label lblDefaultOnDisk = new Label(container, SWT.CENTER);
153 lblDefaultOnDisk.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
154 lblDefaultOnDisk.setText("on Disk : ");
155 lblDefaultOnDisk.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
156 lblDefaultOnDisk.setAlignment(SWT.RIGHT);
157 }
158 {
159 txtDefaultOnDisk = new Text(container, SWT.BORDER);
160 txtDefaultOnDisk.setEditable(false);
161 txtDefaultOnDisk.setText("");
162 GridData gd_txtDefaultOnDisk = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
163 gd_txtDefaultOnDisk.widthHint = 130;
164 txtDefaultOnDisk.setLayoutData(gd_txtDefaultOnDisk);
165 }
166 {
167 Label lblCdmModelCache = new Label(container, SWT.CENTER);
168 lblCdmModelCache.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
169 lblCdmModelCache.setText("CDM Model Cache, ");
170 lblCdmModelCache.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
171 lblCdmModelCache.setAlignment(SWT.RIGHT);
172 }
173 {
174 Label lblCdmModelInMemory = new Label(container, SWT.CENTER);
175 lblCdmModelInMemory.setText("in Memory : ");
176 lblCdmModelInMemory.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
177 lblCdmModelInMemory.setAlignment(SWT.RIGHT);
178 GridData gd_lblCdmModelInMemory = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
179 gd_lblCdmModelInMemory.widthHint = 86;
180 lblCdmModelInMemory.setLayoutData(gd_lblCdmModelInMemory);
181 }
182 {
183 txtCdmModelInMemory = new Text(container, SWT.BORDER);
184 txtCdmModelInMemory.setEditable(false);
185 GridData gd_txtCdmModelInMemory = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
186 gd_txtCdmModelInMemory.widthHint = 130;
187 txtCdmModelInMemory.setLayoutData(gd_txtCdmModelInMemory);
188 }
189 {
190 Label lblCdmModelOnDisk = new Label(container, SWT.CENTER);
191 lblCdmModelOnDisk.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
192 lblCdmModelOnDisk.setText("on Disk : ");
193 lblCdmModelOnDisk.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
194 lblCdmModelOnDisk.setAlignment(SWT.RIGHT);
195 }
196 {
197 txtCdmModelOnDisk = new Text(container, SWT.BORDER);
198 txtCdmModelOnDisk.setEditable(false);
199 txtCdmModelOnDisk.setText("");
200 GridData gd_txtCdmModelOnDisk = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
201 gd_txtCdmModelOnDisk.widthHint = 130;
202 txtCdmModelOnDisk.setLayoutData(gd_txtCdmModelOnDisk);
203 }
204 {
205 Button btnRefresh = new Button(container, SWT.NONE);
206 btnRefresh.addSelectionListener(new SelectionAdapter() {
207 @Override
208 public void widgetSelected(SelectionEvent e) {
209 changed();
210 }
211 });
212 GridData gd_btnRefresh = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
213 gd_btnRefresh.widthHint = 100;
214 btnRefresh.setLayoutData(gd_btnRefresh);
215 btnRefresh.setText("Refresh");
216 }
217 {
218 Button btnInspectActiveSession = new Button(container, SWT.NONE);
219 btnInspectActiveSession.addSelectionListener(new SelectionAdapter() {
220 @Override
221 public void widgetSelected(SelectionEvent e) {
222 Command command = commandService.getCommand(OPEN_INSPECT_SESSIONS_DIALOG_ID);
223 handlerService.executeHandler(ParameterizedCommand.generateCommand(command, null));
224 }
225 });
226 btnInspectActiveSession.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
227 btnInspectActiveSession.setText("Inspect Active Session");
228 }
229 new Label(container, SWT.NONE);
230 new Label(container, SWT.NONE);
231
232 {
233 tableSessions = new Table(container, SWT.BORDER | SWT.FULL_SELECTION);
234 GridData gd_tableSessions = new GridData(SWT.FILL, SWT.FILL, true, true, 5, 1);
235 gd_tableSessions.widthHint = 769;
236 tableSessions.setLayoutData(gd_tableSessions);
237 tableSessions.setHeaderVisible(true);
238 tableSessions.setLinesVisible(true);
239 }
240
241 createActions();
242 }
243
244 /**
245 * Create the actions.
246 */
247 private void createActions() {
248
249
250 for (String title : titles) {
251 TableColumn column = new TableColumn(tableSessions, SWT.NULL);
252 column.setWidth(200);
253 column.setText(title);
254 }
255 changed();
256
257 }
258
259
260 @Override
261 public void changed() {
262 if(tableSessions.isDisposed()) {
263 return;
264 }
265 tableSessions.removeAll();
266 if(cdmEntitySessionManager != null) {
267 Collection<ICdmEntitySession> sessions = cdmEntitySessionManager.getSessions();
268 txtNoOfSessions.setText(String.valueOf(sessions.size()));
269 for(ICdmEntitySession session : sessions) {
270 addSession(session);
271 }
272 ICdmEntitySession nullSession = cdmEntitySessionManager.getNullSession();
273 if(nullSession != null) {
274 addSession(nullSession);
275 }
276 for (int i=0; i<titles.length; i++) {
277 tableSessions.getColumn (i).pack ();
278 }
279 }
280
281 CdmRemoteCacheManager instance = CdmRemoteCacheManager.getInstance();
282 int cdmEnityCacheSize = CacheManager.create().getCacheNames().length;
283 txtNoOfCaches.setText(String.valueOf(cdmEnityCacheSize));
284
285 Cache defaultCache = CacheManager.create().getCache(CdmCacher.DEFAULT_CACHE_NAME);
286 txtDefaultInMemory.setText("");
287 txtDefaultOnDisk.setText("");
288 if(defaultCache != null) {
289 LiveCacheStatistics liveCacheStatistics = defaultCache.getLiveCacheStatistics();
290 txtDefaultInMemory.setText(getInMemoryStats(liveCacheStatistics));
291 txtDefaultOnDisk.setText(getOnDiskStats(liveCacheStatistics));
292 }
293
294 Cache cdmModelGetMethodsCache = instance.getCdmModelGetMethodsCache();
295 txtCdmModelInMemory.setText("");
296 txtCdmModelOnDisk.setText("");
297 if(cdmModelGetMethodsCache != null) {
298 LiveCacheStatistics liveCacheStatistics = cdmModelGetMethodsCache.getLiveCacheStatistics();
299 txtCdmModelInMemory.setText(getInMemoryStats(liveCacheStatistics));
300 txtCdmModelOnDisk.setText(getOnDiskStats(liveCacheStatistics));
301 }
302
303 }
304
305 private void addSession(ICdmEntitySession session) {
306
307 TableItem item = new TableItem(tableSessions, SWT.NULL);
308
309 String activePrefix = "";
310 if(session.isActive()) {
311 activePrefix = "*";
312 }
313 String sessionOwnerString = "";
314 if(session instanceof NullSession) {
315 sessionOwnerString = "Null Session";
316 } else {
317 if(session.getOwner() != null) {
318 sessionOwnerString = session.getOwner().toString();
319 }
320 }
321 item.setText(0, activePrefix + sessionOwnerString);
322 LiveCacheStatistics cacheStatistics = session.getCacheStatistics();
323 item.setText(1, getInMemoryStats(cacheStatistics));
324 item.setText(2, getOnDiskStats(cacheStatistics));
325 }
326
327 private String getInMemoryStats(LiveCacheStatistics statistics) {
328 if(statistics != null) {
329 long localHeapSize = statistics.getLocalHeapSize();
330
331 long localHeapSizeInBytes = statistics.getLocalHeapSizeInBytes();
332 String localHeapSizeInBytesStr =
333 (new DecimalFormat("#.##")).format(localHeapSizeInBytes/1048576.0);
334 return String.valueOf(localHeapSize) + " objects / " + localHeapSizeInBytesStr + " Mb";
335 }
336 return "";
337 }
338
339 private String getOnDiskStats(LiveCacheStatistics statistics) {
340 if(statistics != null) {
341 long localDiskSize = statistics.getLocalDiskSize();
342
343 long localDiskSizeInBytes = statistics.getLocalDiskSizeInBytes();
344 String localDiskSizeInBytesStr =
345 (new DecimalFormat("#.##")).format(localDiskSizeInBytes/1048576.0);
346 return String.valueOf(localDiskSize) + " objects / " + localDiskSizeInBytesStr + " Mb";
347 }
348 return "";
349 }
350
351
352 }