Working w/ Legacy Apps

strong.valid.simple Application Configuration

Strong typing, validation at startup, and semantic property names are nice features for new applications, but what about existing applications that are relying on finding specifically named properties?

AndHow can export property names and values to bridge the gap between legacy code and AndHow Properties. Below is an example that could be used for a legacy code expecting to find configuration in System.Properties.


import org.yarnandtail.andhow.*;

import static org.yarnandtail.andhow.api.Exporter.*;

import org.yarnandtail.andhow.export.SysPropExporter;


@GroupExport(

exporter=SysPropExporter.class,

exportByCanonicalName=EXPORT_CANONICAL_NAME.NEVER,

exportByOutAliases=EXPORT_OUT_ALIASES.ALWAYS

)

public interface ShoppingCartSvsConfig {

StrProp SERVICE_URL = StrProp.builder().mustEndWith("/").aliasInAndOut("cart.svs").build();

IntProp TIMEOUT = IntProp.builder().aliasInAndOut("cart.to").aliasOut("timeout").build();

StrProp QUERY_ENDPOINT = StrProp.builder().aliasInAndOut("cart.query").build();

StrProp ITEM_ENDPOINT = StrProp.builder().required().aliasInAndOut("cart.item").build();

}

The annotation in the example above specifies that as soon as the startup value loading is completed:

  • All the Properties contained directly in the class will be exported as a name-value pair to System.Properties at startup.

  • The property name used will be the 'out' alias, if the property has one, otherwise the canonical name of the Property is used

  • The value will be the validated value loaded by AndHow

Take care to ensure that AndHow is initialized before legacy code attempts to read exported valeus. This can be done by simply calling AndHow.instance(); at your code entry point, or reading the value of any AndHow Property.

Also note that only properties contained directly in the annotated class are exported: Properties in nested inner classes or interfaces are not included, though those inner classes could also be annotated.

In this exmple, each property is given an InAndOut alias matching the name that the legacy code is expectiving to find in the System.Properties. The 'out' portion of that specifies a name available for export. The 'in' portion is an added name that will be recognized when reading property values from a configuration source, like JNDI or a properties file. Aliases can be specified as 'in' or 'out' only as well if needed to avoid name collisions.

By using GroupExport and aliasInAndOut, a legacy application can be virtually unchanged and still benefit from strong typing, validation checks, multi-source loading and other features of AndHow.