1 11 package org.eclipse.core.internal.runtime.auth; 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.osgi.service.datalocation.Location; 19 import org.eclipse.osgi.util.NLS; 20 import org.osgi.framework.InvalidSyntaxException; 21 import org.osgi.framework.ServiceReference; 22 23 public class AuthorizationHandler { 25 static final String F_KEYRING = ".keyring"; 27 private static AuthorizationDatabase keyring = null; 29 private static long keyringTimeStamp; 30 private static String keyringFile = null; 31 private static String password = ""; 33 36 private static void loadKeyring() throws CoreException { 37 if (keyring != null && new File (keyringFile).lastModified() == keyringTimeStamp) 38 return; 39 if (keyringFile == null) { 40 ServiceReference[] refs = null; 41 try { 42 refs = Activator.getContext().getServiceReferences(Location.class.getName(), Location.CONFIGURATION_FILTER); 43 if (refs == null || refs.length == 0) 44 return; 45 } catch (InvalidSyntaxException e) { 46 return; 48 } 49 Location configurationLocation = (Location) Activator.getContext().getService(refs[0]); 50 if (configurationLocation == null) 51 return; 52 File file = new File (configurationLocation.getURL().getPath() + "/org.eclipse.core.runtime"); Activator.getContext().ungetService(refs[0]); 54 file = new File (file, F_KEYRING); 55 keyringFile = file.getAbsolutePath(); 56 } 57 try { 58 keyring = new AuthorizationDatabase(keyringFile, password); 59 } catch (CoreException e) { 60 Activator.log(e.getStatus()); 61 } 62 if (keyring == null) { 63 new java.io.File (keyringFile).delete(); 65 keyring = new AuthorizationDatabase(keyringFile, password); 66 } 68 keyringTimeStamp = new File (keyringFile).lastModified(); 69 } 70 71 75 private static void saveKeyring() throws CoreException { 76 keyring.save(); 77 keyringTimeStamp = new File (keyringFile).lastModified(); 78 } 79 80 107 public static synchronized void addAuthorizationInfo(URL serverUrl, String realm, String authScheme, Map info) throws CoreException { 108 loadKeyring(); 109 keyring.addAuthorizationInfo(serverUrl, realm, authScheme, new HashMap (info)); 110 saveKeyring(); 111 } 112 113 131 public static synchronized void addProtectionSpace(URL resourceUrl, String realm) throws CoreException { 132 loadKeyring(); 133 keyring.addProtectionSpace(resourceUrl, realm); 134 saveKeyring(); 135 } 136 137 158 public static synchronized void flushAuthorizationInfo(URL serverUrl, String realm, String authScheme) throws CoreException { 159 loadKeyring(); 160 keyring.flushAuthorizationInfo(serverUrl, realm, authScheme); 161 saveKeyring(); 162 } 163 164 182 public static synchronized Map getAuthorizationInfo(URL serverUrl, String realm, String authScheme) { 183 Map info = null; 184 try { 185 loadKeyring(); 186 info = keyring.getAuthorizationInfo(serverUrl, realm, authScheme); 187 } catch (CoreException e) { 188 } 190 return info == null ? null : new HashMap (info); 191 } 192 193 203 public static synchronized String getProtectionSpace(URL resourceUrl) { 204 try { 205 loadKeyring(); 206 } catch (CoreException e) { 207 return null; 209 } 210 return keyring.getProtectionSpace(resourceUrl); 211 } 212 213 public static void setKeyringFile(String file) { 214 if (keyringFile != null) 215 throw new IllegalStateException (NLS.bind(Messages.meta_keyringFileAlreadySpecified, keyringFile)); 216 keyringFile = file; 217 } 218 219 public static void setPassword(String keyringPassword) { 220 password = keyringPassword; 221 } 222 } 223 | Popular Tags |