org.zodiac.autoconfigure.application.condition.ConditionalOnJdwpEnabled Maven / Gradle / Ivy
package org.zodiac.autoconfigure.application.condition;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Objects;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.zodiac.commons.util.Asserts;
import org.zodiac.commons.util.JvmUtil;
import org.zodiac.commons.util.lang.Strings;
/**
* Check whether the current JVM process is started in the debugging mode of
* jdwp, otherwise the condition is not enabled.
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(ConditionalOnJdwpEnabled.OnJdwpEnabledCondition.class)
public @interface ConditionalOnJdwpEnabled {
String ENABLED_PROPERTY = "enabledProperty";
/**
* The key-name of the enabled environment configuration attribute property.
*
* @return enabled property name
*/
String enabledProperty();
@Order(Ordered.HIGHEST_PRECEDENCE + 50)
class OnJdwpEnabledCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Object enablePropertyName = metadata.getAnnotationAttributes(ConditionalOnJdwpEnabled.class.getName()).get(ENABLED_PROPERTY);
Asserts.isTrue(Objects.nonNull(enablePropertyName) && Strings.notBlank(enablePropertyName.toString()),
String.format("%s.%s It shouldn't be empty", ConditionalOnJdwpEnabled.class.getSimpleName(), ENABLED_PROPERTY));
/*Obtain environment enable property value.*/
Boolean enabled = context.getEnvironment().getProperty(enablePropertyName.toString(), Boolean.class);
return Objects.isNull(enabled) ? JvmUtil.JVM_DEBUGGING : enabled;
}
}
}