Skip to content

Commit

Permalink
Merge pull request #2 from greg-knox/master
Browse files Browse the repository at this point in the history
[ARCH-274] Dynamic decorators
  • Loading branch information
romank-agfa committed Jun 3, 2015
2 parents d2322d9 + 598b5b0 commit 16dc64a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,37 @@ public class DynamicDecoratorWrapper<T> {

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

@Inject
@ConfiguredDynamicDecorators
Instance<Collection<DelegatingServiceImpl<T>>> dynamicDecorators;
@Inject
@ConfiguredDynamicDecorators
Instance<Collection<DelegatingServiceImpl<T>>> dynamicDecorators;

private T wrappedDecorator = null;

public final T wrapWithDynamicDecorators(T delegate) {
DelegatingServiceImpl<T> theService = new DelegatingServiceImpl<T>();
theService.setOrig(delegate);

for (Collection<DelegatingServiceImpl<T>> collectionDynamicDecoratorsForType : dynamicDecorators) {
for (DelegatingServiceImpl<T> dynamicDecorator : collectionDynamicDecoratorsForType) {
LOG.trace("Iterating over {}", dynamicDecorator.getClass());
dynamicDecorator.setDelegate(theService);
dynamicDecorator.setOrig(dynamicDecorator.getTypeObject());
theService = dynamicDecorator;
}
if (wrappedDecorator == null) {
getWrappedDynamicDecorators(delegate);
}

return theService.getOrig();
LOG.trace("Returning {}.", wrappedDecorator);

return wrappedDecorator;
}

private synchronized void getWrappedDynamicDecorators(T delegate) {
if (wrappedDecorator != null) {
return;
}
DelegatingServiceImpl<T> theService = new DelegatingServiceImpl<T>();
theService.setOrig(delegate);

for (Collection<DelegatingServiceImpl<T>> collectionDynamicDecoratorsForType : dynamicDecorators) {
for (DelegatingServiceImpl<T> dynamicDecorator : collectionDynamicDecoratorsForType) {
LOG.trace("Iterating over {}", dynamicDecorator.getClass());
dynamicDecorator.setDelegate(theService);
dynamicDecorator.setOrig(dynamicDecorator.getTypeObject());
theService = dynamicDecorator;
}
}

wrappedDecorator = theService.getOrig();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,49 @@
import org.slf4j.LoggerFactory;

public class DynamicDecoratorConfigurationProducer {
private static final String DECORATOR_CLASS_NAME = "decoratorClassName";
private static final String PRIORITY = "priority";
private static final double DEFAULT_PRIORITY = 1.0;
//TODO: make sure deterministic ordering if priority is the same

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

@Produces
@ConfiguredDynamicDecorators
public Map<String, Double> getDynamicDecoratorConfiguration() throws ConfigurationException {
String path = getPath();
SingleJsonFileConfigurationStorage storage = new SingleJsonFileConfigurationStorage(path);
List<Map<String, Object>> config = (List<Map<String, Object>>) storage.getConfigurationNode("dynamicDecorators", null);

Map<String, Double> configuredDynamicDecorators = new HashMap<String, Double>();
for (Map<String, Object> object : config) {
if (object.containsKey(DECORATOR_CLASS_NAME)) {
String decoratorClassName = (String) object.get(DECORATOR_CLASS_NAME);
double priority = getPriority(object);
LOG.debug("Found dynamic decorator {} in configuration with priority {}.", decoratorClassName, priority);
configuredDynamicDecorators.put(decoratorClassName, priority);
}
}

return configuredDynamicDecorators;
}

private String getPath() {
return System.getProperty("org.dcm4che.conf.dynamic.decorator.config", "../standalone/configuration/dcm4chee-arc/dynamic-decorators.json");
}

private double getPriority(Map<String, Object> map) {
double priority;
if (map.containsKey(PRIORITY)) {
priority = (double) map.get(PRIORITY);
} else {
priority = DEFAULT_PRIORITY;
}
return priority;
}
private static final String DECORATOR_CLASS_NAME = "decoratorClassName";
private static final String PRIORITY = "priority";
private static final double DEFAULT_PRIORITY = 1.0;
// TODO: make sure deterministic ordering if priority is the same

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

@Produces
@ConfiguredDynamicDecorators
public Map<String, Double> getDynamicDecoratorConfiguration() throws ConfigurationException {
String path = getPath();
SingleJsonFileConfigurationStorage storage = new SingleJsonFileConfigurationStorage(path);
List<Map<String, Object>> config = (List<Map<String, Object>>) storage
.getConfigurationNode("dynamicDecorators", null);

Map<String, Double> configuredDynamicDecorators = new HashMap<String, Double>();
if (config != null) {
for (Map<String, Object> object : config) {
if (object.containsKey(DECORATOR_CLASS_NAME)) {
String decoratorClassName = (String) object.get(DECORATOR_CLASS_NAME);
double priority = getPriority(object);
LOG.debug("Found dynamic decorator {} in configuration with priority {}.", decoratorClassName, priority);
configuredDynamicDecorators.put(decoratorClassName, priority);
}
}
}

return configuredDynamicDecorators;
}

private String getPath() {
return System.getProperty("org.dcm4che.conf.dynamic.decorator.config",
"../standalone/configuration/dcm4chee-arc/dynamic-decorators.json");
}

private double getPriority(Map<String, Object> map) {
double priority;
if (map.containsKey(PRIORITY)) {
priority = (double) map.get(PRIORITY);
} else {
priority = DEFAULT_PRIORITY;
}
return priority;
}
}

0 comments on commit 16dc64a

Please sign in to comment.