java - Plugin framework and bi-directional program communication -


what way let 2 different separate java programs communicate each other?

i not sure if want use osgi (but seems 1 still gets updates; jpf , jspf old). new topic plugin framework. right osgi description of architecture, plugin structure etc. , equinox , other thing real implementation, have use?

i make things more precise: want program core tool should able hot-load plugins, , bidirectionally communicate them , javafx gui.

i use class controlling gui , additional class algorithms , on (mvc). think style no longer helpful structure plugin-based tool.

i want use design pattern beginning, else it'll end mess.

the simplest solution problem use serviceloader (doc here). included in java, , simple use:

  1. load jar file(s) @ runtime
  2. detect classes implement particular interface (e.g.: your.package.yourservice).
  3. instantiate objects these classes.

here pretty post describing how (note: should use second proposal urlclassloader; not extend classpath dynamically). also, not forget declare services inside jar's meta-inf directory:

if com.example.impl.standardcodecs implementation of codecset service jar file contains file named

meta-inf/services/com.example.codecset  

this file contains single line:

com.example.impl.standardcodecs    # standard codecs 

by choosing approach, core program naturally have handle plugins, able communicate them easily. ensure bi-directional communication (i.e. plugins calling core program), suggest create interface core program implement, , pass interface plugins.

plugin interface:

public interface plugin {     public void dopluginstuff(caller caller); } 

core program caller interface:

public interface caller {    public void sendbackresults(object results); } 

plugin implementation (in separate jar file):

public class aparticularplugin implements plugin {    public void dopluginstuff(caller caller){       caller.sendbackresults("hello world");    } } 

core program:

public class coreprogram implements caller {     public void callplugin(url[] urltojarfiles){       urlclassloader ucl = new urlclassloader(urltojarfiles);       serviceloader<plugin> sl = serviceloader.load(plugin.class, ucl);       iterator<plugin> plugins = sl.iterator();       while (plugins.hasnext())          plugins.next().dopluginstuff(this);    }     public void sendbackresults(object results){       system.out.println(results.tostring());    }  } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -