Sunday, February 17, 2013

Connecting to OIM Schema Through the OIMClient In Java

Version: Oracle Identity Manager 11g R1
Description: A Java application to demonstrate how to connect to your OIM Schema provided that you have an OIMClient. The user who is logged in for the OIMClient needs to be an End-User Administrator in order to use the tcDatabaseClient. The “Design Console Access” attribute on the OIM User Profile determines whether a user is an End-User or an End-User Administrator. Accessing the OIM Schema through this method does not need the database user credential.

import Thor.API.Security.XLClientSecurityAssociation;
import com.thortech.xl.client.dataobj.tcDataBaseClient;
import com.thortech.xl.dataaccess.tcClientDataAccessException;
import com.thortech.xl.dataaccess.tcDataProvider;
import com.thortech.xl.dataaccess.tcDataSet;
import com.thortech.xl.dataaccess.tcDataSetException;
import com.thortech.xl.orb.dataaccess.tcDataAccessException;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.login.LoginException;
import oracle.iam.platform.OIMClient;

/**
 * This demonstrates how to connect to the OIM Schema 
 * using the OIMClient. The OIM administrator credential is required.  
 */
public class OIMClientDBConnection 
{
    public static void main(String[] args)  
    {
        tcDataProvider dbProvider = null; //OIM Schema Database Client 
        OIMClient oimClient = null;

        try 
        {
            String ctxFactory = "weblogic.jndi.WLInitialContextFactory"; //WebLogic Context 
            String oimServerURL = "t3://localhost:14000"; //OIM URL
            String authwlConfigPath = "/home/oracle/oimClient_lib/conf/authwl.conf"; //Path to login configuration
            String username = "xelsysadm"; //OIM Administrator 
            String password = "Password1"; //Administrator Password

            System.setProperty("java.security.auth.login.config", authwlConfigPath); //set the login configuration
            Hashtable<string,string> env = new Hashtable<string,string>(); //use to store OIM environment properties
            env.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, ctxFactory);
            env.put(OIMClient.JAVA_NAMING_PROVIDER_URL, oimServerURL);
            oimClient = new OIMClient(env);
            oimClient.login(username, password.toCharArray()); //login to OIM

            //Establish connection to OIM Schema through the OIMClient
            XLClientSecurityAssociation.setClientHandle(oimClient); 
            dbProvider = new tcDataBaseClient();

            String query = "SELECT * FROM USR"; //Query all OIM users
            tcDataSet usersDataSet = new tcDataSet(); //store result set of query
            usersDataSet.setQuery(dbProvider, query); 
            usersDataSet.executeQuery();

            int numColumns = usersDataSet.getColumnCount();
            int numRecords = usersDataSet.getTotalRowCount();

            //iterate through each record
            for(int i = 0; i < numRecords; i++)
            {
                usersDataSet.goToRow(i);

                //iterate through each column of a record
                for(int j = 0; j < numColumns; j++)
                {
                    String columnName = usersDataSet.getColumnName(j);
                    String value = usersDataSet.getString(j);
                    System.out.println(columnName + " = " + value);
                }

                System.out.println();
            }

        } 

        catch (tcDataSetException ex) 
        { 
            Logger.getLogger(OIMClientDBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

        catch (LoginException ex) 
        {
            Logger.getLogger(OIMClientDBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

        //resources need to be closed
        finally
        {
            if(dbProvider != null)
            {
                try {
                    dbProvider.close();
                } catch (tcDataAccessException ex) {
                    Logger.getLogger(OIMClientDBConnection.class.getName()).log(Level.SEVERE, null, ex);
                } catch (tcClientDataAccessException ex) {
                    Logger.getLogger(OIMClientDBConnection.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            XLClientSecurityAssociation.clearThreadLoginSession();   

            if(oimClient != null)
            {
                oimClient.logout();
            }
        }
    }
}

No comments:

Post a Comment