View Javadoc
1   //package org.argeo.eclipse.ui.workbench.osgi;
2   //public class BundlesView {}
3   
4   /*
5    * Copyright (C) 2007-2012 Argeo GmbH
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *         http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.argeo.cms.e4.monitoring;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import javax.annotation.PostConstruct;
26  import javax.naming.ldap.LdapName;
27  
28  import org.argeo.cms.CmsException;
29  import org.argeo.cms.auth.CmsSession;
30  import org.argeo.eclipse.ui.ColumnViewerComparator;
31  import org.argeo.eclipse.ui.specific.EclipseUiSpecificUtils;
32  import org.argeo.util.LangUtils;
33  import org.eclipse.e4.ui.di.Focus;
34  import org.eclipse.jface.viewers.ColumnLabelProvider;
35  import org.eclipse.jface.viewers.IStructuredContentProvider;
36  import org.eclipse.jface.viewers.TableViewer;
37  import org.eclipse.jface.viewers.TableViewerColumn;
38  import org.eclipse.jface.viewers.Viewer;
39  import org.eclipse.swt.SWT;
40  import org.eclipse.swt.widgets.Composite;
41  import org.osgi.framework.BundleContext;
42  import org.osgi.framework.FrameworkUtil;
43  import org.osgi.framework.InvalidSyntaxException;
44  import org.osgi.framework.ServiceReference;
45  
46  /**
47   * Overview of the active CMS sessions.
48   */
49  public class CmsSessionsView {
50  	private final static BundleContext bc = FrameworkUtil.getBundle(CmsSessionsView.class).getBundleContext();
51  
52  	private TableViewer viewer;
53  
54  	@PostConstruct
55  	public void createPartControl(Composite parent) {
56  		viewer = new TableViewer(parent);
57  		viewer.setContentProvider(new CmsSessionContentProvider());
58  		viewer.getTable().setHeaderVisible(true);
59  
60  		EclipseUiSpecificUtils.enableToolTipSupport(viewer);
61  
62  		int longColWidth = 150;
63  		int smallColWidth = 100;
64  
65  		// Display name
66  		TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
67  		column.getColumn().setWidth(longColWidth);
68  		column.getColumn().setText("User");
69  		column.setLabelProvider(new ColumnLabelProvider() {
70  			private static final long serialVersionUID = -5234573509093747505L;
71  
72  			public String getText(Object element) {
73  				return ((CmsSession) element).getAuthorization().toString();
74  			}
75  
76  			public String getToolTipText(Object element) {
77  				return ((CmsSession) element).getUserDn().toString();
78  			}
79  		});
80  		new ColumnViewerComparator(column);
81  
82  		// Creation time
83  		column = new TableViewerColumn(viewer, SWT.NONE);
84  		column.getColumn().setWidth(smallColWidth);
85  		column.getColumn().setText("Since");
86  		column.setLabelProvider(new ColumnLabelProvider() {
87  			private static final long serialVersionUID = -5234573509093747505L;
88  
89  			public String getText(Object element) {
90  				return LangUtils.since(((CmsSession) element).getCreationTime());
91  			}
92  
93  			public String getToolTipText(Object element) {
94  				return ((CmsSession) element).getCreationTime().toString();
95  			}
96  		});
97  		new ColumnViewerComparator(column);
98  
99  		// Username
100 		column = new TableViewerColumn(viewer, SWT.NONE);
101 		column.getColumn().setWidth(smallColWidth);
102 		column.getColumn().setText("Username");
103 		column.setLabelProvider(new ColumnLabelProvider() {
104 			private static final long serialVersionUID = -5234573509093747505L;
105 
106 			public String getText(Object element) {
107 				LdapName userDn = ((CmsSession) element).getUserDn();
108 				return userDn.getRdn(userDn.size() - 1).getValue().toString();
109 			}
110 
111 			public String getToolTipText(Object element) {
112 				return ((CmsSession) element).getUserDn().toString();
113 			}
114 		});
115 		new ColumnViewerComparator(column);
116 
117 		// UUID
118 		column = new TableViewerColumn(viewer, SWT.NONE);
119 		column.getColumn().setWidth(smallColWidth);
120 		column.getColumn().setText("UUID");
121 		column.setLabelProvider(new ColumnLabelProvider() {
122 			private static final long serialVersionUID = -5234573509093747505L;
123 
124 			public String getText(Object element) {
125 				return ((CmsSession) element).getUuid().toString();
126 			}
127 
128 			public String getToolTipText(Object element) {
129 				return getText(element);
130 			}
131 		});
132 		new ColumnViewerComparator(column);
133 
134 		// Local ID
135 		column = new TableViewerColumn(viewer, SWT.NONE);
136 		column.getColumn().setWidth(smallColWidth);
137 		column.getColumn().setText("Local ID");
138 		column.setLabelProvider(new ColumnLabelProvider() {
139 			private static final long serialVersionUID = -5234573509093747505L;
140 
141 			public String getText(Object element) {
142 				return ((CmsSession) element).getLocalId();
143 			}
144 
145 			public String getToolTipText(Object element) {
146 				return getText(element);
147 			}
148 		});
149 		new ColumnViewerComparator(column);
150 
151 		viewer.setInput(bc);
152 
153 	}
154 
155 	@Focus
156 	public void setFocus() {
157 		if (viewer != null)
158 			viewer.getControl().setFocus();
159 	}
160 
161 	/** Content provider managing the array of bundles */
162 	private static class CmsSessionContentProvider implements IStructuredContentProvider {
163 		private static final long serialVersionUID = -8533792785725875977L;
164 
165 		public Object[] getElements(Object inputElement) {
166 			if (inputElement instanceof BundleContext) {
167 				BundleContext bc = (BundleContext) inputElement;
168 				Collection<ServiceReference<CmsSession>> srs;
169 				try {
170 					srs = bc.getServiceReferences(CmsSession.class, null);
171 				} catch (InvalidSyntaxException e) {
172 					throw new CmsException("Cannot retrieve CMS sessions", e);
173 				}
174 				List<CmsSession> res = new ArrayList<>();
175 				for (ServiceReference<CmsSession> sr : srs) {
176 					res.add(bc.getService(sr));
177 				}
178 				return res.toArray();
179 			}
180 			return null;
181 		}
182 
183 		public void dispose() {
184 		}
185 
186 		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
187 		}
188 	}
189 }