|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008 Innoopract Informationssysteme GmbH. |
| 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 |
* Innoopract Informationssysteme GmbH - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
package org.eclipse.rap.ui.internal.preferences; |
| 12 |
|
| 13 |
import java.util.*; |
| 14 |
|
| 15 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
| 16 |
import org.eclipse.core.runtime.preferences.IPreferenceNodeVisitor; |
| 17 |
import org.eclipse.osgi.util.NLS; |
| 18 |
import org.eclipse.rwt.RWT; |
| 19 |
import org.eclipse.rwt.internal.util.ParamCheck; |
| 20 |
import org.eclipse.rwt.service.ISettingStore; |
| 21 |
import org.eclipse.rwt.service.SettingStoreException; |
| 22 |
import org.eclipse.ui.internal.preferences.Base64; |
| 23 |
import org.osgi.service.prefs.BackingStoreException; |
| 24 |
import org.osgi.service.prefs.Preferences; |
| 25 |
|
| 26 |
/** |
| 27 |
* This node use the RWT setting store to persist its preferences. |
| 28 |
*/ |
| 29 |
final class SessionPreferencesNode implements IEclipsePreferences { |
| 30 |
|
| 31 |
private static final String PATH_SEPARATOR = "/"; //$NON-NLS-1$ |
| 32 |
private static final String DOUBLE_PATH_SEPARATOR = "//"; //$NON-NLS-1$ |
| 33 |
private static final String TRUE = "true"; //$NON-NLS-1$ |
| 34 |
private static final String FALSE = "false"; //$NON-NLS-1$ |
| 35 |
|
| 36 |
private final String name; |
| 37 |
private final IEclipsePreferences parent; |
| 38 |
private boolean isRemoved; |
| 39 |
/* cache the absolutePath once it has been computed */ |
| 40 |
private String absolutePath; |
| 41 |
|
| 42 |
private final Map children = new HashMap(); // !thread safe |
| 43 |
|
| 44 |
SessionPreferencesNode( final IEclipsePreferences parent, |
| 45 |
final String name ) |
| 46 |
{ |
| 47 |
ParamCheck.notNull( parent, "parent" ); //$NON-NLS-1$ |
| 48 |
ParamCheck.notNull( name, "name" ); //$NON-NLS-1$ |
| 49 |
checkName( name ); |
| 50 |
this.parent = parent; |
| 51 |
this.name = name; |
| 52 |
} |
| 53 |
|
| 54 |
public void accept( final IPreferenceNodeVisitor visitor ) |
| 55 |
throws BackingStoreException |
| 56 |
{ |
| 57 |
boolean withChildren = visitor.visit( this ); |
| 58 |
if( withChildren ) { |
| 59 |
Object[] childrenArray; |
| 60 |
synchronized( this ) { |
| 61 |
childrenArray = children.values().toArray(); |
| 62 |
} |
| 63 |
for( int i = 0; i < childrenArray.length; i++ ) { |
| 64 |
IEclipsePreferences child = ( IEclipsePreferences )childrenArray[ i ]; |
| 65 |
child.accept( visitor ); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public void addNodeChangeListener( final INodeChangeListener listener ) { |
| 71 |
checkRemoved(); |
| 72 |
if( listener != null ) { |
| 73 |
getNodeCore().addNodeChangeListener( listener ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public void addPreferenceChangeListener( |
| 78 |
final IPreferenceChangeListener listener ) |
| 79 |
{ |
| 80 |
checkRemoved(); |
| 81 |
getNodeCore().addPreferenceChangeListener( listener ); |
| 82 |
} |
| 83 |
|
| 84 |
public Preferences node( final String path ) { |
| 85 |
checkPath( path ); |
| 86 |
checkRemoved(); |
| 87 |
Preferences result; |
| 88 |
if( "".equals( path ) ) { // "" //$NON-NLS-1$ |
| 89 |
result = this; |
| 90 |
} else if( path.startsWith( PATH_SEPARATOR ) ) { // "/absolute/path" |
| 91 |
result = findRoot().node( path.substring( 1 ) ); |
| 92 |
} else if( path.indexOf( PATH_SEPARATOR ) > 0 ) { // "foo/bar/baz" |
| 93 |
int index = path.indexOf( PATH_SEPARATOR ); |
| 94 |
String nodeName = path.substring( 0, index ); |
| 95 |
String rest = path.substring( index + 1, path.length() ); |
| 96 |
result = getChild( nodeName, true ).node( rest ); |
| 97 |
} else { // "foo" |
| 98 |
result = getChild( path, true ); |
| 99 |
} |
| 100 |
return result; |
| 101 |
} |
| 102 |
|
| 103 |
public synchronized void removeNode() throws BackingStoreException { |
| 104 |
checkRemoved(); |
| 105 |
// remove all preferences |
| 106 |
clear(); |
| 107 |
// remove all children |
| 108 |
Object[] childNodes = children.values().toArray(); |
| 109 |
for( int i = 0; i < childNodes.length; i++ ) { |
| 110 |
Preferences child = ( Preferences )childNodes[ i ]; |
| 111 |
if( child.nodeExists( "" ) ) { // if !removed //$NON-NLS-1$ |
| 112 |
child.removeNode(); |
| 113 |
} |
| 114 |
} |
| 115 |
// remove from parent; this is ugly, because the interface |
| 116 |
// Preference has no API for removing oneself from the parent. |
| 117 |
// In general the parent will be a SessionPreferencesNode. |
| 118 |
// The only case in the workbench where this is not true, is one level |
| 119 |
// below the root (i.e. at /session ), but the scope root must not |
| 120 |
// be removable (see IEclipsePreferences#removeNode()) |
| 121 |
if( parent instanceof SessionPreferencesNode ) { |
| 122 |
// this means: |
| 123 |
// (a) we know what kind of parent we have, and |
| 124 |
// (b) we are not the scope root, since that has a |
| 125 |
/// RootPreference as a parent |
| 126 |
SessionPreferencesNode spnParent |
| 127 |
= ( ( SessionPreferencesNode ) parent ); |
| 128 |
spnParent.children.remove( name ); |
| 129 |
spnParent.fireNodeEvent( this, false ); |
| 130 |
|
| 131 |
// the listeners are not needed anymore |
| 132 |
getNodeCore().clear(); |
| 133 |
children.clear(); |
| 134 |
isRemoved = true; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public void removeNodeChangeListener( final INodeChangeListener listener ) { |
| 139 |
checkRemoved(); |
| 140 |
if( listener != null ) { |
| 141 |
getNodeCore().removeNodeChangeListener( listener ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
public void removePreferenceChangeListener( |
| 146 |
final IPreferenceChangeListener listener ) |
| 147 |
{ |
| 148 |
checkRemoved(); |
| 149 |
getNodeCore().removePreferenceChangeListener( listener ); |
| 150 |
} |
| 151 |
|
| 152 |
public String absolutePath() { |
| 153 |
if( absolutePath == null ) { |
| 154 |
if( parent == null ) { |
| 155 |
absolutePath = name; |
| 156 |
} else { |
| 157 |
String parentPath = parent.absolutePath(); |
| 158 |
absolutePath = parentPath.endsWith( PATH_SEPARATOR ) |
| 159 |
? parentPath + name |
| 160 |
: parentPath + PATH_SEPARATOR + name; |
| 161 |
} |
| 162 |
} |
| 163 |
return absolutePath; |
| 164 |
} |
| 165 |
|
| 166 |
public synchronized String[] childrenNames() { |
| 167 |
checkRemoved(); |
| 168 |
Set names = children.keySet(); |
| 169 |
return ( String[] )names.toArray( new String[ names.size() ] ); |
| 170 |
} |
| 171 |
|
| 172 |
public void clear() { |
| 173 |
checkRemoved(); |
| 174 |
String[] keys = internalGetKeys(); |
| 175 |
for( int i = 0; i < keys.length; i++ ) { |
| 176 |
remove( keys[ i ] ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
public void flush() { |
| 181 |
checkRemoved(); |
| 182 |
// the current implementation persists everytime the preferences |
| 183 |
// are modified, so there's nothing to do here |
| 184 |
} |
| 185 |
|
| 186 |
public String get( final String key, final String def ) { |
| 187 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 188 |
checkRemoved(); |
| 189 |
String result = internalGet( key ); |
| 190 |
return result == null ? def : result; |
| 191 |
} |
| 192 |
|
| 193 |
public boolean getBoolean( final String key, final boolean def ) { |
| 194 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 195 |
checkRemoved(); |
| 196 |
String value = internalGet( key ); |
| 197 |
return value == null ? def : Boolean.valueOf( value ).booleanValue(); |
| 198 |
} |
| 199 |
|
| 200 |
public byte[] getByteArray( final String key, final byte[] def ) { |
| 201 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 202 |
checkRemoved(); |
| 203 |
String value = internalGet( key ); |
| 204 |
return value == null ? def : Base64.decode( value.getBytes() ); |
| 205 |
} |
| 206 |
|
| 207 |
public double getDouble( final String key, final double def ) { |
| 208 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 209 |
checkRemoved(); |
| 210 |
String value = internalGet( key ); |
| 211 |
double result = def; |
| 212 |
if( value != null ) { |
| 213 |
try { |
| 214 |
result = Double.parseDouble( value ); |
| 215 |
} catch( NumberFormatException nfe ) { |
| 216 |
// returns def |
| 217 |
} |
| 218 |
} |
| 219 |
return result; |
| 220 |
} |
| 221 |
|
| 222 |
public float getFloat( final String key, final float def ) { |
| 223 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 224 |
checkRemoved(); |
| 225 |
String value = internalGet( key ); |
| 226 |
float result = def; |
| 227 |
if( value != null ) { |
| 228 |
try { |
| 229 |
result = Float.parseFloat( value ); |
| 230 |
} catch( NumberFormatException nfe ) { |
| 231 |
// returns def |
| 232 |
} |
| 233 |
} |
| 234 |
return result; |
| 235 |
} |
| 236 |
|
| 237 |
public int getInt( final String key, final int def ) { |
| 238 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 239 |
checkRemoved(); |
| 240 |
String value = internalGet( key ); |
| 241 |
int result = def; |
| 242 |
if( value != null ) { |
| 243 |
try { |
| 244 |
result = Integer.parseInt( value ); |
| 245 |
} catch( NumberFormatException nfe ) { |
| 246 |
// returns def |
| 247 |
} |
| 248 |
} |
| 249 |
return result; |
| 250 |
} |
| 251 |
|
| 252 |
public long getLong( final String key, final long def ) { |
| 253 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 254 |
checkRemoved(); |
| 255 |
String value = internalGet( key ); |
| 256 |
long result = def; |
| 257 |
if( value != null ) { |
| 258 |
try { |
| 259 |
result = Long.parseLong( value ); |
| 260 |
} catch( NumberFormatException nfe ) { |
| 261 |
// returns def |
| 262 |
} |
| 263 |
} |
| 264 |
return result; |
| 265 |
} |
| 266 |
|
| 267 |
public String[] keys() { |
| 268 |
checkRemoved(); |
| 269 |
return internalGetKeys(); |
| 270 |
} |
| 271 |
|
| 272 |
public String name() { |
| 273 |
return name; |
| 274 |
} |
| 275 |
|
| 276 |
public synchronized boolean nodeExists( final String path ) |
| 277 |
throws BackingStoreException |
| 278 |
{ |
| 279 |
boolean result; |
| 280 |
if( "".equals( path ) ) { //$NON-NLS-1$ |
| 281 |
result = !isRemoved; |
| 282 |
} else { |
| 283 |
checkRemoved(); |
| 284 |
checkPath( path ); |
| 285 |
if( path.startsWith( PATH_SEPARATOR ) ) { // "/absolute/path" |
| 286 |
result = findRoot().nodeExists( path.substring( 1 ) ); |
| 287 |
} else if( path.indexOf( PATH_SEPARATOR ) > 0 ) { // "foo/bar/baz" |
| 288 |
int index = path.indexOf( PATH_SEPARATOR ); |
| 289 |
String nodeName = path.substring( 0, index ); |
| 290 |
String rest = path.substring( index + 1, path.length() ); |
| 291 |
SessionPreferencesNode child = getChild( nodeName, false ); |
| 292 |
result = child == null ? false : child.nodeExists( rest ); |
| 293 |
} else { // "foo" |
| 294 |
result = children.containsKey( path ); |
| 295 |
} |
| 296 |
} |
| 297 |
return result; |
| 298 |
} |
| 299 |
|
| 300 |
public Preferences parent() { |
| 301 |
checkRemoved(); |
| 302 |
return parent; |
| 303 |
} |
| 304 |
|
| 305 |
public void put( final String key, final String newValue ) { |
| 306 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 307 |
ParamCheck.notNull( newValue, "newValue" ); //$NON-NLS-1$ |
| 308 |
checkRemoved(); |
| 309 |
String oldValue = internalPut( key, newValue ); |
| 310 |
if( !newValue.equals( oldValue ) ) { |
| 311 |
getNodeCore().firePreferenceEvent( key, oldValue, newValue ); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
public void putBoolean( final String key, final boolean value ) { |
| 316 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 317 |
checkRemoved(); |
| 318 |
String newValue = value ? TRUE : FALSE; |
| 319 |
String oldValue = internalPut( key, newValue ); |
| 320 |
if( !newValue.equals( oldValue ) ) { |
| 321 |
getNodeCore().firePreferenceEvent( key, oldValue, newValue ); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
public void putByteArray( final String key, final byte[] value ) { |
| 326 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 327 |
ParamCheck.notNull( value, "newValue" ); //$NON-NLS-1$ |
| 328 |
checkRemoved(); |
| 329 |
String newValue = new String( Base64.encode( value ) ); |
| 330 |
String oldValue = internalPut( key, newValue ); |
| 331 |
if( !newValue.equals( oldValue) ) { |
| 332 |
getNodeCore().firePreferenceEvent( key, oldValue, newValue ); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
public void putDouble( final String key, final double value ) { |
| 337 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 338 |
checkRemoved(); |
| 339 |
String newValue = String.valueOf( value ); |
| 340 |
String oldValue = internalPut( key, newValue ); |
| 341 |
if( !newValue.equals( oldValue ) ) { |
| 342 |
getNodeCore().firePreferenceEvent( key, oldValue, newValue ); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
public void putFloat( final String key, final float value ) { |
| 347 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 348 |
checkRemoved(); |
| 349 |
String newValue = String.valueOf( value ); |
| 350 |
String oldValue = internalPut( key, newValue ); |
| 351 |
if( !newValue.equals( oldValue ) ) { |
| 352 |
getNodeCore().firePreferenceEvent( key, oldValue, newValue ); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
public void putInt( final String key, final int value ) { |
| 357 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 358 |
checkRemoved(); |
| 359 |
String newValue = String.valueOf( value ); |
| 360 |
String oldValue = internalPut( key, newValue ); |
| 361 |
if( !newValue.equals( oldValue ) ) { |
| 362 |
getNodeCore().firePreferenceEvent( key, oldValue, newValue ); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
public void putLong( final String key, final long value ) { |
| 367 |
ParamCheck.notNull( key, "key" ); //$NON-NLS-1$ |
| 368 |
checkRemoved(); |
| 369 |
String newValue = String.valueOf( value ); |
| 370 |
String oldValue = internalPut( key, newValue ); |
| 371 |
if( !newValue.equals( oldValue ) ) { |
| 372 |
getNodeCore().firePreferenceEvent( key, oldValue, newValue ); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
public void remove( final String key ) { |
| 377 |
checkRemoved(); |
| 378 |
String oldValue = internalGet( key ); |
| 379 |
if( oldValue != null ) { |
| 380 |
internalPut( key, null ); |
| 381 |
getNodeCore().firePreferenceEvent( key, oldValue, null ); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
public void sync() throws BackingStoreException { |
| 386 |
checkRemoved(); |
| 387 |
ISettingStore store = RWT.getSettingStore(); |
| 388 |
String id = store.getId(); |
| 389 |
try { |
| 390 |
store.loadById( id ); |
| 391 |
} catch( SettingStoreException sse ) { |
| 392 |
throw new BackingStoreException( "Failed to sync() node", sse ); //$NON-NLS-1$ |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
public String toString() { |
| 397 |
return absolutePath() + "@" + hashCode(); //$NON-NLS-1$ |
| 398 |
} |
| 399 |
|
| 400 |
////////////////// |
| 401 |
// helping methods |
| 402 |
|
| 403 |
private void checkName( final String nodeName ) { |
| 404 |
if( nodeName.indexOf( PATH_SEPARATOR ) != -1 ) { |
| 405 |
String unboundMsg = "Name ''{0}'' cannot contain or end with ''{1}''"; //$NON-NLS-1$ |
| 406 |
String msg = NLS.bind( unboundMsg, nodeName, PATH_SEPARATOR ); |
| 407 |
throw new IllegalArgumentException( msg ); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
private void checkPath( final String path ) { |
| 412 |
if( path.indexOf( DOUBLE_PATH_SEPARATOR ) != -1 ) { |
| 413 |
String unboundMsg = "''{0}'' is not allowed in path ''{1}''"; //$NON-NLS-1$ |
| 414 |
String msg = NLS.bind( unboundMsg, DOUBLE_PATH_SEPARATOR, path ); |
| 415 |
throw new IllegalArgumentException( msg ); |
| 416 |
} |
| 417 |
if( path.length() > 1 && path.endsWith( PATH_SEPARATOR ) ) { |
| 418 |
String unboundMsg = "path ''{0}'' cannot end with ''{1}''"; //$NON-NLS-1$ |
| 419 |
String msg = NLS.bind( unboundMsg, path, PATH_SEPARATOR ); |
| 420 |
throw new IllegalArgumentException( msg ); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
private synchronized void checkRemoved() { |
| 425 |
if( isRemoved ) { |
| 426 |
String msg = "node ''{0}'' has been removed"; //$NON-NLS-1$ |
| 427 |
throw new IllegalStateException( NLS.bind( msg, this.absolutePath() ) ); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
private synchronized SessionPreferencesNode createChild( |
| 432 |
final String childName ) |
| 433 |
{ |
| 434 |
SessionPreferencesNode result |
| 435 |
= new SessionPreferencesNode( this, childName ); |
| 436 |
children.put( childName, result ); |
| 437 |
fireNodeEvent( result, true ); |
| 438 |
return result; |
| 439 |
} |
| 440 |
|
| 441 |
private synchronized SessionPreferencesNode getChild( |
| 442 |
final String childName, |
| 443 |
final boolean doCreate ) |
| 444 |
{ |
| 445 |
SessionPreferencesNode result |
| 446 |
= ( SessionPreferencesNode )children.get( childName ); |
| 447 |
if( result == null && doCreate ) { |
| 448 |
result = createChild( childName ); |
| 449 |
} |
| 450 |
return result; |
| 451 |
} |
| 452 |
|
| 453 |
private String[] internalGetKeys() { |
| 454 |
List result = new ArrayList(); |
| 455 |
|
| 456 |
String prefix = absolutePath() + PATH_SEPARATOR; |
| 457 |
int prefixLength = prefix.length(); |
| 458 |
|
| 459 |
Enumeration attrNames = RWT.getSettingStore().getAttributeNames(); |
| 460 |
while( attrNames.hasMoreElements() ) { |
| 461 |
String attr = ( String )attrNames.nextElement(); |
| 462 |
if( attr.startsWith( prefix ) ) { |
| 463 |
String key = attr.substring( prefixLength ); |
| 464 |
result.add( key ); |
| 465 |
} |
| 466 |
} |
| 467 |
return ( String[] )result.toArray( new String[ result.size() ] ); |
| 468 |
} |
| 469 |
|
| 470 |
private Preferences findRoot() { |
| 471 |
Preferences result = this; |
| 472 |
while( result.parent() != null ) { |
| 473 |
result = result.parent(); |
| 474 |
} |
| 475 |
return result; |
| 476 |
} |
| 477 |
|
| 478 |
private String internalGet( final String key ) { |
| 479 |
ISettingStore store = RWT.getSettingStore(); |
| 480 |
String uniqueKey = absolutePath() + PATH_SEPARATOR + key; |
| 481 |
return store.getAttribute( uniqueKey ); |
| 482 |
} |
| 483 |
|
| 484 |
private synchronized String internalPut( final String key, |
| 485 |
final String value ) { |
| 486 |
String uniqueKey = absolutePath() + PATH_SEPARATOR + key; |
| 487 |
return getNodeCore().put( uniqueKey, value ); |
| 488 |
} |
| 489 |
|
| 490 |
private void fireNodeEvent( final Preferences child, |
| 491 |
final boolean wasAdded ) { |
| 492 |
getNodeCore().fireNodeEvent( child, wasAdded, this ); |
| 493 |
} |
| 494 |
|
| 495 |
private SessionPreferenceNodeCore getNodeCore() { |
| 496 |
SessionPreferenceNodeCore result; |
| 497 |
final String key = absolutePath(); |
| 498 |
Object object = RWT.getSessionStore().getAttribute( key ); |
| 499 |
if( object instanceof SessionPreferenceNodeCore ) { |
| 500 |
result = ( SessionPreferenceNodeCore )object; |
| 501 |
} else { |
| 502 |
result = new SessionPreferenceNodeCore( this ); |
| 503 |
RWT.getSessionStore().setAttribute( key, result ); |
| 504 |
} |
| 505 |
return result; |
| 506 |
} |
| 507 |
|
| 508 |
} |