<commit_before>/*
 * 
 * 
 * Copyright (C) 2008 Nortel Corp., certain elements licensed under a Contributor Agreement.  
 * Contributors retain copyright to elements licensed under a Contributor Agreement.
 * Licensed to the User under the LGPL license.
 * 
 *
 */

package org.sipfoundry.sipxbridge;

import gov.nist.javax.sip.SipStackExt;
import gov.nist.javax.sip.SipStackImpl;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentSkipListSet;

import javax.sip.Dialog;
import javax.sip.DialogState;
import javax.sip.ServerTransaction;
import javax.sip.SipProvider;
import javax.sip.header.ViaHeader;
import javax.sip.message.Request;

import org.apache.log4j.Logger;

/**
 * Manages a set of back to back user agents indexed by call id.
 * 
 * @author <NAME>
 * 
 * 
 */
public class BackToBackUserAgentFactory {

	private static final Logger logger = Logger
			.getLogger(BackToBackUserAgentFactory.class);
	
	private ConcurrentSkipListSet<BackToBackUserAgent> backToBackUserAgentTable = new ConcurrentSkipListSet<BackToBackUserAgent> ();
	
	class Scanner extends TimerTask {

	    @Override
	    public void run() {
	        try {
	            Iterator<BackToBackUserAgent> iter = backToBackUserAgentTable.iterator();
	            boolean removed = false;
	            while( iter.hasNext() ) {
	                BackToBackUserAgent b2bua = iter.next();
	                if ( b2bua.isPendingTermination() ) {
	                    b2bua.cleanUp();
	                    logger.debug("Removing BackToBackUserAgent");
	                    iter.remove();
	                    removed = true;
	                }
	            }
	            /*
	             * Check for dialog leaks. This us useful for unit testing.
	             */
	            if ( logger.isDebugEnabled() && removed ) {
	                logger.debug("Dialog Table : ");
	                for (Dialog dialog : ( (SipStackExt) ProtocolObjects.getSipStack()).getDialogs() ) {
	                    logger.debug("Dialog " + dialog + " dialogState = " + dialog.getState() ); 
	                    if ( dialog.getState() != DialogState.TERMINATED ) {
	                        logger.debug("Dialog was allocated at " + DialogContext.get(dialog).getCreationPointStackTrace());
	                    }
	                }
	            }
	        } catch (Exception ex) {
	            logger.error("Exception caught in Timer task",ex);
	        }
	    }


	}
	
	public BackToBackUserAgentFactory() {
	    Gateway.getTimer().schedule(new Scanner(), 10*1000, 10*1000);
	}

	/**
	 * Get a new B2bua instance or return an existing one for a call Id.
	 * 
	 * @param provider
	 *            -- provider associated with request.
	 * @param serverTransaction
	 *            -- server transaction.
	 * @param dialog
	 *            -- dialog for request.
	 */

	synchronized BackToBackUserAgent getBackToBackUserAgent(
			SipProvider provider, Request request,<commit_msg>XX-7653: Not related directly to issue but noticed a problem in log file. Must check for null before accessing dialog context.<commit_after>/*
 * 
 * 
 * Copyright (C) 2008 Nortel Corp., certain elements licensed under a Contributor Agreement.  
 * Contributors retain copyright to elements licensed under a Contributor Agreement.
 * Licensed to the User under the LGPL license.
 * 
 *
 */

package org.sipfoundry.sipxbridge;

import gov.nist.javax.sip.SipStackExt;
import gov.nist.javax.sip.SipStackImpl;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentSkipListSet;

import javax.sip.Dialog;
import javax.sip.DialogState;
import javax.sip.ServerTransaction;
import javax.sip.SipProvider;
import javax.sip.header.ViaHeader;
import javax.sip.message.Request;

import org.apache.log4j.Logger;

/**
 * Manages a set of back to back user agents indexed by call id.
 * 
 * @author <NAME>
 * 
 * 
 */
public class BackToBackUserAgentFactory {

	private static final Logger logger = Logger
			.getLogger(BackToBackUserAgentFactory.class);
	
	private ConcurrentSkipListSet<BackToBackUserAgent> backToBackUserAgentTable = new ConcurrentSkipListSet<BackToBackUserAgent> ();
	
	class Scanner extends TimerTask {

	    @Override
	    public void run() {
	        try {
	            Iterator<BackToBackUserAgent> iter = backToBackUserAgentTable.iterator();
	            boolean removed = false;
	            while( iter.hasNext() ) {
	                BackToBackUserAgent b2bua = iter.next();
	                if ( b2bua.isPendingTermination() ) {
	                    b2bua.cleanUp();
	                    logger.debug("Removing BackToBackUserAgent");
	                    iter.remove();
	                    removed = true;
	                }
	            }
	            /*
	             * Check for dialog leaks. This us useful for unit testing.
	             */
	            if ( logger.isDebugEnabled() && removed ) {
	                logger.debug("Dialog Table : ");
	                for (Dialog dialog : ( (SipStackExt) ProtocolObjects.getSipStack()).getDialogs() ) {
	                    logger.debug("Dialog " + dialog + " dialogState = " + dialog.getState() ); 
	                    if ( dialog.getState() != DialogState.TERMINATED ) {
	                    	if ( DialogContext.get(dialog) != null ) {
	                    		logger.debug("Dialog was allocated at " + DialogContext.get(dialog).getCreationPointStackTrace());
	                    	} else {
	                    		logger.debug("Null dialog context!");
	                    	}
	                    }
	                }
	            }
	        } catch (Exception ex) {
	            logger.error("Exception caught in Timer task",ex);
	        }
	    }


	}
	
	public BackToBackUserAgentFactory() {
	    Gateway.getTimer().schedule(new Scanner(), 10*1000, 10*1000);
	}

	/**
	 * Get a new B2bua instance or return an existing one for a call Id.
	 * 
	 * @param provider
	 *            -- provider associated with request.
	 * @param serverTransaction
	 *            -- server transaction.
	 * @param dialog
	 *            -- dialog for request.
	 */

	synchronized BackToBackUserAgent getBackToBackUserAgent(
			SipProvider provider, Request request,
{'fraction_non_alphanumeric': 0.0949367088607595, 'fraction_numerical': 0.007278481012658228, 'mean_word_length': 2.184382871536524, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5453208', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1667, 'n_words': 428}