Java Code Examples for org.springframework.core.type.AnnotatedTypeMetadata#getAnnotationAttributes()

The following examples show how to use org.springframework.core.type.AnnotatedTypeMetadata#getAnnotationAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OnSpringBootVersion.java    From sofa-ark with Apache License 2.0 8 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Map<String, Object> springBootVersion = metadata
        .getAnnotationAttributes(ConditionalOnSpringBootVersion.class.getCanonicalName());
    if (springBootVersion == null || springBootVersion.get("version") == null) {
        return new ConditionOutcome(false, "No specified spring boot version.");
    }
    ConditionalOnSpringBootVersion.Version version = (ConditionalOnSpringBootVersion.Version) springBootVersion
        .get("version");
    if (ConditionalOnSpringBootVersion.Version.ANY.equals(version)) {
        return new ConditionOutcome(true, "Conditional on Any Spring Boot.");
    } else if (ConditionalOnSpringBootVersion.Version.OneX.equals(version)) {
        if (SpringBootVersion.getVersion().startsWith("1")) {
            return new ConditionOutcome(true, "Conditional on OneX Spring Boot.");
        } else {
            return new ConditionOutcome(false, "Conditional on OneX Spring Boot.");
        }
    } else if (ConditionalOnSpringBootVersion.Version.TwoX.equals(version)) {
        if (SpringBootVersion.getVersion().startsWith("2")) {
            return new ConditionOutcome(true, "Conditional on TwoX Spring Boot.");
        } else {
            return new ConditionOutcome(false, "Conditional on TwoX Spring Boot.");
        }
    }
    throw new IllegalStateException("Error Spring Boot Version.");
}
 
Example 2
Source File: WatsonServiceCondition.java    From spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata metadata) {
  Map<String, Object> attributes =
          metadata.getAnnotationAttributes(ConditionalOnWatsonServiceProperties.class.getName());
  String prefix = (String) attributes.get("prefix");

  // If the service is specifically marked as enabled, the condition is true
  if (Boolean.valueOf(conditionContext.getEnvironment().getProperty(prefix + ".enabled"))) {
    return true;
  }

  // If any of the configuration properties for the service are present, the condition is true
  String url = conditionContext.getEnvironment().getProperty(prefix + ".url");
  String username = conditionContext.getEnvironment().getProperty(prefix + ".username");
  String password = conditionContext.getEnvironment().getProperty(prefix + ".password");
  String apiKey = conditionContext.getEnvironment().getProperty(prefix + ".apiKey");
  String iamApiKey = conditionContext.getEnvironment().getProperty(prefix + ".iamApiKey");
  String versionDate = conditionContext.getEnvironment().getProperty(prefix + ".versionDate");
  if (url != null || username != null || password != null || versionDate != null
          || apiKey != null || iamApiKey != null) {
    return true;
  }

  return false;
}
 
Example 3
Source File: OnGcpEnvironmentCondition.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
/**
 * Determines whether the current runtime environment matches the one passed through the annotation.
 * @param context the spring context at the point in time the condition is being evaluated
 * @param metadata annotation metadata containing all acceptable GCP environments
 * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException if no GcpEnvironmentProvider is found in
 * spring context
 */
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {

	Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnGcpEnvironment.class.getName());
	GcpEnvironment[] targetEnvironments = (GcpEnvironment[]) attributes.get("value");
	Assert.notNull(targetEnvironments, "Value attribute of ConditionalOnGcpEnvironment cannot be null.");

	GcpEnvironmentProvider environmentProvider = context.getBeanFactory().getBean(GcpEnvironmentProvider.class);
	GcpEnvironment currentEnvironment = environmentProvider.getCurrentEnvironment();

	if (Arrays.stream(targetEnvironments).noneMatch((env) -> env == currentEnvironment)) {
		return new ConditionOutcome(false, "Application is not running on any of "
				+ Arrays.stream(targetEnvironments)
				.map(GcpEnvironment::toString)
				.collect(Collectors.joining(", ")));
	}

	return new ConditionOutcome(true, "Application is running on " + currentEnvironment);
}
 
Example 4
Source File: JdbcUrlCondition.java    From logging-log4j-audit with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Environment env = context.getEnvironment();
    Map<String, Object> map = metadata.getAnnotationAttributes(JdbcUrl.class.getName());
    if (map != null && map.containsKey("value")) {
        String value = map.get("value").toString();
        String jdbcUrl = env.getProperty("jdbcUrl");
        boolean isEmbedded = Boolean.parseBoolean(env.getProperty("isEmbedded"));
        boolean result;
        if (value.equals("hsqldb")) {
            result = jdbcUrl == null || isEmbedded;
        } else if (jdbcUrl == null || isEmbedded) {
            result = false;
        } else if (!jdbcUrl.startsWith("jdbc:")) {
            result = false;
        } else {
            result = jdbcUrl.substring(5).toLowerCase().startsWith(value.toLowerCase());
        }
        LOGGER.debug("Returning {} for {}", result, value);
        return result;
    }
    LOGGER.debug("No data provided");
    return false;
}
 
Example 5
Source File: MicroServiceCondition.java    From micro-service with MIT License 6 votes vote down vote up
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
	
	if (metadata.isAnnotated(Bean.class.getName())) {
		
		Map<String, Object> map = metadata.getAnnotationAttributes(Bean.class.getName());
		if(!CollectionUtils.isEmpty(map)) {
			
			String[] names = (String[]) map.get("name");
			if(ArrayUtils.isNotEmpty(names)) {
				
				for(String name : names) {
					
					if(StringUtils.isNotBlank(name) && name.endsWith("ConfigBean")) {
						return true;
					}
				}
			}
		}
	}
	
	return false;
}
 
Example 6
Source File: OnEnvironmentCondition.java    From seppb with MIT License 5 votes vote down vote up
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnEnvironment.class.getName());
    Env[] envs = (Env[]) attributes.get("values");
    Env currentEnv = Env.getCurrentEnv();
    return Sets.newHashSet(envs).contains(currentEnv);
}
 
Example 7
Source File: MultOnPropertyCondition.java    From sds with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(MultConditionalOnProperty.
            class.getName());
    String propertyName = (String) annotationAttributes.get("name");
    if (StringUtils.isBlank(propertyName)) {
        propertyName = (String) annotationAttributes.get("value");
    }

    if (StringUtils.isBlank(propertyName)) {
        return new ConditionOutcome(false, "没发现配置name或value");
    }

    String[] values = (String[]) annotationAttributes.get("havingValue");
    if (values.length == 0) {
        return new ConditionOutcome(false, "没发现配置havingValue");
    }

    String propertyValue = context.getEnvironment().getProperty(propertyName);
    if (StringUtils.isBlank(propertyValue)) {
        propertyValue = ((ConfigurableApplicationContext) context.getResourceLoader()).getEnvironment().
                getProperty(propertyName);

        if (StringUtils.isBlank(propertyValue)) {
            return new ConditionOutcome(false, "没发现配置" + propertyName);
        }
    }

    /**
     * 相当于或的关系,只要有一个能匹配就算成功
     */
    for (String havingValue : values) {
        if (propertyValue.equalsIgnoreCase(havingValue)) {
            return new ConditionOutcome(true, "匹配成功");
        }
    }

    return new ConditionOutcome(false, "匹配失败");
}
 
Example 8
Source File: OnMissingBeanCondition.java    From jasypt-spring-boot with MIT License 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Map<String, Object> beanAttributes = metadata.getAnnotationAttributes(Bean.class.getName());
    String beanName = ((String[]) beanAttributes.get("name"))[0];
    if(StringUtils.isEmpty(beanName)) {
        throw new IllegalStateException("OnMissingBeanCondition can't detect bean name!");
    }
    boolean missingBean = !context.getBeanFactory().containsBean(context.getEnvironment().resolveRequiredPlaceholders(beanName));
    return missingBean ? ConditionOutcome.match(beanName + " not found") : ConditionOutcome.noMatch(beanName + " found");
}
 
Example 9
Source File: WicketSettingsCondition.java    From wicket-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
	String implVersion = null;
	String wicketVersion = retrieveWicketVersion(implVersion);
	
	Map<String, Object> attributes = metadata
			.getAnnotationAttributes(ConditionalOnWicket.class.getName());
	Range range = (Range) attributes.get("range");
	int expectedVersion = (int) attributes.get("value");
	String[] splittedWicketVersion = wicketVersion.split("\\.");
	int majorWicketVersion = Integer.valueOf(splittedWicketVersion[0]);
	return getMatchOutcome(range, majorWicketVersion, expectedVersion);
}
 
Example 10
Source File: AbstractCondition.java    From mongodb-aggregate-query-support with Apache License 2.0 4 votes vote down vote up
protected int getParameterIndex(AnnotatedTypeMetadata annotatedTypeMetadata) {
  Map<String, Object> params = annotatedTypeMetadata.getAnnotationAttributes(Conditional.class.getName());
  return (int) params.get(ConditionalAnnotationMetadata.PARAMETER_INDEX);
}
 
Example 11
Source File: AbstractCondition.java    From mongodb-aggregate-query-support with Apache License 2.0 4 votes vote down vote up
protected int getParameterIndex(AnnotatedTypeMetadata annotatedTypeMetadata) {
  Map<String, Object> params = annotatedTypeMetadata.getAnnotationAttributes(Conditional.class.getName());
  return (int) params.get(ConditionalAnnotationMetadata.PARAMETER_INDEX);
}