1 11 package org.eclipse.core.internal.runtime; 12 13 import java.io.File ; 14 import java.net.URL ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.Platform; 19 import org.eclipse.osgi.util.NLS; 20 21 public class AuthorizationHandler { 23 static final String F_KEYRING = ".keyring"; 25 private static AuthorizationDatabase keyring = null; 27 private static long keyringTimeStamp; 28 private static String keyringFile = null; 29 private static String password = ""; 31 34 private static void loadKeyring() throws CoreException { 35 if (keyring != null && new File (keyringFile).lastModified() == keyringTimeStamp) 36 return; 37 if (keyringFile == null) { 38 File file = new File (InternalPlatform.getDefault().getConfigurationLocation().getURL().getPath() + '/' + Platform.PI_RUNTIME); 39 file = new File (file, F_KEYRING); 40 keyringFile = file.getAbsolutePath(); 41 } 42 try { 43 keyring = new AuthorizationDatabase(keyringFile, password); 44 } catch (CoreException e) { 45 InternalPlatform.getDefault().log(e.getStatus()); 46 } 47 if (keyring == null) { 48 new java.io.File (keyringFile).delete(); 50 keyring = new AuthorizationDatabase(keyringFile, password); 51 } 53 keyringTimeStamp = new File (keyringFile).lastModified(); 54 } 55 56 59 public static void addAuthorizationInfo(URL serverUrl, String realm, String authScheme, Map info) throws CoreException { 60 loadKeyring(); 61 keyring.addAuthorizationInfo(serverUrl, realm, authScheme, new HashMap (info)); 62 keyring.save(); 63 } 64 65 68 public static void addProtectionSpace(URL resourceUrl, String realm) throws CoreException { 69 loadKeyring(); 70 keyring.addProtectionSpace(resourceUrl, realm); 71 keyring.save(); 72 } 73 74 77 public static void flushAuthorizationInfo(URL serverUrl, String realm, String authScheme) throws CoreException { 78 loadKeyring(); 79 keyring.flushAuthorizationInfo(serverUrl, realm, authScheme); 80 keyring.save(); 81 } 82 83 86 public static Map getAuthorizationInfo(URL serverUrl, String realm, String authScheme) { 87 Map info = null; 88 try { 89 loadKeyring(); 90 info = keyring.getAuthorizationInfo(serverUrl, realm, authScheme); 91 } catch (CoreException e) { 92 } 94 return info == null ? null : new HashMap (info); 95 } 96 97 100 public static String getProtectionSpace(URL resourceUrl) { 101 try { 102 loadKeyring(); 103 } catch (CoreException e) { 104 return null; 106 } 107 return keyring.getProtectionSpace(resourceUrl); 108 } 109 110 public static void setKeyringFile(String file) { 111 if (keyringFile != null) 112 throw new IllegalStateException (NLS.bind(Messages.meta_keyringFileAlreadySpecified, keyringFile)); 113 keyringFile = file; 114 } 115 116 public static void setPassword(String keyringPassword) { 117 password = keyringPassword; 118 } 119 } 120 | Popular Tags |