From: <bd...@us...> - 2008-01-25 16:07:42
|
Revision: 2229 http://unicore.svn.sourceforge.net/unicore/?rev=2229&view=rev Author: bdemuth Date: 2008-01-25 08:07:39 -0800 (Fri, 25 Jan 2008) Log Message: ----------- Fixed init bug in IdentityActivator Modified Paths: -------------- eclipseclient/Plugins/de.fzj.unicore.rcp.identity/trunk/src/de/fzj/unicore/rcp/identity/IdentityActivator.java Modified: eclipseclient/Plugins/de.fzj.unicore.rcp.identity/trunk/src/de/fzj/unicore/rcp/identity/IdentityActivator.java =================================================================== --- eclipseclient/Plugins/de.fzj.unicore.rcp.identity/trunk/src/de/fzj/unicore/rcp/identity/IdentityActivator.java 2008-01-25 14:25:57 UTC (rev 2228) +++ eclipseclient/Plugins/de.fzj.unicore.rcp.identity/trunk/src/de/fzj/unicore/rcp/identity/IdentityActivator.java 2008-01-25 16:07:39 UTC (rev 2229) @@ -55,6 +55,7 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.MessageBox; +import org.eclipse.ui.PlatformUI; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; @@ -88,15 +89,15 @@ // The plug-in ID public static final String PLUGIN_ID = "de.fzj.unicore.rcp.identity"; - + public final static String ICONS_PATH = "$nl$/icons/";//$NON-NLS-1$ - + // name of the property representing the user certificates public final static String PROPERTY_KEYS = "property keys"; - + // The shared instance private static IdentityActivator plugin; - + /** * The keystore view singleton. */ @@ -109,20 +110,20 @@ private String passwd; private Collection<IPropertyChangeListener> listeners; private boolean initialized = false; - + /** * The truststore view singleton. Used for accessing CA certificates. */ private TruststoreView truststoreView; - - + + /** * The constructor */ public IdentityActivator() { plugin = this; listeners = Collections.synchronizedCollection(new ArrayList<IPropertyChangeListener>()); - + } /* @@ -133,7 +134,7 @@ super.start(context); IPreferenceStore p = UnicoreCommonActivator.getDefault().getPreferenceStore(); p.addPropertyChangeListener(this); - + } public void init() @@ -141,19 +142,19 @@ initialized = true; createKeystoreManager(); if(keystoreManager == null) return; - - + + certificateList = new CertificateList(keystoreManager); addPropertyChangeListener(certificateList); - + trustedCAList = new TrustedCAList(keystoreManager); addPropertyChangeListener(trustedCAList); - + siteList = SiteList.reloadSiteList(); addPropertyChangeListener(siteList); - + } - + /* * (non-Javadoc) * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) @@ -174,7 +175,7 @@ } - + /** * Returns an image descriptor for the image file at the given * plug-in relative path @@ -188,15 +189,15 @@ if(id == null) id = de.fzj.unicore.rcp.common.UnicoreCommonActivator.imageDescriptorFromPlugin(de.fzj.unicore.rcp.common.UnicoreCommonActivator.PLUGIN_ID, path); return id; } - - + + /** * Log a message via Eclipse logging * Defaults to INFO Level * @param msg the message to log */ public static void log(String msg) { - log(msg, null); + log(msg, null); } /** * Log a message via Eclipse logging @@ -205,7 +206,7 @@ * @param e stacktrace to append */ public static void log(String msg, Exception e) { - log(Status.INFO, msg, e, getAlarmUser(Status.INFO)); + log(Status.INFO, msg, e, getAlarmUser(Status.INFO)); } /** * Log a message via Eclipse logging @@ -213,9 +214,9 @@ * @param msg the message to log */ public static void log(int status, String msg) { - log(status, msg, null, getAlarmUser(status)); + log(status, msg, null, getAlarmUser(status)); } - + /** * Log a message via Eclipse logging * @param status the IStatus level to log to @@ -223,7 +224,7 @@ * @param e stacktrace to append */ public static void log(int status, String msg, Exception e) { - log(status, msg, e, getAlarmUser(status)); + log(status, msg, e, getAlarmUser(status)); } /** @@ -233,7 +234,7 @@ * @param alarmUser true means popup should alert user */ public static void log(int status, String msg, boolean alarmUser) { - log(status, msg, null, alarmUser); + log(status, msg, null, alarmUser); } /** * Log a message via Eclipse logging @@ -243,28 +244,28 @@ * @param alarmUser true means popup should alert user */ public static void log(int status, String msg, Exception e, boolean alarmUser) { - IStatus s = new Status(status, PLUGIN_ID, Status.OK, msg, e); - //log it - plugin.getLog().log(s); - // alarm user if needed - if (alarmUser) - LogActivator.showAlarm(s); + IStatus s = new Status(status, PLUGIN_ID, Status.OK, msg, e); + //log it + plugin.getLog().log(s); + // alarm user if needed + if (alarmUser) + LogActivator.showAlarm(s); } - + public IUASSecurityProperties getUASSecProps(URL url) { - - try { - return getUASSecProps(url.toURI()); - } catch (URISyntaxException e) { - log("Could not transform url to uri for retrieving security properties.",e); - return null; - } - + + try { + return getUASSecProps(url.toURI()); + } catch (URISyntaxException e) { + log("Could not transform url to uri for retrieving security properties.",e); + return null; + } + } public IUASSecurityProperties getUASSecProps(URI uri) { - + UASSecurityProperties secProps = new UASSecurityProperties(); secProps.setProperty(IUASSecurityProperties.WSRF_SSL, "true"); secProps.setProperty(IUASSecurityProperties.WSRF_SSL_CLIENTAUTH, "true"); @@ -296,7 +297,7 @@ } Certificate cert = null; Site match = getSiteList().matchSite(uri); - + if(match != null) { String certName = match.getCertificate().getName(); @@ -307,7 +308,7 @@ cert = certificateList.getDefaultCert(); // log(IStatus.OK,"No certificate specified for this site. Using default certificate: "+cert.getName()); } - + if(cert != null){ secProps.setProperty(IUASSecurityProperties.WSRF_SSL_KEYSTORE, keystore.getAbsolutePath()); secProps.setProperty(IUASSecurityProperties.WSRF_SSL_KEYPASS, passwd); @@ -317,12 +318,12 @@ } else { log(IStatus.ERROR,"Could not load any certificate. Please set a default certificate at least."); } - + return secProps; } - - - + + + private void createKeystoreManager() { IPreferenceStore p = UnicoreCommonActivator.getDefault().getPreferenceStore(); keystore = new File(p.getString(Constants.P_KEYSTORE_PATH)); @@ -342,10 +343,10 @@ propertyChange(event); } log("Selected keystore:"+keystore.getAbsolutePath()); - + } - - + + /** * If password is unknown, ask for a password for an existing or a new keystore. */ @@ -358,6 +359,7 @@ if (passwd == null) errmsg = "No password entered."; } catch(Exception e) { + e.printStackTrace(); errmsg = e.getMessage(); } if (errmsg != null) { @@ -369,7 +371,7 @@ } return passwd; } - + private String askPasswd(File keystore) throws Exception { String title = "Password"; String question = "Please enter the password for your keystore:\n"+keystore.getAbsolutePath(); @@ -377,7 +379,7 @@ int i = 3; while(passwd == null && i > 0) { - HiddenInputDialog id = new HiddenInputDialog(plugin.getWorkbench().getActiveWorkbenchWindow().getShell(),title,question,"321", null); + HiddenInputDialog id = new HiddenInputDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell(),title,question,"321", null); id.open(); if (id.getReturnCode() != InputDialog.OK) return null; passwd = id.getValue(); @@ -389,7 +391,7 @@ } else if(!keystore.exists()) { - id = new HiddenInputDialog(plugin.getWorkbench().getActiveWorkbenchWindow().getShell(),title,"Please confirm the password:","", null); + id = new HiddenInputDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell(),title,"Please confirm the password:","", null); id.open(); if (id.getReturnCode() != InputDialog.OK || !passwd.equals(id.getValue())) { @@ -399,13 +401,13 @@ } } } - + if(passwd == null) throw new Exception("Wrong password entered three times"); - + return passwd; } - + /** * Test if password is correct by opening keystore file * @param passwd @@ -440,13 +442,13 @@ } return keystore; } - - + + private void saveKeystore(File keystore) { UnicoreCommonActivator.getDefault().getPreferenceStore().setValue(Constants.P_KEYSTORE_PATH, keystore.getAbsolutePath()); } - + public Preferences getPreferences() { return plugin.getPluginPreferences(); } @@ -474,7 +476,7 @@ public void setCertificateList(CertificateList certificateList) { this.certificateList = certificateList; } - + public SiteList getSiteList() { return siteList; } @@ -486,54 +488,62 @@ public void setTrustedCAList(TrustedCAList trustedCAList) { this.trustedCAList = trustedCAList; } - + /* - * (non-Javadoc) Method declared on IPreferenceStore. - */ - public void addPropertyChangeListener(IPropertyChangeListener listener) { - synchronized(listeners) + * (non-Javadoc) Method declared on IPreferenceStore. + */ + public void addPropertyChangeListener(IPropertyChangeListener listener) { + synchronized(listeners) { - listeners.add(listener); + listeners.add(listener); } - } - - /* - * (non-Javadoc) Method declared on IPreferenceStore. - */ - public void removePropertyChangeListener(IPropertyChangeListener listener) { - synchronized(listeners) + } + + /* + * (non-Javadoc) Method declared on IPreferenceStore. + */ + public void removePropertyChangeListener(IPropertyChangeListener listener) { + synchronized(listeners) { - listeners.remove(listener); + listeners.remove(listener); } - } - - - + } + + + /* (non-Javadoc) * @see org.eclipse.core.runtime.Preferences.IPropertyChangeListener#propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent) */ - public void propertyChange(PropertyChangeEvent event) { + public void propertyChange(final PropertyChangeEvent event) { if(Constants.P_KEYSTORE_PATH.equals(event.getProperty())) { - try { - keystore = new File((String) event.getNewValue()); - keystoreManager = null; - passwd = null; - if(keystore == null) return; - passwd = getPasswd(keystore); - try { - keystoreManager = KeyStoreManager.getInstance(keystore, passwd); - } catch (Exception e1) { - log("Could not open keystore:",e1); - } - } catch (Exception e) { - e.printStackTrace(); - } + PlatformUI.getWorkbench().getDisplay().syncExec( + new Runnable() { + + public void run() { + try { + keystore = new File((String) event.getNewValue()); + keystoreManager = null; + passwd = null; + if(keystore == null) return; + passwd = getPasswd(keystore); + try { + keystoreManager = KeyStoreManager.getInstance(keystore, passwd); + } catch (Exception e1) { + log("Could not open keystore:",e1); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + }); + } - + firePropertyChange(event); } - + private void firePropertyChange(PropertyChangeEvent event) { synchronized(listeners) @@ -558,8 +568,8 @@ this.keystoreManager = keystoreManager; } - + /** * Adds certificates from the specified keystore to the * lists of the certificates and rewrites an existing keystore. @@ -601,7 +611,7 @@ keystoreManager.mergeKeyStore(newKm, passwd.toCharArray()); keystoreManager.reWriteKeyStore(); - + PropertyChangeEvent event = new PropertyChangeEvent(this,PROPERTY_KEYS,null,certificateList); firePropertyChange(event); } @@ -611,22 +621,22 @@ * @param cert * @throws Exception */ - + public void removeKeyEntry(Certificate cert) throws Exception { if (certificateList.isExistingName(cert.getName())) { log("Removing key entry: " +cert.getName()); certificateList.removeCert(cert); // remove old certificates from the keystore manager String alias = keystoreManager.getAliasFromCertificate(cert.getCert()); - keystoreManager.removeKeyEntry(alias); - keystoreManager.reWriteKeyStore(); - PropertyChangeEvent event = new PropertyChangeEvent(this,PROPERTY_KEYS,null,certificateList); + keystoreManager.removeKeyEntry(alias); + keystoreManager.reWriteKeyStore(); + PropertyChangeEvent event = new PropertyChangeEvent(this,PROPERTY_KEYS,null,certificateList); firePropertyChange(event); } } - - + + /** * Adds a trusted certificate to the list of certificates and rewrites keystore. * @@ -641,7 +651,7 @@ keystoreManager.reWriteKeyStore(); } } - + /** * Removes a trusted certificate from the list of certificates and rewrites keystore. * @@ -657,13 +667,13 @@ keystoreManager.reWriteKeyStore(); } } - + /** * Stores the public key of the specified certificate to the file. * @param cert * @throws Exception */ - + public void exportPublicKey(Certificate cert, File file) throws Exception { if (certificateList.isExistingName(cert.getName())) { String filename = file.getCanonicalPath(); @@ -683,7 +693,7 @@ } } } - + /** * Displays the dialog to change the password of the keystore * and rewrites the keystore with the new password. @@ -701,13 +711,13 @@ passwd = newPasswd; } } - + /** * Displays the dialog with the certificate's details. * @param cert * @throws Exception */ - + public void displayCertificateDetails(Certificate cert) throws Exception { String name = cert.getName(); if (certificateList.isExistingName(name) || trustedCAList.isExistingName(name)) { @@ -717,14 +727,14 @@ d.open(); } } - - + + /** - * Changes the alias name of the certificate - */ - public void changeAlias(Certificate cert) throws Exception { + * Changes the alias name of the certificate + */ + public void changeAlias(Certificate cert) throws Exception { - String name = cert.getName(); + String name = cert.getName(); if (certificateList.isExistingName(name)) { // remove old certificates from the keystore manager String alias = keystoreManager.getAliasFromCertificate(cert.getCert()); @@ -741,17 +751,17 @@ PropertyChangeEvent event = new PropertyChangeEvent(this,PROPERTY_KEYS,null,certificateList); firePropertyChange(event); } - } - } - - /** + } + } + + /** * Generates a certificate request to the file. * @param cert * @throws Exception */ - + public void generateCSR() throws Exception { - + String title = "Generate Certificate Request"; CertificateRequestDialog d = new CertificateRequestDialog(plugin.getWorkbench().getActiveWorkbenchWindow().getShell(), title); d.open(); @@ -760,41 +770,41 @@ String alias = d.getCNString(); File file = askCSRFile(alias); if (file == null) return; - - byte[] encodedCSR = keystoreManager.generateCSR(dnString, 1024, alias); - - log("Generating CSR..."); - - FileOutputStream fos = null; - try { - fos = new FileOutputStream(file); - String certStart = "-----BEGIN CERTIFICATE REQUEST-----\n"; - String certEnd = "-----END CERTIFICATE REQUEST-----\n"; - fos.write(certStart.getBytes()); - fos.write(Base64.encode(encodedCSR)); - fos.write(certEnd.getBytes()); - - } - catch (Exception e) { - throw new Exception(e.getMessage()); - } - finally { - if (fos != null) { - try { fos.close(); } - catch(Exception e) { } - } - } - keystoreManager.rehashKeyStoreEntries(); - keystoreManager.reWriteKeyStore(); - X509Certificate[] X509certs = keystoreManager.getCertificateByAlias(alias); + + byte[] encodedCSR = keystoreManager.generateCSR(dnString, 1024, alias); + + log("Generating CSR..."); + + FileOutputStream fos = null; + try { + fos = new FileOutputStream(file); + String certStart = "-----BEGIN CERTIFICATE REQUEST-----\n"; + String certEnd = "-----END CERTIFICATE REQUEST-----\n"; + fos.write(certStart.getBytes()); + fos.write(Base64.encode(encodedCSR)); + fos.write(certEnd.getBytes()); + + } + catch (Exception e) { + throw new Exception(e.getMessage()); + } + finally { + if (fos != null) { + try { fos.close(); } + catch(Exception e) { } + } + } + keystoreManager.rehashKeyStoreEntries(); + keystoreManager.reWriteKeyStore(); + X509Certificate[] X509certs = keystoreManager.getCertificateByAlias(alias); Certificate cert = new Certificate(X509certs[0]); cert.setName(alias); - certificateList.addCert(cert); - PropertyChangeEvent event = new PropertyChangeEvent(this,PROPERTY_KEYS,null,certificateList); + certificateList.addCert(cert); + PropertyChangeEvent event = new PropertyChangeEvent(this,PROPERTY_KEYS,null,certificateList); firePropertyChange(event); } } - + /** * Opens the dialog to select a file to save a certificate request. * @param alias @@ -814,9 +824,9 @@ } return f; } - + private static boolean getAlarmUser(int status) { return LogActivator.getAlarmUser(PLUGIN_ID, status); } - + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |