Skip to content

Commit

Permalink
use conditional annotation for ioc location manager alfio-event#127
Browse files Browse the repository at this point in the history
  • Loading branch information
bunsenmcdubbs committed Jul 10, 2016
1 parent 9930d24 commit 6a909bd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package alfio.config.support;

import alfio.model.system.ConfigurationKeys;
import org.springframework.context.annotation.Conditional;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Created by andrew on 7/10/16.
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Conditional(OnConfigurationPresentCondition.class)
public @interface ConditionalOnConfigurationPresentProperty {
public ConfigurationKeys value();
public boolean exists() default true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package alfio.config.support;

import alfio.manager.system.ConfigurationManager;
import alfio.model.system.Configuration;
import alfio.model.system.ConfigurationKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
* Created by andrew on 7/10/16.
*/
public class OnConfigurationPresentCondition implements Condition {

@Autowired
private ConfigurationManager configurationManager;

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes =
metadata.getAnnotationAttributes(ConditionalOnConfigurationPresentProperty.class.getName());

Configuration.ConfigurationPathKey key = Configuration.getSystemConfiguration(
(ConfigurationKeys) attributes.get("value"));
boolean configPropExistsCheck = (Boolean) attributes.get("exists");

return configPropExistsCheck ^ configurationManager.getStringConfigValue(key).isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package alfio.manager.location;

import alfio.config.Initializer;
import alfio.config.support.ConditionalOnConfigurationPresentProperty;
import alfio.manager.system.ConfigurationManager;
import alfio.model.system.Configuration;
import alfio.model.system.ConfigurationKeys;
Expand All @@ -27,6 +28,7 @@
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

Expand All @@ -35,7 +37,7 @@
import java.util.concurrent.atomic.AtomicReference;

@Component
@Profile(Initializer.PROFILE_LIVE)
@ConditionalOnConfigurationPresentProperty(ConfigurationKeys.MAPS_SERVER_API_KEY)
public class DefaultLocationManager implements LocationManager {

private static final AtomicReference<GeoApiContext> CTX = new AtomicReference<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
package alfio.manager.location;

import alfio.config.Initializer;
import alfio.config.support.ConditionalOnConfigurationPresentProperty;
import alfio.model.system.ConfigurationKeys;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.TimeZone;

@Component
@Profile(Initializer.PROFILE_DEV)
@ConditionalOnConfigurationPresentProperty(value = ConfigurationKeys.MAPS_SERVER_API_KEY, exists = false)
public class MockLocationManager implements LocationManager {
@Override
public Pair<String, String> geocode(String address) {
Expand Down

1 comment on commit 6a909bd

@bunsenmcdubbs
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: this doesn't actually work!!! Autowired does not work in the definition of the conditional. I'll change the implementation to mirror the structure of the Mailer system.

Please sign in to comment.