KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > BundleSigningTray


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal.dialogs;
13
14 import java.io.IOException JavaDoc;
15 import java.text.DateFormat JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23 import java.util.Map.Entry;
24
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.OperationCanceledException;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.core.runtime.jobs.Job;
30 import org.eclipse.jface.dialogs.Dialog;
31 import org.eclipse.jface.dialogs.DialogTray;
32 import org.eclipse.jface.dialogs.TrayDialog;
33 import org.eclipse.jface.resource.JFaceResources;
34 import org.eclipse.osgi.internal.provisional.verifier.CertificateChain;
35 import org.eclipse.osgi.internal.provisional.verifier.CertificateVerifier;
36 import org.eclipse.osgi.internal.provisional.verifier.CertificateVerifierFactory;
37 import org.eclipse.osgi.util.NLS;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.custom.StyledText;
40 import org.eclipse.swt.graphics.Color;
41 import org.eclipse.swt.graphics.GC;
42 import org.eclipse.swt.graphics.Point;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Control;
47 import org.eclipse.swt.widgets.Display;
48 import org.eclipse.swt.widgets.Label;
49 import org.eclipse.swt.widgets.Shell;
50 import org.eclipse.swt.widgets.Text;
51 import org.eclipse.ui.internal.WorkbenchMessages;
52 import org.eclipse.ui.internal.WorkbenchPlugin;
53 import org.eclipse.ui.internal.about.AboutBundleData;
54 import org.eclipse.ui.statushandlers.StatusManager;
55 import org.osgi.framework.BundleContext;
56 import org.osgi.framework.ServiceReference;
57
58 /**
59  * @since 3.3
60  *
61  */

62 public class BundleSigningTray extends DialogTray {
63
64
65     private Text date;
66     private StyledText certificate;
67     private AboutBundleData data;
68     private TrayDialog dialog;
69     
70     /**
71      *
72      */

73     public BundleSigningTray(TrayDialog dialog) {
74         this.dialog = dialog;
75     }
76     
77     public void setData(AboutBundleData data) {
78         this.data = data;
79         startJobs();
80     }
81
82     /* (non-Javadoc)
83      * @see org.eclipse.jface.dialogs.DialogTray#createContents(org.eclipse.swt.widgets.Composite)
84      */

85     protected Control createContents(Composite parent) {
86         Composite content = new Composite(parent, SWT.NONE);
87         content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
88         GridLayout layout = new GridLayout(2, false);
89         content.setLayout(layout);
90         // date
91
Color backgroundColor = parent.getDisplay().getSystemColor(
92                 SWT.COLOR_WIDGET_BACKGROUND);
93         {
94             Label label = new Label(content, SWT.NONE);
95             label.setText(WorkbenchMessages.BundleSigningTray_Signing_Date);
96             GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
97             date = new Text(content, SWT.READ_ONLY);
98             GC gc = new GC(date);
99             gc.setFont(JFaceResources.getDialogFont());
100             Point size = gc.stringExtent(DateFormat.getDateTimeInstance().format(new Date JavaDoc()));
101             data.widthHint = size.x;
102             gc.dispose();
103             date.setText(WorkbenchMessages.BundleSigningTray_Working);
104             date.setLayoutData(data);
105             date.setBackground(backgroundColor);
106         }
107         // signer
108
{
109             Label label = new Label(content, SWT.NONE);
110             label.setText(WorkbenchMessages.BundleSigningTray_Signing_Certificate);
111             GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false);
112             data.horizontalSpan = 2;
113             data = new GridData(SWT.FILL, SWT.FILL, true, true);
114             data.horizontalSpan = 2;
115             certificate = new StyledText(content, SWT.READ_ONLY | SWT.MULTI | SWT.WRAP);
116             certificate.setText(WorkbenchMessages.BundleSigningTray_Working);
117             certificate.setLayoutData(data);
118         }
119         
120         // problems
121
// {
122
// Label label = new Label(content, SWT.NONE);
123
// label.setText("Problems:"); //$NON-NLS-1$
124
//
125
// }
126
Dialog.applyDialogFont(content);
127
128         startJobs(); // start the jobs that will prime the content
129

130         return content;
131     }
132     
133     /**
134      *
135      */

136     private void startJobs() {
137         if (!isOpen())
138             return;
139         certificate.setText(WorkbenchMessages.BundleSigningTray_Working);
140         date.setText(WorkbenchMessages.BundleSigningTray_Working);
141         final BundleContext bundleContext = WorkbenchPlugin.getDefault()
142                 .getBundleContext();
143         final ServiceReference certRef = bundleContext
144                 .getServiceReference(CertificateVerifierFactory.class.getName());
145         if (certRef == null) {
146             StatusManager.getManager().handle(
147                     new Status(IStatus.WARNING, WorkbenchPlugin.PI_WORKBENCH,
148                             WorkbenchMessages.BundleSigningTray_Cant_Find_Service),
149                     StatusManager.LOG);
150             return;
151         }
152
153         final CertificateVerifierFactory certFactory = (CertificateVerifierFactory) bundleContext
154                 .getService(certRef);
155         if (certFactory == null) {
156             StatusManager.getManager().handle(
157                     new Status(IStatus.WARNING, WorkbenchPlugin.PI_WORKBENCH,
158                             WorkbenchMessages.BundleSigningTray_Cant_Find_Service),
159                     StatusManager.LOG);
160             return;
161         }
162
163         final AboutBundleData myData = data;
164         final Job signerJob = new Job(NLS.bind(WorkbenchMessages.BundleSigningTray_Determine_Signer_For, myData.getId())) {
165
166             protected IStatus run(IProgressMonitor monitor) {
167                 try {
168                     if (myData != data)
169                         return Status.OK_STATUS;
170                     CertificateVerifier verifier = certFactory.getVerifier(myData
171                             .getBundle());
172                     if (myData != data)
173                         return Status.OK_STATUS;
174                     CertificateChain[] chains = verifier.getChains();
175                     final String JavaDoc signerText, dateText;
176                     final Shell dialogShell = dialog.getShell();
177                     if (!isOpen() && BundleSigningTray.this.data == myData)
178                         return Status.OK_STATUS;
179
180                     if (chains.length == 0) {
181                         signerText = WorkbenchMessages.BundleSigningTray_Unsigned;
182                         dateText = WorkbenchMessages.BundleSigningTray_Unsigned;
183                     } else {
184                         Properties JavaDoc [] certs = parseCerts(chains[0].getChain());
185                         if (certs.length == 0)
186                             signerText = WorkbenchMessages.BundleSigningTray_Unknown;
187                         else {
188                             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
189                             for (Iterator JavaDoc i = certs[0].entrySet().iterator(); i.hasNext(); ) {
190                                 Map.Entry JavaDoc entry = (Entry) i.next();
191                                 buffer.append(entry.getKey());
192                                 buffer.append('=');
193                                 buffer.append(entry.getValue());
194                                 if (i.hasNext())
195                                     buffer.append('\n');
196                             }
197                             signerText = buffer.toString();
198                         }
199
200                         Date JavaDoc signDate = chains[0].getSigningTime();
201                         if (signDate != null)
202                             dateText = DateFormat.getDateTimeInstance().format(
203                                     signDate);
204                         else
205                             dateText = WorkbenchMessages.BundleSigningTray_Unknown;
206                     }
207                     
208                     Display display = dialogShell.getDisplay();
209                     display.asyncExec(new Runnable JavaDoc() {
210
211                         public void run() {
212                             // check to see if the tray is still visible and if we're still looking at the same item
213
if (!isOpen() && BundleSigningTray.this.data != myData)
214                                 return;
215                             certificate.setText(signerText);
216                             date.setText(dateText);
217                         }
218                     });
219
220                 } catch (IOException JavaDoc e) {
221                     return new Status(IStatus.ERROR,
222                             WorkbenchPlugin.PI_WORKBENCH, e.getMessage(), e);
223                 }
224                 return Status.OK_STATUS;
225             }
226         };
227         signerJob.setSystem(true);
228         signerJob.belongsTo(signerJob);
229         signerJob.schedule();
230
231         Job cleanup = new Job(WorkbenchMessages.BundleSigningTray_Unget_Signing_Service) {
232
233             protected IStatus run(IProgressMonitor monitor) {
234                 try {
235                     getJobManager().join(signerJob, monitor);
236                 } catch (OperationCanceledException e) {
237                 } catch (InterruptedException JavaDoc e) {
238                 }
239                 bundleContext.ungetService(certRef);
240                 return Status.OK_STATUS;
241             }
242         };
243         cleanup.setSystem(true);
244         cleanup.schedule();
245
246     }
247
248     /**
249      *
250      */

251     private boolean isOpen() {
252         return certificate != null && !certificate.isDisposed();
253     }
254
255     private Properties JavaDoc[] parseCerts(String JavaDoc certString) {
256         List JavaDoc certs = new ArrayList JavaDoc();
257         StringTokenizer JavaDoc toker = new StringTokenizer JavaDoc(certString, ";"); //$NON-NLS-1$
258

259         while (toker.hasMoreTokens()) {
260             Map JavaDoc cert = parseCert(toker.nextToken());
261             if (cert != null)
262                 certs.add(cert);
263         }
264         return (Properties JavaDoc []) certs.toArray(new Properties JavaDoc[certs.size()]);
265
266
267     }
268
269     /**
270      * @param certString
271      * @return
272      */

273     private Properties JavaDoc parseCert(String JavaDoc certString) {
274         StringTokenizer JavaDoc toker = new StringTokenizer JavaDoc(certString, ","); //$NON-NLS-1$
275
Properties JavaDoc cert = new Properties JavaDoc();
276         while (toker.hasMoreTokens()) {
277             String JavaDoc pair = toker.nextToken();
278             int idx = pair.indexOf('=');
279             if (idx > 0 && idx < pair.length() - 2) {
280                 String JavaDoc key = pair.substring(0, idx).trim();
281                 String JavaDoc value = pair.substring(idx + 1).trim();
282                 if (value.length() > 2) {
283                     if (value.charAt(0) == '\"')
284                         value = value.substring(1);
285
286                     if (value.charAt(value.length() - 1) == '\"')
287                         value = value.substring(0, value.length() - 1);
288                 }
289                 cert.setProperty(key, value);
290             }
291         }
292         return cert;
293     }
294
295 }
296
Popular Tags