KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.auth;
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.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 // This class factors out the management of the .keyring location
24
public class AuthorizationHandler {
25     /* package */static final String JavaDoc F_KEYRING = ".keyring"; //$NON-NLS-1$
26

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

33     /**
34      * Opens the password database (if any) initially provided to the platform at startup.
35      */

36     private static void loadKeyring() throws CoreException {
37         if (keyring != null && new File JavaDoc(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                 // ignore this. It should never happen as we have tested the above format.
47
return;
48             }
49             Location configurationLocation = (Location) Activator.getContext().getService(refs[0]);
50             if (configurationLocation == null)
51                 return;
52             File JavaDoc file = new File JavaDoc(configurationLocation.getURL().getPath() + "/org.eclipse.core.runtime"); //$NON-NLS-1$
53
Activator.getContext().ungetService(refs[0]);
54             file = new File JavaDoc(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             //try deleting the file and loading again - format may have changed
64
new java.io.File JavaDoc(keyringFile).delete();
65             keyring = new AuthorizationDatabase(keyringFile, password);
66             //don't bother logging a second failure and let it flows to the callers
67
}
68         keyringTimeStamp = new File JavaDoc(keyringFile).lastModified();
69     }
70     
71     /**
72      * Saves the keyring file to disk.
73      * @exception CoreException
74      */

75     private static void saveKeyring() throws CoreException {
76         keyring.save();
77         keyringTimeStamp = new File JavaDoc(keyringFile).lastModified();
78     }
79
80     /**
81      * Adds the given authorization information to the key ring. The
82      * information is relevant for the specified protection space and the
83      * given authorization scheme. The protection space is defined by the
84      * combination of the given server URL and realm. The authorization
85      * scheme determines what the authorization information contains and how
86      * it should be used. The authorization information is a <code>Map</code>
87      * of <code>String</code> to <code>String</code> and typically
88      * contains information such as user names and passwords.
89      *
90      * @param serverUrl the URL identifying the server for this authorization
91      * information. For example, "http://www.example.com/".
92      * @param realm the subsection of the given server to which this
93      * authorization information applies. For example,
94      * "realm1@example.com" or "" for no realm.
95      * @param authScheme the scheme for which this authorization information
96      * applies. For example, "Basic" or "" for no authorization scheme
97      * @param info a <code>Map</code> containing authorization information
98      * such as user names and passwords (key type : <code>String</code>,
99      * value type : <code>String</code>)
100      * @exception CoreException if there are problems setting the
101      * authorization information. Reasons include:
102      * <ul>
103      * <li>The keyring could not be saved.</li>
104      * </ul>
105      * XXX Move to a plug-in to be defined (JAAS plugin).
106      */

107     public static synchronized void addAuthorizationInfo(URL JavaDoc serverUrl, String JavaDoc realm, String JavaDoc authScheme, Map JavaDoc info) throws CoreException {
108         loadKeyring();
109         keyring.addAuthorizationInfo(serverUrl, realm, authScheme, new HashMap JavaDoc(info));
110         saveKeyring();
111     }
112
113     /**
114      * Adds the specified resource to the protection space specified by the
115      * given realm. All targets at or deeper than the depth of the last
116      * symbolic element in the path of the given resource URL are assumed to
117      * be in the same protection space.
118      *
119      * @param resourceUrl the URL identifying the resources to be added to
120      * the specified protection space. For example,
121      * "http://www.example.com/folder/".
122      * @param realm the name of the protection space. For example,
123      * "realm1@example.com"
124      * @exception CoreException if there are problems setting the
125      * authorization information. Reasons include:
126      * <ul>
127      * <li>The key ring could not be saved.</li>
128      * </ul>
129      * XXX Move to a plug-in to be defined (JAAS plugin).
130      */

131     public static synchronized void addProtectionSpace(URL JavaDoc resourceUrl, String JavaDoc realm) throws CoreException {
132         loadKeyring();
133         keyring.addProtectionSpace(resourceUrl, realm);
134         saveKeyring();
135     }
136
137     /**
138      * Removes the authorization information for the specified protection
139      * space and given authorization scheme. The protection space is defined
140      * by the given server URL and realm.
141      *
142      * @param serverUrl the URL identifying the server to remove the
143      * authorization information for. For example,
144      * "http://www.example.com/".
145      * @param realm the subsection of the given server to remove the
146      * authorization information for. For example,
147      * "realm1@example.com" or "" for no realm.
148      * @param authScheme the scheme for which the authorization information
149      * to remove applies. For example, "Basic" or "" for no
150      * authorization scheme.
151      * @exception CoreException if there are problems removing the
152      * authorization information. Reasons include:
153      * <ul>
154      * <li>The keyring could not be saved.</li>
155      * </ul>
156      * XXX Move to a plug-in to be defined (JAAS plugin).
157      */

158     public static synchronized void flushAuthorizationInfo(URL JavaDoc serverUrl, String JavaDoc realm, String JavaDoc authScheme) throws CoreException {
159         loadKeyring();
160         keyring.flushAuthorizationInfo(serverUrl, realm, authScheme);
161         saveKeyring();
162     }
163
164     /**
165      * Returns the authorization information for the specified protection
166      * space and given authorization scheme. The protection space is defined
167      * by the given server URL and realm. Returns <code>null</code> if no
168      * such information exists.
169      *
170      * @param serverUrl the URL identifying the server for the authorization
171      * information. For example, "http://www.example.com/".
172      * @param realm the subsection of the given server to which the
173      * authorization information applies. For example,
174      * "realm1@example.com" or "" for no realm.
175      * @param authScheme the scheme for which the authorization information
176      * applies. For example, "Basic" or "" for no authorization scheme
177      * @return the authorization information for the specified protection
178      * space and given authorization scheme, or <code>null</code> if no
179      * such information exists
180      *XXX Move to a plug-in to be defined (JAAS plugin).
181      */

182     public static synchronized Map JavaDoc getAuthorizationInfo(URL JavaDoc serverUrl, String JavaDoc realm, String JavaDoc authScheme) {
183         Map JavaDoc info = null;
184         try {
185             loadKeyring();
186             info = keyring.getAuthorizationInfo(serverUrl, realm, authScheme);
187         } catch (CoreException e) {
188             // The error has already been logged in loadKeyring()
189
}
190         return info == null ? null : new HashMap JavaDoc(info);
191     }
192
193     /**
194      * Returns the protection space (realm) for the specified resource, or
195      * <code>null</code> if the realm is unknown.
196      *
197      * @param resourceUrl the URL of the resource whose protection space is
198      * returned. For example, "http://www.example.com/folder/".
199      * @return the protection space (realm) for the specified resource, or
200      * <code>null</code> if the realm is unknown
201      * * XXX Move to a plug-in to be defined (JAAS plugin).
202      */

203     public static synchronized String JavaDoc getProtectionSpace(URL JavaDoc resourceUrl) {
204         try {
205             loadKeyring();
206         } catch (CoreException e) {
207             // The error has already been logged in loadKeyring()
208
return null;
209         }
210         return keyring.getProtectionSpace(resourceUrl);
211     }
212
213     public static void setKeyringFile(String JavaDoc file) {
214         if (keyringFile != null)
215             throw new IllegalStateException JavaDoc(NLS.bind(Messages.meta_keyringFileAlreadySpecified, keyringFile));
216         keyringFile = file;
217     }
218
219     public static void setPassword(String JavaDoc keyringPassword) {
220         password = keyringPassword;
221     }
222 }
223
Popular Tags