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.util.concurrent;
018
019import java.util.concurrent.atomic.AtomicLong;
020import java.util.regex.Pattern;
021
022import org.apache.camel.util.StringHelper;
023
024/**
025 * Various helper method for thread naming.
026 */
027public final class ThreadHelper {
028
029    public static final String DEFAULT_PATTERN = "Camel Thread ##counter# - #name#";
030
031    private static final Pattern INVALID_PATTERN = Pattern.compile(".*#\\w+#.*");
032
033    private static final AtomicLong THREAD_COUNTER = new AtomicLong();
034
035    private ThreadHelper() {
036    }
037
038    private static long nextThreadCounter() {
039        return THREAD_COUNTER.incrementAndGet();
040    }
041
042    /**
043     * Creates a new thread name with the given pattern
044     * <p/>
045     * See {@link org.apache.camel.spi.ExecutorServiceManager#setThreadNamePattern(String)} for supported patterns.
046     *
047     * @param  pattern the pattern
048     * @param  name    the name
049     * @return         the thread name, which is unique
050     */
051    public static String resolveThreadName(String pattern, String name) {
052        if (pattern == null) {
053            pattern = DEFAULT_PATTERN;
054        }
055
056        // we support #longName# and #name# as name placeholders
057        String shortName = name.contains("?") ? StringHelper.before(name, "?") : name;
058
059        // replace tokens
060        String answer = StringHelper.replaceFirst(pattern, "#longName#", name);
061        if (shortName != null) {
062            answer = StringHelper.replaceFirst(answer, "#name#", shortName);
063        }
064        String next = Long.toString(nextThreadCounter());
065        answer = StringHelper.replaceFirst(answer, "#counter#", next);
066
067        // are there any #word# combos left, if so they should be considered invalid tokens
068        if (INVALID_PATTERN.matcher(answer).matches()) {
069            throw new IllegalArgumentException(
070                    "Pattern is invalid: [" + pattern + "] in resolved thread name: [" + answer + "]");
071        }
072
073        return answer;
074    }
075
076}