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.List;
020
021import org.apache.camel.CamelContext;
022import org.apache.camel.api.management.ManagedResource;
023import org.apache.camel.api.management.mbean.ManagedThrottlingExceptionRoutePolicyMBean;
024import org.apache.camel.throttling.ThrottlingExceptionHalfOpenHandler;
025import org.apache.camel.throttling.ThrottlingExceptionRoutePolicy;
026
027@ManagedResource(description = "Managed ThrottlingExceptionRoutePolicy")
028public class ManagedThrottlingExceptionRoutePolicy extends ManagedService
029        implements ManagedThrottlingExceptionRoutePolicyMBean {
030
031    private final ThrottlingExceptionRoutePolicy policy;
032
033    public ManagedThrottlingExceptionRoutePolicy(CamelContext context, ThrottlingExceptionRoutePolicy policy) {
034        super(context, policy);
035        this.policy = policy;
036    }
037
038    public ThrottlingExceptionRoutePolicy getPolicy() {
039        return policy;
040    }
041
042    @Override
043    public String[] getExceptionTypes() {
044        if (policy.getThrottledExceptions() != null) {
045            List<String> types = policy.getThrottledExceptions().stream().map(Class::getName)
046                    .toList();
047            return types.toArray(new String[0]);
048        } else {
049            return null;
050        }
051    }
052
053    @Override
054    public Long getHalfOpenAfter() {
055        return getPolicy().getHalfOpenAfter();
056    }
057
058    @Override
059    public void setHalfOpenAfter(Long milliseconds) {
060        getPolicy().setHalfOpenAfter(milliseconds);
061    }
062
063    @Override
064    public Long getFailureWindow() {
065        return getPolicy().getFailureWindow();
066    }
067
068    @Override
069    public void setFailureWindow(Long milliseconds) {
070        getPolicy().setFailureWindow(milliseconds);
071    }
072
073    @Override
074    public Integer getFailureThreshold() {
075        return getPolicy().getFailureThreshold();
076    }
077
078    @Override
079    public void setFailureThreshold(Integer numberOfFailures) {
080        getPolicy().setFailureThreshold(numberOfFailures);
081    }
082
083    @Override
084    public String currentState() {
085        return getPolicy().dumpState();
086    }
087
088    @Override
089    public String getHalfOpenHandlerName() {
090        ThrottlingExceptionHalfOpenHandler obj = getPolicy().getHalfOpenHandler();
091        if (obj != null) {
092            return obj.getClass().getSimpleName();
093        } else {
094            return "";
095        }
096    }
097
098    @Override
099    public Integer getCurrentFailures() {
100        return getPolicy().getFailures();
101    }
102
103    @Override
104    public Long getLastFailure() {
105        if (getPolicy().getLastFailure() == 0) {
106            return 0L;
107        } else {
108            return System.currentTimeMillis() - getPolicy().getLastFailure();
109        }
110    }
111
112    @Override
113    public Long getOpenAt() {
114        if (getPolicy().getOpenedAt() == 0) {
115            return 0L;
116        } else {
117            return System.currentTimeMillis() - getPolicy().getOpenedAt();
118        }
119    }
120
121}