1 11 package org.eclipse.core.runtime.adaptor; 12 13 import java.io.*; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.Properties ; 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 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 READ_ONLY_AREA_SUFFIX = ".readOnly"; public static final String PROP_INSTALL_AREA = "osgi.install.area"; public static final String PROP_CONFIG_AREA = "osgi.configuration.area"; public static final String PROP_CONFIG_AREA_DEFAULT = "osgi.configuration.area.default"; public static final String PROP_SHARED_CONFIG_AREA = "osgi.sharedConfiguration.area"; public static final String PROP_INSTANCE_AREA = "osgi.instance.area"; public static final String PROP_INSTANCE_AREA_DEFAULT = "osgi.instance.area.default"; public static final String PROP_USER_AREA = "osgi.user.area"; public static final String PROP_USER_AREA_DEFAULT = "osgi.user.area.default"; public static final String PROP_MANIFEST_CACHE = "osgi.manifest.cache"; public static final String PROP_USER_HOME = "user.home"; public static final String PROP_USER_DIR = "user.dir"; 49 public static final String BUNDLES_DIR = "bundles"; public static final String STATE_FILE = ".state"; public static final String LAZY_FILE = ".lazy"; public static final String BUNDLE_DATA_FILE = ".bundledata"; public static final String MANIFESTS_DIR = "manifests"; public static final String CONFIG_FILE = "config.ini"; public static final String ECLIPSE_PROPERTIES = "eclipse.properties"; 58 private static final String ECLIPSE = "eclipse"; private static final String PRODUCT_SITE_MARKER = ".eclipseproduct"; private static final String PRODUCT_SITE_ID = "id"; private static final String PRODUCT_SITE_VERSION = "version"; 64 private static final String CONFIG_DIR = "configuration"; 66 private static final String NONE = "@none"; private static final String NO_DEFAULT = "@noDefault"; private static final String USER_HOME = "@user.home"; private static final String USER_DIR = "@user.dir"; 72 78 public static URL buildURL(String spec, boolean trailingSlash) { 79 return LocationHelper.buildURL(spec, trailingSlash); 80 } 81 82 private static void mungeConfigurationLocation() { 83 String location = FrameworkProperties.getProperty(PROP_CONFIG_AREA); 85 if (location != null) { 86 if (location.endsWith(".cfg")) { 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 99 public static void initializeLocations() { 100 installLocation = buildLocation(PROP_INSTALL_AREA, null, "", true); 104 Location temp = buildLocation(PROP_USER_AREA_DEFAULT, null, "", false); URL defaultLocation = temp == null ? null : temp.getURL(); 106 if (defaultLocation == null) 107 defaultLocation = buildURL(new File(FrameworkProperties.getProperty(PROP_USER_HOME), "user").getAbsolutePath(), true); userLocation = buildLocation(PROP_USER_AREA, defaultLocation, "", false); 110 temp = buildLocation(PROP_INSTANCE_AREA_DEFAULT, null, "", false); defaultLocation = temp == null ? null : temp.getURL(); 112 if (defaultLocation == null) 113 defaultLocation = buildURL(new File(FrameworkProperties.getProperty(PROP_USER_DIR), "workspace").getAbsolutePath(), true); instanceLocation = buildLocation(PROP_INSTANCE_AREA, defaultLocation, "", false); 116 mungeConfigurationLocation(); 117 temp = buildLocation(PROP_CONFIG_AREA_DEFAULT, null, "", false); defaultLocation = temp == null ? null : temp.getURL(); 120 if (defaultLocation == null) 121 defaultLocation = buildURL(computeDefaultConfigurationLocation(), true); 122 configurationLocation = buildLocation(PROP_CONFIG_AREA, defaultLocation, "", false); URL 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 property, URL defaultLocation, String userDefaultAppendage, boolean readOnlyDefault) { 135 String location = FrameworkProperties.clearProperty(property); 136 String userReadOnlySetting = FrameworkProperties.getProperty(property + READ_ONLY_AREA_SUFFIX); 138 boolean readOnly = (userReadOnlySetting == null ? readOnlyDefault : Boolean.valueOf(userReadOnlySetting).booleanValue()); 139 if (location == null) 142 return new BasicLocation(property, defaultLocation, readOnly); 143 String 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 base = substituteVar(location, USER_HOME, PROP_USER_HOME); 150 location = new File(base, userDefaultAppendage).getAbsolutePath(); 151 } else if (trimmedLocation.startsWith(USER_DIR)) { 152 String base = substituteVar(location, USER_DIR, PROP_USER_DIR); 153 location = new File(base, userDefaultAppendage).getAbsolutePath(); 154 } 155 URL 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 substituteVar(String source, String var, String prop) { 165 String value = FrameworkProperties.getProperty(prop, ""); 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 computeInstallConfigurationLocation() { 175 String property = FrameworkProperties.getProperty(PROP_INSTALL_AREA); 176 if (property != null) { 177 try { 178 return new URL (property); 179 } catch (MalformedURLException e) { 180 } 182 } 183 return null; 184 } 185 186 private static URL computeSharedConfigurationLocation() { 187 String property = FrameworkProperties.getProperty(PROP_SHARED_CONFIG_AREA); 188 if (property == null) 189 return null; 190 try { 191 URL sharedConfigurationURL = new URL (property); 192 if (sharedConfigurationURL.getPath().startsWith("/")) return sharedConfigurationURL; 195 URL installURL = installLocation.getURL(); 196 if (!sharedConfigurationURL.getProtocol().equals(installURL.getProtocol())) 197 return sharedConfigurationURL; 199 sharedConfigurationURL = new URL (installURL, sharedConfigurationURL.getPath()); 200 FrameworkProperties.setProperty(PROP_SHARED_CONFIG_AREA, sharedConfigurationURL.toExternalForm()); 201 } catch (MalformedURLException e) { 202 } 204 return null; 205 } 206 207 private static String computeDefaultConfigurationLocation() { 208 215 URL installURL = computeInstallConfigurationLocation(); 216 if (installURL != null) { 217 File installDir = new File(installURL.getFile()); 218 if ("file".equals(installURL.getProtocol()) && canWrite(installDir)) return new File(installDir, CONFIG_DIR).getAbsolutePath(); 220 } 221 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 fileTest = File.createTempFile("writtableArea", ".dll", installDir); } catch (IOException e) { 238 return false; 240 } finally { 241 if (fileTest != null) 242 fileTest.delete(); 243 } 244 return true; 245 } 246 247 private static String computeDefaultUserAreaLocation(String pathAppendage) { 248 String installProperty = FrameworkProperties.getProperty(PROP_INSTALL_AREA); 253 URL installURL = buildURL(installProperty, true); 254 if (installURL == null) 255 return null; 256 File installDir = new File(installURL.getFile()); 257 int hashCode; 259 try { 260 hashCode = installDir.getCanonicalPath().hashCode(); 261 } catch (IOException ioe) { 262 hashCode = installDir.getAbsolutePath().hashCode(); 264 } 265 if (hashCode < 0) 266 hashCode = -(hashCode); 267 String installDirHash = String.valueOf(hashCode); 268 269 String appName = "." + ECLIPSE; File eclipseProduct = new File(installDir, PRODUCT_SITE_MARKER); 271 if (eclipseProduct.exists()) { 272 Properties props = new Properties (); 273 try { 274 props.load(new FileInputStream(eclipseProduct)); 275 String appId = props.getProperty(PRODUCT_SITE_ID); 276 if (appId == null || appId.trim().length() == 0) 277 appId = ECLIPSE; 278 String appVersion = props.getProperty(PRODUCT_SITE_VERSION); 279 if (appVersion == null || appVersion.trim().length() == 0) 280 appVersion = ""; appName += File.separator + appId + "_" + appVersion + "_" + installDirHash; } catch (IOException e) { 283 appName += File.separator + installDirHash; 287 } 288 } else { 289 appName += File.separator + installDirHash; 291 } 292 String userHome = FrameworkProperties.getProperty(PROP_USER_HOME); 293 return new File(userHome, appName + "/" + pathAppendage).getAbsolutePath(); } 295 296 300 public static Location getUserLocation() { 301 return userLocation; 302 } 303 304 308 public static Location getConfigurationLocation() { 309 return configurationLocation; 310 } 311 312 316 public static Location getInstallLocation() { 317 return installLocation; 318 } 319 320 324 public static Location getInstanceLocation() { 325 return instanceLocation; 326 } 327 328 332 public static File getOSGiConfigurationDir() { 333 return new File(configurationLocation.getURL().getFile(), FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME); 335 } 336 337 342 public static File getConfigurationFile(String filename) { 343 File dir = getOSGiConfigurationDir(); 344 if (!dir.exists()) 345 dir.mkdirs(); 346 return new File(dir, filename); 347 } 348 } 349 | Popular Tags |