/*
 * Copyright (c) 2016. Universidad Politecnica de Madrid
 *
 * @author Badenes Olmedo, Carlos <cbadenes@fi.upm.es>
 *
 */

package org.librairy.boot.eventbus.guava;

import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * Created by cbadenes on 26/11/15.
 */
public class GuavaCondition implements Condition {

    private static final Logger LOG = LoggerFactory.getLogger(GuavaCondition.class);

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        String envVar  = System.getenv("LIBRAIRY_EVENTBUS_HOST");
        boolean condition = (!Strings.isNullOrEmpty(envVar) && envVar.equalsIgnoreCase("local"))
                || (Strings.isNullOrEmpty(envVar) &&
                conditionContext.getEnvironment().getProperty("librairy.eventbus.host").equalsIgnoreCase("local"));
        return condition;
    }
}

@@ -767,17 +767,16 @@
  envVar 
- 
 = System
@@ -873,22 +873,23 @@
 Var) && 
-envVar
+%22local%22
 .equalsI
@@ -898,23 +898,22 @@
 oreCase(
-%22local%22
+envVar
 ))%0A     
@@ -959,33 +959,42 @@
 vVar) &&
-%0A                
+ %22local%22.equalsIgnoreCase(
 conditio
@@ -1060,33 +1060,8 @@
 st%22)
-.equalsIgnoreCase(%22local%22
 ));%0A

fix (condition evaluation): avoid NullPointerException