<commit_before>/*
 * $Log: IfsaProviderListener.java,v $
 * Revision 1.10  2005-06-13 15:08:37  europe\L190409
 * avoid excessive logging in debug mode
 *
 * Revision 1.9  2005/06/13 12:43:03  <NAME> <<EMAIL>>
 * added support for pooled sessions and for XA-support
 *
 * Revision 1.8  2005/02/17 09:45:30  <NAME> <<EMAIL>>
 * increased logging
 *
 * Revision 1.7  2005/01/13 08:55:37  <NAME> <<EMAIL>>
 * Make threadContext-attributes available in PipeLineSession
 *
 * Revision 1.6  2004/09/22 07:03:36  <NAME> <<EMAIL>>
 * Added logstatements for closing receiver and session
 *
 * Revision 1.5  2004/09/22 06:48:08  <NAME> <<EMAIL>>
 * Changed loglevel in getStringFromRawMessage to warn
 *
 * Revision 1.4  2004/07/19 09:50:03  <NAME> <<EMAIL>>
 * try to send exceptionmessage as reply when sending reply results in exception
 *
 * Revision 1.3  2004/07/15 07:43:04  <NAME> <<EMAIL>>
 * updated javadoc
 *
 * Revision 1.2  2004/07/08 12:55:57  <NAME> <<EMAIL>>
 * logging refinements
 *
 * Revision 1.1  2004/07/05 14:28:38  <NAME> <<EMAIL>>
 * First version, converted from IfsaServiceListener
 *
 * Revision 1.4  2004/03/26 07:25:42  <NAME> <<EMAIL>>
 * Updated erorhandling
 *
 * Revision 1.3  2004/03/24 15:27:24  <NAME> <<EMAIL>>
 * solved uncaught exception in error message
 *
 */
package nl.nn.adapterframework.extensions.ifsa;

import nl.nn.adapterframework.core.IPullingListener;
import nl.nn.adapterframework.core.ListenerException;
import nl.nn.adapterframework.core.PipeLineResult;
import nl.nn.adapterframework.core.INamedObject;

import com.ing.ifsa.IFSAMessage;
import com.ing.ifsa.IFSAPoisonMessage;
import com.ing.ifsa.IFSAHeader;
import com.ing.ifsa.IFSAServiceName;
import com.ing.ifsa.IFSAServicesProvided;

import java.util.HashMap;
import java.util.Date;

import javax.jms.Message;
import javax.jms.QueueSession;
import javax.jms.QueueReceiver;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.TextMessage;
import javax.jms.JMSException;


import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * Implementation of {@link IPullingListener} that acts as an IFSA-service.
 * 
 * There is no need or possibility to set the ServiceId as the Provider will receive all messages
 * for this Application on the same serviceQueue.
 *
 * <p><b>Configuration:</b>
 * <table border="1">
 * <tr><th>attributes</th><th>description</th><th>default</th></tr>
 * <tr><td>classname</td><td>nl.nn.adapterframework.extensions.ifsa.IfsaProviderListener</td><td>&nbsp;</td></tr>
 * <tr><td>{@link #setName(String) name}</td><td>name of the object</td><td>&nbsp;</td></tr>
 * <tr><td>{@link #setApplicationId(String) applicationId}</td><td>the ApplicationID, in the form of "IFSA://<i>AppId</i>"</td><td>&nbsp;</td></tr>
 * <tr><td>{@link #setMessageProtocol(String) messageProtocol}</td><td>protocol of IFSA-Service to be called. Possible values 
 * <ul>
 *   <li>"FF": Fire & Forget protocol</li>
 *   <li>"RR": Request-Reply protocol</li>
 * </ul></td><td>&nbsp;</td></tr>
 * </table>
 * @author <NAME>
 * @since 4.2
 */
public class IfsaProviderListener extends IfsaFacade implements IPullingListener, INamedObject {
	public static final String version = "$RCSfile: IfsaProviderListener.java,v $ $Revision: 1.10 $ $Date: 2005-06-13 15:08:37 $";

    private final static String THREAD_CONTEXT_SESSION_KEY = "session";
    private final static String THREAD_CONTEXT_RECEIVER_KEY = "receiver";

    private String commitOnState;
    private long timeOut = 3000;

	public IfsaProviderListener() {
		super(true); //instantiate as a provider
	}

	public void open() throws ListenerException {
		try {
			openService();
			
			IFSAServicesProvided services = getServiceQueue().getIFSAServicesProvided();

			for (int i = 0; i < services.getNumberOfServices(); i++) {
				IFSAServiceName service = services.getService(i);
				
				String protocol=(service.IsFireAndForgetService() ? "Fire and Forget" : "Request/Reply");
				log.info(getLogPrefix()+"providing ServiceName ["+service.getServiceName()+"] ServiceGroup [" + service.getServiceGroup()+"] protocol [" + protocol+"] ServiceVersion [" + service.getServiceVersion()+"]");				
			}
		} catch (Exception e) {
			throw new ListenerException(getLogPrefix(),e);
		}
	}
	
	protected QueueSession getSession(HashMap threadContext) throws ListenerException {
		if (isSessionsArePooled()) {
			try {
				return createSession();
			} catch (IfsaException e) {
				throw new ListenerException(getLogPrefix()+"exception creating QueueSession", e);
			}
		} else {
			return (QueueSession) threadContext.get(THREAD_CONTEXT_SESSION_KEY);
		}
	}
	
	protected void releaseSession(QueueSession session) throws ListenerException {
		if (isSessionsArePooled() && session != null) {
			try {
				session.close();
				if (log.isDebugEnabled() && !isSessionsArePooled()) {
					log.debug("closed session for receiver ["+getName()+"]");
				}
			} catch (Exception e) {
				throw new ListenerException(getLogPrefix()+"exception closing QueueSession", e);
			}
		}
	}

	protected QueueReceiver getReceiver(HashMap threadContext, QueueSession session) throws ListenerException {
		if (isSessionsArePooled()) {
			try {
				return getServiceReceiver(session);
			} catch (IfsaException e) {
				throw new ListenerException(getLogPrefix()+"exception creating QueueReceiver", e);
			}
		} else {
			return (QueueReceiver) threadContext.get(THREAD_CONTEXT_RECEIVER_KEY);
		}
	}
	
	protected void releaseReceiver(QueueReceiver receiver) throws ListenerException {
		if (isSessionsArePooled() && receiver != null) {
			try {
				receiver.close();
				if (log.isDebugEnabled() && !isSessionsArePooled()) {
					log.debug("closed QueueReceiver ["+getName()+"]");
				}
			} catch (Exception e) {
				throw new ListenerException(getLogPrefix()+"exception closing QueueReceiver", e);
			}
		}
	}
	
	
	
	public HashMap openThread() throws ListenerException {
		HashMap threadContext = new HashMap();
	
		try {
			if (!isSessionsArePooled()) {
				QueueSession session = createSession();
				threadContext.put(THREAD_CONTEXT_SESSION_KEY, session);
	
				QueueReceiver receiver;
				receiver = getServiceReceiver(session);
				threadContext.put(THREAD_CONTEXT_RECEIVER_KEY, receiver);<commit_msg>avoid excessive logging<commit_after>/*
 * $Log: IfsaProviderListener.java,v $
 * Revision 1.11  2005-06-20 09:14:17  europe\L190409
 * avoid excessive logging
 *
 * Revision 1.10  2005/06/13 15:08:37  <NAME> <<EMAIL>>
 * avoid excessive logging in debug mode
 *
 * Revision 1.9  2005/06/13 12:43:03  <NAME> <<EMAIL>>
 * added support for pooled sessions and for XA-support
 *
 * Revision 1.8  2005/02/17 09:45:30  <NAME> <<EMAIL>>
 * increased logging
 *
 * Revision 1.7  2005/01/13 08:55:37  <NAME> <<EMAIL>>
 * Make threadContext-attributes available in PipeLineSession
 *
 * Revision 1.6  2004/09/22 07:03:36  <NAME> <<EMAIL>>
 * Added logstatements for closing receiver and session
 *
 * Revision 1.5  2004/09/22 06:48:08  <NAME> <<EMAIL>>
 * Changed loglevel in getStringFromRawMessage to warn
 *
 * Revision 1.4  2004/07/19 09:50:03  <NAME> <<EMAIL>>
 * try to send exceptionmessage as reply when sending reply results in exception
 *
 * Revision 1.3  2004/07/15 07:43:04  <NAME> <<EMAIL>>
 * updated javadoc
 *
 * Revision 1.2  2004/07/08 12:55:57  <NAME> <<EMAIL>>
 * logging refinements
 *
 * Revision 1.1  2004/07/05 14:28:38  <NAME> <<EMAIL>>
 * First version, converted from IfsaServiceListener
 *
 * Revision 1.4  2004/03/26 07:25:42  <NAME> <<EMAIL>>
 * Updated erorhandling
 *
 * Revision 1.3  2004/03/24 15:27:24  <NAME> <<EMAIL>>
 * solved uncaught exception in error message
 *
 */
package nl.nn.adapterframework.extensions.ifsa;

import nl.nn.adapterframework.core.IPullingListener;
import nl.nn.adapterframework.core.ListenerException;
import nl.nn.adapterframework.core.PipeLineResult;
import nl.nn.adapterframework.core.INamedObject;

import com.ing.ifsa.IFSAMessage;
import com.ing.ifsa.IFSAPoisonMessage;
import com.ing.ifsa.IFSAHeader;
import com.ing.ifsa.IFSAServiceName;
import com.ing.ifsa.IFSAServicesProvided;

import java.util.HashMap;
import java.util.Date;

import javax.jms.Message;
import javax.jms.QueueSession;
import javax.jms.QueueReceiver;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.TextMessage;
import javax.jms.JMSException;


import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * Implementation of {@link IPullingListener} that acts as an IFSA-service.
 * 
 * There is no need or possibility to set the ServiceId as the Provider will receive all messages
 * for this Application on the same serviceQueue.
 *
 * <p><b>Configuration:</b>
 * <table border="1">
 * <tr><th>attributes</th><th>description</th><th>default</th></tr>
 * <tr><td>classname</td><td>nl.nn.adapterframework.extensions.ifsa.IfsaProviderListener</td><td>&nbsp;</td></tr>
 * <tr><td>{@link #setName(String) name}</td><td>name of the object</td><td>&nbsp;</td></tr>
 * <tr><td>{@link #setApplicationId(String) applicationId}</td><td>the ApplicationID, in the form of "IFSA://<i>AppId</i>"</td><td>&nbsp;</td></tr>
 * <tr><td>{@link #setMessageProtocol(String) messageProtocol}</td><td>protocol of IFSA-Service to be called. Possible values 
 * <ul>
 *   <li>"FF": Fire & Forget protocol</li>
 *   <li>"RR": Request-Reply protocol</li>
 * </ul></td><td>&nbsp;</td></tr>
 * </table>
 * @author <NAME>
 * @since 4.2
 */
public class IfsaProviderListener extends IfsaFacade implements IPullingListener, INamedObject {
	public static final String version = "$RCSfile: IfsaProviderListener.java,v $ $Revision: 1.11 $ $Date: 2005-06-20 09:14:17 $";

    private final static String THREAD_CONTEXT_SESSION_KEY = "session";
    private final static String THREAD_CONTEXT_RECEIVER_KEY = "receiver";

    private String commitOnState;
    private long timeOut = 3000;

	public IfsaProviderListener() {
		super(true); //instantiate as a provider
	}

	public void open() throws ListenerException {
		try {
			openService();
			
			IFSAServicesProvided services = getServiceQueue().getIFSAServicesProvided();

			for (int i = 0; i < services.getNumberOfServices(); i++) {
				IFSAServiceName service = services.getService(i);
				
				String protocol=(service.IsFireAndForgetService() ? "Fire and Forget" : "Request/Reply");
				log.info(getLogPrefix()+"providing ServiceName ["+service.getServiceName()+"] ServiceGroup [" + service.getServiceGroup()+"] protocol [" + protocol+"] ServiceVersion [" + service.getServiceVersion()+"]");				
			}
		} catch (Exception e) {
			throw new ListenerException(getLogPrefix(),e);
		}
	}
	
	protected QueueSession getSession(HashMap threadContext) throws ListenerException {
		if (isSessionsArePooled()) {
			try {
				return createSession();
			} catch (IfsaException e) {
				throw new ListenerException(getLogPrefix()+"exception creating QueueSession", e);
			}
		} else {
			return (QueueSession) threadContext.get(THREAD_CONTEXT_SESSION_KEY);
		}
	}
	
	protected void releaseSession(QueueSession session) throws ListenerException {
		if (isSessionsArePooled() && session != null) {
			try {
				session.close();
				// do not write to log, this occurs too often
			} catch (Exception e) {
				throw new ListenerException(getLogPrefix()+"exception closing QueueSession", e);
			}
		}
	}

	protected QueueReceiver getReceiver(HashMap threadContext, QueueSession session) throws ListenerException {
		if (isSessionsArePooled()) {
			try {
				return getServiceReceiver(session);
			} catch (IfsaException e) {
				throw new ListenerException(getLogPrefix()+"exception creating QueueReceiver", e);
			}
		} else {
			return (QueueReceiver) threadContext.get(THREAD_CONTEXT_RECEIVER_KEY);
		}
	}
	
	protected void releaseReceiver(QueueReceiver receiver) throws ListenerException {
		if (isSessionsArePooled() && receiver != null) {
			try {
				receiver.close();
				// do not write to log, this occurs too often
			} catch (Exception e) {
				throw new ListenerException(getLogPrefix()+"exception closing QueueReceiver", e);
			}
		}
	}
	
	
	
	public HashMap openThread() throws ListenerException {
		HashMap threadContext = new HashMap();
	
		try {
			if (!isSessionsArePooled()) {
				QueueSession session = createSession();
				threadContext.put(THREAD_CONTEXT_SESSION_KEY, session);
	
				QueueReceiver receiver;
				receiver = getServiceReceiver(session);
				threadContext.put(THREAD_CONTEXT_RECEIVER_KEY, receiver);
{'fraction_non_alphanumeric': 0.13245564742714938, 'fraction_numerical': 0.037328409729469374, 'mean_word_length': 4.544281263907432, 'pattern_counts': {'":': 4, '<': 182, '<?xml version=': 0, '>': 180, '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': '377988', 'n_tokens_mistral': 4558, 'n_tokens_neox': 3977, 'n_words': 954}