KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > adaptor > LocationManager


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.runtime.adaptor;
12
13 import java.io.*;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Properties JavaDoc;
17 import org.eclipse.core.runtime.internal.adaptor.BasicLocation;
18 import org.eclipse.core.runtime.internal.adaptor.LocationHelper;
19 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
20 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
21 import org.eclipse.osgi.service.datalocation.Location;
22
23 /**
24  * This class is used to manage the various Locations for Eclipse.
25  * <p>
26  * Clients may not extend this class.
27  * </p>
28  * @since 3.1
29  */

30 public class LocationManager {
31     private static Location installLocation = null;
32     private static Location configurationLocation = null;
33     private static Location userLocation = null;
34     private static Location instanceLocation = null;
35
36     public static final String JavaDoc READ_ONLY_AREA_SUFFIX = ".readOnly"; //$NON-NLS-1$
37
public static final String JavaDoc PROP_INSTALL_AREA = "osgi.install.area"; //$NON-NLS-1$
38
public static final String JavaDoc PROP_CONFIG_AREA = "osgi.configuration.area"; //$NON-NLS-1$
39
public static final String JavaDoc PROP_CONFIG_AREA_DEFAULT = "osgi.configuration.area.default"; //$NON-NLS-1$
40
public static final String JavaDoc PROP_SHARED_CONFIG_AREA = "osgi.sharedConfiguration.area"; //$NON-NLS-1$
41
public static final String JavaDoc PROP_INSTANCE_AREA = "osgi.instance.area"; //$NON-NLS-1$
42
public static final String JavaDoc PROP_INSTANCE_AREA_DEFAULT = "osgi.instance.area.default"; //$NON-NLS-1$
43
public static final String JavaDoc PROP_USER_AREA = "osgi.user.area"; //$NON-NLS-1$
44
public static final String JavaDoc PROP_USER_AREA_DEFAULT = "osgi.user.area.default"; //$NON-NLS-1$
45
public static final String JavaDoc PROP_MANIFEST_CACHE = "osgi.manifest.cache"; //$NON-NLS-1$
46
public static final String JavaDoc PROP_USER_HOME = "user.home"; //$NON-NLS-1$
47
public static final String JavaDoc PROP_USER_DIR = "user.dir"; //$NON-NLS-1$
48

49     // configuration area file/dir names
50
public static final String JavaDoc BUNDLES_DIR = "bundles"; //$NON-NLS-1$
51
public static final String JavaDoc STATE_FILE = ".state"; //$NON-NLS-1$
52
public static final String JavaDoc LAZY_FILE = ".lazy"; //$NON-NLS-1$
53
public static final String JavaDoc BUNDLE_DATA_FILE = ".bundledata"; //$NON-NLS-1$
54
public static final String JavaDoc MANIFESTS_DIR = "manifests"; //$NON-NLS-1$
55
public static final String JavaDoc CONFIG_FILE = "config.ini"; //$NON-NLS-1$
56
public static final String JavaDoc ECLIPSE_PROPERTIES = "eclipse.properties"; //$NON-NLS-1$
57

58     // Constants for configuration location discovery
59
private static final String JavaDoc ECLIPSE = "eclipse"; //$NON-NLS-1$
60
private static final String JavaDoc PRODUCT_SITE_MARKER = ".eclipseproduct"; //$NON-NLS-1$
61
private static final String JavaDoc PRODUCT_SITE_ID = "id"; //$NON-NLS-1$
62
private static final String JavaDoc PRODUCT_SITE_VERSION = "version"; //$NON-NLS-1$
63

64     private static final String JavaDoc CONFIG_DIR = "configuration"; //$NON-NLS-1$
65

66     // Data mode constants for user, configuration and data locations.
67
private static final String JavaDoc NONE = "@none"; //$NON-NLS-1$
68
private static final String JavaDoc NO_DEFAULT = "@noDefault"; //$NON-NLS-1$
69
private static final String JavaDoc USER_HOME = "@user.home"; //$NON-NLS-1$
70
private static final String JavaDoc USER_DIR = "@user.dir"; //$NON-NLS-1$
71

72     /**
73      * Builds a URL with the given specification
74      * @param spec the URL specification
75      * @param trailingSlash flag to indicate a trailing slash on the spec
76      * @return a URL
77      */

78     public static URL JavaDoc buildURL(String JavaDoc spec, boolean trailingSlash) {
79         return LocationHelper.buildURL(spec, trailingSlash);
80     }
81
82     private static void mungeConfigurationLocation() {
83         // if the config property was set, munge it for backwards compatibility.
84
String JavaDoc location = FrameworkProperties.getProperty(PROP_CONFIG_AREA);
85         if (location != null) {
86             if (location.endsWith(".cfg")) { //$NON-NLS-1$
87
int index = location.lastIndexOf('/');
88                 if (index < 0)
89                     index = location.lastIndexOf('\\');
90                 location = location.substring(0, index + 1);
91                 FrameworkProperties.setProperty(PROP_CONFIG_AREA, location);
92             }
93         }
94     }
95
96     /**
97      * Initializes the Location objects for the LocationManager.
98      */

99     public static void initializeLocations() {
100         // do install location initialization first since others may depend on it
101
// assumes that the property is already set
102
installLocation = buildLocation(PROP_INSTALL_AREA, null, "", true); //$NON-NLS-1$
103

104         Location temp = buildLocation(PROP_USER_AREA_DEFAULT, null, "", false); //$NON-NLS-1$
105
URL JavaDoc defaultLocation = temp == null ? null : temp.getURL();
106         if (defaultLocation == null)
107             defaultLocation = buildURL(new File(FrameworkProperties.getProperty(PROP_USER_HOME), "user").getAbsolutePath(), true); //$NON-NLS-1$
108
userLocation = buildLocation(PROP_USER_AREA, defaultLocation, "", false); //$NON-NLS-1$
109

110         temp = buildLocation(PROP_INSTANCE_AREA_DEFAULT, null, "", false); //$NON-NLS-1$
111
defaultLocation = temp == null ? null : temp.getURL();
112         if (defaultLocation == null)
113             defaultLocation = buildURL(new File(FrameworkProperties.getProperty(PROP_USER_DIR), "workspace").getAbsolutePath(), true); //$NON-NLS-1$
114
instanceLocation = buildLocation(PROP_INSTANCE_AREA, defaultLocation, "", false); //$NON-NLS-1$
115

116         mungeConfigurationLocation();
117         // compute a default but it is very unlikely to be used since main will have computed everything
118
temp = buildLocation(PROP_CONFIG_AREA_DEFAULT, null, "", false); //$NON-NLS-1$
119
defaultLocation = temp == null ? null : temp.getURL();
120         if (defaultLocation == null)
121             defaultLocation = buildURL(computeDefaultConfigurationLocation(), true);
122         configurationLocation = buildLocation(PROP_CONFIG_AREA, defaultLocation, "", false); //$NON-NLS-1$
123
// get the parent location based on the system property. This will have been set on the
124
// way in either by the caller/user or by main. There will be no parent location if we are not
125
// cascaded.
126
URL JavaDoc parentLocation = computeSharedConfigurationLocation();
127         if (parentLocation != null && !parentLocation.equals(configurationLocation.getURL())) {
128             Location parent = new BasicLocation(null, parentLocation, true);
129             ((BasicLocation) configurationLocation).setParent(parent);
130         }
131         initializeDerivedConfigurationLocations();
132     }
133
134     private static Location buildLocation(String JavaDoc property, URL JavaDoc defaultLocation, String JavaDoc userDefaultAppendage, boolean readOnlyDefault) {
135         String JavaDoc location = FrameworkProperties.clearProperty(property);
136         // the user/product may specify a non-default readOnly setting
137
String JavaDoc userReadOnlySetting = FrameworkProperties.getProperty(property + READ_ONLY_AREA_SUFFIX);
138         boolean readOnly = (userReadOnlySetting == null ? readOnlyDefault : Boolean.valueOf(userReadOnlySetting).booleanValue());
139         // if the instance location is not set, predict where the workspace will be and
140
// put the instance area inside the workspace meta area.
141
if (location == null)
142             return new BasicLocation(property, defaultLocation, readOnly);
143         String JavaDoc trimmedLocation = location.trim();
144         if (trimmedLocation.equalsIgnoreCase(NONE))
145             return null;
146         if (trimmedLocation.equalsIgnoreCase(NO_DEFAULT))
147             return new BasicLocation(property, null, readOnly);
148         if (trimmedLocation.startsWith(USER_HOME)) {
149             String JavaDoc base = substituteVar(location, USER_HOME, PROP_USER_HOME);
150             location = new File(base, userDefaultAppendage).getAbsolutePath();
151         } else if (trimmedLocation.startsWith(USER_DIR)) {
152             String JavaDoc base = substituteVar(location, USER_DIR, PROP_USER_DIR);
153             location = new File(base, userDefaultAppendage).getAbsolutePath();
154         }
155         URL JavaDoc url = buildURL(location, true);
156         BasicLocation result = null;
157         if (url != null) {
158             result = new BasicLocation(property, null, readOnly);
159             result.setURL(url, false);
160         }
161         return result;
162     }
163
164     private static String JavaDoc substituteVar(String JavaDoc source, String JavaDoc var, String JavaDoc prop) {
165         String JavaDoc value = FrameworkProperties.getProperty(prop, ""); //$NON-NLS-1$
166
return value + source.substring(var.length());
167     }
168
169     private static void initializeDerivedConfigurationLocations() {
170         if (FrameworkProperties.getProperty(PROP_MANIFEST_CACHE) == null)
171             FrameworkProperties.setProperty(PROP_MANIFEST_CACHE, getConfigurationFile(MANIFESTS_DIR).getAbsolutePath());
172     }
173
174     private static URL JavaDoc computeInstallConfigurationLocation() {
175         String JavaDoc property = FrameworkProperties.getProperty(PROP_INSTALL_AREA);
176         if (property != null) {
177             try {
178                 return new URL JavaDoc(property);
179             } catch (MalformedURLException JavaDoc e) {
180                 // do nothing here since it is basically impossible to get a bogus url
181
}
182         }
183         return null;
184     }
185
186     private static URL JavaDoc computeSharedConfigurationLocation() {
187         String JavaDoc property = FrameworkProperties.getProperty(PROP_SHARED_CONFIG_AREA);
188         if (property == null)
189             return null;
190         try {
191             URL JavaDoc sharedConfigurationURL = new URL JavaDoc(property);
192             if (sharedConfigurationURL.getPath().startsWith("/")) //$NON-NLS-1$
193
// absolute
194
return sharedConfigurationURL;
195             URL JavaDoc installURL = installLocation.getURL();
196             if (!sharedConfigurationURL.getProtocol().equals(installURL.getProtocol()))
197                 // different protocol
198
return sharedConfigurationURL;
199             sharedConfigurationURL = new URL JavaDoc(installURL, sharedConfigurationURL.getPath());
200             FrameworkProperties.setProperty(PROP_SHARED_CONFIG_AREA, sharedConfigurationURL.toExternalForm());
201         } catch (MalformedURLException JavaDoc e) {
202             // do nothing here since it is basically impossible to get a bogus url
203
}
204         return null;
205     }
206
207     private static String JavaDoc computeDefaultConfigurationLocation() {
208         // 1) We store the config state relative to the 'eclipse' directory if possible
209
// 2) If this directory is read-only
210
// we store the state in <user.home>/.eclipse/<application-id>_<version> where <user.home>
211
// is unique for each local user, and <application-id> is the one
212
// defined in .eclipseproduct marker file. If .eclipseproduct does not
213
// exist, use "eclipse" as the application-id.
214

215         URL JavaDoc installURL = computeInstallConfigurationLocation();
216         if (installURL != null) {
217             File installDir = new File(installURL.getFile());
218             if ("file".equals(installURL.getProtocol()) && canWrite(installDir)) //$NON-NLS-1$
219
return new File(installDir, CONFIG_DIR).getAbsolutePath();
220         }
221         // We can't write in the eclipse install dir so try for some place in the user's home dir
222
return computeDefaultUserAreaLocation(CONFIG_DIR);
223     }
224
225     private static boolean canWrite(File installDir) {
226         if (installDir.canWrite() == false)
227             return false;
228
229         if (!installDir.isDirectory())
230             return false;
231
232         File fileTest = null;
233         try {
234             // we use the .dll suffix to properly test on Vista virtual directories
235
// on Vista you are not allowed to write executable files on virtual directories like "Program Files"
236
fileTest = File.createTempFile("writtableArea", ".dll", installDir); //$NON-NLS-1$ //$NON-NLS-2$
237
} catch (IOException e) {
238             //If an exception occured while trying to create the file, it means that it is not writtable
239
return false;
240         } finally {
241             if (fileTest != null)
242                 fileTest.delete();
243         }
244         return true;
245     }
246
247     private static String JavaDoc computeDefaultUserAreaLocation(String JavaDoc pathAppendage) {
248         // we store the state in <user.home>/.eclipse/<application-id>_<version> where <user.home>
249
// is unique for each local user, and <application-id> is the one
250
// defined in .eclipseproduct marker file. If .eclipseproduct does not
251
// exist, use "eclipse" as the application-id.
252
String JavaDoc installProperty = FrameworkProperties.getProperty(PROP_INSTALL_AREA);
253         URL JavaDoc installURL = buildURL(installProperty, true);
254         if (installURL == null)
255             return null;
256         File installDir = new File(installURL.getFile());
257         // compute an install dir hash to prevent configuration area collisions with other eclipse installs
258
int hashCode;
259         try {
260             hashCode = installDir.getCanonicalPath().hashCode();
261         } catch (IOException ioe) {
262             // fall back to absolute path
263
hashCode = installDir.getAbsolutePath().hashCode();
264         }
265         if (hashCode < 0)
266             hashCode = -(hashCode);
267         String JavaDoc installDirHash = String.valueOf(hashCode);
268
269         String JavaDoc appName = "." + ECLIPSE; //$NON-NLS-1$
270
File eclipseProduct = new File(installDir, PRODUCT_SITE_MARKER);
271         if (eclipseProduct.exists()) {
272             Properties JavaDoc props = new Properties JavaDoc();
273             try {
274                 props.load(new FileInputStream(eclipseProduct));
275                 String JavaDoc appId = props.getProperty(PRODUCT_SITE_ID);
276                 if (appId == null || appId.trim().length() == 0)
277                     appId = ECLIPSE;
278                 String JavaDoc appVersion = props.getProperty(PRODUCT_SITE_VERSION);
279                 if (appVersion == null || appVersion.trim().length() == 0)
280                     appVersion = ""; //$NON-NLS-1$
281
appName += File.separator + appId + "_" + appVersion + "_" + installDirHash; //$NON-NLS-1$ //$NON-NLS-2$
282
} catch (IOException e) {
283                 // Do nothing if we get an exception. We will default to a standard location
284
// in the user's home dir.
285
// add the hash to help prevent collisions
286
appName += File.separator + installDirHash;
287             }
288         } else {
289             // add the hash to help prevent collisions
290
appName += File.separator + installDirHash;
291         }
292         String JavaDoc userHome = FrameworkProperties.getProperty(PROP_USER_HOME);
293         return new File(userHome, appName + "/" + pathAppendage).getAbsolutePath(); //$NON-NLS-1$
294
}
295
296     /**
297      * Returns the user Location object
298      * @return the user Location object
299      */

300     public static Location getUserLocation() {
301         return userLocation;
302     }
303
304     /**
305      * Returns the configuration Location object
306      * @return the configuration Location object
307      */

308     public static Location getConfigurationLocation() {
309         return configurationLocation;
310     }
311
312     /**
313      * Returns the install Location object
314      * @return the install Location object
315      */

316     public static Location getInstallLocation() {
317         return installLocation;
318     }
319
320     /**
321      * Returns the instance Location object
322      * @return the instance Location object
323      */

324     public static Location getInstanceLocation() {
325         return instanceLocation;
326     }
327
328     /**
329      * Returns the File object under the configuration location used for the OSGi configuration
330      * @return the OSGi configuration directory
331      */

332     public static File getOSGiConfigurationDir() {
333         // TODO assumes the URL is a file: url
334
return new File(configurationLocation.getURL().getFile(), FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME);
335     }
336
337     /**
338      * Returns a file from the configuration area that can be used by the framework
339      * @param filename the filename
340      * @return a file from the configuration area
341      */

342     public static File getConfigurationFile(String JavaDoc filename) {
343         File dir = getOSGiConfigurationDir();
344         if (!dir.exists())
345             dir.mkdirs();
346         return new File(dir, filename);
347     }
348 }
349
Popular Tags