EMC Developer Network

Session Manager Event Listener

December 2005

Software Environment

FeatureTested on
Operating SystemWindows 2000 Server SP4
CompilerSun JDK 1.4.2_08
RuntimeSun JRE 1.4.2_08
DFC5.3 SP1
Content Server5.3 SP1

Abstract

This code snippet demonstrates the use of IDfSessionManagerEventListener to trap the session creation and destruction events.

Snippet


/*
 * Created on Dec 15, 2005
 *
 * EMC Documentum Developer Program 2005
 */
package com.documentum.devprog.snippets.session;

import com.documentum.fc.common.DfException;
import com.documentum.fc.common.IDfLoginInfo;

import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;
import com.documentum.fc.client.IDfSessionManagerEventListener;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;

public class SessionListener
{
    
    public static void main(String[] args)
    {
        String repoName = "devprog53sp1";
        String username = "dmadmin";
        String password = "dmadmin";
        
        IDfSession sess = null;
        IDfSessionManager sessMgr = null;
        
        try
        {
            sessMgr = createSessionManager();
            sessMgr.setListener(new SessionListenerImpl());
            addIdentity(sessMgr,username,password,repoName);
            
            
            
            sess = sessMgr.getSession(repoName);
            //use session
            
            //we release the session here instead of the finally
            //clause to simulate a session destroy event.
            //Its a best practise to always release the session in a finally
            //clause so that its guranteed to be released.
            sessMgr.release(sess);
            
            Thread.sleep(5500);
            
            
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }        

    }
    
    
    /**
     * Creates a new session manager instance. The session manager does not have
     * any identities associated with it.
     * 
     * @return a new session manager object.
     * @throws DfException
     */
    private static IDfSessionManager createSessionManager() throws DfException
    {
        IDfClientX clientX = new DfClientX();
        IDfClient localClient = clientX.getLocalClient();
        IDfSessionManager sessMgr = localClient.newSessionManager();
        return sessMgr;
    }

    /**
     * Adds a new identity to the session manager.
     * 
     */
    private static void addIdentity(IDfSessionManager sm, String username,
            String password, String repoName) throws DfException
    {
        IDfClientX clientX = new DfClientX();

        IDfLoginInfo li = clientX.getLoginInfo();
        li.setUser(username);
        li.setPassword(password);

        // check if session manager already has an identity.
        // if yes, remove it.
        if (sm.hasIdentity(repoName))
        {
            sm.clearIdentity(repoName);
        }

        sm.setIdentity(repoName, li);
    }
    
    
    static class SessionListenerImpl implements IDfSessionManagerEventListener
    {

        public void onSessionCreate(IDfSession sess) throws DfException
        {
            System.out.println("Session created: "  + sess.getSessionId());
            
        }

        public void onSessionDestroy(IDfSession sess) throws DfException
        {
            System.out.println("Session Destroyed: " + sess.getSessionId());
        }
        
    }

}