001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.transport.amqp.protocol;
018
019import java.io.UnsupportedEncodingException;
020import java.util.Iterator;
021import java.util.LinkedHashSet;
022import java.util.Set;
023
024/**
025 * Utility class that can generate and if enabled pool the binary tag values
026 * used to identify transfers over an AMQP link.
027 */
028public final class AmqpTransferTagGenerator {
029
030    public static final int DEFAULT_TAG_POOL_SIZE = 1024;
031
032    private long nextTagId;
033    private int maxPoolSize = DEFAULT_TAG_POOL_SIZE;
034
035    private final Set<byte[]> tagPool;
036
037    public AmqpTransferTagGenerator() {
038        this(false);
039    }
040
041    public AmqpTransferTagGenerator(boolean pool) {
042        if (pool) {
043            this.tagPool = new LinkedHashSet<byte[]>();
044        } else {
045            this.tagPool = null;
046        }
047    }
048
049    /**
050     * Retrieves the next available tag.
051     *
052     * @return a new or unused tag depending on the pool option.
053     */
054    public byte[] getNextTag() {
055        byte[] rc;
056        if (tagPool != null && !tagPool.isEmpty()) {
057            final Iterator<byte[]> iterator = tagPool.iterator();
058            rc = iterator.next();
059            iterator.remove();
060        } else {
061            try {
062                rc = Long.toHexString(nextTagId++).getBytes("UTF-8");
063            } catch (UnsupportedEncodingException e) {
064                // This should never happen since we control the input.
065                throw new RuntimeException(e);
066            }
067        }
068        return rc;
069    }
070
071    /**
072     * When used as a pooled cache of tags the unused tags should always be returned once
073     * the transfer has been settled.
074     *
075     * @param data
076     *        a previously borrowed tag that is no longer in use.
077     */
078    public void returnTag(byte[] data) {
079        if (tagPool != null && tagPool.size() < maxPoolSize) {
080            tagPool.add(data);
081        }
082    }
083
084    /**
085     * Gets the current max pool size value.
086     *
087     * @return the current max tag pool size.
088     */
089    public int getMaxPoolSize() {
090        return maxPoolSize;
091    }
092
093    /**
094     * Sets the max tag pool size.  If the size is smaller than the current number
095     * of pooled tags the pool will drain over time until it matches the max.
096     *
097     * @param maxPoolSize
098     *        the maximum number of tags to hold in the pool.
099     */
100    public void setMaxPoolSize(int maxPoolSize) {
101        this.maxPoolSize = maxPoolSize;
102    }
103}