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.management.mbean;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Optional;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.ServiceStatus;
025import org.apache.camel.StatefulService;
026import org.apache.camel.api.management.mbean.ManagedClusterServiceMBean;
027import org.apache.camel.cluster.CamelClusterService;
028import org.apache.camel.impl.cluster.ClusterServiceHelper;
029import org.apache.camel.spi.ManagementStrategy;
030
031public class ManagedClusterService implements ManagedClusterServiceMBean {
032    private final CamelContext context;
033    private final CamelClusterService service;
034
035    public ManagedClusterService(CamelContext context, CamelClusterService service) {
036        this.context = context;
037        this.service = service;
038    }
039
040    public void init(ManagementStrategy strategy) {
041        // do nothing
042    }
043
044    public CamelContext getContext() {
045        return context;
046    }
047
048    public CamelClusterService getService() {
049        return service;
050    }
051
052    @Override
053    public void start() throws Exception {
054        if (!context.getStatus().isStarted()) {
055            throw new IllegalArgumentException("CamelContext is not started");
056        }
057        service.start();
058    }
059
060    @Override
061    public void stop() throws Exception {
062        if (!context.getStatus().isStarted()) {
063            throw new IllegalArgumentException("CamelContext is not started");
064        }
065        service.stop();
066    }
067
068    @Override
069    public String getState() {
070        // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
071        if (service instanceof StatefulService) {
072            ServiceStatus status = ((StatefulService) service).getStatus();
073            return status.name();
074        }
075
076        // assume started if not a ServiceSupport instance
077        return ServiceStatus.Started.name();
078    }
079
080    @Override
081    public String getCamelId() {
082        return context.getName();
083    }
084
085    @Override
086    public Collection<String> getNamespaces() {
087        return ClusterServiceHelper.lookupService(context)
088            .map(CamelClusterService::getNamespaces)
089            .orElseGet(Collections::emptyList);
090    }
091
092    @Override
093    public void startView(String namespace) throws Exception {
094        Optional<CamelClusterService> service = ClusterServiceHelper.lookupService(context);
095        if (service.isPresent()) {
096            service.get().startView(namespace);
097        }
098    }
099
100    @Override
101    public void stopView(String namespace) throws Exception {
102        Optional<CamelClusterService> service = ClusterServiceHelper.lookupService(context);
103        if (service.isPresent()) {
104            service.get().stopView(namespace);
105        }
106    }
107
108    @Override
109    public boolean isLeader(String namespace) {
110        return ClusterServiceHelper.lookupService(context)
111            .map(s -> s.isLeader(namespace))
112            .orElse(false);
113    }
114}