<commit_before>/*
   Copyright 2013 Nationale-Nederlanden

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nl.nn.adapterframework.extensions.tibco;

import java.util.Enumeration;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;

import nl.nn.adapterframework.util.CredentialFactory;
import nl.nn.adapterframework.util.LogUtil;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.tibco.tibjms.admin.QueueInfo;
import com.tibco.tibjms.admin.TibjmsAdmin;
import com.tibco.tibjms.admin.TibjmsAdminException;

/**
 * Some utilities for working with TIBCO.
 * 
 * @author <NAME>
 */
public class TibcoUtils {
	static Logger log = LogUtil.getLogger(TibcoUtils.class);

	/**
	 * return -1: no message found
	 * return -2: message found, but not of type Message.
	 */
	public static long getQueueFirstMessageAge(String provUrl, String authAlias,
			String userName, String password, String queueName)
			throws JMSException, TibjmsAdminException {
		String url = StringUtils.replace(provUrl, "tibjmsnaming:", "tcp:");
		CredentialFactory cf = new CredentialFactory(authAlias, userName,
				password);
		Connection connection = null;
		Session jSession = null;
		TibjmsAdmin admin = null;
		try {
			admin = new TibjmsAdmin(url, cf.getUsername(), cf.getPassword());
			QueueInfo qInfo = admin.getQueue(queueName);
			if (qInfo.getPendingMessageCount() > 0) {
				ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(
						url);
				connection = factory.createConnection(cf.getUsername(),
						cf.getPassword());
				jSession = connection.createSession(false,
						javax.jms.Session.AUTO_ACKNOWLEDGE);
				return getQueueFirstMessageAge(jSession, queueName);
			} else {
				return -1;
			}
		} finally {
			if (admin != null) {
				try {
					admin.close();
				} catch (TibjmsAdminException e) {
					log.warn("Exception on closing Tibjms Admin", e);
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					log.warn("Exception on closing connection", e);
				}
			}
		}
	}

	protected static long getQueueFirstMessageAge(Session jSession,
			String queueName) throws JMSException {
		return getQueueFirstMessageAge(jSession, queueName, System.currentTimeMillis());
	}

	
	/**
	 * return -1: no message found
	 * return -2: message found, but not of type Message.
	 */
	protected static long getQueueFirstMessageAge(Session jSession,
			String queueName, long currentTime) throws JMSException {
		Queue queue = jSession.createQueue(queueName);
		QueueBrowser queueBrowser = jSession.createBrowser(queue);
		Enumeration enm = queueBrowser.getEnumeration();
		if (enm.hasMoreElements()) {
			Object o = enm.nextElement();
			if (o instanceof Message) {
				Message msg = (Message) o;
				long jmsTimestamp = msg.getJMSTimestamp();
				return currentTime - jmsTimestamp;
			} else {
				log.warn("message was not of type Message, but ["
						+ o.getClass().getName() + "]");
				return -2;
			}
		} else {
			return -1;
		}
	}
}<commit_msg>add MessageSelector argument to getQueueFirstMessageAge method<commit_after>/*
   Copyright 2013 Nationale-Nederlanden

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nl.nn.adapterframework.extensions.tibco;

import java.util.Enumeration;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;

import nl.nn.adapterframework.util.CredentialFactory;
import nl.nn.adapterframework.util.LogUtil;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.tibco.tibjms.admin.TibjmsAdminException;

/**
 * Some utilities for working with TIBCO.
 * 
 * @author <NAME>
 */
public class TibcoUtils {
	static Logger log = LogUtil.getLogger(TibcoUtils.class);

	public static long getQueueFirstMessageAge(String provUrl,
			String authAlias, String userName, String password, String queueName)
			throws JMSException, TibjmsAdminException {
		return getQueueFirstMessageAge(provUrl, authAlias, userName, password,
				queueName, null);
	}

	/**
	 * return -1: no message found
	 * return -2: message found, but not of type Message.
	 */
	public static long getQueueFirstMessageAge(String provUrl,
			String authAlias, String userName, String password,
			String queueName, String messageSelector) throws JMSException,
			TibjmsAdminException {
		String url = StringUtils.replace(provUrl, "tibjmsnaming:", "tcp:");
		CredentialFactory cf = new CredentialFactory(authAlias, userName,
				password);
		Connection connection = null;
		Session jSession = null;
		try {
			ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(
					url);
			connection = factory.createConnection(cf.getUsername(),
					cf.getPassword());
			jSession = connection.createSession(false,
					javax.jms.Session.AUTO_ACKNOWLEDGE);
			return getQueueFirstMessageAge(jSession, queueName, messageSelector);
		} finally {
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					log.warn("Exception on closing connection", e);
				}
			}
		}
	}

	protected static long getQueueFirstMessageAge(Session jSession,
			String queueName) throws JMSException {
		return getQueueFirstMessageAge(jSession, queueName, null);
	}

	protected static long getQueueFirstMessageAge(Session jSession,
			String queueName, String messageSelector) throws JMSException {
		return getQueueFirstMessageAge(jSession, queueName, messageSelector,
				System.currentTimeMillis());
	}

	protected static long getQueueFirstMessageAge(Session jSession,
			String queueName, long currentTime) throws JMSException {
		return getQueueFirstMessageAge(jSession, queueName, null, currentTime);
	}

	protected static long getQueueFirstMessageAge(Session jSession,
			String queueName, String messageSelector, long currentTime)
			throws JMSException {
		Queue queue = jSession.createQueue(queueName);
		QueueBrowser queueBrowser;
		if (messageSelector == null) {
			queueBrowser = jSession.createBrowser(queue);
		} else {
			queueBrowser = jSession.createBrowser(queue, messageSelector);
		}
		Enumeration enm = queueBrowser.getEnumeration();
		if (enm.hasMoreElements()) {
			Object o = enm.nextElement();
			if (o instanceof Message) {
				Message msg = (Message) o;
				long jmsTimestamp = msg.getJMSTimestamp();
				return currentTime - jmsTimestamp;
			} else {
				log.warn("message was not of type Message, but ["
						+ o.getClass().getName() + "]");
				return -2;
			}
		} else {
			return -1;
		}
	}
}
{'fraction_non_alphanumeric': 0.0935058281029845, 'fraction_numerical': 0.0038427052645062126, 'mean_word_length': 4.106605624591236, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1118577', 'n_tokens_mistral': 2602, 'n_tokens_neox': 2296, 'n_words': 656}