Thursday, May 28, 2020

Getting Application Module Configuration

Using Oracle ADF, sometime is necessary access to properties used by the Application module, for example to get the jndi used DS.
This is possible using class oracle.jbo.client.Configuration.
You can use this class to manage at runtime a configuration defined at design.

Follow an example of usage:

public static void main(String[] args) {
    //A helper function which hides the details of building a package qualified filename for the Configuration file.
    //Pass as parameter the name of the ApplicationModule package., ex., pass package10, for package10.Package10Module
    String bc4jPath = Configuration.buildConfigurationFileName(IModelConstants.APP_MODULE_PACKAGE_PATH);

    //Create a Configuration object instance to manage 'Configuration' file created during the design time (bc4j.xcfg)
    Configuration conf = new Configuration();
    //Pass the name of the Configuration file.
    // Package qualified name of the Configuration file. The configuratin file named bc4j.xcfg is stored in the common package of an AppModule
    conf.loadFromClassPath(bc4jPath);
    // get the configuration name listed into the loaded bc4j.xcfg
    String[] configurations = conf.getConfigurationNameList();
    // print some property for each configuration
    for (String str: configurations) {
        System.out.println("AM CONFIGURATION: " + str);
        java.util.Hashtable ht = conf.getConfiguration(str);
        System.out.println(Configuration.JDBC_DS_NAME + "= " + ht.get(Configuration.JDBC_DS_NAME));
        System.out.println(Configuration.JDBC_CONNECTION_NAME + "= " + ht.get(Configuration.JDBC_CONNECTION_NAME));
        System.out.println(Configuration.DB_CONNECT_STRING_PROPERTY + "= " + ht.get(Configuration.DB_CONNECT_STRING_PROPERTY));
        System.out.println(Configuration.DB_CONNECTION_PROPERTY + "= " + ht.get(Configuration.DB_CONNECTION_PROPERTY));
        System.out.println(Configuration.DB_USERNAME_PROPERTY + "= " + ht.get(Configuration.DB_USERNAME_PROPERTY));
        System.out.println(Configuration.DB_PASSWORD_PROPERTY + "= " + ht.get(Configuration.DB_PASSWORD_PROPERTY));
        System.out.println("------------------------------------------------------------------------");
    }
}

At runtime the code print all datasource listed configuration registered at design time in bc4j.xcfg file:

AM CONFIGURATION: AppModuleDeploy
JDBCDataSource= jdbc/MyDB
JDBCName= null
jbo.jdbc.connectstring= null
DBconnection= jdbc/MyDB
user= null
password= null
------------------------------------------------------------------------
AM CONFIGURATION: AppModuleLocal
JDBCDataSource= null
JDBCName= DB_Svil_Connection
jbo.jdbc.connectstring= null
DBconnection= jdbc:oracle:thin:@//hostname:port/db
user= <schema username>
password= <schema password>
------------------------------------------------------------------------

Code output is just what I expacted! Look at my bc4j.xcfg:


<?xml version = '1.0' encoding = 'UTF-8'?>
<!---->
<BC4JConfig version="11.1" xmlns="http://xmlns.oracle.com/bc4j/configuration">
   <AppModuleConfigBag ApplicationName="application.module.package.AppModule" default="AppModuleDeploy">
      <AppModuleConfig DeployPlatform="LOCAL" name="AppModuleDeploy" ApplicationName="application.module.package.AppModule" jbo.project="application.module.package.Model">         <Database JDBCDataSource="jdbc/MyDB"/>
         <Security AppModuleJndiName="application.module.package.AppModule"/>
      </AppModuleConfig>
      <AppModuleConfig name="AppModuleLocal" jbo.project="application.module.package.Model"
                       ApplicationName="application.module.package.AppModule" JDBCName="DB_Svil_Connection"
                       DeployPlatform="LOCAL">
         <Database jbo.TypeMapEntries="OracleApps"/>
         <Security AppModuleJndiName="application.module.package.AppModule"/>
      </AppModuleConfig>
   </AppModuleConfigBag>
</BC4JConfig>

Enjoy!!

No comments:

Post a Comment