KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > security > JarVerificationService


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.update.internal.ui.security;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.jface.dialogs.*;
17 import org.eclipse.swt.widgets.*;
18 import org.eclipse.update.core.*;
19 import org.eclipse.update.internal.core.*;
20 import org.eclipse.update.internal.ui.*;
21 /**
22  *
23  */

24 public class JarVerificationService implements IVerificationListener {
25
26     /*
27      * The JarVerifier is a instance variable
28      * bacause we want to reuse it upon multiple calls
29      */

30
31     private Shell shell;
32     // keep track of the last verify code.
33
private int lastVerifyCode = -1;
34     
35     /**
36      * Processed ContentRefernces. They will be skipped if prompted
37      * to verify the same reference again.
38      */

39     private Map JavaDoc processed=new HashMap JavaDoc();
40
41     /*
42      * If no shell, create a new shell
43      */

44     public JarVerificationService() {
45         this(null);
46     }
47     
48     /*
49      *
50      */

51     public JarVerificationService(Shell aShell) {
52         shell = aShell;
53
54         // find the default display and get the active shell
55
if (shell == null) {
56             final Display disp = Display.getDefault();
57             if (disp == null) {
58                 shell = new Shell(new Display());
59             } else {
60                 disp.syncExec(new Runnable JavaDoc() {
61                     public void run() {
62                         shell = disp.getActiveShell();
63                     }
64                 });
65             }
66         }
67     }
68
69     /*
70      *
71      */

72     private int openWizard(IVerificationResult result) {
73         int code;
74         IDialogPage page = new JarVerificationPage(result);
75         JarVerificationDialog dialog =
76                 new JarVerificationDialog(shell,page,result);
77         dialog.create();
78         dialog.getShell().setSize(600, 500);
79         dialog.getShell().setText(UpdateUIMessages.JarVerificationDialog_wtitle);
80         dialog.open();
81         if (dialog.getReturnCode() == JarVerificationDialog.OK) {
82             code = CHOICE_INSTALL_TRUST_ONCE;
83         } else if (dialog.getReturnCode() == JarVerificationDialog.INSTALL_ALL) {
84             code = CHOICE_INSTALL_TRUST_ALWAYS;
85         } else {
86             code = CHOICE_ABORT;
87         }
88         return code;
89
90     }
91
92     /*
93      *
94      */

95     public int prompt(final IVerificationResult verificationResult){
96         if (!UpdateCore.getPlugin().getPluginPreferences().getBoolean(UpdateCore.P_CHECK_SIGNATURE))
97             return CHOICE_INSTALL_TRUST_ALWAYS;
98
99         if (verificationResult.alreadySeen()) return CHOICE_INSTALL_TRUST_ALWAYS;
100         
101         if(see(verificationResult)) return CHOICE_INSTALL_TRUST_ALWAYS;
102         
103         if (lastVerifyCode == CHOICE_INSTALL_TRUST_ALWAYS) return CHOICE_INSTALL_TRUST_ALWAYS;
104
105         switch (verificationResult.getVerificationCode()) {
106             case IVerificationResult.UNKNOWN_ERROR :
107                     return CHOICE_ERROR;
108
109             case IVerificationResult.VERIFICATION_CANCELLED:
110                     return CHOICE_ABORT;
111
112             // cannot verify it: do not prompt user.
113
case IVerificationResult.TYPE_ENTRY_UNRECOGNIZED:
114                 return CHOICE_INSTALL_TRUST_ALWAYS;
115             
116             default :
117                 {
118                     shell.getDisplay().syncExec(new Runnable JavaDoc() {
119                         public void run() {
120                             lastVerifyCode = openWizard(verificationResult);
121                         }
122                     });
123                     return lastVerifyCode;
124                 }
125         }
126     }
127
128     /**
129      * Checks whether feature archive has been seen already.
130      * Remembers the fact that archive is being seen now.
131      * @param verificationResult
132      * @return true if the archive has been seen before, false if first time
133      */

134     private boolean see(final IVerificationResult verificationResult) {
135         String JavaDoc key = verificationResult.getFeature().getVersionedIdentifier().toString()
136             +"/"+verificationResult.getContentReference().getIdentifier(); //$NON-NLS-1$
137
Long JavaDoc value = new Long JavaDoc(verificationResult.getContentReference().getLastModified());
138         Long JavaDoc cachedValue = (Long JavaDoc)processed.get(key);
139         if(value.equals(cachedValue)){
140             return true;
141         }else{
142             processed.put(key, value);
143             return false;
144         }
145     }
146 }
147
Popular Tags