Monday, February 9, 2015

JAR Utilities using OIM API

Description: The Oracle Identity Manager APIs contain utilities to upload, update, delete, and download JAR file. The entire project can be downloaded  here.
Reference: https://docs.oracle.com/cd/E37472_01/apirefs.1112/e28159/toc.htm


package com.blogspot.oraclestack.constants;
/**
* Represents all the possible jar types for the UploadJars.sh utility.
* @author rayedchan
*/
public enum JarElementType
{
JavaTasks, ScheduleTasks, ThirdParty, ICFBundle;
}
package com.blogspot.oraclestack.testdriver;
import com.blogspot.oraclestack.constants.JarElementType;
import com.blogspot.oraclestack.services.OracleIdentityManagerClient;
import com.blogspot.oraclestack.utilities.PlatformServiceUtilities;
import oracle.iam.platform.OIMClient;
/**
* Test Driver for PlatformServiceUtilities. Use to upload, update, or remove
* OIM JARS.
* @author rayedchan
*/
public class JarUtilityTestDriver
{
// Adjust constant variables according to you OIM environment
public static final String OIM_HOSTNAME = "localhost";
public static final String OIM_PORT = "14000"; // For SSL, use 14001; For non-SSL, use 14000
public static final String OIM_PROVIDER_URL = "t3://"+ OIM_HOSTNAME + ":" + OIM_PORT; // For SSL, use t3s protocol; For non-SSL, use t3 protocol
public static final String AUTHWL_PATH = "lib/config/authwl.conf";
public static final String APPSERVER_TYPE = "wls";
public static final String FACTORY_INITIAL_TYPE = "weblogic.jndi.WLInitialContextFactory";
public static final String OIM_ADMIN_USERNAME = "xelsysadm";
public static final String OIM_ADMIN_PASSWORD = "Password1";
public static final String JAR_PATH = "/home/oracle/NetBeansProjects/OIMUtilities/dist/OIMUtilities.jar"; // Absolute Path of JAR file on machine where OIM is running
public static void main(String[] args) throws Exception
{
OracleIdentityManagerClient oimClientWrapper = null;
try
{
// Establish an OIM Client
oimClientWrapper = new OracleIdentityManagerClient(OIM_ADMIN_USERNAME, OIM_ADMIN_PASSWORD, AUTHWL_PATH, APPSERVER_TYPE, FACTORY_INITIAL_TYPE, OIM_PROVIDER_URL, false, null);
OIMClient oimClient = oimClientWrapper.getOIMClient();
// Test platform service utilities
PlatformServiceUtilities platServUtil = new PlatformServiceUtilities(oimClient);
// Upload JAR to OIM; the jar must exist on the machine where OIM is running
// platServUtil.uploadJar(JarElementType.JavaTasks, JAR_PATH); // Specify JAR type and path of JAR on machine where OIM is running
// Update an existing JAR in OIM
//platServUtil.updateJar(JarElementType.JavaTasks, JAR_PATH);
// Remove a JAR from OIM
// platServUtil.deleteJar(JarElementType.JavaTasks, "OIMUtilities.jar");
// Download a JAR from OIM
// platServUtil.downloadJar(JarElementType.JavaTasks, "OIMUtilities.jar" , "/home/oracle/Desktop/");
// Purge OIM Cache
platServUtil.purgeCache();
}
finally
{
if( oimClientWrapper != null)
{
oimClientWrapper.logout();
}
}
}
}
package com.blogspot.oraclestack.services;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import javax.security.auth.login.LoginException;
import oracle.core.ojdl.logging.ODLLevel;
import oracle.core.ojdl.logging.ODLLogger;
import oracle.iam.identity.exception.UserSearchException;
import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.usermgmt.vo.User;
import oracle.iam.platform.OIMClient;
import oracle.iam.platform.authz.exception.AccessDeniedException;
import oracle.iam.platform.entitymgr.vo.SearchCriteria;
/**
* This class uses the OIMClient to access the API services of an Oracle
* Identity Manager environment.
* @author rayedchan
*/
public class OracleIdentityManagerClient
{
// Logger
public static ODLLogger logger = ODLLogger.getODLLogger(OracleIdentityManagerClient.class.getName());
// Instance Variables
private OIMClient oimClient; // OIM Client to use API services
/**
* This constructor initializes the OIMClient by logging in as an
* OIM Identity. A system administrator is necessary to perform
* all the OIM API services.
* @param username User Login of the OIM Identity
* @param password Plain text password of the OIM Identity
* @param authwlPath Path to "authwl.conf" file. This can be found in "$MW_HOME/Oracle_IDM1/designconsole/config".
* @param appServerType Type of application server OIM is deployed on. For WebLogic, this value should be "wls".
* @param factoryInitialType Type of factory initial. For WebLogic, this value should be "weblogic.jndi.WLInitialContextFactory".
* @param oimProviderURL The OIM provider URL. For non-SSL protocol, the value should be "t3://<oimhostname>:<oimport>". For SSL protocol, the value should be "t3s://<oimhostname>:<oimport>".
* @param isSSL Set to true if SSL protocol is in use.
* @param trustKeystorePath Set path to trust key store if SSL is being used.
*/
public OracleIdentityManagerClient(String username, String password, String authwlPath, String appServerType, String factoryInitialType, String oimProviderURL, boolean isSSL, String trustKeystorePath) throws LoginException
{
// Initializes OIMClient with environment information
this.initializeOIMClient(authwlPath, appServerType, factoryInitialType, oimProviderURL, isSSL, trustKeystorePath);
// Login to OIM with System Administrator Credentials
oimClient.login(username, password.toCharArray());
}
/**
* Setup the necessary system properties and environment information in
* order to use the OIM Client.
* @param authwlPath Path to "authwl.conf" file. This can be found in "$MW_HOME/Oracle_IDM1/designconsole/config".
* @param appServerType Type of application server OIM is deployed on. For WebLogic, this value should be "wls".
* @param factoryInitialType Type of factory initial. For WebLogic, this value should be "weblogic.jndi.WLInitialContextFactory".
* @param oimProviderURL The OIM provider URL. For non-SSL protocol, the value should be "t3://<oimhostname>:<oimport>". For SSL protocol, the value should be "t3s://<oimhostname>:<oimport>".
* @param isSSL Set to true if SSL is in use.
* @param trustKeystorePath Set path to trust key store if SSL is being used.
*/
private void initializeOIMClient(String authwlPath, String appServerType, String factoryInitialType, String oimProviderURL, boolean isSSL, String trustKeystorePath)
{
// Set system properties required for OIMClient
System.setProperty("java.security.auth.login.config", authwlPath);
System.setProperty("APPSERVER_TYPE", appServerType);
// Set SSL argument on runtime to point to trusted key store
if(isSSL)
{
System.setProperty("weblogic.security.SSL.trustedCAKeyStore", trustKeystorePath);
}
// Create an instance of OIMClient with OIM environment information
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, factoryInitialType);
env.put(OIMClient.JAVA_NAMING_PROVIDER_URL, oimProviderURL);
this.oimClient = new OIMClient(env);
}
/**
* Get the OIMClient
* @return OIMClient object
*/
public OIMClient getOIMClient()
{
return this.oimClient;
}
/**
* Log out user from OIMClient.
*/
public void logout()
{
if(this.oimClient != null)
{
this.oimClient.logout();
logger.log(ODLLevel.TRACE, "Logout user from OIMClient.");
}
}
/**
* Method to test the OIMClient. All the Identities (users) are queried from
* OIM environment.
* @param args
*/
public void test() throws AccessDeniedException, UserSearchException
{
// Lookup User Manager service
UserManager usermgr = this.oimClient.getService(UserManager.class);
// Only fetch attributes defined in HashSet
HashSet attrQuery = new HashSet();
attrQuery.add("usr_key");
attrQuery.add("usr_login");
attrQuery.add("Display Name");
attrQuery.add("First Name");
attrQuery.add("Last Name");
// Call a method from User Manager service
List<User> users = usermgr.search(new SearchCriteria("User Login", "*", SearchCriteria.Operator.EQUAL), attrQuery, new HashMap());
logger.log(ODLLevel.NOTIFICATION, "OIM Users: {0}", new Object[]{users});
}
}
package com.blogspot.oraclestack.utilities;
import java.util.HashSet;
import java.util.Set;
import oracle.core.ojdl.logging.ODLLevel;
import oracle.core.ojdl.logging.ODLLogger;
import oracle.iam.platform.OIMClient;
import oracle.iam.platformservice.api.PlatformUtilsService;
import oracle.iam.platformservice.exception.InvalidCacheCategoryException;
import oracle.iam.platformservice.exception.PlatformServiceException;
import oracle.iam.platformservice.vo.JarElement;
import com.blogspot.oraclestack.constants.JarElementType;;
/**
* This class contain methods that have the same functionality as the out of the box
* scripts given in $MW_HOME/Oracle_IDM1/server/bin/" directory.
* @author rayedchan
*/
public class PlatformServiceUtilities
{
// Logger
public static ODLLogger logger = ODLLogger.getODLLogger(PlatformServiceUtilities.class.getName());
private PlatformUtilsService platformUtilsServiceOps = null;
/**
* Constructor
* Precondition: A system administrator is logged in with the OIM client
* @param oimClient
*/
public PlatformServiceUtilities(OIMClient oimClient)
{
this.platformUtilsServiceOps = oimClient.getService(PlatformUtilsService.class);
}
/**
* Purges the entire OIM cache. Same functionality as the "PurgeCache.sh" script
* which is located in "$MW_HOME/Oracle_IDM1/server/bin/" directory.
* @throws InvalidCacheCategoryException
*/
public void purgeCache() throws InvalidCacheCategoryException
{
platformUtilsServiceOps.purgeCache("ALL");
logger.log(ODLLevel.NOTIFICATION, "Successfully purged the cache.");
}
/**
* Uploads a single jar file to database. A record will be added to OIM.OIMHOME_JARS
* table to indicate the jar is successfully uploaded. Same functionality as the "UploadJars.sh" script
* which is located in "$MW_HOME/Oracle_IDM1/server/bin/" directory.
* @param jarType The type of jar to be uploaded.
* @param jarPath The absolute path to the jar file that is being uploaded.
*/
public void uploadJar(JarElementType jarType, String jarPath) throws PlatformServiceException
{
// Build a jar element containing path and type data
JarElement jarElement = new JarElement();
jarElement.setType(jarType.name());
jarElement.setPath(jarPath);
// Build a set object to put jar element
Set<JarElement> jarElements = new HashSet<JarElement>();
jarElements.add(jarElement);
// Service to upload jar to OIM Schema
platformUtilsServiceOps.uploadJars(jarElements);
logger.log(ODLLevel.NOTIFICATION, "Successfully uploaded jar: Type = {0}, Path = {1}", new Object[]{jarType.name(), jarPath});
}
/**
* Removes a jar from the database. The corresponding jar record will be removed
* from OIM.OIMHOME_JARS table. Same functionality as the "DeleteJars.sh" script
* which is located in "$MW_HOME/Oracle_IDM1/server/bin/" directory.
* @param jarType The type of jar to be removed.
* @param jarName The name of the jar in the backend. Use the value in OIM.OIMHOME_JARS.OJ_NAME column.
* @throws PlatformServiceException
*/
public void deleteJar(JarElementType jarType, String jarName) throws PlatformServiceException
{
// Build a jar element containing path and type data
JarElement jarElement = new JarElement();
jarElement.setType(jarType.name());
jarElement.setName(jarName);
// Build a set object to put jar element
Set<JarElement> jarElements = new HashSet<JarElement>();
jarElements.add(jarElement);
// Service to remove jar from OIM Schema
platformUtilsServiceOps.deleteJars(jarElements);
logger.log(ODLLevel.NOTIFICATION, "Successfully deleted jar: Type = {0}, Name = {1}", new Object[]{jarType.name(),jarName});
}
/**
* Updates a single jar file to database. UPDATE_ON and UPDATED_BY columns in the OIMHOME_JARS table will be updated with current date
* to indicate the jar is successfully updated. Same functionality as the "UpdateJars.sh" script
* which is located in "$MW_HOME/Oracle_IDM1/server/bin/" directory.
* @param jarType The type of jar to be updated.
* @param jarPath The absolute path to the jar file that is being updated. The jar name must exist in the database.
*/
public void updateJar(JarElementType jarType, String jarPath) throws PlatformServiceException
{
// Build a jar element containing path and type data
JarElement jarElement = new JarElement();
jarElement.setType(jarType.name());
jarElement.setPath(jarPath);
// Build a set object to put jar element
Set<JarElement> jarElements = new HashSet<JarElement>();
jarElements.add(jarElement);
// Service to update jar to OIM Schema
platformUtilsServiceOps.updateJars(jarElements);
logger.log(ODLLevel.NOTIFICATION, "Successfully updated jar: Type = {0}, Path = {1}", new Object[]{jarType.name(), jarPath});
}
/**
* Download a jar from the database. Same functionality as the "DownloadJars.sh" script
* which is located in "$MW_HOME/Oracle_IDM1/server/bin/" directory.
* @param jarType The type of jar to be downloaded.
* @param jarName The name of the jar in the backend to be downloaded. Use the value in OIM.OIMHOME_JARS.OJ_NAME column.
* @throws PlatformServiceException
*/
public void downloadJar(JarElementType jarType, String jarName, String destinationPath) throws PlatformServiceException
{
// Build a jar element containing path and type data
JarElement jarElement = new JarElement();
jarElement.setType(jarType.name());
jarElement.setName(jarName);
jarElement.setPath(destinationPath);
// Build a set object to put jar element
Set<JarElement> jarElements = new HashSet<JarElement>();
jarElements.add(jarElement);
// Service to download jar from OIM Schema
platformUtilsServiceOps.downloadJars(jarElements);
logger.log(ODLLevel.NOTIFICATION, "Successfully downloaded jar: Type = {0}, Name = {1}", new Object[]{jarType.name(),jarName});
}
}

1 comment:

  1. NO class def error for OIM client jar while deploying on jboss.

    ReplyDelete