Instantiating Beehive JdbcControl manually

Common usage pattern for Beehive Jdbc control is:

  • Create DB control class by extending JdbcControl
  • Declare DB control variable in page controller and use @Control annotation to have it automatically instantiated and injected
  • Use the DB control

The above works fine. But what happens if we would like to use our DB control class from within JSP page which is not part of our Beehive application and has no access to our controller?

The following text presents one possible approach.

Custom DB control class:
[java]
@ControlExtension
@JdbcControl.ConnectionDataSource(jndiName = “dataSource”)
public interface MyDBControl extends JdbcControl {
@JdbcControl.SQL(
statement=”SELECT * FROM \”SCHEMA\”.\”MYTABLE\””)
public MyObject[] getAllObjects();

}
[/java]

Classic approach, instantiate via @Control annotation in controller:
[java]
public class MyController extends PageFlowController {
@Control
private MyDBControl myControl;

//you can now use myControl…
[/java]

But what if we need to instantiate the DB control class manually? One possible approach might be creating a factory like this:
[java]
@ControlReferences({com.dolejsky.MyDBControl.class})
public class MyDBControlFactory {
public static MyDBControl createDBControl() {
try {
ControlBeanContext ctx = new ServletBeanContext();
MyDBControlBean controlBean =
(MyDBControlBean)Controls.instantiate(
Thread.currentThread().getContextClassLoader(),
“com.dolejsky.MyDBControlBean”,
null,
ctx,
null);
return controlBean;
} catch (ClassNotFoundException e) {
//handle it…
}
return null;
}
}
[/java]

Comments are closed.