Skip to content

Add support for ingesting stored configurations in XML and INI format #45

@sbabcoc

Description

@sbabcoc

Remote Session is intended to simplify the process of interacting with remote hosts easier. Storing host-specific settings in what are essentially global variables makes interacting with multiple remote hosts simultaneously messy, to say the least.

Using System properties to store host-specific settings makes simultaneous interaction with multiple hosts challenging. The only real options at this point are to define multiple sets of properties (not scalable) or expect JSON-format values (error-prone).

To improve support for settings with complex values, I'm going to support content in three different formats:

  • XML format
  • INI format
  • Properties (currently the only option)

These will be ingested using a "try-with-fallback" strategy. Because I support provision of the content via InputStream, I'll need to be able to rewind the stream before falling back to the next format in the even of an initialization failure.

public Configuration ingest(InputStream rawStream) throws IOException {
    // 1. Wrap the stream to support re-reading
    BufferedInputStream bis = new BufferedInputStream(rawStream);
    // Use a large enough buffer to hold the entire config file
    bis.mark(1024 * 1024); // 1MB mark limit

    // 2. Try XML First
    try {
        XMLConfiguration xmlConfig = new XMLConfiguration();
        new FileHandler(xmlConfig).load(bis);
        return xmlConfig;
    } catch (ConfigurationException e) {
        bis.reset(); // Rewind for the next attempt
    }

    // 3. Try INI Second
    try {
        INIConfiguration iniConfig = new INIConfiguration();
        new FileHandler(iniConfig).load(bis);
        return iniConfig;
    } catch (ConfigurationException e) {
        bis.reset(); // Rewind for the final fallback
    }

    // 4. Fallback to Properties
    try {
        PropertiesConfiguration propConfig = new PropertiesConfiguration();
        new FileHandler(propConfig).load(bis);
        return propConfig;
    } catch (ConfigurationException e) {
        throw new IOException("Unable to parse stream as XML, INI, or Properties", e);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions