/** 
 * 
 * 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.store.vm;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.message.ActiveMQMessage;
import org.codehaus.activemq.message.MessageAck;
import org.codehaus.activemq.service.MessageIdentity;
import org.codehaus.activemq.service.QueueMessageContainer;
import org.codehaus.activemq.store.MessageStore;

import javax.jms.JMSException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * An implementation of {@link org.codehaus.activemq.store.MessageStore} which uses a
 *
 * @version $Revision: 1.2 $
 */
public class VMMessageStore implements MessageStore {
    private static final Log log = LogFactory.getLog(VMMessageStore.class);

    protected Map messageTable;

    public VMMessageStore() {
        this(new LinkedHashMap());
    }

    public VMMessageStore(LinkedHashMap messageTable) {
        this.messageTable = Collections.synchronizedMap(messageTable);
    }

    public MessageIdentity addMessage(ActiveMQMessage message) throws JMSException {
        messageTable.put(message.getJMSMessageID(), message);
        return message.getJMSMessageIdentity();
    }

    public ActiveMQMessage getMessage(MessageIdentity identity) throws JMSException {
        String messageID = identity.getMessageID();
        return (ActiveMQMessage) messageTable.get(messageID);
    }

    public void removeMessage(MessageIdentity identity, MessageAck ack) throws JMSException {
        String messageID = identity.getMessageID();
        messageTable.remove(messageID);
    }

    public void recover(QueueMessageContainer container) throws JMSException {
    }

    public void start() throws JMSException {
    }

    public void stop() throws JMSException {
        messageTable.clear();
    }

    public void removeAllMessages() throws JMSException {
        messageTable.clear();
    }

}
