Example: Using a profile token credential to swap the i5/OS thread identity

Note: Read the Code example disclaimer for important legal information.

The following code example shows you how to use a profile token credential to swap the i5/OS™ thread identity and perform work on behalf of a specific user:

     // Prepare to work with the local AS/400 system.
     AS400 system = new AS400("localhost", "*CURRENT", "*CURRENT");

     // Create a single-use ProfileTokenCredential with a 60 second timeout.
     // A valid user ID and password must be substituted.
     ProfileTokenCredential pt = new ProfileTokenCredential();
     pt.setSystem(system);
     pt.setTimeoutInterval(60);
     pt.setTokenType(ProfileTokenCredential.TYPE_SINGLE_USE);

     pt.setTokenExtended("USERID", "PASSWORD");

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

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

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

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