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