ajayz15 / magja

Automatically exported from code.google.com/p/magja
1 stars 0 forks source link

CDI #46

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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 issue reported on code.google.com by laluz...@gmail.com on 22 Sep 2011 at 8:10

GoogleCodeExporter commented 9 years ago
I am all for CDI, but I disagree with the described implementation.

Especially the 'interface MagentoApiConfiguration', this is just a Map... A Map 
with a @Qualifier will be more like it. But even then, I'm not sure if using 
CDI for configuration purpose is a good idea.

Original comment by ceefour666@gmail.com on 31 Oct 2011 at 2:15

GoogleCodeExporter commented 9 years ago
So you'd prefere something like
@Qualifier
@Retention(RUNTIME)
@Target({FIELD, PARAMETER})
@interface MagjaConfig {}

with 

@Inject
@MagjaConfig
String apiUser; ?

Actually the map was just added for showing the design principle. Standard 
config impl could internally hold a Properties. But with using @Alternative the 
developer could provide his/her implementation may be referencing is global 
config impl or db backend

Original comment by laluz...@gmail.com on 22 Jan 2012 at 4:35

GoogleCodeExporter commented 9 years ago
or maybe a better way:

@Qualifier
@Retention(RUNTIME)
@Target({FIELD, PARAMETER})
@interface MagjaConfig {
    String value();
}

Original comment by laluz...@gmail.com on 22 Jan 2012 at 4:46