0

so, we have a properties file per vendor where vendors will never really exceed 50(we are at about 20 maybe right now). Each has about 20 properties or so.

We would like to have each instance of VendorStream(AND all his children) to be injected with a whole new set of properties for the specific vendor. Every one uses the VendorStream but with a different array of properties.

Is there a clean way to do this with guice or do we have to have a module per vendor which we really really do not want as we don't want 20 modules.

thanks, Dean

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212

1 Answers1

0

I would do it like this!

I would create ParentModul which would scan a predefined folder for vendors configs and install a Private module for each vendor config found. Private module encapsulate binding from other modules so, you can bind your properties from config as constants like @Named("vendor_name") String vendorName.

Look at this presentation, it could give you better explanation how to use private module property. There is also a github example. http://slid.es/milanbaran/dec

There are some snippets:

Parent Module:

protected void loadVendorModules(String config) {
File configDir = new File(config);
if(!configDir.exists()) {
  addError("Configuration directory [%s] doesn't exist. Also, it should contain broker definition files. [broker*.yml]",config);
}
if(!configDir.isDirectory()) {
  addError("The `dir.config` parameter is supposed to be a directory. Actually it is not %s",config);
}
broker_files = new File(config).listFiles(new PatternFilenameFilter(FILE_PATTERN));
if(broker_files==null || broker_files.length==0) {
  addError("Configuration directory [%s] doesn't contain any broker definition files, please add at least one to start.",config);
}
for(File f : broker_files) {
  install(new VendorPrivateModule<T>(f.getAbsolutePath()));
}
return;

}

Vendor Private Module:

protected void configure() {
....
if(config.getConfig()!=null) {
  for(String name : config.getConfig().keySet()) {
    String value = config.getConfig().get(name);
    if(value!=null) {
      bind(Key.get(String.class, Names.named(name)))
          .toInstance(value);
    } else {
      bind(Key.get(String.class, Names.named(name)))
          .toProvider(Providers.of((String) null));
    }
  }
}
...
Milan Baran
  • 4,133
  • 2
  • 32
  • 49
  • something is not too clear here to me. In my VendorStream.java and my VendorStreamChild.java, I would have @Inject on a field but his property needs to vary...I guess I can only do this with multiple modules which seemed kind of ugly. – Dean Hiller Mar 03 '14 at 15:13
  • Dean, the complexity is not going to disappear just by using some cool sexy technology. If your property needs to vary you have no other option. I think reusing private modules with high level of binding encapsulation is kind of pure excellence. If you want me to help you further send me a email and share the source code. It is hard to explain by my imagination of your problem. – Milan Baran Mar 03 '14 at 15:20