apache felix - OSGi force bundle start twice with different configurations -


i'm using embedded felix in application. application can potentially deal lot of plugins exposes similar interface ifoo. there default implementation fooimpl plugins default fooimpl can used specific configuration files.

i dynamically install , start same bundle (with fooimpl) when new configuration file appears. i've reviewed fileinstall have no idea how apply there.

update: deployment sequence. jar containing fooimpl , ifoo stable, need hot-deploy of new instances result of uploading new .cfg file scope of fileinstall. desired simple - user uploads .cfg, new service (instance of fooimpl) appeared.

using factory configurations allow create different instances of fooimpl based on different configurations.

for example in declarative services can create component

import org.apache.felix.scr.annotations.*; import org.apache.sling.commons.osgi.propertiesutil;  @component(metatype = true,          name = fooimpl.service_pid,         configurationfactory = true,          specversion = "1.1",         policy = configurationpolicy.require) public class fooimpl implements ifoo {     //the pid can defined in interface     public static final string service_pid = "com.foo.factory";      private static final string default_bar = "yahoo";     @property     private static final string prop_bar = "bar";      @property(intvalue = 0)     static final string prop_ranking = "ranking";      private serviceregistration reg;      @activate     public void activate(bundlecontext context, map<string, ?> conf)         throws invalidsyntaxexception     {         dictionary<string, object> props = new hashtable<string, object>();         props.put("type", propertiesutil.tostring(config.get(prop_bar), default_bar));         props.put(constants.service_ranking,             propertiesutil.tointeger(config.get(prop_ranking), 0));         reg = context.registerservice(ifoo.class.getname(), this, props);     }      @deactivate     private void deactivate()     {         if (reg != null)         {             reg.unregister();         }     } } 

key points here being

  1. you use component of type configurationfactory
  2. in activate method read config , based on register service
  3. in deactivate explicitly unregister service
  4. end users create config file name <pid>-<some name>.cfg. ds activate component.

then can create multiple instances creating configuration (using file install like) file name <pid>-<some name>.cfg com.foo.factory-type1.cfg

refer jdbcloginmodulefactory , associated config 1 such example.

if want achieve same via plain osgi need register managedservicefactory. refer jaasconfigfactory 1 such example.

key points here being

  1. you register managedservicefactory instance configuration pid service property
  2. in managedservicefactory(string pid, dictionary properties) callback register instances of fooimpl based on config properties

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

php - HTTP_REFERER woes: How can I allow access to a specific page, only when a visitor has visited another specific page beforehand? -

java Extracting Zip file -