Wednesday, 21 September 2011

Using an Environment Info Provider to switch between Data Sources

I had to deal with a requirement where the Application Module had to choose between two configured Data Sources depending on some Principal as configured in the Security Context's Subject. The way to do it is as follow:

1. Implement a class that extends oracle.jbo.common.ampool.EnvInfoProvider
2. Override the "public Object getInfo(String infoType, Object env)"-method
    i.e. body for the above method to be overridden below:

    if(EnvInfoProvider.INFO_TYPE_JDBC_PROPERTIES.equals(infoType)) {
      String dsName = DEFAULT_DS_JNDI_NAME; // the default Data Source' JNDI Name    
      if(ADFContext.getCurrent().getSecurityContext().getSubject() != null) {
        Subject sbj = ADFContext.getCurrent().getSecurityContext().getSubject();
        // utitlity method to extract the FcoCode principal from the given subject
        String fcoCode = AMUtil.interrogateSubjectForFinanceCompanyPrincipal(sbj);       
        if(fcoCode != null) {
            // calling private method to determine the appropriate Data Source JNDI Name
            // based on FcoCode
          dsName = this.determineDataSourceFromFcoCode(fcoCode);        
      ((Hashtable) env).put(Configuration.JDBC_DS_NAME, dsName);
    return null;

3. Configure the Application Module to make use of the implemented Environment Info Provider by
    specifying your custom class as value for property: "jbo.envinfoprovider".
    (see screenshot below)


No comments:

Post a Comment