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.List;
020import java.util.Optional;
021
022import org.apache.camel.CamelContextAware;
023import org.apache.camel.Service;
024
025/**
026 * Represents the View of the cluster at some given period of time.
027 */
028public interface CamelClusterView extends Service, CamelContextAware {
029    /**
030     * @return the cluster.
031     */
032    CamelClusterService getClusterService();
033
034    /**
035     * @return the namespace for this view.
036     */
037    String getNamespace();
038
039    /**
040     * Provides the leader member if elected.
041     *
042     * @return the leader member.
043     */
044    Optional<CamelClusterMember> getLeader();
045
046    /**
047     * Provides the local member.
048     *
049     * @return the local member.
050     */
051    CamelClusterMember getLocalMember();
052
053    /**
054     * Provides the list of members of the cluster.
055     *
056     * @return the list of members.
057     */
058    List<CamelClusterMember> getMembers();
059
060    /**
061     * Add an event listener.
062     *
063     * @param listener the event listener.
064     */
065    void addEventListener(CamelClusterEventListener listener);
066
067    /**
068     * Remove the event listener.
069     *
070     * @param listener the event listener.
071     */
072    void removeEventListener(CamelClusterEventListener listener);
073
074    /**
075     * Access the underlying concrete CamelClusterView implementation to
076     * provide access to further features.
077     *
078     * @param clazz the proprietary class or interface of the underlying concrete CamelClusterView.
079     * @return an instance of the underlying concrete CamelClusterView as the required type.
080     */
081    default <T extends CamelClusterView> T unwrap(Class<T> clazz) {
082        if (CamelClusterView.class.isAssignableFrom(clazz)) {
083            return clazz.cast(this);
084        }
085
086        throw new IllegalArgumentException(
087            "Unable to unwrap this CamelClusterView type (" + getClass() + ") to the required type (" + clazz + ")"
088        );
089    }
090}