KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > preferences > legacy > ProductPreferencesService


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.preferences.legacy;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.Properties JavaDoc;
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$"); //$NON-NLS-1$
26

27     // declared in org.eclipse.ui.branding.IProductConstants
28
public static final String JavaDoc PRODUCT_KEY = "preferenceCustomization"; //$NON-NLS-1$
29
private static final String JavaDoc LEGACY_PRODUCT_CUSTOMIZATION_FILENAME = "plugin_customization.ini"; //$NON-NLS-1$
30
private static final String JavaDoc PROPERTIES_FILE_EXTENSION = "properties"; //$NON-NLS-1$
31

32     private boolean initialized = false;
33     private String JavaDoc customizationValue = null; // it won't change during the product run time
34
private Bundle customizationBundle = null;
35     private String JavaDoc 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."); //$NON-NLS-1$
46
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."); //$NON-NLS-1$
52
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); //$NON-NLS-1$
58
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"); //$NON-NLS-1$//$NON-NLS-2$
64
customizationValue = LEGACY_PRODUCT_CUSTOMIZATION_FILENAME;
65         }
66     }
67
68     public Properties JavaDoc getProductCustomization() {
69         initValues();
70         URL JavaDoc url = null;
71         if (customizationValue != null) {
72             // try to convert the key to a URL
73
try {
74                 url = new URL JavaDoc(customizationValue);
75             } catch (MalformedURLException JavaDoc e) {
76                 // didn't work so treat it as a filename
77
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); //$NON-NLS-1$//$NON-NLS-2$
84
}
85
86         return loadProperties(url);
87     }
88
89     public Properties JavaDoc getProductTranslation() {
90         initValues();
91         URL JavaDoc 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); //$NON-NLS-1$
98

99         return loadProperties(transURL);
100     }
101
102     private Properties JavaDoc loadProperties(URL JavaDoc url) {
103         Properties JavaDoc result = new Properties JavaDoc();
104         if (url == null)
105             return result;
106         InputStream JavaDoc input = null;
107         try {
108             input = url.openStream();
109             result.load(input);
110         } catch (IOException JavaDoc e) {
111             if (InternalPlatform.DEBUG_PLUGIN_PREFERENCES) {
112                 InternalPlatform.message("Problem opening stream to preference customization file: " + url); //$NON-NLS-1$
113
e.printStackTrace();
114             }
115         } finally {
116             if (input != null)
117                 try {
118                     input.close();
119                 } catch (IOException JavaDoc e) {
120                     // ignore
121
}
122         }
123         return result;
124     }
125
126 }
127
Popular Tags