The Real World

We finish this chapter up as we have finished our others: with a look at issues that affect using the tools in this chapter in a real-world situation. In this chapter, these include threading issues with writing XML data, alternatives to using JDOM for writing XML data, and handling lost references to the XmlRpcConfiguration utility class.

Threading, Writing, and Arithmetic

Although we sped through our look at the saveConfiguration( ) method and how it handled writing out XML, you may have noticed something key in the method declaration:

/**
 * <p>
 * This will save the current state out to the specified 
 *   <code>OutputStream</code>.
 * </p>
 *
 * @throws <code>IOException</code> - when errors occur in saving.
 */
public synchronized void saveConfiguration(OutputStream out)
    throws IOException {

    // Method implementation
}

We use the synchronized keyword here to ensure that the lock for our XmlRpcConfiguration object is obtained before the configuration data is written. This is particularly important when using XML, as APIs like JDOM can be built on implementations that periodically reload the underlying data; in other words, changes by other programs to the XML data could cause serious errors or corrupted data if this method writes to the data as well.

Additionally, you should be very careful when multiple applications write to the same XML data source. Entire database systems have been written to handle pessimistic and optimistic locking, sharing data, and the other complex ...

Get Java and XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.