Author: nextgens
Date: 2006-09-24 10:44:38 +0000 (Sun, 24 Sep 2006)
New Revision: 10506

Added:
   trunk/freenet/src/freenet/config/FreenetFilePersistentConfig.java
Modified:
   trunk/freenet/src/freenet/config/FilePersistentConfig.java
   trunk/freenet/src/freenet/node/Node.java
   trunk/freenet/src/freenet/node/NodeStarter.java
Log:
Create a FreenetFilePersistentConfig class so that our config. framework is 
independant from a Node object

Modified: trunk/freenet/src/freenet/config/FilePersistentConfig.java
===================================================================
--- trunk/freenet/src/freenet/config/FilePersistentConfig.java  2006-09-24 
01:02:49 UTC (rev 10505)
+++ trunk/freenet/src/freenet/config/FilePersistentConfig.java  2006-09-24 
10:44:38 UTC (rev 10506)
@@ -11,7 +11,6 @@
 import java.io.OutputStreamWriter;
 import java.util.Iterator;

-import freenet.node.Node;
 import freenet.support.Logger;
 import freenet.support.SimpleFieldSet;
 import freenet.support.io.LineReadingInputStream;
@@ -30,10 +29,8 @@
        final File filename;
        final File tempFilename;
        private SimpleFieldSet origConfigFileContents;
-       private boolean finishedInit;
-       private final Object storeSync = new Object();
-       private boolean isWritingConfig = false;
-       private Node node;
+       protected boolean finishedInit;
+       protected final Object storeSync = new Object();

        public FilePersistentConfig(File f) throws IOException {
                this.filename = f;
@@ -82,7 +79,7 @@

        /** Load the config file into a SimpleFieldSet. 
         * @throws IOException */
-       private void initialLoad(File toRead) throws IOException {
+       protected void initialLoad(File toRead) throws IOException {
                FileInputStream fis = new FileInputStream(toRead);
                BufferedInputStream bis = new BufferedInputStream(fis);
                try {
@@ -126,37 +123,20 @@
                                return;
                        }
                }
-               synchronized(storeSync) {
-                       if(isWritingConfig || node == null){
-                               Logger.normal(this, "Already writing the config 
file to disk or the node object hasn't been set : refusing to proceed");
-                               return;
+               try {
+                       synchronized(storeSync) {
+                               innerStore();
                        }
-                       isWritingConfig = true;
-
-                       node.ps.queueTimedJob(new Runnable() {
-                               public void run() {
-                                       try{
-                                               while(!node.isHasStarted())
-                                                       Thread.sleep(1000);
-                                       }catch (InterruptedException e) {}
-                                       try {
-                                               innerStore();
-                                       } catch (IOException e) {
-                                               String err = "Cannot store 
config: "+e;
-                                               Logger.error(this, err, e);
-                                               System.err.println(err);
-                                               e.printStackTrace();
-                                       }
-                                       synchronized (storeSync) {
-                                               isWritingConfig = false;
-                                       }
-                               }
-                       }, 0);
+               } catch (IOException e) {
+                       String err = "Cannot store config: "+e;
+                       Logger.error(this, err, e);
+                       System.err.println(err);
+                       e.printStackTrace();
                }
        }

        /** Don't call without taking storeSync first */
-       private void innerStore() throws IOException {
+       protected void innerStore() throws IOException {
                SimpleFieldSet fs = exportFieldSet();
                if(Logger.shouldLog(Logger.MINOR, this))
                        Logger.minor(this, "fs = "+fs);
@@ -217,12 +197,4 @@
                        Logger.error(this, "Could not parse config option 
"+name+": "+e, e);
                }
        }
-       
-       public void setNode(Node n){
-               if(node != null){
-                       Logger.error(this, "The node object has already been 
initialized! it's likely to be a bug.");
-                       return;
-               }
-               this.node = n;
-       }
 }

Added: trunk/freenet/src/freenet/config/FreenetFilePersistentConfig.java
===================================================================
--- trunk/freenet/src/freenet/config/FreenetFilePersistentConfig.java   
2006-09-24 01:02:49 UTC (rev 10505)
+++ trunk/freenet/src/freenet/config/FreenetFilePersistentConfig.java   
2006-09-24 10:44:38 UTC (rev 10506)
@@ -0,0 +1,61 @@
+package freenet.config;
+
+import java.io.File;
+import java.io.IOException;
+
+import freenet.node.Node;
+import freenet.support.Logger;
+
+public class FreenetFilePersistentConfig extends FilePersistentConfig {
+
+       private Node node;
+       private boolean isWritingConfig = false;
+       
+       public FreenetFilePersistentConfig(File f) throws IOException {
+               super(f);
+       }
+
+       public void store() {
+               synchronized(this) {
+                       if(!finishedInit) {
+                               Logger.error(this, "Initialization not 
finished, refusing to write config", new Exception("error"));
+                               return;
+                       }
+               }
+               synchronized(storeSync) {
+                       if(isWritingConfig || node == null){
+                               Logger.normal(this, "Already writing the config 
file to disk or the node object hasn't been set : refusing to proceed");
+                               return;
+                       }
+                       isWritingConfig = true;
+
+                       node.ps.queueTimedJob(new Runnable() {
+                               public void run() {
+                                       try{
+                                               while(!node.isHasStarted())
+                                                       Thread.sleep(1000);
+                                       }catch (InterruptedException e) {}
+                                       try {
+                                               innerStore();
+                                       } catch (IOException e) {
+                                               String err = "Cannot store 
config: "+e;
+                                               Logger.error(this, err, e);
+                                               System.err.println(err);
+                                               e.printStackTrace();
+                                       }
+                                       synchronized (storeSync) {
+                                               isWritingConfig = false;
+                                       }
+                               }
+                       }, 0);
+               }
+       }
+       
+       public void setNode(Node n){
+               if(node != null){
+                       Logger.error(this, "The node object has already been 
initialized! it's likely to be a bug.");
+                       return;
+               }
+               this.node = n;
+       }
+}

Modified: trunk/freenet/src/freenet/node/Node.java
===================================================================
--- trunk/freenet/src/freenet/node/Node.java    2006-09-24 01:02:49 UTC (rev 
10505)
+++ trunk/freenet/src/freenet/node/Node.java    2006-09-24 10:44:38 UTC (rev 
10506)
@@ -33,7 +33,7 @@
 import com.sleepycat.je.DatabaseException;

 import freenet.client.FetcherContext;
-import freenet.config.FilePersistentConfig;
+import freenet.config.FreenetFilePersistentConfig;
 import freenet.config.IntCallback;
 import freenet.config.InvalidConfigValueException;
 import freenet.config.LongCallback;
@@ -157,7 +157,7 @@
        }

        /** Config object for the whole node. */
-       public final FilePersistentConfig config;
+       public final FreenetFilePersistentConfig config;

        // Static stuff related to logger

@@ -681,7 +681,7 @@
         * @param the loggingHandler
         * @throws NodeInitException If the node initialization fails.
         */
-        Node(FilePersistentConfig config, RandomSource random, 
LoggingConfigHandler lc, NodeStarter ns) throws NodeInitException {
+        Node(FreenetFilePersistentConfig config, RandomSource random, 
LoggingConfigHandler lc, NodeStarter ns) throws NodeInitException {
                // Easy stuff
                logMINOR = Logger.shouldLog(Logger.MINOR, this);
                Logger.normal(this, "Initializing Node using SVN 
r"+Version.cvsRevision+" and freenet-ext r"+NodeStarter.extRevisionNumber);

Modified: trunk/freenet/src/freenet/node/NodeStarter.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeStarter.java     2006-09-24 01:02:49 UTC 
(rev 10505)
+++ trunk/freenet/src/freenet/node/NodeStarter.java     2006-09-24 10:44:38 UTC 
(rev 10506)
@@ -6,7 +6,7 @@
 import org.tanukisoftware.wrapper.WrapperManager;
 import org.tanukisoftware.wrapper.WrapperListener;

-import freenet.config.FilePersistentConfig;
+import freenet.config.FreenetFilePersistentConfig;
 import freenet.config.InvalidConfigValueException;
 import freenet.config.SubConfig;
 import freenet.crypt.DiffieHellman;
@@ -34,7 +34,7 @@
                
(System.getProperty("os.arch").toLowerCase().matches("(i?[x0-9]86_64|amd64)")) 
? 6 : 2;
        public static int extBuildNumber;
        public static String extRevisionNumber;
-       private FilePersistentConfig cfg;
+       private FreenetFilePersistentConfig cfg;

     /*---------------------------------------------------------------
      * Constructors
@@ -80,7 +80,7 @@
        java.security.Security.setProperty("networkaddress.cache.negative.ttl" 
, "0");

        try{
-               cfg = new FilePersistentConfig(configFilename); 
+               cfg = new FreenetFilePersistentConfig(configFilename);  
        }catch(IOException e){
                System.out.println("Error : "+e);
                e.printStackTrace();


Reply via email to