Skip to main content

Static polymorphism

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.
  • 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

Popular posts from this blog

the attributes of web 3.0...

As the US economy continues to suffer the doldrums of stagnant investment in many industries, belt tightening budgets in many of the largest cities and continuous rounds of lay offs at some of the oldest of corporations, it is little comfort to those suffering through economic problems that what is happening now, has happened before. True, the severity of the downturn might have been different but the common factors of people and businesses being forced to do more with less is the theme of the times. Like environmental shocks to an ecosystem, stresses to the economic system lead to people hunkering down to last the storm, but it is instructive to realize that during the storm, all that idle time in the shelter affords people the ability to solve previous or existing problems. Likewise, economic downturns enable enterprising individuals and corporations the ability to make bold decisions with regard to marketing , sales or product focus that can lead to incredible gains as the economic

How many cofactors for inducing expression of every cell type?

Another revolution in iPSC technology announced: "Also known as iPS cells, these cells can become virtually any cell type in the human body -- just like embryonic stem cells. Then last year, Gladstone Senior Investigator Sheng Ding, PhD, announced that he had used a combination of small molecules and genetic factors to transform skin cells directly into neural stem cells. Today, Dr. Huang takes a new tack by using one genetic factor -- Sox2 -- to directly reprogram one cell type into another without reverting to the pluripotent state." -- So the method invented by Yamanaka is now refined to rely only 1 cofactor and b) directly generate the target cell type from the source cell type (skin to neuron) without the stem like intermediate stage.  It also mentions that oncogenic triggering was eliminated in their testing. Now comparative methods can be used to discover other types...the question is..is Sox2 critical for all types? It may be that skin to neuron relies on Sox2

AgilEntity Architecture: Action Oriented Workflow

Permissions, fine grained versus management headache The usual method for determining which users can perform a given function on a given object in a managed system, employs providing those Users with specific access rights via the use of permissions. Often these permissions are also able to be granted to collections called Groups, to which Users are added. The combination of Permissions and Groups provides the ability to provide as atomic a dissemination of rights across the User space as possible. However, this granularity comes at the price of reduced efficiency for managing the created permissions and more importantly the Groups that collect Users designated to perform sets of actions. Essentially the Groups serve as access control lists in many systems, which for the variable and often changing environment of business applications means a need to constantly update the ACL’s (groups) in order to add or remove individuals based on their ability to perform cert