1.Code here below updates the password for the no of users in the excel sheet.
2.Inputs required are userid and password.
3.Code reads the attributes from the excel sheet and establishes connection with the oin and pass the values to the updatePassword method.
4.upadtes the password for all the OIM users as in the excel sheet
Code
*****
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.Cell;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import com.thortech.xl.util.config.ConfigurationClient;
import Thor.API.tcResultSet;
import Thor.API.tcUtilityFactory;
import Thor.API.Exceptions.tcAPIException;
import Thor.API.Exceptions.tcChallengeNotSetException;
import Thor.API.Exceptions.tcLoginAttemptsExceededException;
import Thor.API.Exceptions.tcPasswordExpiredException;
import Thor.API.Exceptions.tcPasswordResetAttemptsExceededException;
import Thor.API.Exceptions.tcUserAccountDisabledException;
import Thor.API.Exceptions.tcUserAccountInvalidException;
import Thor.API.Exceptions.tcUserAlreadyLoggedInException;
import Thor.API.Operations.tcGroupOperationsIntf;
import Thor.API.Operations.tcUserOperationsIntf;
public class Blog {
static ReadProperty readProperty = new ReadProperty();
public long returnVal = -1;
public static String homeDir = readProperty.getProp("homeDir");
public static String policyDir = readProperty.getProp("policyDir");
public static String confDir = readProperty.getProp("confDir");
public static String username = readProperty.getProp("username");
public static String password = readProperty.getProp("password");
public static String providerUrl = readProperty.getProp("providerUrl");
public tcUtilityFactory ioUtilityFactory;
public tcUserOperationsIntf moUserUtility;
public tcGroupOperationsIntf moGroupUtility;
FileLogger fL;
public Blog(){
fL = new FileLogger();
fL.setLogFile("D:\\readerLog.txt");
}
public void writeFile(String text)
{
try{
// Create file
FileWriter fstream = new FileWriter("D:\\OfficeCode.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(text);
out.newLine();
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public String roleAuthenticate(String usr, String pass){
try {
System.setProperty("XL.HomeDir", homeDir);
System.setProperty("java.security.policy", policyDir);//server or client
System.setProperty("java.security.auth.login.config", confDir);//server or client
System.setProperty("java.naming.provider.url", providerUrl);
ConfigurationClient.ComplexSetting config =
ConfigurationClient.getComplexSettingByPath("Discovery.CoreServer");
Hashtable env = config.getAllSettings();
System.out.println("test1");
System.out.println("Getting Information before login..");
this.writeFile("Getting Information before login..");
tcUtilityFactory ioUtilityFactory = new tcUtilityFactory(env,usr,pass);
System.out.println("Getting utility interfaces...");
System.out.println("Connection Established");
this.writeFile("Getting utility interfaces...\nConnection Established");
moUserUtility = (tcUserOperationsIntf)ioUtilityFactory.getUtility("Thor.API.Operations.tcUserOperationsIntf");
moGroupUtility = (tcGroupOperationsIntf)ioUtilityFactory.getUtility("Thor.API.Operations.tcGroupOperationsIntf");
Logger logger = oracle.j2ee.rmi.RMIMessages.getLogger();
logger.setLevel(Level.OFF);
return "Valid";
} catch(Exception e){
e.printStackTrace();
return "Invalid";
}
}
public long getUserKey(String obj){
long key = 0;
HashMap
userMap = new HashMap();
try {
//ConnectionEstablish cT = new ConnectionEstablish();
System.out.println("In getUserKey method, uid is :"+obj);
this.writeFile("In getUserKey method, uid is :"+obj);
userMap.put("Users.User ID", obj);
System.out.println("userMap contents: "+userMap.keySet()+userMap.values());
this.writeFile("userMap contents: "+userMap.keySet()+userMap.values());
tcResultSet moResultSet = moUserUtility.findUsers(userMap);
System.out.println("MoResult set count is :"+moResultSet.getRowCount());
this.writeFile("MoResult set count is :"+moResultSet.getRowCount());
for (int i=0; i < moResultSet.getRowCount(); i++){
moResultSet.goToRow(i);
key = moResultSet.getLongValue("Users.Key");
System.out.println("In getUserKey method, key is :"+key);
}
}catch(Exception e){
System.out.println("Error in getting user key" + e);
e.printStackTrace();
}
return key;
}
public long getGroupKey(String obj){
long key = 0;
HashMap userMap = new HashMap();
try {
userMap.put("Groups.Group Name", obj);
tcResultSet moResultSet = moGroupUtility.findGroups(userMap);
for (int i=0; i < moResultSet.getRowCount(); i++){
moResultSet.goToRow(i);
key = moResultSet.getLongValue("Groups.Key");
}
}catch(Exception e){
e.printStackTrace();
}
return key;
}
public void UpdateUserPassword( String UID,String pwd) throws tcAPIException, tcChallengeNotSetException, tcLoginAttemptsExceededException, tcPasswordResetAttemptsExceededException, tcPasswordExpiredException, tcUserAccountDisabledException, tcUserAccountInvalidException, tcUserAlreadyLoggedInException {
this.roleAuthenticate("xelsysadm","admin123");
HashMap userMap = new HashMap();
HashMap hmUser = new HashMap();
try {
userMap.put("Users.User ID", UID);
tcResultSet moResultSet = moUserUtility.findUsers(userMap);
hmUser.put("Users.User ID", UID);
hmUser.put("Users.Password", pwd);
moUserUtility.updateUser(moResultSet, hmUser);
System.out.println("Updated password");
} catch (Exception e) {
System.out.println("Exception in OIM server while updating password for user <===" + UID +"===>");
e.printStackTrace();
}
}
public void init(String filePath)
{
System.out.println("Opening file...........\n");
FileInputStream fs = null;
try
{
fs = new FileInputStream(new File(filePath));
readContent(fs);
}
catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void updatePassword(Sheet sheet) throws tcAPIException, tcChallengeNotSetException, tcLoginAttemptsExceededException, tcPasswordResetAttemptsExceededException, tcUserAccountDisabledException, tcPasswordExpiredException, tcUserAlreadyLoggedInException, tcUserAccountInvalidException{
HashMap userMap = new HashMap();
//HashMap hmUser = new HashMap();
int columnCount = sheet.getColumns();
int rowCount = sheet.getRows();
String userID = null;
String password = null;
for(int j=1;j < rowCount; j++)
{
userID = sheet.getCell(0, j).getContents();
password = sheet.getCell(1, j).getContents();
System.out.println("For UserIDs User ID :"+userID);
userMap.put("Users.User ID", userID);
System.out.println("User Map values "+userMap.values());
this.roleAuthenticate("xelsysadm","admin123");
this.UpdateUserPassword(userID, password);
System.out.println("\n\nupdated Password "+password+"for user: "+userID+"..............\n");
}
}
public void readContent(InputStream fileInputStream) throws tcAPIException, tcChallengeNotSetException, tcLoginAttemptsExceededException, tcPasswordResetAttemptsExceededException, tcUserAccountDisabledException, tcPasswordExpiredException, tcUserAlreadyLoggedInException, tcUserAccountInvalidException
{
WorkbookSettings ws = null;
Workbook workbook = null;
Sheet s = null;
Cell rowData[] = null;
int rowCount = '0';
int columnCount = '0';
DateCell dc = null;
int totalSheet = 0;
ArrayList al=new ArrayList();
ArrayList all=new ArrayList();
try
{
ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));
workbook = Workbook.getWorkbook(fileInputStream, ws);
totalSheet = workbook.getNumberOfSheets();
if(totalSheet > 0)
{
System.out.println("Reading file contents............\n");
System.out.println("Total Sheet Found:" + totalSheet);
for(int j=0;j
{
System.out.println("Sheet Name:" + workbook.getSheet(j).getName());
}
}
//Getting Default Sheet i.e. 0
s = workbook.getSheet(0);
//Reading Individual Cell
// getHeadingFromXlsFile(s);
//Total Total No Of Rows in Sheet, will return you no of rows that are occupied with some data
System.out.println("Total Rows inside Sheet:" + s.getRows());
rowCount = s.getRows();
//Total Total No Of Columns in Sheet
System.out.println("Total Column inside Sheet:" + s.getColumns());
columnCount = s.getColumns();
updatePassword(s);
}
catch (IOException e)
{
e.printStackTrace();
} catch (BiffException e)
{
e.printStackTrace();
}
}
public static void main(String[] args){
try
{
Blog blog = new Blog();
blog.init("D:\\Book2.xls");
System.exit(0);
} catch (Exception e)
{
e.printStackTrace();
}
}
}