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.cluster;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Map;
022import java.util.Optional;
023
024import org.apache.camel.CamelContextAware;
025import org.apache.camel.Ordered;
026import org.apache.camel.Service;
027import org.apache.camel.spi.IdAware;
028
029public interface CamelClusterService extends Service, CamelContextAware, IdAware, Ordered {
030
031    @Override
032    default int getOrder() {
033        return Ordered.LOWEST;
034    }
035
036    /**
037     * Get a view of the cluster bound to a namespace creating it if needed. Multiple
038     * calls to this method with the same namespace should return the same instance.
039     * The instance is automatically started the first time it is instantiated and
040     * if the cluster service is ready.
041     *
042     * @param namespace the namespace the view refer to.
043     * @return the view.
044     * @throws Exception if the view can't be created.
045     */
046    CamelClusterView getView(String namespace) throws Exception;
047
048    /**
049     * Release a view if it has no references.
050     *
051     * @param view the view.
052     * @throws Exception
053     */
054    void releaseView(CamelClusterView view) throws Exception;
055
056    /**
057     * Return the namespaces handled by this service.
058     */
059    Collection<String> getNamespaces();
060
061    /**
062     * Force start of the view associated to the give namespace.
063     */
064    void startView(String namespace) throws Exception;
065
066    /**
067     * Force stop of the view associated to the give namespace.
068     */
069    void stopView(String namespace) throws Exception;
070
071    /**
072     * Check if the service is the leader on the given namespace.
073     *
074     * @param namespace the namespace.
075     * @return
076     */
077    boolean isLeader(String namespace);
078
079    /**
080     * Attributes associated to the service.
081     */
082    default Map<String, Object> getAttributes() {
083        return Collections.emptyMap();
084    }
085
086    /**
087     * Access the underlying concrete CamelClusterService implementation to
088     * provide access to further features.
089     *
090     * @param clazz the proprietary class or interface of the underlying concrete CamelClusterService.
091     * @return an instance of the underlying concrete CamelClusterService as the required type.
092     */
093    default <T extends CamelClusterService> T unwrap(Class<T> clazz) {
094        if (CamelClusterService.class.isAssignableFrom(clazz)) {
095            return clazz.cast(this);
096        }
097
098        throw new IllegalArgumentException(
099            "Unable to unwrap this CamelClusterService type (" + getClass() + ") to the required type (" + clazz + ")"
100        );
101    }
102
103    @FunctionalInterface
104    interface Selector {
105        /**
106         * Select a specific CamelClusterService instance among a collection.
107         */
108        Optional<CamelClusterService> select(Collection<CamelClusterService> services);
109    }
110}