1 11 package org.eclipse.core.internal.preferences.legacy; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.Properties ; 18 import org.eclipse.core.internal.preferences.exchange.IProductPreferencesService; 19 import org.eclipse.core.internal.runtime.InternalPlatform; 20 import org.eclipse.core.runtime.*; 21 import org.osgi.framework.Bundle; 22 23 public class ProductPreferencesService implements IProductPreferencesService { 24 25 private static final IPath NL_DIR = new Path("$nl$"); 27 public static final String PRODUCT_KEY = "preferenceCustomization"; private static final String LEGACY_PRODUCT_CUSTOMIZATION_FILENAME = "plugin_customization.ini"; private static final String PROPERTIES_FILE_EXTENSION = "properties"; 32 private boolean initialized = false; 33 private String customizationValue = null; private Bundle customizationBundle = null; 35 private String productID = null; 36 37 private void initValues() { 38 if (initialized) 39 return; 40 initialized = true; 41 42 IProduct product = Platform.getProduct(); 43 if (product == null) { 44 if (InternalPlatform.DEBUG_PLUGIN_PREFERENCES) 45 InternalPlatform.message("Product not available to set product default preference overrides."); return; 47 } 48 productID = product.getId(); 49 if (productID == null) { 50 if (InternalPlatform.DEBUG_PLUGIN_PREFERENCES) 51 InternalPlatform.message("Product ID not available to apply product-level preference defaults."); return; 53 } 54 customizationBundle = product.getDefiningBundle(); 55 if (customizationBundle == null) { 56 if (InternalPlatform.DEBUG_PLUGIN_PREFERENCES) 57 InternalPlatform.message("Bundle not available to apply product-level preference defaults for product id: " + productID); return; 59 } 60 customizationValue = product.getProperty(PRODUCT_KEY); 61 if (customizationValue == null) { 62 if (InternalPlatform.DEBUG_PLUGIN_PREFERENCES) 63 InternalPlatform.message("Product : " + productID + " does not define preference customization file. Using legacy file: plugin_customization.ini"); customizationValue = LEGACY_PRODUCT_CUSTOMIZATION_FILENAME; 65 } 66 } 67 68 public Properties getProductCustomization() { 69 initValues(); 70 URL url = null; 71 if (customizationValue != null) { 72 try { 74 url = new URL (customizationValue); 75 } catch (MalformedURLException e) { 76 url = FileLocator.find(customizationBundle, new Path(customizationValue), null); 78 } 79 } 80 81 if (url == null) { 82 if (InternalPlatform.DEBUG_PLUGIN_PREFERENCES) 83 InternalPlatform.message("Product preference customization file: " + customizationValue + " not found for bundle: " + productID); } 85 86 return loadProperties(url); 87 } 88 89 public Properties getProductTranslation() { 90 initValues(); 91 URL transURL = null; 92 93 if (customizationValue != null) 94 transURL = FileLocator.find(customizationBundle, NL_DIR.append(customizationValue).removeFileExtension().addFileExtension(PROPERTIES_FILE_EXTENSION), null); 95 96 if (transURL == null && InternalPlatform.DEBUG_PLUGIN_PREFERENCES) 97 InternalPlatform.message("No preference translations found for product/file: " + customizationBundle.getSymbolicName() + '/' + customizationValue); 99 return loadProperties(transURL); 100 } 101 102 private Properties loadProperties(URL url) { 103 Properties result = new Properties (); 104 if (url == null) 105 return result; 106 InputStream input = null; 107 try { 108 input = url.openStream(); 109 result.load(input); 110 } catch (IOException e) { 111 if (InternalPlatform.DEBUG_PLUGIN_PREFERENCES) { 112 InternalPlatform.message("Problem opening stream to preference customization file: " + url); e.printStackTrace(); 114 } 115 } finally { 116 if (input != null) 117 try { 118 input.close(); 119 } catch (IOException e) { 120 } 122 } 123 return result; 124 } 125 126 } 127 | Popular Tags |