import java.util.HashMap; import com.hyperion.css.CSSException; import com.hyperion.css.common.CSSNativeUserIF; /* * This sample demonstrates how to * 1. Create a user. * 2. Modify a user. * 3. Lookup a user by identity. * 4. Delete a user. * * The initialization code is shared by all samples * and is in Sample.java * */ public class UserFunctionsSample extends Sample { public UserFunctionsSample() throws Exception { super(); } /** * Create User. Only possible with a native provider. */ public CSSNativeUserIF createUser() throws CSSException { CSSNativeUserIF user = null; HashMap context = new HashMap(); user = cssDMAPI.getNativeUserInstance(context, principal); user.setLoginName("testuser" ); user.setDescription("Created for test"); user.setFirstName("First"); user.setLastName("Last"); user.setEmailAddress("testuser@xyz.com"); user.setPassword("password"); cssDMAPI.addNativeUser(context, principal, user); return user; } /** * Modify User. Only possible with a native provider. */ public void modifyUser(CSSNativeUserIF user) throws CSSException { if (user != null){ System.out.println("Modifying user description ..."); user.setDescription("Created for modify test"); user.setFirstName("First-modified"); user.setLastName("Last-modified"); cssDMAPI.updateNativeUser(context, principal, user); } } public CSSNativeUserIF lookupUser(String identity) throws CSSException { return cssDMAPI.getNativeUserByIdentity(context, principal, identity); } /** * Delete User */ public void deleteUser(String identity) throws CSSException { cssDMAPI.deleteNativeUsers(new HashMap(), principal, new String[] { identity } ); } /*clean up after the test case */ public void cleanup(String[]ids){ // delete the test users created if (ids != null && ids.length != 0) { for (int i = 0; i < ids.length; i++) { try { deleteUser(ids[i]); } catch (CSSException ce) { ce.printStackTrace(); } } } } public static void main (String [] args) { UserFunctionsSample cu = null; CSSNativeUserIF user = null; try { cu = new UserFunctionsSample(); cu.printMsg(System.out, "Main methods:"); /* Create a native user */ user = cu.createUser(); cu.dumpUsers(System.out, "Created User ", new CSSNativeUserIF[]{user}); /* Modify native user */ cu.printMsg(System.out, "Modifying the user ..."); cu.modifyUser(user); /*lookup the user just created by identity */ CSSNativeUserIF u = cu.lookupUser(user.getIdentity()); cu.dumpUsers(System.out, "Looked up modified user", new CSSNativeUserIF[]{u}); /*delete native user */ if (user != null){ cu.deleteUser(user.getIdentity()); cu.printMsg(System.out, "Deleted User."); } } catch (Throwable e) { e.printStackTrace(); System.out.println("Could not run sample because of error " + e.getClass().getName() + ": " + e.getMessage()); // delete user if some operation failed after creation if (user != null) cu.cleanup(new String[] {user.getIdentity()}); } finally { if (cu != null) cu.shutdown(); } } }