001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.about;
011:
012: import java.io.IOException;
013:
014: import org.eclipse.core.runtime.Platform;
015: import org.eclipse.osgi.internal.provisional.verifier.CertificateVerifier;
016: import org.eclipse.osgi.internal.provisional.verifier.CertificateVerifierFactory;
017: import org.eclipse.ui.internal.WorkbenchMessages;
018: import org.eclipse.ui.internal.WorkbenchPlugin;
019: import org.osgi.framework.Bundle;
020: import org.osgi.framework.BundleContext;
021: import org.osgi.framework.Constants;
022: import org.osgi.framework.ServiceReference;
023:
024: /**
025: * A small class to manage the about dialog information for a single bundle.
026: * @since 3.0
027: */
028: public class AboutBundleData extends AboutData {
029:
030: private Bundle bundle;
031:
032: private boolean isSignedDetermined = false;
033:
034: private boolean isSigned;
035:
036: public AboutBundleData(Bundle bundle) {
037: super (getResourceString(bundle, Constants.BUNDLE_VENDOR),
038: getResourceString(bundle, Constants.BUNDLE_NAME),
039: getResourceString(bundle, Constants.BUNDLE_VERSION),
040: bundle.getSymbolicName());
041:
042: this .bundle = bundle;
043:
044: }
045:
046: public int getState() {
047: return bundle.getState();
048: }
049:
050: /**
051: * Return a string representation of the arugment state. Does not return
052: * null.
053: */
054: public String getStateName() {
055: switch (getState()) {
056: case Bundle.INSTALLED:
057: return WorkbenchMessages.AboutPluginsDialog_state_installed;
058: case Bundle.RESOLVED:
059: return WorkbenchMessages.AboutPluginsDialog_state_resolved;
060: case Bundle.STARTING:
061: return WorkbenchMessages.AboutPluginsDialog_state_starting;
062: case Bundle.STOPPING:
063: return WorkbenchMessages.AboutPluginsDialog_state_stopping;
064: case Bundle.UNINSTALLED:
065: return WorkbenchMessages.AboutPluginsDialog_state_uninstalled;
066: case Bundle.ACTIVE:
067: return WorkbenchMessages.AboutPluginsDialog_state_active;
068: default:
069: return WorkbenchMessages.AboutPluginsDialog_state_unknown;
070: }
071: }
072:
073: /**
074: * A function to translate the resource tags that may be embedded in a
075: * string associated with some bundle.
076: *
077: * @param headerName
078: * the used to retrieve the correct string
079: * @return the string or null if the string cannot be found
080: */
081: private static String getResourceString(Bundle bundle,
082: String headerName) {
083: String value = (String) bundle.getHeaders().get(headerName);
084: return value == null ? null : Platform.getResourceString(
085: bundle, value);
086: }
087:
088: public boolean isSignedDetermined() {
089: return isSignedDetermined;
090: }
091:
092: public boolean isSigned() throws IllegalStateException {
093:
094: if (isSignedDetermined)
095: return isSigned;
096:
097: BundleContext bundleContext = WorkbenchPlugin.getDefault()
098: .getBundleContext();
099: ServiceReference certRef = bundleContext
100: .getServiceReference(CertificateVerifierFactory.class
101: .getName());
102: if (certRef == null)
103: throw new IllegalStateException();
104: CertificateVerifierFactory certFactory = (CertificateVerifierFactory) bundleContext
105: .getService(certRef);
106: try {
107: CertificateVerifier verifier = certFactory
108: .getVerifier(bundle);
109: isSigned = verifier.isSigned();
110: isSignedDetermined = true;
111: return isSigned;
112: } catch (IOException e) {
113: throw (IllegalStateException) new IllegalStateException()
114: .initCause(e);
115: } finally {
116: bundleContext.ungetService(certRef);
117: }
118: }
119:
120: /**
121: * @return
122: */
123: public Bundle getBundle() {
124: return bundle;
125: }
126:
127: // private boolean isBundleSigned(Bundle bundle)
128: // {
129: // isSignedDetermined = true;
130: //
131: // boolean bRet = false;
132: // if ( null != bundle.findEntries("META-INF", "*.SF", false) ) { //$NON-NLS-1$//$NON-NLS-2$
133: // bRet = true;
134: // }
135: // return bRet;
136: //
137: //
138: // /*
139: // * The below code features implementations that do more.
140: // */
141: //
142: //
143: // /* String loc = "";
144: //
145: // StringTokenizer st = new StringTokenizer(this.location,"@");
146: //
147: // try
148: // {
149: // st.nextToken();
150: // loc = st.nextToken();
151: // }
152: // catch( NoSuchElementException e )
153: // {
154: // return "Not a jar file";
155: // }
156: // if(! loc.endsWith(".jar"))
157: // {
158: // return "Not a jar file";
159: // }
160: //
161: // try
162: // {
163: // String fileLocation = Platform.getInstallLocation().getURL().getFile() + loc;
164: // JarFile jar = new JarFile( fileLocation );
165: //
166: // Enumeration e = jar.entries();
167: // /*ArrayList list = new ArrayList();
168: // byte[] buffer = new byte[8192];
169: // while( e.hasMoreElements())
170: // {
171: // JarEntry currentEntry = (JarEntry)e.nextElement();
172: // list.add(currentEntry);
173: // InputStream is = jar.getInputStream(currentEntry);
174: // while(( is.read(buffer, 0, buffer.length)) != -1 ){
175: // //can't get certificates until you read the whole dang thing.
176: // }
177: // if(is != null ) is.close();
178: // }
179: //
180: //
181: // int checked = 0;
182: // boolean signed =false;
183: // for( int index = 0; index < list.size(); index++ )
184: // {
185: // checked++;
186: // JarEntry je = (JarEntry)list.get(index);
187: // Certificate[] certs = je.getCertificates();
188: // if( certs != null )
189: // {
190: // //Something in this jar is signed by something!!
191: // signed = true;
192: // infoLine[5] = "Signed by "+certs[0].toString();
193: // break;
194: // }
195: //
196: // }
197: // if( !signed )
198: // {
199: // infoLine[5] = "Not signed";
200: // }
201: //
202: // boolean isSigned = false;
203: // while( e.hasMoreElements() )
204: // {
205: // JarEntry entry = (JarEntry)e.nextElement();
206: //
207: // if( entry.getName().matches("[mM][eE][tT][aA][-][iI][nN][fF]/.*[.][sS][fF]"))
208: // {
209: // isSigned = true;
210: // break;
211: // }
212: // if( !entry.getName().matches("[mM][eE][tT][aA][-][iI][nN][fF].*"))
213: // {
214: // break;
215: // }
216: // }
217: // return isSigned ? "Signed!" : "Not Signed.";
218: //
219: // } catch (IOException e) {
220: // return "Couldn't open jar file: " + e.toString();
221: // } */
222: // }
223: }
|