20.3 Notification Example

The following code example shows how one might use the Notification service to send an e-mail message using a pre-defined system template. To get a reference to the SOAP endpoint for the Notification service, a call is made to the getNotificationStub() method. After acquiring the stub interface, the code sets the e-mail notification template as well as values for the built-in tokens in the template. In addition, the code specifies values for the requestTitle and initiatorFullName tokens. For each token, the code creates an Entry object. Once all of the entries have been created, it packages the entry array into a map of type NotificationMap, which is then passed to the sendNotification method on the stub.

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.xml.rpc.Stub;
import java.rmi.RemoteException;
// Notification imports
import com.novell.ws.client.notification.IRemoteNotification;
import com.novell.ws.client.notification.BuiltInTokens;
import com.novell.ws.client.notification.Entry;
import com.novell.ws.client.notification.EntryArray;
import com.novell.ws.client.notification.StringArray;
import com.novell.ws.client.notification.NotificationMap;
import com.novell.ws.client.notification.IRemoteNotification;
import com.novell.ws.client.notification.NotificationService;

public class NotificationTest
  private static final int LOCALHOST    = 0;  // localhost
  private static final int TESTSERVER      = 1;  // testserver
  private static final int SELECTED_URL     = TESTSERVER;

      private String [] SERVER_URLS = {
  private String url = SERVER_URLS[SELECTED_URL];
  private String username = "cn=admin,ou=idmsample,o=novell";
  private String password = "test";

  public void emailNotificationTestCase()
  throws Exception
        System.out.println("\nCalling emailNotificationTestCase() test

    String targetEmailAddress = "jsmith@somewhere.com";
            // Get the notification stub
            IRemoteNotification notificationStub =
getNotificationStub(url, username, password);
            BuiltInTokens builtInTokens = new BuiltInTokens();
            // Set the To: entry
            Entry to = new Entry();
            StringArray arr = new StringArray(new
String[]{targetEmailAddress} );
            // Set which email template to use : list in iManager
(Workflow Admin->Email Templates)
            Entry notificationTemplate = new Entry();

            // Use one of the email templates specifying DN
            String EMAIL_TEMPLATE_NAME = "Provisioning Notification";
            String templateDN = "cn=" + EMAIL_TEMPLATE_NAME +
",cn=Default Notification Collection,cn=Security";
            arr = new StringArray(new String[]{templateDN} );
            // Substitute key values defined in email templates
            Entry token1 = new Entry();
            token1.setKey("requestTitle"); // key is %requestTitle%
            arr = new StringArray(new String[]{"Sample Email using
Notification Web Service"} );
            Entry token2 = new Entry();
            arr = new StringArray(new String[]{username} );
            // Setup the notification map
            NotificationMap map = new NotificationMap();
            Entry[] entries = new
            EntryArray entryArray = new EntryArray();
            // Make the notification endpoint call
        catch(RemoteException error) 
            System.out.println(error.getMessage() );
            throw new Exception(error.getMessage() );

     * Method to obtain the remote interface to the Notification
     * @param _url
     * @param _username
     * @param _password
     * @return IRemoteNotification interface 
     * @throws Exception
    private IRemoteNotification getNotificationStub(String _url,
String _username, String _password)
    throws Exception
        Properties properties = new Properties();

        String lookup =

        InitialContext ctx = new InitialContext();
        NotificationService svc = (NotificationService)

        Stub stub = (Stub)svc.getIRemoteNotificationPort();

        stub._setProperty(Stub.USERNAME_PROPERTY, _username);
        stub._setProperty(Stub.PASSWORD_PROPERTY, _password);
        stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, _url);

        return (IRemoteNotification) stub;