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.camel.impl;
018
019import java.util.concurrent.atomic.AtomicInteger;
020
021import org.apache.camel.spi.CamelContextNameStrategy;
022
023/**
024 * A default name strategy which auto assigns a name using a prefix-counter pattern.
025 *
026 * @version 
027 */
028public class DefaultCamelContextNameStrategy implements CamelContextNameStrategy {
029
030    private static final AtomicInteger CONTEXT_COUNTER = new AtomicInteger(0);
031    private final String prefix;
032    private String name;
033
034    public DefaultCamelContextNameStrategy() {
035        this("camel");
036    }
037
038    public DefaultCamelContextNameStrategy(String prefix) {
039        this.prefix = prefix;
040        this.name = getNextName();
041    }
042
043    @Override
044    public String getName() {
045        if (name == null) {
046            name = getNextName();
047        }
048        return name;
049    }
050
051    @Override
052    public String getNextName() {
053        return prefix + "-" + getNextCounter();
054    }
055
056    @Override
057    public boolean isFixedName() {
058        return false;
059    }
060
061    public static int getNextCounter() {
062        // we want to start counting from 1, so increment first
063        return CONTEXT_COUNTER.incrementAndGet();
064    }
065
066    /**
067     * To reset the counter, should only be used for testing purposes.
068     *
069     * @param value the counter value
070     */
071    public static void setCounter(int value) {
072        CONTEXT_COUNTER.set(value);
073    }
074
075}