/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * 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 org.codehaus.activemq.filter;

import org.codehaus.activemq.message.ActiveMQMessage;

import javax.jms.JMSException;
import javax.jms.Message;


/**
 * Checks that messages are not filtered by NoLocal
 *
 * @version $Revision: 1.1 $
 */
public class NoLocalFilter implements Filter {

    private String clientId;

    /**
     * Construct a NoLocalFilter
     *
     * @param newClientId
     */
    public NoLocalFilter(String newClientId) {
        this.clientId = newClientId;
    }

    /**
     * ensure messages are filtered if they have the same clientId
     * as the filter
     *
     * @param message - the message to match
     * @return true if the message has a different clientId to the filter
     * @throws JMSException
     */
    public boolean matches(Message message) throws JMSException {
        if (message != null && message instanceof ActiveMQMessage) {
            ActiveMQMessage activeMQMessage = (ActiveMQMessage) message;

            // TODO we should have a mechanism to
            // maintain a list of clientIDs through which a message has travelled
            // so we can filter out recursive graphs

            String producerID = activeMQMessage.getProducerID();
            if (producerID != null) {
                if (producerID.equals(clientId)) {
                    return false;
                }
            }
            if (clientId.equals(activeMQMessage.getJMSClientID())) {
                return false;
            }
        }
        return true;
    }

    public boolean isWildcard() {
        return false;
    }
}
