Example: Using a profile token credential to swap the IBM i thread identity

The following code example shows you how to use a profile token credential to swap the IBM® i thread identity and perform work on behalf of a specific user.

Note: Read the Code example disclaimer for important legal information.
Start of change
// Create a single-use ProfileTokenCredential with a 60 second timeout.
AS400 system = new AS400();
ProfileTokenCredential pt = new ProfileTokenCredential();
pt.setSystem(system);
pt.setTimeoutInterval(60);
pt.setTokenType(ProfileTokenCredential.TYPE_SINGLE_USE);

// A valid user ID and password must be substituted.
pt.setTokenExtended("user", "password");

// Swap the OS/400 thread identity, retrieving a credential to
// Swap back to the original identity later.
AS400Credential cr = pt.swap(true);

// Create a second AS400 object 
AS400 system2 = new AS400();   
CommandCall cmd = new CommandCall(system);   

// Perform work under the swapped identity at this point.  

// Swap back to the original OS/400 thread identity.
cr.swap();

// Clean up the credentials.
cr.destroy();
pt.destroy();     

system.disconnectAllServices();   
system2.disconnectAllServices();   
End of change