The AgilEntity framework has a db vendor agnostic sub api that allows the client code for the platform to interact with the clusters loaded database without knowing the specific vendor type of the system in place. This enables the client programmers to write standard ANSI compliant SQL to perform calls on the the various tables added to the datastore. The design of the datastore promotes a relational model between Entities that are managed in an Entity relations table. Other Entities have specific mapping relationships as well but all db access occurs through the API. This is done using an abstraction of the db accessing methods through a class called CreateTable. The CreateTable base class , which is abstract as defined using the java OO syntax is extended by sub classes that implement the specific functionality that is required to mediate specific vendor databases. The base class enforces implemenations for methods that perform several of the following functions that tend to be database variable.
These functions will vary depending on the vendor in use. For example, the date time format defaults vary significantly between mysql , mssql server and oracle. By building the change into a method in the db vendor class and calling those polymorphically we get automatic db agnostic operation. Some of the methods are purely abstract , others are abstract and static these methods allow invoking subclass specific implemenations without instantiating instances of the subclass objects. This is done using the static polymorphism pattern that I describe below:
The java API allows deep control over how methods and classes are called, it also allows us to define how the jvm loads up class path locations for reference of class files. This class loading mechanism is the power behind dynamic run time extension capabilities of many java application servers. These abilities are part of what java calls the reflection API. Static polymorphism uses the Class class to instantiate an object from its FQCN (fully qualified class name). This allows static polymorphism to proceed by having the a concrete static base class method call an implementation of a FQCN named subclass of that base class hieararchy...thus effecting a polymorphic call on a static method.
1) Define the base class of your inheritance hieararchy. In my case it was the "CreateTable" class which was defined as a public and abstract class (to allow both concrete and abstract implemenation signatures which is important to enable the static polymorphism.)
public abstract class CreateTable {
//empty arg abstract method goes here ...see below.
}
2) define the empty arg. abstract method signature for the method we require to override via polymorphism
in inheriting classes. This should be public or protected so that the subclasses can see it and thus be constrained to provide implemenations. The reason the argument is empty is that we just want to extract the db specific version of the function being requested. In this case the db specific default time pattern. We have the method name but we now need a way to statically select the class which is invoking that method. That is where the abstract FQCN argument method comes in as shown in 3.
public abstract class CreateTable {
public abstract String timePattern() ;
//static FQCN argument method with string argument goes below...
}
3) The implemented string argument version of timePattern() is shown. The green call to a method "getVendorInstance(fqcn)" is where the reflection happens as shown in the method implemenation.
public abstract class CreateTable {
public abstract String timePattern() ;
public static String timePattern(String fqcn) {
try {
//Extract in instance of the currently loaded derived createTable class.
CreateTable vinny = this.getVendorInstance(fqcn);
return vinny.timePattern();
} catch (GVIOException efio) {
throw new EntegraDBAccessException("IO Error from CreateTable static insertForVendorDB method.",efio);
}
}
}
public static CreateTable getVendorInstance(String vndrfqcn) {
//From the grouptype fqcn string generate a Class object.
Class childclass = Class.forName(vndrfqcn);
//From the class object generate a new instance of the class specified by the fqcn in object form.
java.lang.Object childobject = childclass.newInstance();
//caste object to its known basetype and return
return (CreateTable)childobject;
}
In my framework, the call to "Class.forName(..)" is abstracted further to a custom class loader, this class loader overrides the ClassLoader class of the java API to allow custom loading of packages from specific paths so that the framework can dynamically load classes during run time. This is how the framework enables run time extensibility just like standard java web application servers do.
4) The final step is to create a subclass of CreateTable and implement the forced implemenation of the abstract method signature for the no argument version of "timePattern()" It is this version of the method that is called when the corresponding "fqcn" is provided in the string argument method of the same name. Because the object is instanced from the class name at run time (when the name is specified) a dynamic selection of the code (polymorphism) is realized despite the call being made on a static method. In my framework the "fqcn" string comes from the system bootstrap configuration class DPSConfig which bootstraps from settings indicated in an xml configuration file. The file is managable by the UI , so changing back end db's is trivially performed without requiring down time on any of the nodes in the cluster. As well the desire to call necessary db vendor agnostic methods is achieved from all client code that utilizes the static method and passes in the "fqcn" from the system bootstrap settings without knowledge or care of which db vendor class it happens to be. Below I provide an example for a hypothetical subclass with customization for the DB2 DBMS.
public class CreateTableDB2 extends CreateTable {
public String timePattern() {
return " hh:mi:ss ";
}
}
Using static polymorphism has allowed me to get around the sticky problem of calling vendor specific methods in a static way without instancing the subclass that currently maps to the db in use on the system and is straight forward to implement, you might find it useful in your designs.
- date/time conversion and formatting
- editColumn
- addRow
- createTable
- select for Vendor
These functions will vary depending on the vendor in use. For example, the date time format defaults vary significantly between mysql , mssql server and oracle. By building the change into a method in the db vendor class and calling those polymorphically we get automatic db agnostic operation. Some of the methods are purely abstract , others are abstract and static these methods allow invoking subclass specific implemenations without instantiating instances of the subclass objects. This is done using the static polymorphism pattern that I describe below:
The java API allows deep control over how methods and classes are called, it also allows us to define how the jvm loads up class path locations for reference of class files. This class loading mechanism is the power behind dynamic run time extension capabilities of many java application servers. These abilities are part of what java calls the reflection API. Static polymorphism uses the Class class to instantiate an object from its FQCN (fully qualified class name). This allows static polymorphism to proceed by having the a concrete static base class method call an implementation of a FQCN named subclass of that base class hieararchy...thus effecting a polymorphic call on a static method.
1) Define the base class of your inheritance hieararchy. In my case it was the "CreateTable" class which was defined as a public and abstract class (to allow both concrete and abstract implemenation signatures which is important to enable the static polymorphism.)
public abstract class CreateTable {
//empty arg abstract method goes here ...see below.
}
2) define the empty arg. abstract method signature for the method we require to override via polymorphism
in inheriting classes. This should be public or protected so that the subclasses can see it and thus be constrained to provide implemenations. The reason the argument is empty is that we just want to extract the db specific version of the function being requested. In this case the db specific default time pattern. We have the method name but we now need a way to statically select the class which is invoking that method. That is where the abstract FQCN argument method comes in as shown in 3.
public abstract class CreateTable {
public abstract String timePattern() ;
//static FQCN argument method with string argument goes below...
}
3) The implemented string argument version of timePattern() is shown. The green call to a method "getVendorInstance(fqcn)" is where the reflection happens as shown in the method implemenation.
public abstract class CreateTable {
public abstract String timePattern() ;
public static String timePattern(String fqcn) {
try {
//Extract in instance of the currently loaded derived createTable class.
CreateTable vinny = this.getVendorInstance(fqcn);
return vinny.timePattern();
} catch (GVIOException efio) {
throw new EntegraDBAccessException("IO Error from CreateTable static insertForVendorDB method.",efio);
}
}
}
public static CreateTable getVendorInstance(String vndrfqcn) {
//From the grouptype fqcn string generate a Class object.
Class childclass = Class.forName(vndrfqcn);
//From the class object generate a new instance of the class specified by the fqcn in object form.
java.lang.Object childobject = childclass.newInstance();
//caste object to its known basetype and return
return (CreateTable)childobject;
}
In my framework, the call to "Class.forName(..)" is abstracted further to a custom class loader, this class loader overrides the ClassLoader class of the java API to allow custom loading of packages from specific paths so that the framework can dynamically load classes during run time. This is how the framework enables run time extensibility just like standard java web application servers do.
4) The final step is to create a subclass of CreateTable and implement the forced implemenation of the abstract method signature for the no argument version of "timePattern()" It is this version of the method that is called when the corresponding "fqcn" is provided in the string argument method of the same name. Because the object is instanced from the class name at run time (when the name is specified) a dynamic selection of the code (polymorphism) is realized despite the call being made on a static method. In my framework the "fqcn" string comes from the system bootstrap configuration class DPSConfig which bootstraps from settings indicated in an xml configuration file. The file is managable by the UI , so changing back end db's is trivially performed without requiring down time on any of the nodes in the cluster. As well the desire to call necessary db vendor agnostic methods is achieved from all client code that utilizes the static method and passes in the "fqcn" from the system bootstrap settings without knowledge or care of which db vendor class it happens to be. Below I provide an example for a hypothetical subclass with customization for the DB2 DBMS.
public class CreateTableDB2 extends CreateTable {
public String timePattern() {
return " hh:mi:ss ";
}
}
Using static polymorphism has allowed me to get around the sticky problem of calling vendor specific methods in a static way without instancing the subclass that currently maps to the db in use on the system and is straight forward to implement, you might find it useful in your designs.
Comments