Components for each SDK version
The major versions of the SDK differ in their files, in their metadata and in their EFX. For example, SDK 1 describes its fields and nodes in one file and SDK 2 in two, and the view templates of SDK 2 use a new version of EFX. An application that displays notices of both versions needs different code for each, while the rest of the application stays the same.
You could test the version wherever the code differs, with if statements throughout the application. Every new
major version would then mean finding and changing all of them. Instead, the library lets you write one class for
each major version, and chooses the right one at run time from the version of the SDK. Supporting a new major version
then means adding classes, not changing existing ones. The EFX Toolkit and the library itself use this mechanism for
all their components.
Marking a class as a component
You write an interface, or a base class, for what all versions have in common. Then you write one implementation for
each major version, and mark it with the annotation @SdkComponent:
@SdkComponent(versions = {"1"}, componentType = SdkComponentType.MARKUP_GENERATOR)
public class HtmlGeneratorV1 implements HtmlGenerator {
public HtmlGeneratorV1(String language) {
// ...
}
}
@SdkComponent(versions = {"2"}, componentType = SdkComponentType.MARKUP_GENERATOR)
public class HtmlGeneratorV2 extends HtmlGeneratorV1 {
public HtmlGeneratorV2(String language) {
super(language);
}
}
The annotation has three parts:
versions-
The major versions of the SDK that the class serves, such as
"1"and"2". Write the major number alone: a class marked"2.0"is never found. One class can serve several major versions:{"1", "2"}. componentType-
What the class is, chosen from the enumeration
SdkComponentType, for exampleEFX_EXPRESSION_TRANSLATOR,SYMBOL_RESOLVER,SCRIPT_GENERATORorMARKUP_GENERATOR. The list is fixed: choose the type that is closest to your component. qualifier-
Optional. A name that tells apart several classes of the same type for the same major version, for example
"pdf"for a second markup generator that produces PDF instead of HTML.
For each major version, component type and qualifier, there must be exactly one class.
Creating the components
A factory finds the marked classes and creates instances of them. You write the factory as a subclass of SdkComponentFactory,
with one method for each of your component types. These methods call getComponentImpl, which chooses the class:
public class HtmlGeneratorFactory extends SdkComponentFactory {
private static final HtmlGeneratorFactory INSTANCE = new HtmlGeneratorFactory();
private HtmlGeneratorFactory() {
super();
}
public static HtmlGenerator getHtmlGenerator(String sdkVersion, String language)
throws InstantiationException {
return INSTANCE.getComponentImpl(sdkVersion, SdkComponentType.MARKUP_GENERATOR,
HtmlGenerator.class, language);
}
public static HtmlGenerator getHtmlGenerator(String sdkVersion, String qualifier, String language)
throws InstantiationException {
return INSTANCE.getComponentImpl(sdkVersion, SdkComponentType.MARKUP_GENERATOR, qualifier,
HtmlGenerator.class, language);
}
}
The application then asks for a component with the version of the SDK, for example the version of the notice that it displays:
HtmlGenerator generator = HtmlGeneratorFactory.getHtmlGenerator("2.0.0-alpha.6", "en");
The factory reduces the version to its major number, here 2, and creates an instance of the class marked for that
major version, here HtmlGeneratorV2. The arguments after the interface, here "en", are passed to the constructor of
the class. Each class of the component type needs a constructor that accepts the arguments that your factory passes.
Exactly one constructor must accept them: when several do, the factory throws an IllegalStateException.
The factory looks for the marked classes the first time a factory is created. It searches the whole class path once, for all factories, so that creating more factories later costs little.
When a component cannot be created
- No class for the version, type or qualifier
-
getComponentImplthrows anIllegalArgumentException, for example "No implementation found for SDK [3.0], component type [MARKUP_GENERATOR] and qualifier []". - No constructor for the arguments
-
getComponentImplthrows anInstantiationException, for example when the arguments are of other types than the parameters of the constructor. - Two classes for the same version, type and qualifier
-
Every factory fails when it is created, and the error names the two classes: "More than one components of type [MARKUP_GENERATOR] have been found for SDK version [2]". This affects all factories in the application, including those of the EFX Toolkit, because each factory registers every marked class on the class path. If your class has the same component type and major version as a component of the EFX Toolkit, give it a qualifier. For an example, see Implementing ScriptGenerator.
The annotation @SdkComponent is inherited. A subclass without an annotation of its own takes the same versions,
component type and qualifier as its parent class, so the two classes conflict. Give such a subclass its own annotation, with another version or another
qualifier, as HtmlGeneratorV2 above does.
See also: Assembling and running a translator, which shows how the EFX Toolkit uses its factories.