2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.view
.sessions
;
11 import java
.text
.DecimalFormat
;
12 import java
.util
.Collection
;
14 import net
.sf
.ehcache
.Cache
;
15 import net
.sf
.ehcache
.CacheManager
;
16 import net
.sf
.ehcache
.statistics
.LiveCacheStatistics
;
18 import org
.eclipse
.jface
.action
.IMenuManager
;
19 import org
.eclipse
.jface
.action
.IToolBarManager
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.SelectionAdapter
;
22 import org
.eclipse
.swt
.events
.SelectionEvent
;
23 import org
.eclipse
.swt
.layout
.GridData
;
24 import org
.eclipse
.swt
.layout
.GridLayout
;
25 import org
.eclipse
.swt
.widgets
.Button
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Label
;
28 import org
.eclipse
.swt
.widgets
.Table
;
29 import org
.eclipse
.swt
.widgets
.TableColumn
;
30 import org
.eclipse
.swt
.widgets
.TableItem
;
31 import org
.eclipse
.swt
.widgets
.Text
;
32 import org
.eclipse
.ui
.part
.ViewPart
;
33 import org
.eclipse
.wb
.swt
.SWTResourceManager
;
35 import eu
.etaxonomy
.cdm
.api
.cache
.CdmCacher
;
36 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
37 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
38 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
39 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
40 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManagerObserver
;
41 import eu
.etaxonomy
.taxeditor
.session
.NullSession
;
42 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
49 public class SessionsViewPart
extends ViewPart
implements ICdmEntitySessionManagerObserver
{
51 public static final String ID
= "eu.etaxonomy.taxeditor.view.sessions.SessionsViewPart"; //$NON-NLS-1$
52 public static final String OPEN_INSPECT_SESSIONS_DIALOG_ID
= "eu.etaxonomy.taxeditor.store.open.InspectSessionsDialog";
53 private Table tableSessions
;
55 private final String
[] titles
= { "Owner", "In Memory", "On Disk"};
57 private final ICdmEntitySessionManager cdmEntitySessionManager
;
58 private Text txtNoOfSessions
;
59 private Text txtNoOfCaches
;
60 private Text txtDefaultInMemory
;
61 private Text txtCdmModelInMemory
;
62 private Text txtCdmModelOnDisk
;
63 private Text txtDefaultOnDisk
;
65 public SessionsViewPart() {
66 this.cdmEntitySessionManager
= CdmStore
.getCurrentSessionManager();
67 if(cdmEntitySessionManager
!= null) {
68 cdmEntitySessionManager
.addSessionObserver(this);
73 * Create contents of the view part.
77 public void createPartControl(Composite parent
) {
78 Composite container
= new Composite(parent
, SWT
.NONE
);
79 container
.setLayout(new GridLayout(5, false));
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 : ");
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
);
98 new Label(container
, SWT
.NONE
);
99 new Label(container
, SWT
.NONE
);
100 new Label(container
, SWT
.NONE
);
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 : ");
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
);
117 new Label(container
, SWT
.NONE
);
118 new Label(container
, SWT
.NONE
);
119 new Label(container
, SWT
.NONE
);
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
);
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));
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
196 Button btnRefresh
= new Button(container
, SWT
.NONE
);
197 btnRefresh
.addSelectionListener(new SelectionAdapter() {
199 public void widgetSelected(SelectionEvent e
) {
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");
209 Button btnInspectActiveSession
= new Button(container
, SWT
.NONE
);
210 btnInspectActiveSession
.addSelectionListener(new SelectionAdapter() {
212 public void widgetSelected(SelectionEvent e
) {
213 AbstractUtility
.executeCommand(OPEN_INSPECT_SESSIONS_DIALOG_ID
, this, null);
216 btnInspectActiveSession
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false, 2, 1));
217 btnInspectActiveSession
.setText("Inspect Active Session");
219 new Label(container
, SWT
.NONE
);
220 new Label(container
, SWT
.NONE
);
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);
237 * Create the actions.
239 private void createActions() {
242 for (String title
: titles
) {
243 TableColumn column
= new TableColumn(tableSessions
, SWT
.NULL
);
244 column
.setWidth(200);
245 column
.setText(title
);
253 public void changed() {
254 if(tableSessions
.isDisposed()) {
257 tableSessions
.removeAll();
258 if(cdmEntitySessionManager
!= null) {
259 Collection
<ICdmEntitySession
> sessions
= cdmEntitySessionManager
.getSessions();
260 txtNoOfSessions
.setText(String
.valueOf(sessions
.size()));
261 for(ICdmEntitySession session
: sessions
) {
264 ICdmEntitySession nullSession
= cdmEntitySessionManager
.getNullSession();
265 if(nullSession
!= null) {
266 addSession(nullSession
);
268 for (int i
=0; i
<titles
.length
; i
++) {
269 tableSessions
.getColumn (i
).pack ();
273 CdmRemoteCacheManager instance
= CdmRemoteCacheManager
.getInstance();
274 int cdmEnityCacheSize
= CacheManager
.create().getCacheNames().length
;
275 txtNoOfCaches
.setText(String
.valueOf(cdmEnityCacheSize
));
277 Cache defaultCache
= CacheManager
.create().getCache(CdmCacher
.DEFAULT_CACHE_NAME
);
278 txtDefaultInMemory
.setText("");
279 txtDefaultOnDisk
.setText("");
280 if(defaultCache
!= null) {
281 LiveCacheStatistics liveCacheStatistics
= defaultCache
.getLiveCacheStatistics();
282 txtDefaultInMemory
.setText(getInMemoryStats(liveCacheStatistics
));
283 txtDefaultOnDisk
.setText(getOnDiskStats(liveCacheStatistics
));
286 Cache cdmModelGetMethodsCache
= instance
.getCdmModelGetMethodsCache();
287 txtCdmModelInMemory
.setText("");
288 txtCdmModelOnDisk
.setText("");
289 if(cdmModelGetMethodsCache
!= null) {
290 LiveCacheStatistics liveCacheStatistics
= cdmModelGetMethodsCache
.getLiveCacheStatistics();
291 txtCdmModelInMemory
.setText(getInMemoryStats(liveCacheStatistics
));
292 txtCdmModelOnDisk
.setText(getOnDiskStats(liveCacheStatistics
));
297 private void addSession(ICdmEntitySession session
) {
299 TableItem item
= new TableItem(tableSessions
, SWT
.NULL
);
301 String activePrefix
= "";
302 if(session
.isActive()) {
305 String sessionOwnerString
= "";
306 if(session
instanceof NullSession
) {
307 sessionOwnerString
= "Null Session";
309 if(session
.getOwner() != null) {
310 sessionOwnerString
= session
.getOwner().toString();
313 item
.setText(0, activePrefix
+ sessionOwnerString
);
314 LiveCacheStatistics cacheStatistics
= session
.getCacheStatistics();
315 item
.setText(1, getInMemoryStats(cacheStatistics
));
316 item
.setText(2, getOnDiskStats(cacheStatistics
));
319 private String
getInMemoryStats(LiveCacheStatistics statistics
) {
320 if(statistics
!= null) {
321 long localHeapSize
= statistics
.getLocalHeapSize();
323 long localHeapSizeInBytes
= statistics
.getLocalHeapSizeInBytes();
324 String localHeapSizeInBytesStr
=
325 (new DecimalFormat("#.##")).format(localHeapSizeInBytes
/1048576.0);
326 return String
.valueOf(localHeapSize
) + " objects / " + localHeapSizeInBytesStr
+ " Mb";
331 private String
getOnDiskStats(LiveCacheStatistics statistics
) {
332 if(statistics
!= null) {
333 long localDiskSize
= statistics
.getLocalDiskSize();
335 long localDiskSizeInBytes
= statistics
.getLocalDiskSizeInBytes();
336 String localDiskSizeInBytesStr
=
337 (new DecimalFormat("#.##")).format(localDiskSizeInBytes
/1048576.0);
338 return String
.valueOf(localDiskSize
) + " objects / " + localDiskSizeInBytesStr
+ " Mb";
344 * Initialize the toolbar.
346 private void initializeToolBar() {
347 IToolBarManager toolbarManager
= getViewSite().getActionBars().getToolBarManager();
351 * Initialize the menu.
353 private void initializeMenu() {
354 IMenuManager menuManager
= getViewSite().getActionBars().getMenuManager();
358 public void setFocus() {