KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > about > AboutBundleData


1 /*******************************************************************************
2  * Copyright (c) 2004, 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 package org.eclipse.ui.internal.about;
12
13 import java.io.IOException JavaDoc;
14
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.osgi.internal.provisional.verifier.CertificateVerifier;
17 import org.eclipse.osgi.internal.provisional.verifier.CertificateVerifierFactory;
18 import org.eclipse.ui.internal.WorkbenchMessages;
19 import org.eclipse.ui.internal.WorkbenchPlugin;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.Constants;
23 import org.osgi.framework.ServiceReference;
24
25
26
27 /**
28  * A small class to manage the about dialog information for a single bundle.
29  * @since 3.0
30  */

31 public class AboutBundleData extends AboutData {
32     
33     private Bundle bundle;
34     
35     private boolean isSignedDetermined = false;
36
37     private boolean isSigned;
38     
39     public AboutBundleData(Bundle bundle) {
40         super(getResourceString(bundle, Constants.BUNDLE_VENDOR),
41                 getResourceString(bundle, Constants.BUNDLE_NAME),
42                 getResourceString(bundle, Constants.BUNDLE_VERSION), bundle
43                         .getSymbolicName());
44         
45         this.bundle = bundle;
46         
47     }
48
49     public int getState() {
50         return bundle.getState();
51     }
52
53     /**
54      * Return a string representation of the arugment state. Does not return
55      * null.
56      */

57     public String JavaDoc getStateName() {
58         switch (getState()) {
59         case Bundle.INSTALLED:
60             return WorkbenchMessages.AboutPluginsDialog_state_installed;
61         case Bundle.RESOLVED:
62             return WorkbenchMessages.AboutPluginsDialog_state_resolved;
63         case Bundle.STARTING:
64             return WorkbenchMessages.AboutPluginsDialog_state_starting;
65         case Bundle.STOPPING:
66             return WorkbenchMessages.AboutPluginsDialog_state_stopping;
67         case Bundle.UNINSTALLED:
68             return WorkbenchMessages.AboutPluginsDialog_state_uninstalled;
69         case Bundle.ACTIVE:
70             return WorkbenchMessages.AboutPluginsDialog_state_active;
71         default:
72             return WorkbenchMessages.AboutPluginsDialog_state_unknown;
73         }
74     }
75
76     /**
77      * A function to translate the resource tags that may be embedded in a
78      * string associated with some bundle.
79      *
80      * @param headerName
81      * the used to retrieve the correct string
82      * @return the string or null if the string cannot be found
83      */

84     private static String JavaDoc getResourceString(Bundle bundle, String JavaDoc headerName) {
85         String JavaDoc value = (String JavaDoc) bundle.getHeaders().get(headerName);
86         return value == null ? null : Platform.getResourceString(bundle, value);
87     }
88     
89     public boolean isSignedDetermined() {
90         return isSignedDetermined;
91     }
92     
93     public boolean isSigned() throws IllegalStateException JavaDoc {
94
95         if (isSignedDetermined)
96             return isSigned;
97
98         BundleContext bundleContext = WorkbenchPlugin.getDefault()
99                 .getBundleContext();
100         ServiceReference certRef = bundleContext
101                 .getServiceReference(CertificateVerifierFactory.class.getName());
102         if (certRef == null)
103             throw new IllegalStateException JavaDoc();
104         CertificateVerifierFactory certFactory = (CertificateVerifierFactory) bundleContext
105                 .getService(certRef);
106         try {
107             CertificateVerifier verifier = certFactory.getVerifier(bundle);
108             isSigned = verifier.isSigned();
109             isSignedDetermined = true;
110             return isSigned;
111         } catch (IOException JavaDoc e) {
112             throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc()
113                     .initCause(e);
114         } finally {
115             bundleContext.ungetService(certRef);
116         }
117     }
118
119     /**
120      * @return
121      */

122     public Bundle getBundle() {
123         return bundle;
124     }
125     
126 // private boolean isBundleSigned(Bundle bundle)
127
// {
128
// isSignedDetermined = true;
129
//
130
// boolean bRet = false;
131
// if ( null != bundle.findEntries("META-INF", "*.SF", false) ) { //$NON-NLS-1$//$NON-NLS-2$
132
// bRet = true;
133
// }
134
// return bRet;
135
//
136
//
137
// /*
138
// * The below code features implementations that do more.
139
// */
140
//
141
//
142
// /* String loc = "";
143
//
144
// StringTokenizer st = new StringTokenizer(this.location,"@");
145
//
146
// try
147
// {
148
// st.nextToken();
149
// loc = st.nextToken();
150
// }
151
// catch( NoSuchElementException e )
152
// {
153
// return "Not a jar file";
154
// }
155
// if(! loc.endsWith(".jar"))
156
// {
157
// return "Not a jar file";
158
// }
159
//
160
// try
161
// {
162
// String fileLocation = Platform.getInstallLocation().getURL().getFile() + loc;
163
// JarFile jar = new JarFile( fileLocation );
164
//
165
// Enumeration e = jar.entries();
166
// /*ArrayList list = new ArrayList();
167
// byte[] buffer = new byte[8192];
168
// while( e.hasMoreElements())
169
// {
170
// JarEntry currentEntry = (JarEntry)e.nextElement();
171
// list.add(currentEntry);
172
// InputStream is = jar.getInputStream(currentEntry);
173
// while(( is.read(buffer, 0, buffer.length)) != -1 ){
174
// //can't get certificates until you read the whole dang thing.
175
// }
176
// if(is != null ) is.close();
177
// }
178
//
179
//
180
// int checked = 0;
181
// boolean signed =false;
182
// for( int index = 0; index < list.size(); index++ )
183
// {
184
// checked++;
185
// JarEntry je = (JarEntry)list.get(index);
186
// Certificate[] certs = je.getCertificates();
187
// if( certs != null )
188
// {
189
// //Something in this jar is signed by something!!
190
// signed = true;
191
// infoLine[5] = "Signed by "+certs[0].toString();
192
// break;
193
// }
194
//
195
// }
196
// if( !signed )
197
// {
198
// infoLine[5] = "Not signed";
199
// }
200
//
201
// boolean isSigned = false;
202
// while( e.hasMoreElements() )
203
// {
204
// JarEntry entry = (JarEntry)e.nextElement();
205
//
206
// if( entry.getName().matches("[mM][eE][tT][aA][-][iI][nN][fF]/.*[.][sS][fF]"))
207
// {
208
// isSigned = true;
209
// break;
210
// }
211
// if( !entry.getName().matches("[mM][eE][tT][aA][-][iI][nN][fF].*"))
212
// {
213
// break;
214
// }
215
// }
216
// return isSigned ? "Signed!" : "Not Signed.";
217
//
218
// } catch (IOException e) {
219
// return "Couldn't open jar file: " + e.toString();
220
// } */
221
// }
222
}
223
Popular Tags