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

import com.sleepycat.db.Db;
import com.sleepycat.db.DbException;
import com.sleepycat.db.DbTxn;
import com.sleepycat.db.Dbt;
import org.codehaus.activemq.io.WireFormat;
import org.codehaus.activemq.message.ActiveMQMessage;
import org.codehaus.activemq.message.MessageAck;
import org.codehaus.activemq.service.MessageContainer;
import org.codehaus.activemq.service.MessageIdentity;
import org.codehaus.activemq.service.QueueMessageContainer;
import org.codehaus.activemq.store.MessageStore;
import org.codehaus.activemq.util.JMSExceptionHelper;

import javax.jms.JMSException;
import java.io.IOException;

/**
 * An implementation of {@link MessageStore} using
 * <a href="http://www.sleepycat.com/">native C Berkeley DB</a>
 *
 * @version $Revision: 1.3 $
 */
public class BDbMessageStore implements MessageStore {
    private static final int SUCCESS = 0;

    private Db database;
    private WireFormat wireFormat;
    private MessageContainer container;

    public void setMessageContainer(MessageContainer container) {
        this.container = container;
    }

    public MessageIdentity addMessage(ActiveMQMessage message) throws JMSException {
        String messageID = message.getJMSMessageID();
        try {
            Dbt key = createKey(messageID);
            Dbt value = new Dbt(asBytes(message));
            database.put(BDbHelper.getTransaction(), key, value, 0);
            return new MessageIdentity(messageID);
        }
        catch (DbException e) {
            throw JMSExceptionHelper.newJMSException("Failed to broker message: " + messageID + " in container: " + e, e);
        }
        catch (IOException e) {
            throw JMSExceptionHelper.newJMSException("Failed to broker message: " + messageID + " in container: " + e, e);
        }
    }

    public ActiveMQMessage getMessage(MessageIdentity identity) throws JMSException {
        String messageID = identity.getMessageID();
        ActiveMQMessage answer = null;
        try {
            Dbt key = createKey(messageID);
            Dbt value = new Dbt();
            if (database.get(null, key, value, 0) == SUCCESS) {
                answer = extractMessage(value);
            }
            return answer;
        }
        catch (DbException e) {
            throw JMSExceptionHelper.newJMSException("Failed to peek next message after: " + messageID + " from container: " + e, e);
        }
        catch (IOException e) {
            throw JMSExceptionHelper.newJMSException("Failed to peek next message after: " + messageID + " from container: " + e, e);
        }
    }


    public void removeMessage(MessageIdentity identity, MessageAck ack) throws JMSException {
        DbTxn transaction = null;
        String messageID = identity.getMessageID();
        try {
            database.delete(BDbHelper.getTransaction(), createKey(messageID), 0);
        }
        catch (DbException e) {
            throw JMSExceptionHelper.newJMSException("Failed to delete message: " + messageID + " from container: " + e, e);
        }
    }

    public void recover(QueueMessageContainer container) throws JMSException {
        /** TODO */
    }


    public void start() throws JMSException {
    }

    public void stop() throws JMSException {
        try {
            database.close(0);
        }
        catch (DbException e) {
            throw JMSExceptionHelper.newJMSException("Failed to close MessageStore. Reason: " + e, e);
        }
    }


    // Implementation methods
    //-------------------------------------------------------------------------
    protected Dbt createKey(String messageID) {
        Dbt key = new Dbt(asBytes(messageID));
        return key;
    }

    protected ActiveMQMessage extractMessage(Dbt value) throws IOException {
        // we must synchronize access to WireFormat
        synchronized (wireFormat) {
            return (ActiveMQMessage) wireFormat.fromBytes(value.getData());
        }
    }


    protected byte[] asBytes(ActiveMQMessage message) throws IOException, JMSException {
        // we must synchronize access to WireFormat
        synchronized (wireFormat) {
            return wireFormat.toBytes(message);
        }
    }

    protected byte[] asBytes(String messageID) {
        return messageID.getBytes();
    }

    /**
     * @see org.codehaus.activemq.store.MessageStore#removeAllMessages()
     */
    public void removeAllMessages() throws JMSException {
        // TODO: implement me.
    }
}
