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 boolean getKeepOpen() {
085        return getPolicy().getKeepOpen();
086    }
087
088    @Override
089    public void setKeepOpen(boolean keepOpen) {
090        getPolicy().setKeepOpen(keepOpen);
091    }
092
093    @Override
094    public String getStateLoggingLevel() {
095        return getPolicy().getStateLoggingLevel().name();
096    }
097
098    @Override
099    public void setStateLoggingLevel(String stateLoggingLevel) {
100        getPolicy().setStateLoggingLevel(stateLoggingLevel);
101    }
102
103    @Override
104    public String currentState() {
105        return getPolicy().dumpState();
106    }
107
108    @Override
109    public String getHalfOpenHandlerName() {
110        ThrottlingExceptionHalfOpenHandler obj = getPolicy().getHalfOpenHandler();
111        if (obj != null) {
112            return obj.getClass().getSimpleName();
113        } else {
114            return "";
115        }
116    }
117
118    @Override
119    public Integer getCurrentFailures() {
120        return getPolicy().getFailures();
121    }
122
123    @Override
124    public Long getLastFailure() {
125        if (getPolicy().getLastFailure() == 0) {
126            return 0L;
127        } else {
128            return System.currentTimeMillis() - getPolicy().getLastFailure();
129        }
130    }
131
132    @Override
133    public Long getOpenAt() {
134        if (getPolicy().getOpenedAt() == 0) {
135            return 0L;
136        } else {
137            return System.currentTimeMillis() - getPolicy().getOpenedAt();
138        }
139    }
140
141}