KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > preferences > InstancePreferences


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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;
12
13 import java.io.*;
14 import java.util.*;
15 import org.eclipse.core.internal.runtime.MetaDataKeeper;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
19 import org.eclipse.osgi.service.datalocation.Location;
20
21 /**
22  * @since 3.0
23  */

24 public class InstancePreferences extends EclipsePreferences {
25
26     // cached values
27
private String JavaDoc qualifier;
28     private int segmentCount;
29     private IEclipsePreferences loadLevel;
30     private IPath location;
31     // cache which nodes have been loaded from disk
32
private static Set loadedNodes = new HashSet();
33     private static boolean initialized = false;
34     private static IPath baseLocation;
35
36     private static IPath getBaseLocation() {
37         // If we are running with -data=@none we won't have an instance location.
38
// By leaving the value of baseLocation as null we still allow the users
39
// to set preferences in this scope but the values will not be persisted
40
// to disk when #flush() is called.
41
if (baseLocation == null) {
42             Location instanceLocation = PreferencesOSGiUtils.getDefault().getInstanceLocation();
43             if (instanceLocation != null && (instanceLocation.isSet() || instanceLocation.allowsDefault()))
44                 baseLocation = MetaDataKeeper.getMetaArea().getStateLocation(IPreferencesConstants.RUNTIME_NAME);
45         }
46         return baseLocation;
47     }
48
49     /**
50      * Default constructor. Should only be called by #createExecutableExtension.
51      */

52     public InstancePreferences() {
53         this(null, null);
54     }
55
56     private InstancePreferences(EclipsePreferences parent, String JavaDoc name) {
57         super(parent, name);
58
59         initializeChildren();
60
61         // cache the segment count
62
String JavaDoc path = absolutePath();
63         segmentCount = getSegmentCount(path);
64         if (segmentCount < 2)
65             return;
66
67         // cache the qualifier
68
qualifier = getSegment(path, 1);
69
70         // don't cache the location until later in case instance prefs are
71
// accessed before the instance location is set.
72
}
73
74     protected boolean isAlreadyLoaded(IEclipsePreferences node) {
75         return loadedNodes.contains(node.name());
76     }
77
78     protected void loaded() {
79         loadedNodes.add(name());
80     }
81
82     /**
83      * Load the Eclipse 2.1 preferences for the given bundle. If a file
84      * doesn't exist then assume that conversion has already occurred
85      * and do nothing.
86      */

87     protected void loadLegacy() {
88         IPath path = new Path(absolutePath());
89         if (path.segmentCount() != 2)
90             return;
91         // If we are running with -data=@none we won't have an instance location.
92
if (PreferencesOSGiUtils.getDefault().getInstanceLocation() == null) {
93             if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
94                 PrefsMessages.message("Cannot load Legacy plug-in preferences since instance location is not set."); //$NON-NLS-1$
95
return;
96         }
97         String JavaDoc bundleName = path.segment(1);
98         // the preferences file is located in the plug-in's state area at a well-known name
99
// don't need to create the directory if there are no preferences to load
100
File prefFile = null;
101         Location instanceLocation = PreferencesOSGiUtils.getDefault().getInstanceLocation();
102         if (instanceLocation != null && instanceLocation.isSet())
103             prefFile = MetaDataKeeper.getMetaArea().getPreferenceLocation(bundleName, false).toFile();
104         if (prefFile == null) {
105             if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
106                 PrefsMessages.message("Cannot load legacy values because instance location is not set."); //$NON-NLS-1$
107
return;
108         }
109         if (!prefFile.exists()) {
110             // no preference file - that's fine
111
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
112                 PrefsMessages.message("Legacy plug-in preference file not found: " + prefFile); //$NON-NLS-1$
113
return;
114         }
115
116         if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
117             PrefsMessages.message("Loading legacy preferences from " + prefFile); //$NON-NLS-1$
118

119         // load preferences from file
120
InputStream input = null;
121         Properties values = new Properties();
122         try {
123             input = new BufferedInputStream(new FileInputStream(prefFile));
124             values.load(input);
125         } catch (IOException e) {
126             // problems loading preference store - quietly ignore
127
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
128                 PrefsMessages.message("IOException encountered loading legacy preference file " + prefFile); //$NON-NLS-1$
129
return;
130         } finally {
131             if (input != null) {
132                 try {
133                     input.close();
134                 } catch (IOException e) {
135                     // ignore problems with close
136
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL) {
137                         PrefsMessages.message("IOException encountered closing legacy preference file " + prefFile); //$NON-NLS-1$
138
e.printStackTrace();
139                     }
140                 }
141             }
142         }
143
144         // Store values in the preferences object
145
for (Iterator i = values.keySet().iterator(); i.hasNext();) {
146             String JavaDoc key = (String JavaDoc) i.next();
147             String JavaDoc value = values.getProperty(key);
148             // value shouldn't be null but check just in case...
149
if (value != null) {
150                 if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
151                     PrefsMessages.message("Loaded legacy preference: " + key + " -> " + value); //$NON-NLS-1$ //$NON-NLS-2$
152
// call these 2 methods rather than #put() so we don't send out unnecessary notification
153
Object JavaDoc oldValue = internalPut(key, value);
154                 if (!value.equals(oldValue))
155                     makeDirty();
156             }
157         }
158
159         // Delete the old file so we don't try and load it next time.
160
if (!prefFile.delete())
161             //Only print out message in failure case if we are debugging.
162
if (EclipsePreferences.DEBUG_PREFERENCE_GENERAL)
163                 PrefsMessages.message("Unable to delete legacy preferences file: " + prefFile); //$NON-NLS-1$
164
}
165
166     protected IPath getLocation() {
167         if (location == null)
168             location = computeLocation(getBaseLocation(), qualifier);
169         return location;
170     }
171
172     /*
173      * Return the node at which these preferences are loaded/saved.
174      */

175     protected IEclipsePreferences getLoadLevel() {
176         if (loadLevel == null) {
177             if (qualifier == null)
178                 return null;
179             // Make it relative to this node rather than navigating to it from the root.
180
// Walk backwards up the tree starting at this node.
181
// This is important to avoid a chicken/egg thing on startup.
182
IEclipsePreferences node = this;
183             for (int i = 2; i < segmentCount; i++)
184                 node = (IEclipsePreferences) node.parent();
185             loadLevel = node;
186         }
187         return loadLevel;
188     }
189
190     /*
191      * Initialize the children for the root of this node. Store the names as
192      * keys in the children table so we can lazily load them later.
193      */

194     protected void initializeChildren() {
195         if (initialized || parent == null)
196             return;
197         try {
198             synchronized (this) {
199                 String JavaDoc[] names = computeChildren(getBaseLocation());
200                 for (int i = 0; i < names.length; i++)
201                     addChild(names[i], null);
202             }
203         } finally {
204             initialized = true;
205         }
206     }
207
208     protected EclipsePreferences internalCreate(EclipsePreferences nodeParent, String JavaDoc nodeName, Object JavaDoc context) {
209         return new InstancePreferences(nodeParent, nodeName);
210     }
211 }
212
Popular Tags