1
edit
Changes
→Install and run the Service Consumer Bundle
==Create the Chat Interface==
===Define the service interface===
===Create the bundle with the interface===
===Export the Package===
:To register our service we must export the package into the interface bundle :a) Select the MANIFEST.MF :b) In the Runtime tab select Add :[[image:08ExportPackage.png|300px]] :c) Select the chatinterface package :[[image:09PackageExported.png|300px]] ==Implement the Chat Provider== ===Register the service in the class Activator===:a) Create a new Plug-in Project :b) Name your Plug-in Project (cs.dps914.osgi.lab.provider) again be sure to select the target platform correctly :c) This time you can leave the Activator checked as you will need it. (Change the name of the activator to cs.dps914.osgi.lab.provider.ChatActivator) :[[image:10PActivator.png|300px]] ===Define MANIFEST.MF=== :a) Select the MANIFEST.MF:b) In the Dependencies tab select Add for Required Plug-ins:c) Add cs.dps914.osgilab.chatinterface :[[==image:11PManifest.png|600px]] ===Create Java Classes Required===:a) Create 2 Java Classes (SimpleMessage and SimpleChatSystem):b) Make sure the package is defined correctly (cs.dps914.osgi.lab.provider.internals):[[image:12PClass.png |300px]]:c) Fill Java Classes with following Data ::SimpleMessage.java:: import java.util.Calendar; import java.util.Date; import cs.dps914.osgi.lab.chatinterface.IMessage; public class SimpleMessage implements IMessage { private String source; private Date time; private String message; public SimpleMessage(String src,String msg) { setSource(src); setTime(Calendar.getInstance().getTime()); setMessage(msg); } public void setSource(String src) { if (src!=null) { source=src; } else { source=""; } } public String getSource() { return source; } public void setTime(Date time) { if (time!=null) { this.time=time; } else { this.time=Calendar.getInstance().getTime(); } } public Date getTime() { return time; } public void setMessage(String msg) { if (msg!=null) { message=msg; } else { message=""; } } public String getMessage() { return message; } } SimpleChatSystem.java import java.util.ArrayList; import cs.dps914.osgi.lab.chatinterface.IChatClient; import cs.dps914.osgi.lab.chatinterface.IChatSystem; import cs.dps914.osgi.lab.chatinterface.IMessage; public class SimpleChatSystem implements IChatSystem { public static ArrayList<IChatClient> clientList=new ArrayList<IChatClient>(); public static ArrayList<IMessage> messageList=new ArrayList<IMessage>(); public void registerClient(IChatClient client) { clientList.add(client); } public ArrayList<IMessage> appendMessage(String src, String msg) { IMessage newMsg=new SimpleMessage(src,msg); messageList.add(newMsg); for (IChatClient client:clientList) { client.receiveMessage(newMsg); } return messageList; } } ===Edit ChatActivator.java to add the following:=== import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import cs.dps914.osgi.lab.chatinterface.IChatSystem; import cs.dps914.osgi.lab.provider.internals.SimpleChatSystem; and public void start(BundleContext bundleContext) throws Exception { ChatActivator.context = bundleContext; IChatSystem vs=new SimpleChatSystem(); context.registerService(IChatSystem.class.getName(),vs,null); } ===Install and Run the Chat Provider Bundle=== : a) Right click on MAINFEST.MF of the Provider, and go to Run As then Run Configuration: b) Right click on OSGI Framework and create a new Configuration: c) In Workspace, check the chatinterface bundle and the provider bundle.: d) Deselect Target Platform and click on Add Required Bundles, then check the Only show selected.: e) Make sure "validate bundles automatically..." is selected: f) Click apply[[image:13RunProvider.png |500px]] ==Implement Chat Client==The Chat Client is dependent on the Chat Interface and Provider.===Define the MANIFEST.MF for the service consumer bundle===:a) Create a new Plug-in Project:b) Name your Plug-in Project (cs.dps914.osgi.lab.provider) again be sure to select the target platform correctly:c) This time you can leave the Activator checked as you will need it. (Change the name of the activator to cs.dps914.osgi.lab.provider.ClientActivator):d) Edit the MANIFEST.MF dependencies to add the chat interface (similar to Provider):e) Edit the MANIFEST.MF[[image:14CManifest.png | 500px]] ===Use the Client Activator to find the service=== :add the following code to ClientActivator public void start(BundleContext bundleContext) throws Exception { ClientActivator.context = bundleContext; ServiceReference reference=context .getServiceReference(IChatSystem.class.getName()); if (reference!=null) { chatService=(IChatSystem)context.getService(reference); if (chatService!=null) { ChatApp.setup(chatService); context.ungetService(reference); } } } ===Implement classes required to consume the service=== :a) Create 2 Java Classes (ChatApp and ChatDialog):b) Make sure the package is defined correctly (cs.dps914.osgi.lab.client.app and cs.dps914.osgi.lab.client.gui respectively)