Java Code Examples for org.springframework.context.annotation.ConditionContext#getEnvironment()

The following examples show how to use org.springframework.context.annotation.ConditionContext#getEnvironment() . 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: RemoteConfigSourceConfigured.java    From kork with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(ConditionContext context, @NotNull AnnotatedTypeMetadata metadata) {
  ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
  if (beanFactory != null) {
    Environment environment = context.getEnvironment();
    String remoteRepoTypes =
        environment.getProperty(
            "spring.cloud.config.remote-repo-types", DEFAULT_REMOTE_REPO_TYPES);

    for (String remoteRepoType : StringUtils.split(remoteRepoTypes, ',')) {
      if (beanFactory.containsBean(remoteRepoType + "EnvironmentRepository")
          || beanFactory.containsBean(remoteRepoType + "-env-repo0")) {
        return true;
      }
    }
  }
  return false;
}
 
Example 2
Source File: PrefixPropertyCondition.java    From loc-framework with MIT License 6 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
    AnnotatedTypeMetadata metadata) {
  String prefix = (String) attribute(metadata, "prefix");
  Class<?> value = (Class<?>) attribute(metadata, "value");
  ConfigurableEnvironment environment = (ConfigurableEnvironment) context.getEnvironment();
  try {
    new Binder(ConfigurationPropertySources.from(environment.getPropertySources()))
        .bind(prefix, Bindable.of(value))
        .orElseThrow(
            () -> new FatalBeanException("Could not bind DataSourceSettings properties"));
    return new ConditionOutcome(true, String.format("Map property [%s] is not empty", prefix));
  } catch (Exception e) {
    //ignore
  }
  return new ConditionOutcome(false, String.format("Map property [%s] is empty", prefix));
}
 
Example 3
Source File: AuthorizationServerTokenServicesConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
	ConditionMessage.Builder message = ConditionMessage.forCondition("OAuth JWT KeyStore Condition");
	Environment environment = context.getEnvironment();
	String keyStore = environment.getProperty("security.oauth2.authorization.jwt.key-store");
	if (StringUtils.hasText(keyStore)) {
		return ConditionOutcome.match(message.foundExactly("provided key store location"));
	}
	return ConditionOutcome.noMatch(message.didNotFind("provided key store location").atAll());
}
 
Example 4
Source File: CloudCondition.java    From solace-java-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata annoMetaData) {
  Environment env =  context.getEnvironment();
  String VCAP_APPLICATION = env.getProperty("VCAP_APPLICATION");
  if ( VCAP_APPLICATION != null  ) {
      String VCAP_SERVICES = env.getProperty("VCAP_SERVICES");
      return VCAP_SERVICES != null && VCAP_SERVICES.contains("\"solace-pubsub\"");
  }
  return false;
}
 
Example 5
Source File: OnMockSqsServerBaseCondition.java    From spring-boot-aws-mock with MIT License 5 votes vote down vote up
boolean isRunning(ConditionContext context){
    Environment environment = context.getEnvironment();
    if(isRandomPort(environment)){
        return false;
    }

    return isRunning(getSqsServerPort(environment));
}
 
Example 6
Source File: MagicExistsCondition.java    From Project with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
	//获取环境
	Environment env = context.getEnvironment();
	//包含环境属性 magic 则返回true
	return env.containsProperty("magic");
}
 
Example 7
Source File: CloudCondition.java    From solace-jms-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata annoMetaData) {
  Environment env =  context.getEnvironment();
  String VCAP_APPLICATION = env.getProperty("VCAP_APPLICATION");
  if ( VCAP_APPLICATION != null  ) {
      String VCAP_SERVICES = env.getProperty("VCAP_SERVICES");
      return VCAP_SERVICES != null && VCAP_SERVICES.contains("\"solace-pubsub\"");
  }
  return false;
}
 
Example 8
Source File: JetCacheCondition.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
    ConfigTree ct = new ConfigTree((ConfigurableEnvironment) conditionContext.getEnvironment(), "jetcache.");
    if (match(ct, "local.") || match(ct, "remote.")) {
        return ConditionOutcome.match();
    } else {
        return ConditionOutcome.noMatch("no match for " + cacheTypes[0]);
    }
}
 
Example 9
Source File: ClusterAwareConfiguration.java    From spring-boot-data-geode with Apache License 2.0 5 votes vote down vote up
boolean isMatch(ConditionContext context) {

			Environment environment = context.getEnvironment();

			return isAvailable()
				|| Boolean.TRUE.equals(environment.getProperty(SPRING_BOOT_DATA_GEMFIRE_CLUSTER_CONDITION_MATCH_PROPERTY,
					Boolean.class, false));
		}
 
Example 10
Source File: ResourceServerTokenServicesConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
	ConditionMessage.Builder message = ConditionMessage.forCondition("OAuth JWK Condition");
	Environment environment = context.getEnvironment();
	String keyUri = environment.getProperty("security.oauth2.resource.jwk.key-set-uri");
	if (StringUtils.hasText(keyUri)) {
		return ConditionOutcome.match(message.foundExactly("provided jwk key set URI"));
	}
	return ConditionOutcome.noMatch(message.didNotFind("key jwk set URI not provided").atAll());
}
 
Example 11
Source File: BasicServiceConfigCondition.java    From spring-boot-starter-motan with Apache License 2.0 5 votes vote down vote up
/**
 * match [motan.basicservice.exportPort, motan.basicservice.export] config property
 * 
 * @see org.springframework.context.annotation.Condition#matches(org.springframework.context.annotation.ConditionContext, org.springframework.core.type.AnnotatedTypeMetadata)
 */
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
	Environment env = context.getEnvironment();
	return (!StringUtils.isEmpty(env.getProperty("motan.basicservice.exportPort"))
			|| !StringUtils.isEmpty(env.getProperty("motan.basicservice.export")));
}
 
Example 12
Source File: ResourceServerTokenServicesConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
	ConditionMessage.Builder message = ConditionMessage.forCondition("OAuth JWT KeyStore Condition");
	Environment environment = context.getEnvironment();
	String keyStore = environment.getProperty("security.oauth2.resource.jwt.key-store");
	if (StringUtils.hasText(keyStore)) {
		return ConditionOutcome.match(message.foundExactly("provided key store location"));
	}
	return ConditionOutcome.noMatch(message.didNotFind("key store location not provided").atAll());
}
 
Example 13
Source File: LinuxCondition.java    From code with Apache License 2.0 5 votes vote down vote up
/**
 * 匹配linux操作系统
 *
 * @param context
 * @param metadata
 * @return boolean
 */
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    // 获取运行时环境
    Environment environment = context.getEnvironment();
    // 获取操作系统
    String property = environment.getProperty("os.name");

    if (property.toLowerCase().contains("linux")) {
        return true;
    }
    return false;
}
 
Example 14
Source File: GroupCondition.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
    final ConditionMessage.Builder message = ConditionMessage.forCondition(this.single);
    final Environment environment = conditionContext.getEnvironment();

    return HierarchicalPropertiesEvaluator.evaluate(environment, this.group, this.single)
        ? ConditionOutcome.match(message.because("enabled"))
        : ConditionOutcome.noMatch(message.because("not enabled"));
}
 
Example 15
Source File: IgniteConditionalCommunicate.java    From ext-opensource-netty with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
   	Environment env = context.getEnvironment();
       return "ignite".equalsIgnoreCase(env.getProperty("netty.server.interal.communicate"));
   }
 
Example 16
Source File: EvenProfileCondition.java    From geekbang-lessons with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    // 条件上下文
    Environment environment = context.getEnvironment();
    return environment.acceptsProfiles("even");
}
 
Example 17
Source File: ClusterAwareConfiguration.java    From spring-boot-data-geode with Apache License 2.0 4 votes vote down vote up
ConditionContext doMatch(ConditionContext conditionContext) {

			Environment environment = conditionContext.getEnvironment();

			ConnectionEndpointList connectionEndpoints =
				new ConnectionEndpointList(getDefaultConnectionEndpoints())
					.add(getConfiguredConnectionEndpoints(environment));

			int connectionCount = countConnections(connectionEndpoints);

			configureTopology(environment, connectionEndpoints, connectionCount);

			clusterAvailable.set(isMatch(connectionEndpoints, connectionCount));

			return conditionContext;
		}
 
Example 18
Source File: OnPropertyPrefixCondition.java    From spring-boot-web-support with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {

    AnnotationAttributes annotationAttributes = AnnotationAttributes.fromMap(
            metadata.getAnnotationAttributes(ConditionalOnPropertyPrefix.class.getName()));

    String[] prefixValues = annotationAttributes.getStringArray("value");

    ConfigurableEnvironment environment = (ConfigurableEnvironment) context.getEnvironment();

    boolean matched = false;

    for (String prefix : prefixValues) {

        if (startsWith(environment, prefix)) {
            matched = true;
            break;
        }

    }

    return matched ? ConditionOutcome.match() : ConditionOutcome.noMatch("The prefix values " +
            Arrays.asList(prefixValues) + " were not found in Environment!");

}
 
Example 19
Source File: RedisDisabledCondition.java    From ueboot with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Environment env = context.getEnvironment();
    String storeType = env.getProperty("spring.session.store-type");
    return !"redis".equalsIgnoreCase(storeType);
}
 
Example 20
Source File: VelocityToolsBasePackagesCondition.java    From velocity-spring-boot-project with Apache License 2.0 3 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {

    Environment environment = context.getEnvironment();

    String toolsBasePackages = environment.getProperty(VELOCITY_TOOLS_BASE_PACKAGES_PROPERTY_NAME);

    if (!StringUtils.hasText(toolsBasePackages)) {
        return ConditionOutcome.noMatch("Velocity Tools scanning is disabled , since its base packages is empty !");
    }

    return ConditionOutcome.match();

}