magja/magja — Issue #46: 46 - CDI 
Author: ceefour
Created: 2011-11-02 11:34:42+00:00
State: open

What is the primary target? Java SE or JEE?
I'd guess EE, but SE can be easily adopted to use Weld SE

I'd prefer to move Config from properties to using CDI
like

@Inject
MagentoApiConfiguration config;

and

@Singleton
@Startup
public class MagentoApiConfigurationImpl implements MagentoApiConfiguration {

private Map<String,Object> config;

@PostConstruct
public void initConfig() {
this.config = new HashMap<String,Object>() {
{
// default config
put(DEFAULT_ATTRIBUTE_SET, 4);
put(DEFAULT_ROOT_CATEGORY, 2);
}
};
}

public Object get(String key) {
return this.config.get(key);
}

public Object set(String key, Object value) {
return this.config.put(key, value);
}

public Integer getInteger(String key) {
Object value = this.config.get(key);
if ( value!=null && (value instanceof Integer)) {
return (Integer) value;
}

throw new IllegalStateException("invalid config value type requested");
}

public Object setInteger(String key, Integer value) {
return this.set(key, value);
}

@Produces
public MagentoApiConfiguration getMagentoApiConfiguration(InjectionPoint point) {
// maybe check injection point
return this;
}

}

public interface MagentoApiConfiguration {
public final static String API_USER = "api-user";
public final static String API_KEY = "api-key";
public final static String API_URL = "api-url";
public final static String DEFAULT_ATTRIBUTE_SET = "default-attr-set";
public final static String DEFAULT_ROOT_CATEGORY = "default-root-cat";

public Object get(String key);
public Object set(String key, Object value);
public Integer getInteger(String key);
public Object setInteger(String key, Integer value);
}

Tests could extend basic test class wich handles default test config
and sets config to test to avoid CDI.

Original link: http://code.google.com/p/magja/issues/detail?id=46