You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2007/07/16 15:25:45 UTC
svn commit: r556621 - in
/felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences:
PreferencesImpl.java impl/DataFileBackingStoreImpl.java
impl/StreamBackingStoreImpl.java
Author: cziegeler
Date: Mon Jul 16 06:25:44 2007
New Revision: 556621
URL: http://svn.apache.org/viewvc?view=rev&rev=556621
Log:
Implement synchronization of preferences.
Modified:
felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/PreferencesImpl.java
felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/impl/DataFileBackingStoreImpl.java
felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/impl/StreamBackingStoreImpl.java
Modified: felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/PreferencesImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/PreferencesImpl.java?view=diff&rev=556621&r1=556620&r2=556621
==============================================================================
--- felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/PreferencesImpl.java (original)
+++ felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/PreferencesImpl.java Mon Jul 16 06:25:44 2007
@@ -421,11 +421,35 @@
}
/**
+ * Get or create the node.
+ * If the node already exists, it's just returned. If not
+ * it is created.
+ * @param pathName
+ * @return
+ */
+ public PreferencesImpl getOrCreateNode(String pathName) {
+ if ( pathName == null ) {
+ throw new NullPointerException("Path must not be null.");
+ }
+ PreferencesImpl executingNode= this;
+ if ( pathName.length() == 0 ) {
+ return this;
+ }
+ if ( pathName.startsWith("/") && this.parent != null ) {
+ executingNode = this.getRoot();
+ }
+ if ( pathName.startsWith("/") ) {
+ pathName = pathName.substring(1);
+ }
+ return executingNode.getNode(pathName, false, true);
+ }
+
+ /**
* Get a relative node.
* @param path
* @return
*/
- protected Preferences getNode(String path, boolean saveNewlyCreatedNode, boolean create) {
+ protected PreferencesImpl getNode(String path, boolean saveNewlyCreatedNode, boolean create) {
if ( path.startsWith("/") ) {
throw new IllegalArgumentException("Path must not contained consecutive slashes");
}
@@ -455,7 +479,7 @@
}
saveNewlyCreatedNode = false;
}
- final Preferences result;
+ final PreferencesImpl result;
if ( subPath == null ) {
result = child;
} else {
@@ -598,6 +622,41 @@
this.node(name);
}
((PreferencesImpl)this.children.get(name)).update(child);
+ }
+ }
+
+ /***
+ * Apply the changes done to the passed preferences object.
+ * @param prefs
+ */
+ public void applyChanges(PreferencesImpl prefs) {
+ final ChangeSet changeSet = prefs.getChangeSet();
+ if ( changeSet.hasChanges ) {
+ Iterator i;
+ // remove properties
+ i = changeSet.removedProperties.iterator();
+ while ( i.hasNext() ) {
+ this.properties.remove(i.next());
+ }
+ // set/update properties
+ i = changeSet.changedProperties.iterator();
+ while ( i.hasNext() ) {
+ final String key = (String)i.next();
+ this.properties.put(key, prefs.properties.get(key));
+ }
+ // remove children
+ i = changeSet.removedChildren.iterator();
+ while ( i.hasNext() ) {
+ final String name = (String)i.next();
+ this.children.remove(name);
+ }
+ // added childs are processed in the next loop
+ }
+ final Iterator cI = prefs.getChildren().iterator();
+ while ( cI.hasNext() ) {
+ final PreferencesImpl current = (PreferencesImpl)cI.next();
+ final PreferencesImpl child = this.getOrCreateNode(current.name());
+ child.applyChanges(current);
}
}
Modified: felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/impl/DataFileBackingStoreImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/impl/DataFileBackingStoreImpl.java?view=diff&rev=556621&r1=556620&r2=556621
==============================================================================
--- felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/impl/DataFileBackingStoreImpl.java (original)
+++ felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/impl/DataFileBackingStoreImpl.java Mon Jul 16 06:25:44 2007
@@ -176,20 +176,6 @@
}
/**
- * @see org.apache.felix.sandbox.preferences.BackingStore#update(org.apache.felix.sandbox.preferences.PreferencesImpl)
- */
- public void update(PreferencesImpl prefs) throws BackingStoreException {
- final PreferencesImpl root = this.load(prefs.getDescription());
- if ( root != null ) {
- // and now update
- if ( root.nodeExists(prefs.absolutePath()) ) {
- final PreferencesImpl updated = (PreferencesImpl)root.node(prefs.absolutePath());
- prefs.update(updated);
- }
- }
- }
-
- /**
* Get the file fo the preferences tree.
* @param desc
* @return
Modified: felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/impl/StreamBackingStoreImpl.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/impl/StreamBackingStoreImpl.java?view=diff&rev=556621&r1=556620&r2=556621
==============================================================================
--- felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/impl/StreamBackingStoreImpl.java (original)
+++ felix/sandbox/cziegeler/preferences/src/main/java/org/apache/felix/sandbox/preferences/impl/StreamBackingStoreImpl.java Mon Jul 16 06:25:44 2007
@@ -67,6 +67,14 @@
return;
}
this.checkAccess();
+ // load existing data
+ final PreferencesImpl savedData = this.load(prefs.getDescription());
+ if ( savedData != null ) {
+ // merge with saved version
+ final PreferencesImpl n = savedData.getOrCreateNode(prefs.absolutePath());
+ n.applyChanges(prefs);
+ prefs = savedData;
+ }
final PreferencesImpl root = prefs.getRoot();
try {
final OutputStream os = this.getOutputStream(root.getDescription());
@@ -92,6 +100,20 @@
}
}
return false;
+ }
+
+ /**
+ * @see org.apache.felix.sandbox.preferences.BackingStore#update(org.apache.felix.sandbox.preferences.PreferencesImpl)
+ */
+ public void update(PreferencesImpl prefs) throws BackingStoreException {
+ final PreferencesImpl root = this.load(prefs.getDescription());
+ if ( root != null ) {
+ // and now update
+ if ( root.nodeExists(prefs.absolutePath()) ) {
+ final PreferencesImpl updated = (PreferencesImpl)root.node(prefs.absolutePath());
+ prefs.update(updated);
+ }
+ }
}
/**