001    /*
002     * Copyright (c) 2005 Your Corporation. All Rights Reserved.
003     */
004    package org.activemq.transport.stomp;
005    
006    import org.activemq.message.TransactionInfo;
007    import org.activemq.message.KeepAlive;
008    import org.activemq.message.TransactionType;
009    
010    import java.io.DataInput;
011    import java.io.IOException;
012    import java.util.Properties;
013    
014    public class Begin implements Command
015    {
016        private StompWireFormat format;
017        private static final HeaderParser parser = new HeaderParser();
018    
019        public Begin(StompWireFormat format)
020        {
021            this.format = format;
022        }
023    
024        public PacketEnvelope build(String commandLine, DataInput in) throws IOException
025        {
026            Properties headers = parser.parse(in);
027            while (in.readByte() != 0) {}
028    
029            if (format.isInTransaction())
030            {
031                return new PacketEnvelope(new KeepAlive(), headers);
032            }
033            TransactionInfo tx = new TransactionInfo();
034            String tx_id = StompWireFormat.clientIds.generateId();
035            format.setTransactionId(tx_id);
036            tx.setTransactionId(tx_id);
037            tx.setType(TransactionType.START);
038            return new PacketEnvelope(tx, headers);
039        }
040    }