/*
 * Copyright 2022 Thoughtworks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.thoughtworks.go.server.service.support;

import com.thoughtworks.go.util.SystemEnvironment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.lang.reflect.Method;

@Component
public class ResourceMonitoring {
    private SystemEnvironment systemEnvironment;
    private static final Logger LOGGER = LoggerFactory.getLogger(ResourceMonitoring.class.getName());

    @Autowired
    public ResourceMonitoring(SystemEnvironment systemEnvironment) {
        this.systemEnvironment = systemEnvironment;
    }

    public void enableIfDiagnosticsModeIsEnabled() {
        if (systemEnvironment.get(SystemEnvironment.GO_DIAGNOSTICS_MODE)) {
            ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
            if (threadMXBean.isThreadContentionMonitoringSupported()) {
                threadMXBean.setThreadContentionMonitoringEnabled(true);
            }
            if (threadMXBean.isThreadCpuTimeSupported()) {
                threadMXBean.setThreadCpuTimeEnabled(true);
            }

            if (isThreadAllocatedMemorySupported(threadMXBean)) {
                setThreadAllocatedMemoryEnabled(threadMXBean);
            }
        }
    }

    private boolean isThreadAllocatedMemorySupported(ThreadMXBean threadMXBean) {
        Method method = ReflectionUtils.findMethod(threadMXBean.getClass(), "isThreadAllocatedMemorySupported");
        if (method != null) {
            try {
                method.setAccessible(true);
                return (boolean) method.invoke(threadMXBean);
            } catch (Exception e) {
                LOGGER.error("Error on threadMXBean.isThreadAllocatedMemorySupported : {}", e.getMessage());
            }
        }
        return false;
    }

    private void setThreadAllocatedMemoryEnabled(ThreadMXBean threadMXBean) {
        Method method = ReflectionUtils.findMethod(threadMXBean.getClass(), "setThreadAllocatedMemoryEnabled", boolean.class);
        if (method != null) {
            try {
                method.setAccessible(true);
                method.invoke(threadMXBean, true);
            } catch (Exception e) {
                LOGGER.error("Error on threadMXBean.setThreadAllocatedMemoryEnabled : {}", e.getMessage());
            }
        }
    }
}
@@ -703,65 +703,8 @@
 nt;%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 impo
@@ -814,57 +814,8 @@
 ent;
-%0Aimport org.springframework.util.ReflectionUtils;
 %0A%0Aim
@@ -904,41 +904,8 @@
 ean;
-%0Aimport java.lang.reflect.Method;
 %0A%0A@C
@@ -999,110 +999,8 @@
 ent;
-%0A    private static final Logger LOGGER = LoggerFactory.getLogger(ResourceMonitoring.class.getName());
 %0A%0A  
@@ -1509,1355 +1509,8 @@
   %7D%0A
-            if (threadMXBean.isThreadCpuTimeSupported()) %7B%0A                threadMXBean.setThreadCpuTimeEnabled(true);%0A            %7D%0A%0A            if (isThreadAllocatedMemorySupported(threadMXBean)) %7B%0A                setThreadAllocatedMemoryEnabled(threadMXBean);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private boolean isThreadAllocatedMemorySupported(ThreadMXBean threadMXBean) %7B%0A        Method method = ReflectionUtils.findMethod(threadMXBean.getClass(), %22isThreadAllocatedMemorySupported%22);%0A        if (method != null) %7B%0A            try %7B%0A                method.setAccessible(true);%0A                return (boolean) method.invoke(threadMXBean);%0A            %7D catch (Exception e) %7B%0A                LOGGER.error(%22Error on threadMXBean.isThreadAllocatedMemorySupported : %7B%7D%22, e.getMessage());%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A    private void setThreadAllocatedMemoryEnabled(ThreadMXBean threadMXBean) %7B%0A        Method method = ReflectionUtils.findMethod(threadMXBean.getClass(), %22setThreadAllocatedMemoryEnabled%22, boolean.class);%0A        if (method != null) %7B%0A            try %7B%0A                method.setAccessible(true);%0A                method.invoke(threadMXBean, true);%0A            %7D catch (Exception e) %7B%0A                LOGGER.error(%22Error on threadMXBean.setThreadAllocatedMemoryEnabled : %7B%7D%22, e.getMessage());%0A            %7D%0A
     

Remove unnecessary reflection for go.diagnostics.mode