Creating New Config Class
When porting an existing DTK agent, using a configuration contract XML file, you will need to add code to be able to read old configurations otherwise importing any old system export will leave the agent with empty configuration.
This is done by overriding the "readFrom" method in the configuration class and use the "readGenFieldValue" help method to read all field values from the old configuration.
Make sure that you have set your version on the new configuration class such that this will be different from the old version.
See the below example code using the Disk Forwarding agent from the DTK examples
Old Configuration Contract
<?xml version="1.0" encoding="UTF-8"?>
<contract mz-version='1.0'>
<class-name>DiskForwardingConfig</class-name>
<package-name>com.digitalroute.devkit.examples.diskforwarding</package-name>
<storable-id>devkitexamples.DiskForwardingConfig</storable-id>
<section id='Disk Forwarding'>
<field id='directory'>
<title>Directory</title>
<name>Directory</name>
<description>The path to the source directory</description>
<type><object-type name='java.lang.String'/></type>
<default-value>""</default-value>
<validation>
<validate minLength='1' message='Directory may not be empty'/>
</validation>
</field>
</section>
</contract>
New Configuration Class
package com.digitalroute.devkit.examples.diskforwarding;
import com.digitalroute.devkit.config.DRAbstractConfigObject;
import com.digitalroute.devkit.config.annotation.DRConfigFieldInfo;
import com.digitalroute.devkit.config.annotation.DRConfigTypeInfo;
import com.digitalroute.devkit.exception.DRException;
import com.digitalroute.devkit.misc.DREnvironment;
import com.digitalroute.devkit.storable.DRInputStream;
import com.digitalroute.devkit.storable.DRStorableId;
// Compared to the old XML config definition from version 8, we are here
// using the same DRStorableId as well as a higher version.
@DRStorableId("devkitexamples.DiskForwardingConfig")
@DRConfigTypeInfo(version = 10f, section = "Disk Forwarding")
public class DiskForwardingConfig extends DRAbstractConfigObject {
public static final String DIRECTORY = "Directory";
private String _directory;
public DiskForwardingConfig() {}
@DRConfigFieldInfo(title = DIRECTORY, description = "The path to the target directory")
public void setDirectory(String directory) { _directory = directory; }
public String getDirectory() { return _directory; }
@Override
public String validateField(String fieldName, Object fieldValue, DREnvironment env) throws DRException {
if (fieldName.equalsIgnoreCase("directory")) {
if (fieldValue == null || ((String) fieldValue).length() < 1)
return "Directory may not be empty";
}
return null;
}
// To just be able to read configurations from the current configuration
// class framework, there is no need to implement the readFrom method.
// However, to be able to also read configurations from version 8
// configurations, this is necessary.
@Override
public void readFrom(DRInputStream in, float version) throws DRException {
if (version >= 10.0f) {
super.readFrom(in, version);
} else {
// Read earlier configuration versions (from version 8 agent version)
// The label used is the "name" element from the XML
_directory = readGenFieldValue(in, "Directory", null);
}
}
}