KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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;
12
13 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
14 import org.osgi.service.prefs.*;
15 import org.osgi.service.prefs.PreferencesService;
16
17 /**
18  * <p>
19  * Implements OSGi PreferencesService using the Eclipse preference system.
20  * </p>
21  *
22  * <p>
23  * Note: Eclipse preferences are accessible through the OSGi Preferences API and vice
24  * versa.
25  * </p>
26  */

27 public class OSGiPreferencesServiceImpl implements PreferencesService {
28
29     /**
30      * Adaptor that implements OSGi Preferences interface on top of EclipsePreferences.
31      * Creates a "local root" since OSGi preferences have lots of roots but eclipse
32      * only has one.
33      */

34     private static final class OSGiLocalRootPreferences implements Preferences {
35
36         //The "local" root of this preference tree (not the real Eclipse root)
37
private Preferences root;
38
39         //the node this node is wrappering
40
private Preferences wrapped;
41
42         private OSGiLocalRootPreferences(Preferences root) {
43             this(root, root);
44         }
45
46         private OSGiLocalRootPreferences(Preferences wrapped, Preferences root) {
47             this.root = root;
48             this.wrapped = wrapped;
49         }
50
51         /**
52          * If pathName is absolute make it "absolute" with respect to this root.
53          * If pathName is relative, just return it
54          * @param pathName
55          * @return
56          */

57         private String JavaDoc fixPath(String JavaDoc pathName) {
58             if (pathName.startsWith("/")) {
59                 if (pathName.equals("/")) {
60                     return root.absolutePath();
61                 } else {
62                     //fix absolute path
63
return root.absolutePath().concat(pathName);
64                 }
65             } else {
66                 //pass-through relative path
67
return pathName;
68             }
69         }
70
71         /**
72          * Override node(String pathName) to be more strict about forbidden names -
73          * EclipsePreferences implementation does a best-effort instead of throwing
74          * {@link IllegalArgumentException}.
75          */

76         public Preferences node(String JavaDoc pathName) {
77             pathName = fixPath(pathName);
78
79             if ((pathName.length() > 1 && pathName.endsWith("/")) //$NON-NLS-1$
80
|| pathName.indexOf("//") != -1) { //$NON-NLS-1$
81
throw new IllegalArgumentException JavaDoc();
82             }
83             return new OSGiLocalRootPreferences(wrapped.node(pathName), root);
84         }
85
86         /**
87          * <p>
88          * Override getByteArray(String key, byte [] defaultValue) to be more strict when
89          * decoding byte values. EclipsePreferences implementation pads bytes if they are not 4
90          * bytes long, but the OSGi TCK expects this function to return null if the length of
91          * the byte array is not an even multiple of 4.
92          * </p>
93          * <p>
94          * Also catches any decoding exceptions and returns the default value instead of
95          * propagating the exception.
96          * </p>
97          */

98         public byte[] getByteArray(String JavaDoc key, byte[] defaultValue) {
99             String JavaDoc value = wrapped.get(key, null);
100             byte[] byteArray = null;
101             if (value != null) {
102                 byte[] encodedBytes = value.getBytes();
103                 if (encodedBytes.length % 4 == 0) {
104                     try {
105                         byteArray = Base64.decode(encodedBytes);
106                     } catch (Exception JavaDoc e) {
107                         //do not raise exception - return defaultValue
108
}
109                 }
110             }
111             return byteArray == null ? defaultValue : byteArray;
112         }
113
114         public Preferences parent() {
115             if (wrapped == root) {
116                 try {
117                     if (!wrapped.nodeExists("")) {
118                         throw new IllegalStateException JavaDoc();
119                     }
120                 } catch (BackingStoreException e) {
121                     //best effort
122
}
123                 return null;
124             } else {
125                 return new OSGiLocalRootPreferences(wrapped.parent(), root);
126             }
127         }
128
129         public boolean nodeExists(String JavaDoc pathName) throws BackingStoreException {
130             return wrapped.nodeExists(fixPath(pathName));
131         }
132
133         public String JavaDoc absolutePath() {
134             if (wrapped == root) {
135                 return "/";
136             } else {
137                 return wrapped.absolutePath().substring(root.absolutePath().length(), wrapped.absolutePath().length());
138             }
139         }
140
141         public String JavaDoc name() {
142             if (wrapped == root) {
143                 return "";
144             } else {
145                 return wrapped.name();
146             }
147         }
148
149         //delegate to wrapped preference
150
public void put(String JavaDoc key, String JavaDoc value) {
151             wrapped.put(key, value);
152         }
153
154         public String JavaDoc get(String JavaDoc key, String JavaDoc def) {
155             return wrapped.get(key, def);
156         }
157
158         public void remove(String JavaDoc key) {
159             wrapped.remove(key);
160         }
161
162         public void clear() throws BackingStoreException {
163             wrapped.clear();
164         }
165
166         public void putInt(String JavaDoc key, int value) {
167             wrapped.putInt(key, value);
168         }
169
170         public int getInt(String JavaDoc key, int def) {
171             return wrapped.getInt(key, def);
172         }
173
174         public void putLong(String JavaDoc key, long value) {
175             wrapped.putLong(key, value);
176         }
177
178         public long getLong(String JavaDoc key, long def) {
179             return wrapped.getLong(key, def);
180         }
181
182         public void putBoolean(String JavaDoc key, boolean value) {
183             wrapped.putBoolean(key, value);
184         }
185
186         public boolean getBoolean(String JavaDoc key, boolean def) {
187             return wrapped.getBoolean(key, def);
188         }
189
190         public void putFloat(String JavaDoc key, float value) {
191             wrapped.putFloat(key, value);
192         }
193
194         public float getFloat(String JavaDoc key, float def) {
195             return wrapped.getFloat(key, def);
196         }
197
198         public void putDouble(String JavaDoc key, double value) {
199             wrapped.putDouble(key, value);
200         }
201
202         public double getDouble(String JavaDoc key, double def) {
203             return wrapped.getDouble(key, def);
204         }
205
206         public void putByteArray(String JavaDoc key, byte[] value) {
207             wrapped.putByteArray(key, value);
208         }
209
210         public String JavaDoc[] keys() throws BackingStoreException {
211             return wrapped.keys();
212         }
213
214         public String JavaDoc[] childrenNames() throws BackingStoreException {
215             return wrapped.childrenNames();
216         }
217
218         public void removeNode() throws BackingStoreException {
219             wrapped.removeNode();
220         }
221
222         public void flush() throws BackingStoreException {
223             wrapped.flush();
224         }
225
226         public void sync() throws BackingStoreException {
227             wrapped.sync();
228         }
229
230     } //end static inner class OSGiLocalRootPreferences
231

232     private IEclipsePreferences bundlePreferences;
233
234     OSGiPreferencesServiceImpl(IEclipsePreferences bundlePreferences) {
235         this.bundlePreferences = bundlePreferences;
236     }
237
238     public Preferences getSystemPreferences() {
239         return new OSGiLocalRootPreferences(bundlePreferences.node("system"));
240     }
241
242     public Preferences getUserPreferences(String JavaDoc name) {
243         return new OSGiLocalRootPreferences(bundlePreferences.node("user/" + name));
244     }
245
246     public String JavaDoc[] getUsers() {
247         String JavaDoc[] users = null;
248         try {
249             users = bundlePreferences.node("user").childrenNames();
250         } catch (BackingStoreException e) {
251             //best effort
252
}
253         return users == null ? new String JavaDoc[0] : users;
254     }
255
256 }
257
Popular Tags