1 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 27 public class OSGiPreferencesServiceImpl implements PreferencesService { 28 29 34 private static final class OSGiLocalRootPreferences implements Preferences { 35 36 private Preferences root; 38 39 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 57 private String fixPath(String pathName) { 58 if (pathName.startsWith("/")) { 59 if (pathName.equals("/")) { 60 return root.absolutePath(); 61 } else { 62 return root.absolutePath().concat(pathName); 64 } 65 } else { 66 return pathName; 68 } 69 } 70 71 76 public Preferences node(String pathName) { 77 pathName = fixPath(pathName); 78 79 if ((pathName.length() > 1 && pathName.endsWith("/")) || pathName.indexOf("//") != -1) { throw new IllegalArgumentException (); 82 } 83 return new OSGiLocalRootPreferences(wrapped.node(pathName), root); 84 } 85 86 98 public byte[] getByteArray(String key, byte[] defaultValue) { 99 String 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 e) { 107 } 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 (); 119 } 120 } catch (BackingStoreException e) { 121 } 123 return null; 124 } else { 125 return new OSGiLocalRootPreferences(wrapped.parent(), root); 126 } 127 } 128 129 public boolean nodeExists(String pathName) throws BackingStoreException { 130 return wrapped.nodeExists(fixPath(pathName)); 131 } 132 133 public String 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 name() { 142 if (wrapped == root) { 143 return ""; 144 } else { 145 return wrapped.name(); 146 } 147 } 148 149 public void put(String key, String value) { 151 wrapped.put(key, value); 152 } 153 154 public String get(String key, String def) { 155 return wrapped.get(key, def); 156 } 157 158 public void remove(String key) { 159 wrapped.remove(key); 160 } 161 162 public void clear() throws BackingStoreException { 163 wrapped.clear(); 164 } 165 166 public void putInt(String key, int value) { 167 wrapped.putInt(key, value); 168 } 169 170 public int getInt(String key, int def) { 171 return wrapped.getInt(key, def); 172 } 173 174 public void putLong(String key, long value) { 175 wrapped.putLong(key, value); 176 } 177 178 public long getLong(String key, long def) { 179 return wrapped.getLong(key, def); 180 } 181 182 public void putBoolean(String key, boolean value) { 183 wrapped.putBoolean(key, value); 184 } 185 186 public boolean getBoolean(String key, boolean def) { 187 return wrapped.getBoolean(key, def); 188 } 189 190 public void putFloat(String key, float value) { 191 wrapped.putFloat(key, value); 192 } 193 194 public float getFloat(String key, float def) { 195 return wrapped.getFloat(key, def); 196 } 197 198 public void putDouble(String key, double value) { 199 wrapped.putDouble(key, value); 200 } 201 202 public double getDouble(String key, double def) { 203 return wrapped.getDouble(key, def); 204 } 205 206 public void putByteArray(String key, byte[] value) { 207 wrapped.putByteArray(key, value); 208 } 209 210 public String [] keys() throws BackingStoreException { 211 return wrapped.keys(); 212 } 213 214 public String [] 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 } 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 name) { 243 return new OSGiLocalRootPreferences(bundlePreferences.node("user/" + name)); 244 } 245 246 public String [] getUsers() { 247 String [] users = null; 248 try { 249 users = bundlePreferences.node("user").childrenNames(); 250 } catch (BackingStoreException e) { 251 } 253 return users == null ? new String [0] : users; 254 } 255 256 } 257 | Popular Tags |