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.ui.workbench.osgi;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import javax.naming.ldap.LdapName;
26  
27  import org.argeo.cms.CmsException;
28  import org.argeo.cms.auth.CmsSession;
29  import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
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.jface.viewers.ColumnLabelProvider;
34  import org.eclipse.jface.viewers.IStructuredContentProvider;
35  import org.eclipse.jface.viewers.TableViewer;
36  import org.eclipse.jface.viewers.TableViewerColumn;
37  import org.eclipse.jface.viewers.Viewer;
38  import org.eclipse.swt.SWT;
39  import org.eclipse.swt.widgets.Composite;
40  import org.eclipse.ui.part.ViewPart;
41  import org.osgi.framework.BundleContext;
42  import org.osgi.framework.InvalidSyntaxException;
43  import org.osgi.framework.ServiceReference;
44  
45  /**
46   * Overview of the active CMS sessions.
47   */
48  public class CmsSessionsView extends ViewPart {
49  	private TableViewer viewer;
50  
51  	@Override
52  	public void createPartControl(Composite parent) {
53  		viewer = new TableViewer(parent);
54  		viewer.setContentProvider(new CmsSessionContentProvider());
55  		viewer.getTable().setHeaderVisible(true);
56  
57  		EclipseUiSpecificUtils.enableToolTipSupport(viewer);
58  
59  		int longColWidth = 150;
60  		int smallColWidth = 100;
61  
62  		// Display name
63  		TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
64  		column.getColumn().setWidth(longColWidth);
65  		column.getColumn().setText("User");
66  		column.setLabelProvider(new ColumnLabelProvider() {
67  			private static final long serialVersionUID = -5234573509093747505L;
68  
69  			public String getText(Object element) {
70  				return ((CmsSession) element).getAuthorization().toString();
71  			}
72  
73  			public String getToolTipText(Object element) {
74  				return ((CmsSession) element).getUserDn().toString();
75  			}
76  		});
77  		new ColumnViewerComparator(column);
78  
79  		// Creation time
80  		column = new TableViewerColumn(viewer, SWT.NONE);
81  		column.getColumn().setWidth(smallColWidth);
82  		column.getColumn().setText("Since");
83  		column.setLabelProvider(new ColumnLabelProvider() {
84  			private static final long serialVersionUID = -5234573509093747505L;
85  
86  			public String getText(Object element) {
87  				return LangUtils.since(((CmsSession) element).getCreationTime());
88  			}
89  
90  			public String getToolTipText(Object element) {
91  				return ((CmsSession) element).getCreationTime().toString();
92  			}
93  		});
94  		new ColumnViewerComparator(column);
95  
96  		// Username
97  		column = new TableViewerColumn(viewer, SWT.NONE);
98  		column.getColumn().setWidth(smallColWidth);
99  		column.getColumn().setText("Username");
100 		column.setLabelProvider(new ColumnLabelProvider() {
101 			private static final long serialVersionUID = -5234573509093747505L;
102 
103 			public String getText(Object element) {
104 				LdapName userDn = ((CmsSession) element).getUserDn();
105 				return userDn.getRdn(userDn.size() - 1).getValue().toString();
106 			}
107 
108 			public String getToolTipText(Object element) {
109 				return ((CmsSession) element).getUserDn().toString();
110 			}
111 		});
112 		new ColumnViewerComparator(column);
113 
114 		// UUID
115 		column = new TableViewerColumn(viewer, SWT.NONE);
116 		column.getColumn().setWidth(smallColWidth);
117 		column.getColumn().setText("UUID");
118 		column.setLabelProvider(new ColumnLabelProvider() {
119 			private static final long serialVersionUID = -5234573509093747505L;
120 
121 			public String getText(Object element) {
122 				return ((CmsSession) element).getUuid().toString();
123 			}
124 
125 			public String getToolTipText(Object element) {
126 				return getText(element);
127 			}
128 		});
129 		new ColumnViewerComparator(column);
130 
131 		// Local ID
132 		column = new TableViewerColumn(viewer, SWT.NONE);
133 		column.getColumn().setWidth(smallColWidth);
134 		column.getColumn().setText("Local ID");
135 		column.setLabelProvider(new ColumnLabelProvider() {
136 			private static final long serialVersionUID = -5234573509093747505L;
137 
138 			public String getText(Object element) {
139 				return ((CmsSession) element).getLocalId();
140 			}
141 
142 			public String getToolTipText(Object element) {
143 				return getText(element);
144 			}
145 		});
146 		new ColumnViewerComparator(column);
147 
148 		viewer.setInput(WorkbenchUiPlugin.getDefault().getBundle().getBundleContext());
149 
150 	}
151 
152 	@Override
153 	public void setFocus() {
154 		if (viewer != null)
155 			viewer.getControl().setFocus();
156 	}
157 
158 	/** Content provider managing the array of bundles */
159 	private static class CmsSessionContentProvider implements IStructuredContentProvider {
160 		private static final long serialVersionUID = -8533792785725875977L;
161 
162 		public Object[] getElements(Object inputElement) {
163 			if (inputElement instanceof BundleContext) {
164 				BundleContext bc = (BundleContext) inputElement;
165 				Collection<ServiceReference<CmsSession>> srs;
166 				try {
167 					srs = bc.getServiceReferences(CmsSession.class, null);
168 				} catch (InvalidSyntaxException e) {
169 					throw new CmsException("Cannot retrieve CMS sessions", e);
170 				}
171 				List<CmsSession> res = new ArrayList<>();
172 				for (ServiceReference<CmsSession> sr : srs) {
173 					res.add(bc.getService(sr));
174 				}
175 				return res.toArray();
176 			}
177 			return null;
178 		}
179 
180 		public void dispose() {
181 		}
182 
183 		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
184 		}
185 	}
186 }