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