3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.view
.sessions
;
12 import java
.text
.DecimalFormat
;
13 import java
.util
.Collection
;
15 import net
.sf
.ehcache
.Cache
;
16 import net
.sf
.ehcache
.CacheManager
;
17 import net
.sf
.ehcache
.statistics
.LiveCacheStatistics
;
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
;
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
;
50 public class SessionsViewPart
extends ViewPart
implements ICdmEntitySessionManagerObserver
{
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
;
56 private final String
[] titles
= { "Owner", "In Memory", "On Disk"};
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
;
66 public SessionsViewPart() {
67 this.cdmEntitySessionManager
= CdmStore
.getCurrentSessionManager();
68 if(cdmEntitySessionManager
!= null) {
69 cdmEntitySessionManager
.addSessionObserver(this);
74 * Create contents of the view part.
78 public void createPartControl(Composite parent
) {
79 Composite container
= new Composite(parent
, SWT
.NONE
);
80 container
.setLayout(new GridLayout(5, false));
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 : ");
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
);
99 new Label(container
, SWT
.NONE
);
100 new Label(container
, SWT
.NONE
);
101 new Label(container
, SWT
.NONE
);
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 : ");
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
);
118 new Label(container
, SWT
.NONE
);
119 new Label(container
, SWT
.NONE
);
120 new Label(container
, SWT
.NONE
);
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
);
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));
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
197 Button btnRefresh
= new Button(container
, SWT
.NONE
);
198 btnRefresh
.addSelectionListener(new SelectionAdapter() {
200 public void widgetSelected(SelectionEvent e
) {
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");
210 Button btnInspectActiveSession
= new Button(container
, SWT
.NONE
);
211 btnInspectActiveSession
.addSelectionListener(new SelectionAdapter() {
213 public void widgetSelected(SelectionEvent e
) {
214 AbstractUtility
.executeCommand(OPEN_INSPECT_SESSIONS_DIALOG_ID
, this, null);
217 btnInspectActiveSession
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false, 2, 1));
218 btnInspectActiveSession
.setText("Inspect Active Session");
220 new Label(container
, SWT
.NONE
);
221 new Label(container
, SWT
.NONE
);
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);
238 * Create the actions.
240 private void createActions() {
243 for (String title
: titles
) {
244 TableColumn column
= new TableColumn(tableSessions
, SWT
.NULL
);
245 column
.setWidth(200);
246 column
.setText(title
);
254 public void changed() {
255 if(tableSessions
.isDisposed()) {
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
) {
265 ICdmEntitySession nullSession
= cdmEntitySessionManager
.getNullSession();
266 if(nullSession
!= null) {
267 addSession(nullSession
);
269 for (int i
=0; i
<titles
.length
; i
++) {
270 tableSessions
.getColumn (i
).pack ();
274 CdmRemoteCacheManager instance
= CdmRemoteCacheManager
.getInstance();
275 int cdmEnityCacheSize
= CacheManager
.create().getCacheNames().length
;
276 txtNoOfCaches
.setText(String
.valueOf(cdmEnityCacheSize
));
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
));
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
));
298 private void addSession(ICdmEntitySession session
) {
300 TableItem item
= new TableItem(tableSessions
, SWT
.NULL
);
302 String activePrefix
= "";
303 if(session
.isActive()) {
306 String sessionOwnerString
= "";
307 if(session
instanceof NullSession
) {
308 sessionOwnerString
= "Null Session";
310 if(session
.getOwner() != null) {
311 sessionOwnerString
= session
.getOwner().toString();
314 item
.setText(0, activePrefix
+ sessionOwnerString
);
315 LiveCacheStatistics cacheStatistics
= session
.getCacheStatistics();
316 item
.setText(1, getInMemoryStats(cacheStatistics
));
317 item
.setText(2, getOnDiskStats(cacheStatistics
));
320 private String
getInMemoryStats(LiveCacheStatistics statistics
) {
321 if(statistics
!= null) {
322 long localHeapSize
= statistics
.getLocalHeapSize();
324 long localHeapSizeInBytes
= statistics
.getLocalHeapSizeInBytes();
325 String localHeapSizeInBytesStr
=
326 (new DecimalFormat("#.##")).format(localHeapSizeInBytes
/1048576.0);
327 return String
.valueOf(localHeapSize
) + " objects / " + localHeapSizeInBytesStr
+ " Mb";
332 private String
getOnDiskStats(LiveCacheStatistics statistics
) {
333 if(statistics
!= null) {
334 long localDiskSize
= statistics
.getLocalDiskSize();
336 long localDiskSizeInBytes
= statistics
.getLocalDiskSizeInBytes();
337 String localDiskSizeInBytesStr
=
338 (new DecimalFormat("#.##")).format(localDiskSizeInBytes
/1048576.0);
339 return String
.valueOf(localDiskSize
) + " objects / " + localDiskSizeInBytesStr
+ " Mb";
345 * Initialize the toolbar.
347 private void initializeToolBar() {
348 IToolBarManager toolbarManager
= getViewSite().getActionBars().getToolBarManager();
352 * Initialize the menu.
354 private void initializeMenu() {
355 IMenuManager menuManager
= getViewSite().getActionBars().getMenuManager();
359 public void setFocus() {