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