Professional Documents
Culture Documents
OSGI Bundle
1. Plug-in
2.
3.
Finish
4.
Dependencies
5.
Workspace bundle Add Required Bundles, 3 org.eclipse.osgi org.eclipse.osgi.services org.eclipse.equinox.ds org.eclipse.equinox.ds Eclipse jar service.xml
6.
Debug
/**
* IDisplay.java */ public class Display implements com.infotech.bpr.autodisplay.display.IDisplay { public void setDescInfor(String strInfor) { System.out.println(strInfor); } } /** * Display.java */ package com.infotech.bpr.autodisplay.display; import org.osgi.service.component.ComponentContext; public class Display implements com.infotech.bpr.autodisplay.display.IDisplay { public void setDescInfor(String strInfor) { System.out.println("" +strInfor); } /* activate Bundle deactivate Bundle */ protected void activate(ComponentContext context) { System.out.println(""); } protected void deactivate(ComponentContext context) { System.out.println(""); } }
/** * Activator.java */ package com.infotech.bpr.autodisplay; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println(""); } public void stop(BundleContext context) throws Exception { System.out.println(""); } } OSGI-INF service.xml
<?xml version="1.0" encoding="UTF-8"?> <component name="DisplayService"> <implementation class="com.infotech.bpr.autodisplay.display.Display"/> <service> <provide interface="com.infotech.bpr.autodisplay.display.IDisplay"/> </service> </component> DisplayService Idisplay Display 8. MANIFEST.MF Runtime Exported packages Idispaly
OSGI Bundle
1. 2. 1~6 Bundle dependencies Imported Packages bundle
3.
public void stop(BundleContext context) throws Exception { System.out.println(""); } } /** * AutoTallyTaskProc.java */ package com.infotech.brp.autotally;
import java.util.Map; import com.infotech.bpr.autodisplay.display.IDisplay; import org.osgi.service.component.ComponentContext; public class AutoTallyTaskProc implements IAutoTallyTaskProc { private IDisplay display; public IDisplay getDisplay() { return display; } public void setDisplay(IDisplay dispaly) { System.out.println(" setDisplay"); this.display = dispaly; } public void unSetDisplay(IDisplay dispaly) { System.out.println(" unSetDisplay"); if (this.display == dispaly) { display = null; } } protected void activate(ComponentContext context) { display.setDescInfor(""); } public void deactivate(ComponentContext context) { display.setDescInfor(""); } } 4. IDisplay OSGI-INF components.xml <?xml version="1.0" encoding="UTF-8"?> <component name="Autotally"> <implementation class="com.infotech.brp.autotally.AutoTallyTaskProc"/> <reference name="Displayer" interface="com.infotech.bpr.autodisplay.display.IDisplay" bind="setDisplay" unbind="unSetDisplay" cardinality="1..1" policy="dynamic"/>
</component>
Autotally com.infotech.brp.autotally.AutoTallyTaskProc
5. MANIFEST.MF Service-Component: OSGI-INF/components.xml MANIFEST.MF Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Autotally Plug-in Bundle-SymbolicName: com.infotech.brp.autotally Bundle-Version: 1.0.0 Bundle-Activator: com.infotech.brp.autotally.Activator Bundle-Vendor: INFOTECH Import-Package: com.infotech.bpr.autodisplay.display, org.osgi.framework;version="1.4.0", org.osgi.service.component;version="1.0.0" Service-Component: OSGI-INF/components.xml 6.
ss
stop 86 stop 87
Spring
1. Bundle Spring Spring Dynamic Modules for OSGi(tm) Service Platforms
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"> <bean id="AutoDisplay" class="com.infotech.bpr.autodisplay.display.Display"> </bean> <osgi:service id="SAutoDisplay" ref="AutoDisplay" interface="com.infotech.bpr.autodisplay.display.IDisplay"> </osgi:service> </beans> 3. MANIFEST.MF Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Autodisplay Plug-in Bundle-SymbolicName: com.infotech.bpr.autodisplay Bundle-Version: 1.0.0 Bundle-Activator: com.infotech.bpr.autodisplay.Activator Bundle-Vendor: INFOTECH Import-Package: org.osgi.framework;version="1.4.0", org.osgi.service.component;version="1.0.0" Export-Package: com.infotech.bpr.autodisplay.display Spring-Context: SPRING-INF/springservice.xml
MANIFEST.MF Service-Component Spring-Context.
4.
unSet deactivate
Spring
1 12 xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"> <bean id="AutoTallyTaskProc" class="com.infotech.brp.autotally.AutoTallyTaskProc"> </bean> <osgi:reference id="ConfigControl" interface="com.infotech.bpr.autodisplay.display.IDisplay" > <osgi:listener ref="AutoTallyTaskProc" bind-method="onBind" unbind-method="onUnbind"></osgi:listener> </osgi:reference> <osgi:service id="SAutoTally" ref="AutoTallyTaskProc" interface="com.infotech.brp.autotally.IAutoTallyTaskProc"> <registration-listener ref="AutoTallyTaskProc" registration-method="serviceRegistered" unregistration-method="serviceUnregistered"/> </osgi:service> </beans>
bean id AutoTallyTaskProc bean osgi:reference Idisplay AutoTallyTaskProc onBind onUnbind osgi:service SautoTally IautoTallyTaskProc Bean AutoTallyTaskProc AutoTallyTaskProc serviceRegisteredserviceUnregistered 2 AutoTallyTaskProc package com.infotech.brp.autotally; import java.util.Map; import com.infotech.bpr.autodisplay.display.IDisplay; public class AutoTallyTaskProc implements IAutoTallyTaskProc{ private IDisplay display; public IDisplay getDisplay() { return display; } public void setDisplay(IDisplay dispaly) { System.out.println(" setDisplay"); this.display = dispaly; }
public void serviceRegistered(IAutoTallyTaskProc serviceInstance, Map serviceProperties) { System.out.println(""); display.setDescInfor("Hello"); } public void serviceUnregistered(IAutoTallyTaskProc serviceInstance, Map serviceProperties) { System.out.println(""); } public void onBind(IDisplay display, Map argMap) throws Exception { System.out.println(""+ display.getClass().getName()); this.display = (IDisplay)display; }
public void onUnBind(IDisplay display, Map argMap) throws Exception { System.out.println(""+ display.getClass().getName()); if (this.display == display) { display = null; } }} IautoTallyTaskProc Spring OSGi API 3
OK
org.eclipse.equinox.ds_1.0.0.v20070226.jar OSGI-INF/server.xml
XML
<?xml version="1.0" encoding="UTF-8"?> <component name="osgitest2.invokers"> <implementation class="osgitest2.invokers"/> <reference name="autotallytest" interface="osgitest.autotally.ILog" bind="setLogger" unbind="unSetLogger" cardinality="1..1" policy="dynamic"/> </component>