The basic approach is given in the OSGi principles and best practices book, where a few lines of code are added to the Lin Hao went to complete a usable example:
1 first set up several plug-in projects, and exported into the plug-in package, where the export of the plug-in package is uniformly placed under the G://osgi_prj//system_bundle directory
2 build Java project and write a class with the main method for starting OSGi and previous Plug-ins
Source:
public class Startosgi {private static Bundlecontext context = NULL;/** * @param args * @throws Exception/Publi c static void Main (string[] args) throws Exception {String osgibundles = "org.eclipse.osgi.services_3.2.0.v20090520-1800 . Jar@start,javax.servlet_2.5.0.v200806031605.jar@start,cxf-dosgi-ri-singlebundle-distribution-1.2.jar@start "+" , Databaseplugin_1.0.0.jar@start,baseplugin_1.0.0.jar@start,messageplugin_1.0.0.jar@start "; Configure the Equinox startup Frameworkproperties.setproperty ("Osgi.noshutdown", "true"); Frameworkproperties.setproperty ("Eclipse.ignoreapp", "true"); Frameworkproperties.setproperty ("Osgi.bundles.defaultStartLevel", "4"); Frameworkproperties.setproperty ("Osgi.bundles", osgibundles); Set bundle path String Bundlepath = "g://osgi_prj//system_bundle" as needed; Specifies the directory where the plugins is to be loaded Frameworkproperties.setproperty ("Osgi.syspath", Bundlepath); Call Eclipsestarter, complete the start of the container, specify the configuration directory Eclipsestarter.run (new string[] {"-configuration", "Configuration", "- Console}, NULL); Obtained through eclipestarter to bundlecontext context = Eclipsestarter.getsystembundlecontext (); Object obj = Getosgiservice (MsgService.class.getName ()); Class<?> cls = Getbundleclass ("Messageplugin", MsgService.class.getName ()); Method m = Cls.getmethod ("Findbyproperty", new class[] {string.class, object.class}); List List = (list) M.invoke (obj, new object[] {"id", 2}); System.out.println (List.get (0). toString ()); Messege msg = new Messege (); Msg.setfrom (10); Msg.setto (10); Msg.settitle ("ttttt"); Msg.setcontent ("6666"); Msg.setisread (1); Msg.setreaddate (New Timestamp () (New Date (). GetTime ())); Msg.setsenddate (New Timestamp () (New Date (). GetTime ())); Msg.setsubjectid ("111"); Msg.setcompanyid (4); Method m2 = Cls.getmethod ("Add", new class[] {string.class}); M2.invoke (obj, jsonconvertor.obj2string (msg)); The public static Object Getosgiservice (String serviceName) {ServiceReference serviceref=context.getservicereference ( ServiceName); if (serviceref==null) return null; Return Context.getservice(SERVICEREF); /** * Gets the class of plug-ins in the OSGi container/public static class<?> Getbundleclass (String bundlename,string className) throws exception{ Bundle[] Bundles=context.getbundles (); for (int i = 0; i < bundles.length i++) {if bundlename.equalsignorecase (Bundles[i].getsymbolicname ())} {return bundle S[i].loadclass (ClassName); } return null; }
The 3 runtime can see the internal class method invoked to OSGi
Description: The bundle inside OSGi is loaded into the inner class through its own classloader, so it is possible to get class only through reflection into the specified bundle, instead of importing the package into the calling project. Here you can also see that the Findbyproperty method can only return a list with no type, and that if replaced by list<message>, it will go wrong because the calling project is not the same as the class of the plug-in.