public static String findFreeCamelContextName()

in core/camel-core-osgi/src/main/java/org/apache/camel/karaf/core/OsgiNamingHelper.java [49:84]


    public static String findFreeCamelContextName(BundleContext context, String prefix, String key, AtomicInteger counter, boolean checkFirst) {
        String candidate = null;
        boolean clash = false;

        do {
            try {
                clash = false;

                if (candidate == null && checkFirst) {
                    // try candidate as-is
                    candidate = prefix;
                } else {
                    // generate new candidate
                    candidate = prefix + "-" + getNextCounter(counter);
                }
                LOG.trace("Checking OSGi Service Registry for existence of existing CamelContext with name: {}", candidate);

                ServiceReference<?>[] refs = context.getServiceReferences(CamelContext.class.getName(), "(" + key + "=" + candidate + ")");
                if (refs != null && refs.length > 0) {
                    for (ServiceReference<?> ref : refs) {
                        Object id = ref.getProperty(key);
                        if (id != null && candidate.equals(id)) {
                            clash = true;
                            break;
                        }
                    }
                }
            } catch (InvalidSyntaxException e) {
                LOG.debug("Error finding free Camel name in OSGi Service Registry due " + e.getMessage() + ". This exception is ignored.", e);
                break;
            }
        } while (clash);

        LOG.debug("Generated free name for bundle id: {}, clash: {} -> {}", context.getBundle().getBundleId(), clash, candidate);
        return candidate;
    }