1 11 package org.eclipse.core.internal.runtime; 12 13 import java.io.*; 14 import java.net.URL ; 15 import java.util.*; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.osgi.util.NLS; 18 19 28 public class AuthorizationDatabase { 29 32 private static final int KEYRING_FILE_VERSION = 1; 33 38 private Hashtable authorizationInfo = new Hashtable(5); 39 40 43 private Hashtable protectionSpace = new Hashtable(5); 44 45 private File file = null; 46 private String password = null; 47 private boolean needsSaving = true; 48 49 53 public AuthorizationDatabase() { 54 super(); 55 } 56 57 75 public AuthorizationDatabase(String filename, String password) throws CoreException { 76 Assert.isNotNull(filename); 77 Assert.isNotNull(password); 78 this.password = password; 79 file = new File(filename); 80 load(); 81 } 82 83 103 public void addAuthorizationInfo(URL serverUrl, String realm, String authScheme, Map info) { 104 Assert.isNotNull(serverUrl); 105 Assert.isNotNull(realm); 106 Assert.isNotNull(authScheme); 107 Assert.isNotNull(info); 108 109 String url = serverUrl.toString(); 110 Hashtable realmToAuthScheme = (Hashtable) authorizationInfo.get(url); 111 if (realmToAuthScheme == null) { 112 realmToAuthScheme = new Hashtable(5); 113 authorizationInfo.put(url, realmToAuthScheme); 114 } 115 116 Hashtable authSchemeToInfo = (Hashtable) realmToAuthScheme.get(realm); 117 if (authSchemeToInfo == null) { 118 authSchemeToInfo = new Hashtable(5); 119 realmToAuthScheme.put(realm, authSchemeToInfo); 120 } 121 122 authSchemeToInfo.put(authScheme.toLowerCase(), info); 123 needsSaving = true; 124 } 125 126 138 public void addProtectionSpace(URL resourceUrl, String realm) { 139 Assert.isNotNull(resourceUrl); 140 Assert.isNotNull(realm); 141 142 if (!resourceUrl.getFile().endsWith("/")) { resourceUrl = URLTool.getParent(resourceUrl); 144 } 145 146 String oldRealm = getProtectionSpace(resourceUrl); 147 if (oldRealm != null && oldRealm.equals(realm)) { 148 return; 149 } 150 151 String url1 = resourceUrl.toString(); 152 Enumeration urls = protectionSpace.keys(); 153 while (urls.hasMoreElements()) { 154 String url2 = (String ) urls.nextElement(); 155 if (url1.startsWith(url2) || url2.startsWith(url1)) { 156 protectionSpace.remove(url2); 157 break; 158 } 159 } 160 161 protectionSpace.put(url1, realm); 162 needsSaving = true; 163 } 164 165 180 public void flushAuthorizationInfo(URL serverUrl, String realm, String authScheme) { 181 Hashtable realmToAuthScheme = (Hashtable) authorizationInfo.get(serverUrl.toString()); 182 183 if (realmToAuthScheme == null) { 184 return; 185 } 186 187 Hashtable authSchemeToInfo = (Hashtable) realmToAuthScheme.get(realm); 188 189 if (authSchemeToInfo == null) { 190 return; 191 } 192 193 authSchemeToInfo.remove(authScheme.toLowerCase()); 194 195 needsSaving = true; 196 } 197 198 215 public Map getAuthorizationInfo(URL serverUrl, String realm, String authScheme) { 216 Hashtable realmToAuthScheme = (Hashtable) authorizationInfo.get(serverUrl.toString()); 217 if (realmToAuthScheme == null) { 218 return null; 219 } 220 221 Hashtable authSchemeToInfo = (Hashtable) realmToAuthScheme.get(realm); 222 if (authSchemeToInfo == null) { 223 return null; 224 } 225 226 return (Map) authSchemeToInfo.get(authScheme.toLowerCase()); 227 } 228 229 238 public String getProtectionSpace(URL resourceUrl) { 239 while (resourceUrl != null) { 240 String realm = (String ) protectionSpace.get(resourceUrl.toString()); 241 if (realm != null) { 242 return realm; 243 } 244 resourceUrl = URLTool.getParent(resourceUrl); 245 } 246 247 return null; 248 } 249 250 private void load() throws CoreException { 251 if (file == null) 252 return; 253 if (!file.exists()) { 254 save(); 255 return; 256 } 257 try { 258 InputStream input = new FileInputStream(file); 259 try { 260 load(input); 261 } finally { 262 input.close(); 263 } 264 } catch (IOException e) { 265 throw new CoreException(new Status(IStatus.ERROR, Platform.PI_RUNTIME, Platform.FAILED_READ_METADATA, NLS.bind(Messages.meta_unableToReadAuthorization, file), e)); 266 } catch (ClassNotFoundException e) { 267 throw new CoreException(new Status(IStatus.ERROR, Platform.PI_RUNTIME, Platform.FAILED_READ_METADATA, NLS.bind(Messages.meta_unableToReadAuthorization, file), e)); 268 } 269 } 270 271 private void load(InputStream is) throws IOException, ClassNotFoundException , CoreException { 272 int version = is.read(); 274 if (version == KEYRING_FILE_VERSION) { 275 CipherInputStream cis = new CipherInputStream(is, password); 277 ObjectInputStream ois = new ObjectInputStream(cis); 278 try { 279 authorizationInfo = (Hashtable) ois.readObject(); 280 protectionSpace = (Hashtable) ois.readObject(); 281 } finally { 282 ois.close(); 283 } 284 } else { 285 InternalPlatform.getDefault().log(new Status(IStatus.WARNING, Platform.PI_RUNTIME, Platform.FAILED_READ_METADATA, Messages.meta_authFormatChanged, null)); 287 try { 289 is.close(); 290 } catch (IOException e) { 291 } 293 needsSaving = true; 294 save(); 295 } 296 } 297 298 301 public void save() throws CoreException { 302 if (!needsSaving || file == null) 303 return; 304 try { 305 file.delete(); 306 if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs()) || !canWrite(file.getParentFile())) 307 throw new CoreException(new Status(IStatus.ERROR, Platform.PI_RUNTIME, Platform.FAILED_WRITE_METADATA, NLS.bind(Messages.meta_unableToWriteAuthorization, file), null)); 308 file.createNewFile(); 309 FileOutputStream out = new FileOutputStream(file); 310 try { 311 save(out); 312 } finally { 313 out.close(); 314 } 315 } catch (IOException e) { 316 throw new CoreException(new Status(IStatus.ERROR, Platform.PI_RUNTIME, Platform.FAILED_WRITE_METADATA, NLS.bind(Messages.meta_unableToWriteAuthorization, file), e)); 317 } 318 needsSaving = false; 319 } 320 321 private static boolean canWrite(File installDir) { 322 if (!installDir.canWrite()) 323 return false; 324 325 if (!installDir.isDirectory()) 326 return false; 327 328 File fileTest = null; 329 try { 330 fileTest = File.createTempFile("writtableArea", null, installDir); } catch (IOException e) { 332 return false; 334 } finally { 335 if (fileTest != null) 336 fileTest.delete(); 337 } 338 return true; 339 } 340 341 private void save(FileOutputStream os) throws IOException { 342 os.write(KEYRING_FILE_VERSION); 344 345 CipherOutputStream cos = new CipherOutputStream(os, password); 346 ObjectOutputStream oos = new ObjectOutputStream(cos); 347 try { 349 oos.writeObject(authorizationInfo); 350 oos.writeObject(protectionSpace); 351 os.flush(); 352 os.getFD().sync(); 353 } finally { 354 oos.close(); 355 } 356 } 357 358 362 public boolean setPassword(String oldValue, String newValue) { 363 if (!oldValue.equals(password)) 364 return false; 365 password = newValue; 366 needsSaving = true; 367 return true; 368 } 369 } 370 | Popular Tags |