AnsweredAssumed Answered

new File() returning old XML data for building block config

Question asked by andy.tumas on Jan 11, 2017
Latest reply on Jan 19, 2017 by shane

Hi,

 

We have a customer with multiple front end servers, when they update their config in one they need to perform rolling restarts on the others for the changes to take effect. The customer claims this is the first building building block with this issue. I tried to change the code so that when our block's main workhorse object is initialized it creates a new settings object and will use a DocumentBuilder to parse the config file specified by bb. We also save these changes using a similar method.

 

For some reason when I attempt to make a new settings object it looks like it is reading cached data, I honestly don't understand why the data is getting cached, some help from BB development would be greatly appreciated! I do not believe I am getting any errors, the logs appear to be empty.


The reading and saving does work, but we only read once at the top of the Utils.java file, I removed the 'final' keyword from our settings and set it to update before we used it, this revealed the caching issue to me.

 

Here is the reading\saving code, and the small changes I made to try and get it to update when needed:

 

 

Utils.java

(this used to be static final, I removed the final and added the below updatePluginSettings() function)

// Global settings for the plugin, applicable across all courses.

public static Settings pluginSettings = new Settings();

 

(added small function to create new settings object, this is called before we do any work with our block)

public static void updatePluginSettings() {

    pluginSettings = new Settings();

}

 

 

Settings.java

(constructor used in above function)

// Parse current settings from XML file in config directory.

public Settings() {

    try {

        // Get path to plugin config directory.

        File configDir = PlugInUtil.getConfigDirectory(Utils.vendorID, Utils.pluginHandle);

        File settingsFile = new File(configDir, "settings.xml");

 

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();

 

        if (settingsFile.exists()) {

            // Parse XML

            Document settingsDocument = db.parse(settingsFile);

 

            // Walk document tree and set corresponding setting values.

            readSettings(settingsDocument); (this works from what I see, but the data it receives is cached?)

        }

    } catch (Exception e) {

        // Utils has static reference to instance of Settings, so can't use Utils.log() here

        e.printStackTrace();

    }

}

 

(function used to save changes)

// Serialize current settings to XML settings file in config directory.

private void save() {

    try {

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();

        Document settingsDocument = db.newDocument();

 

      (I removed the huge chunk of code here, just goes over all settings and formats them)

 

        OutputFormat format = new OutputFormat(settingsDocument);

        format.setIndenting(true);

        format.setLineSeparator("\r\n");

 

        File configDir = PlugInUtil.getConfigDirectory(Utils.vendorID, Utils.pluginHandle);

        File settingsFile = new File(configDir, "settings.xml");

 

        FileOutputStream outStream = new FileOutputStream(settingsFile);

 

        // API is deprecated but still works, suppress warnings for clean build.

        XMLSerializer serializer = new XMLSerializer(outStream, format);

 

 

        // Serialize to XML.

        serializer.serialize(settingsDocument);

    } catch (Exception e) {

        Utils.log(e, "Error saving settings.");

    }

}

Outcomes