All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bitronix.tm.resource.jms.MessageProducerConsumerKey Maven / Gradle / Ivy

There is a newer version: 62
Show newest version
/*
 * Copyright (C) 2006-2013 Bitronix Software (http://www.bitronix.be)
 *
 * 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 bitronix.tm.resource.jms;

import bitronix.tm.internal.BitronixRuntimeException;

import jakarta.jms.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.Queue;
import jakarta.jms.Topic;

/**
 * JMS destination wrapper optimized for use with hashed collections where it is the key and a
 * {@link jakarta.jms.MessageProducer} or a {@link jakarta.jms.MessageConsumer} is the value.
 *
 * @author Ludovic Orban
 */
public class MessageProducerConsumerKey
{

	private final Destination destination;
	private final String messageSelector;
	private final Boolean noLocal;

	/**
	 * Constructor MessageProducerConsumerKey creates a new MessageProducerConsumerKey instance.
	 *
	 * @param destination
	 * 		of type Destination
	 */
	public MessageProducerConsumerKey(Destination destination)
	{
		this.destination = destination;
		this.messageSelector = null;
		this.noLocal = null;
	}

	/**
	 * Constructor MessageProducerConsumerKey creates a new MessageProducerConsumerKey instance.
	 *
	 * @param destination
	 * 		of type Destination
	 * @param messageSelector
	 * 		of type String
	 */
	public MessageProducerConsumerKey(Destination destination, String messageSelector)
	{
		this.destination = destination;
		this.messageSelector = messageSelector;
		this.noLocal = null;
	}

	/**
	 * Constructor MessageProducerConsumerKey creates a new MessageProducerConsumerKey instance.
	 *
	 * @param destination
	 * 		of type Destination
	 * @param messageSelector
	 * 		of type String
	 * @param noLocal
	 * 		of type boolean
	 */
	public MessageProducerConsumerKey(Destination destination, String messageSelector, boolean noLocal)
	{
		this.destination = destination;
		this.messageSelector = messageSelector;
		this.noLocal = noLocal;
	}

	/**
	 * Method hashCode ...
	 *
	 * @return int
	 */
	@Override
	public int hashCode()
	{
		return hash(getDestinationName()) + hash(messageSelector) + hash(noLocal);
	}

	/**
	 * Method equals ...
	 *
	 * @param obj
	 * 		of type Object
	 *
	 * @return boolean
	 */
	@Override
	public boolean equals(Object obj)
	{
		if (obj instanceof MessageProducerConsumerKey)
		{
			MessageProducerConsumerKey otherKey = (MessageProducerConsumerKey) obj;

			if (!areEquals(getDestinationName(), otherKey.getDestinationName()))
			{
				return false;
			}
			if (!areEquals(messageSelector, otherKey.messageSelector))
			{
				return false;
			}
			return areEquals(noLocal, otherKey.noLocal);
		}
		return false;
	}

	/**
	 * Method areEquals ...
	 *
	 * @param o1
	 * 		of type Object
	 * @param o2
	 * 		of type Object
	 *
	 * @return boolean
	 */
	private static boolean areEquals(Object o1, Object o2)
	{
		if (o1 == null && o2 == null)
		{
			return true;
		}
		if (o1 != null && o2 == null)
		{
			return false;
		}
		if (o1 == null)
		{
			return false;
		}
		return o1.equals(o2);
	}

	/**
	 * Method toString ...
	 *
	 * @return String
	 */
	@Override
	public String toString()
	{
		return "a MessageProducerConsumerKey on " + destination +
		       (messageSelector == null ? "" : (" with selector '" + messageSelector) + "'") +
		       (noLocal == null ? "" : (" and noLocal=" + noLocal));
	}

	/**
	 * Method hash ...
	 *
	 * @param o
	 * 		of type Object
	 *
	 * @return int
	 */
	private static int hash(Object o)
	{
		if (o == null)
		{
			return 0;
		}
		return o.hashCode();
	}

	/**
	 * Method getDestinationName returns the destinationName of this MessageProducerConsumerKey object.
	 *
	 * @return the destinationName (type String) of this MessageProducerConsumerKey object.
	 */
	private String getDestinationName()
	{
		if (destination == null)
		{
			return null;
		}
		else if (destination instanceof Queue)
		{
			try
			{
				return ((Queue) destination).getQueueName();
			}
			catch (JMSException ex)
			{
				throw new BitronixRuntimeException("error getting queue name of " + destination, ex);
			}
		}
		else if (destination instanceof Topic)
		{
			try
			{
				return ((Topic) destination).getTopicName();
			}
			catch (JMSException ex)
			{
				throw new BitronixRuntimeException("error getting topic name of " + destination, ex);
			}
		}
		else
		{
			throw new IllegalArgumentException("unsupported destination: " + destination);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy