KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > AuthorizationHandler


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.runtime;
12
13 import java.io.File JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.osgi.util.NLS;
20
21 // This class factors out the management of the .keyring location
22
public class AuthorizationHandler {
23     /* package */static final String JavaDoc F_KEYRING = ".keyring"; //$NON-NLS-1$
24

25     //Authorization related informations
26
private static AuthorizationDatabase keyring = null;
27     private static long keyringTimeStamp;
28     private static String JavaDoc keyringFile = null;
29     private static String JavaDoc password = ""; //$NON-NLS-1$
30

31     /**
32      * Opens the password database (if any) initally provided to the platform at startup.
33      */

34     private static void loadKeyring() throws CoreException {
35         if (keyring != null && new File JavaDoc(keyringFile).lastModified() == keyringTimeStamp)
36             return;
37         if (keyringFile == null) {
38             File JavaDoc file = new File JavaDoc(InternalPlatform.getDefault().getConfigurationLocation().getURL().getPath() + '/' + Platform.PI_RUNTIME);
39             file = new File JavaDoc(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             //try deleting the file and loading again - format may have changed
49
new java.io.File JavaDoc(keyringFile).delete();
50             keyring = new AuthorizationDatabase(keyringFile, password);
51             //don't bother logging a second failure and let it flows to the callers
52
}
53         keyringTimeStamp = new File JavaDoc(keyringFile).lastModified();
54     }
55
56     /**
57      * @see org.eclipse.core.runtime.Platform
58      */

59     public static void addAuthorizationInfo(URL JavaDoc serverUrl, String JavaDoc realm, String JavaDoc authScheme, Map JavaDoc info) throws CoreException {
60         loadKeyring();
61         keyring.addAuthorizationInfo(serverUrl, realm, authScheme, new HashMap JavaDoc(info));
62         keyring.save();
63     }
64
65     /**
66      * @see org.eclipse.core.runtime.Platform
67      */

68     public static void addProtectionSpace(URL JavaDoc resourceUrl, String JavaDoc realm) throws CoreException {
69         loadKeyring();
70         keyring.addProtectionSpace(resourceUrl, realm);
71         keyring.save();
72     }
73
74     /**
75      * @see org.eclipse.core.runtime.Platform
76      */

77     public static void flushAuthorizationInfo(URL JavaDoc serverUrl, String JavaDoc realm, String JavaDoc authScheme) throws CoreException {
78         loadKeyring();
79         keyring.flushAuthorizationInfo(serverUrl, realm, authScheme);
80         keyring.save();
81     }
82
83     /**
84      * @see org.eclipse.core.runtime.Platform
85      */

86     public static Map JavaDoc getAuthorizationInfo(URL JavaDoc serverUrl, String JavaDoc realm, String JavaDoc authScheme) {
87         Map JavaDoc info = null;
88         try {
89             loadKeyring();
90             info = keyring.getAuthorizationInfo(serverUrl, realm, authScheme);
91         } catch (CoreException e) {
92             // The error has already been logged in loadKeyring()
93
}
94         return info == null ? null : new HashMap JavaDoc(info);
95     }
96
97     /**
98      * @see org.eclipse.core.runtime.Platform
99      */

100     public static String JavaDoc getProtectionSpace(URL JavaDoc resourceUrl) {
101         try {
102             loadKeyring();
103         } catch (CoreException e) {
104             // The error has already been logged in loadKeyring()
105
return null;
106         }
107         return keyring.getProtectionSpace(resourceUrl);
108     }
109
110     public static void setKeyringFile(String JavaDoc file) {
111         if (keyringFile != null)
112             throw new IllegalStateException JavaDoc(NLS.bind(Messages.meta_keyringFileAlreadySpecified, keyringFile));
113         keyringFile = file;
114     }
115
116     public static void setPassword(String JavaDoc keyringPassword) {
117         password = keyringPassword;
118     }
119 }
120
Popular Tags