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.Map;
020
021import org.apache.camel.Endpoint;
022import org.apache.camel.ServiceStatus;
023import org.apache.camel.StatefulService;
024import org.apache.camel.api.management.ManagedInstance;
025import org.apache.camel.api.management.ManagedResource;
026import org.apache.camel.api.management.mbean.ManagedEndpointMBean;
027import org.apache.camel.spi.EndpointServiceLocation;
028import org.apache.camel.spi.ManagementStrategy;
029
030@ManagedResource(description = "Managed Endpoint")
031public class ManagedEndpoint implements ManagedInstance, ManagedEndpointMBean {
032    private final Endpoint endpoint;
033
034    public ManagedEndpoint(Endpoint endpoint) {
035        this.endpoint = endpoint;
036    }
037
038    public void init(ManagementStrategy strategy) {
039        // noop
040    }
041
042    public Endpoint getEndpoint() {
043        return endpoint;
044    }
045
046    @Override
047    public String getCamelId() {
048        return endpoint.getCamelContext().getName();
049    }
050
051    @Override
052    public String getCamelManagementName() {
053        return endpoint.getCamelContext().getManagementName();
054    }
055
056    @Override
057    public String getEndpointUri() {
058        return endpoint.getEndpointUri();
059    }
060
061    @Override
062    public String getEndpointBaseUri() {
063        return endpoint.getEndpointBaseUri();
064    }
065
066    @Override
067    public boolean isSingleton() {
068        return endpoint.isSingleton();
069    }
070
071    @Override
072    public boolean isRemote() {
073        return endpoint.isRemote();
074    }
075
076    @Override
077    public String getState() {
078        // must use String type to be sure remote JMX can read the attribute without requiring Camel classes.
079        if (endpoint instanceof StatefulService statefulService) {
080            ServiceStatus status = statefulService.getStatus();
081            return status.name();
082        }
083
084        // assume started if not a ServiceSupport instance
085        return ServiceStatus.Started.name();
086    }
087
088    @Override
089    public String getServiceLocationUrl() {
090        if (endpoint instanceof EndpointServiceLocation raa) {
091            return raa.getServiceUrl();
092        }
093        return null;
094    }
095
096    @Override
097    public String getServiceLocationProtocol() {
098        if (endpoint instanceof EndpointServiceLocation raa) {
099            return raa.getServiceProtocol();
100        }
101        return null;
102    }
103
104    @Override
105    public Map<String, String> getServiceLocationMetadata() {
106        if (endpoint instanceof EndpointServiceLocation raa) {
107            return raa.getServiceMetadata();
108        }
109        return null;
110    }
111
112    @Override
113    public Endpoint getInstance() {
114        return endpoint;
115    }
116
117}