Difference between revisions of "Teams Winter 2011/team6/lab2"

From CDOT Wiki
Jump to: navigation, search
(Create Java Classes Required)
(Install and run the Service Consumer Bundle)
 
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Lab 2 OSGI Tutorial=
+
'''Lab 2 OSGI Tutorial'''
 
:After downloading the “Eclipse classic” from [http://www.eclipse.org/downloads/ Eclipse] then proceed with the following steps to create Chat Program with OSGi.
 
:After downloading the “Eclipse classic” from [http://www.eclipse.org/downloads/ Eclipse] then proceed with the following steps to create Chat Program with OSGi.
  
Line 115: Line 115:
 
             return source;
 
             return source;
 
             }
 
             }
+
           
public void setTime(Date time) {
+
            public void setTime(Date time) {
if (time!=null) {
+
            if (time!=null) {
this.time=time;
+
            this.time=time;
} else {
+
            } else {
this.time=Calendar.getInstance().getTime();
+
            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;
 +
            }
 +
            }
  
public Date getTime() {
+
SimpleChatSystem.java
return time;
 
}
 
  
public void setMessage(String msg) {
+
            import java.util.ArrayList;
if (msg!=null) {
+
            import cs.dps914.osgi.lab.chatinterface.IChatClient;
message=msg;
+
            import cs.dps914.osgi.lab.chatinterface.IChatSystem;
} else {
+
            import cs.dps914.osgi.lab.chatinterface.IMessage;
message="";
+
           
}
+
            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)
 +
 
 +
:c) Fill Java Classes with following Data
 +
'''ChatApp.java'''
 +
import java.util.ArrayList;
 +
 +
import javax.swing.SwingUtilities;
 +
 +
import cs.dps914.osgi.lab.chatinterface.IChatClient;
 +
import cs.dps914.osgi.lab.chatinterface.IChatSystem;
 +
import cs.dps914.osgi.lab.chatinterface.IMessage;
 +
import cs.dps914.osgi.lab.client.gui.ChatDialog;
 +
 +
public class ChatApp implements IChatClient {
 +
 +
private IChatSystem service;
 +
private ChatDialog dialog;
 +
 +
public ChatApp(IChatSystem sys,ChatDialog gui) {
 +
service=sys;
 +
service.registerClient(this);
 +
dialog=gui;
 +
gui.setApp(this);
 +
}
 +
 +
public ArrayList<IMessage> sendMessage(
 +
String src, String msg) {
 +
return service.appendMessage(src,msg);
 +
}
 +
 +
public void receiveMessage(IMessage msg) {
 +
dialog.displayMessage(msg.getSource(),
 +
msg.getMessage(),msg.getTime());
 +
}
 +
 +
public static void exit() {
 +
System.exit(0);
 +
}
 +
 +
public static void setup(final IChatSystem service) {
 +
 +
SwingUtilities.invokeLater(new Runnable() {
 +
public void run() {
 +
ChatDialog window=new ChatDialog();
 +
 +
new ChatApp(service,window);
 +
 +
window.pack();
 +
window.setVisible(true);
 +
}
 +
});
 +
}
 +
}
  
public String getMessage() {
+
'''ChatDialog.java'''
return message;
 
}
 
}
 
  
lalala
+
import java.awt.BorderLayout;
 +
import java.awt.GridLayout;
 +
import java.awt.event.ActionEvent;
 +
import java.awt.event.ActionListener;
 +
import java.util.Date;
 +
import javax.swing.BorderFactory;
 +
import javax.swing.JButton;
 +
import javax.swing.JFrame;
 +
import javax.swing.JMenu;
 +
import javax.swing.JMenuBar;
 +
import javax.swing.JMenuItem;
 +
import javax.swing.JPanel;
 +
import javax.swing.JScrollPane;
 +
import javax.swing.JTextArea;
 +
import javax.swing.JTextField;
 +
import javax.swing.KeyStroke;
 +
import cs.dps914.osgi.lab.client.app.ChatApp;
 +
 +
public class ChatDialog extends JFrame {
 +
 +
private static final long serialVersionUID = -4817932904935871995L;
 +
 +
private ChatApp app;
 +
 +
private JScrollPane sp_display;
 +
private JTextArea ta_display;
 +
private JTextField tf_name;
 +
private JTextArea ta_msg;
 +
 +
public ChatDialog() {
 +
 +
super("Talk-to-Self Chat App");
 +
setLayout(new GridLayout(1,2));
 +
 +
getContentPane().add(makeDisplayArea());
 +
 +
getContentPane().add(makeCompositionArea());
 +
 +
setJMenuBar(makeMenu());
 +
}
 +
 +
private JPanel makeDisplayArea() {
 +
 +
JPanel panel=new JPanel();
 +
panel.setBorder(BorderFactory.createTitledBorder(
 +
"Messages"));
 +
panel.setLayout(new BorderLayout());
 +
 +
ta_display=new JTextArea();
 +
ta_display.setEnabled(false);
 +
sp_display=new JScrollPane(ta_display);
 +
 +
panel.add(sp_display,BorderLayout.CENTER);
 +
 +
JButton bt_clear=new JButton("Clear");
 +
bt_clear.setToolTipText("Clear message display");
 +
bt_clear.addActionListener(new ActionListener() {
 +
public void actionPerformed(ActionEvent e) {
 +
ta_display.setText("");
 +
}
 +
});
 +
 +
panel.add(bt_clear,BorderLayout.PAGE_END);
 +
 +
return panel;
 +
}
 +
 +
private JPanel makeCompositionArea() {
 +
 +
JPanel panel=new JPanel();
 +
panel.setBorder(BorderFactory.createTitledBorder(
 +
"Compose"));
 +
panel.setLayout(new BorderLayout());
 +
 +
tf_name=new JTextField();
 +
tf_name.setBorder(BorderFactory.createTitledBorder(
 +
"Your Name"));
 +
 +
panel.add(tf_name,BorderLayout.PAGE_START);
 +
 +
JPanel pn_msg=new JPanel();
 +
pn_msg.setLayout(new BorderLayout());
 +
pn_msg.setBorder(BorderFactory.createTitledBorder(
 +
"Your Message"));
 +
ta_msg=new JTextArea();
 +
JScrollPane sp_msg=new JScrollPane(ta_msg);
 +
pn_msg.add(sp_msg);
 +
 +
panel.add(pn_msg,BorderLayout.CENTER);
 +
 +
JButton bt_send=new JButton("Send");
 +
bt_send.setToolTipText("Send messages to yourself");
 +
bt_send.addActionListener(new ActionListener() {
 +
public void actionPerformed(ActionEvent e) {
 +
 +
app.sendMessage(tf_name.getText(),
 +
ta_msg.getText());
 +
 +
ta_msg.setText("");
 +
}
 +
});
 +
 +
panel.add(bt_send,BorderLayout.PAGE_END);
 +
 +
return panel;
 +
}
 +
 +
private JMenuBar makeMenu() {
 +
 +
// Setup menu bar
 +
JMenuBar menuBar = new JMenuBar();
 +
 +
// Setup menu options
 +
JMenu fileMenu = new JMenu("File");
 +
fileMenu.setMnemonic('F');
 +
menuBar.add(fileMenu);
 +
 +
// File menu
 +
JMenuItem fileClose = new JMenuItem("Quit");
 +
fileClose.setAccelerator(KeyStroke.getKeyStroke("ctrl W") );
 +
fileClose.addActionListener(new ActionListener() {
 +
public void actionPerformed(ActionEvent e) {
 +
ChatApp.exit();
 +
}
 +
});
 +
fileMenu.add(fileClose);
 +
 +
return menuBar;
 +
}
 +
 +
public void setApp(ChatApp app) {
 +
this.app=app;
 +
}
 +
 +
public void displayMessage(String source,
 +
String message,Date time) {
 +
 +
ta_display.append(source+" ("+time.toString()+"): "+
 +
message+"\n\n");
 +
}
 +
}
 +
===Install and run the Chat Client Bundle===
 +
:a) Go to Run Configuration then OSGi Framework and make sure the client provider and interface are selected.
 +
:b) Select Add Required Bundles

Latest revision as of 14:52, 12 April 2011

Lab 2 OSGI Tutorial

After downloading the “Eclipse classic” from Eclipse then proceed with the following steps to create Chat Program with OSGi.

Create the Chat Interface

The first step is to create the Chat Interface which is the Service Provider in the Program. This defines and provides the services available and the requirements and outcomes for those services.

Define the service interface

a) Create a new Plug-in Project
01NewProj.png
b) Name your Plug-in Project (cs.dps914.osgi.lab.chatinterface) and be sure to select the target platform (OSGi framework: Equinox)
02newproj02.png
c) Be sure to uncheck the “Generate an activator” option as you will not be needing an activator then go ahead and click finish.
03newproj3.png

Create the bundle with the interface

a) You must now create three interfaces: IChatClient, IChatSystem and IMessage
Be sure to edit the package name and the Interface Name.
300px
b) Fill the interfaces with the following information:
Message


05IMessage.png
Chat System
06ChatSystem.png
Chat Client
07ChatClient.png

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
08ExportPackage.png
c) Select the chatinterface package
09PackageExported.png


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)
10PActivator.png


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
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)
12PClass.png
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

13RunProvider.png


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

14CManifest.png

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)
c) Fill Java Classes with following Data

ChatApp.java

import java.util.ArrayList;

import javax.swing.SwingUtilities;

import cs.dps914.osgi.lab.chatinterface.IChatClient;
import cs.dps914.osgi.lab.chatinterface.IChatSystem;
import cs.dps914.osgi.lab.chatinterface.IMessage;
import cs.dps914.osgi.lab.client.gui.ChatDialog;

public class ChatApp implements IChatClient {

	private IChatSystem service;
	private ChatDialog dialog;
	
	public ChatApp(IChatSystem sys,ChatDialog gui) {
		service=sys;
		service.registerClient(this);
		dialog=gui;
		gui.setApp(this);
	}
	
	public ArrayList<IMessage> sendMessage(
			String src, String msg) {
		return service.appendMessage(src,msg);
	}
	
	public void receiveMessage(IMessage msg) {
		dialog.displayMessage(msg.getSource(),
				msg.getMessage(),msg.getTime());
	}
	
	public static void exit() {
		System.exit(0);
	}

	public static void setup(final IChatSystem service) {
		
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ChatDialog window=new ChatDialog();
				
				new ChatApp(service,window);
				
				window.pack();
				window.setVisible(true);
			}
		});
	}
}

ChatDialog.java

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import cs.dps914.osgi.lab.client.app.ChatApp;

public class ChatDialog extends JFrame {

	private static final long serialVersionUID = -4817932904935871995L;

	private ChatApp app;
	
	private JScrollPane sp_display;
	private JTextArea ta_display;
	private JTextField tf_name;
	private JTextArea ta_msg;

	public ChatDialog() {

		super("Talk-to-Self Chat App");
		setLayout(new GridLayout(1,2));

		getContentPane().add(makeDisplayArea());

		getContentPane().add(makeCompositionArea());

		setJMenuBar(makeMenu());
	}

	private JPanel makeDisplayArea() {

		JPanel panel=new JPanel();
		panel.setBorder(BorderFactory.createTitledBorder(
				"Messages"));
		panel.setLayout(new BorderLayout());
		
		ta_display=new JTextArea();
		ta_display.setEnabled(false);
		sp_display=new JScrollPane(ta_display);
		
		panel.add(sp_display,BorderLayout.CENTER);
		
		JButton bt_clear=new JButton("Clear");
		bt_clear.setToolTipText("Clear message display");
		bt_clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ta_display.setText("");
			}
		});
		
		panel.add(bt_clear,BorderLayout.PAGE_END);
		
		return panel;
	}

	private JPanel makeCompositionArea() {

		JPanel panel=new JPanel();
		panel.setBorder(BorderFactory.createTitledBorder(
				"Compose"));
		panel.setLayout(new BorderLayout());
		
		tf_name=new JTextField();
		tf_name.setBorder(BorderFactory.createTitledBorder(
		"Your Name"));
		
		panel.add(tf_name,BorderLayout.PAGE_START);
		
		JPanel pn_msg=new JPanel();
		pn_msg.setLayout(new BorderLayout());
		pn_msg.setBorder(BorderFactory.createTitledBorder(
		"Your Message"));
		ta_msg=new JTextArea();
		JScrollPane sp_msg=new JScrollPane(ta_msg);
		pn_msg.add(sp_msg);
		
		panel.add(pn_msg,BorderLayout.CENTER);
		
		JButton bt_send=new JButton("Send");
		bt_send.setToolTipText("Send messages to yourself");
		bt_send.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				app.sendMessage(tf_name.getText(),
						ta_msg.getText());
				
				ta_msg.setText("");
			}
		});
		
		panel.add(bt_send,BorderLayout.PAGE_END);
		
		return panel;
	}

	private JMenuBar makeMenu() {

		// Setup menu bar
		JMenuBar menuBar = new JMenuBar();

		// Setup menu options
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic('F');
		menuBar.add(fileMenu);

		// File menu
		JMenuItem fileClose = new JMenuItem("Quit");
		fileClose.setAccelerator(KeyStroke.getKeyStroke("ctrl W") );
		fileClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ChatApp.exit();
			}
		});
		fileMenu.add(fileClose);

		return menuBar;
	}
	
	public void setApp(ChatApp app) {
		this.app=app;
	}

	public void displayMessage(String source,
			String message,Date time) {
		
		ta_display.append(source+" ("+time.toString()+"): "+
				message+"\n\n");
	}
}

Install and run the Chat Client Bundle

a) Go to Run Configuration then OSGi Framework and make sure the client provider and interface are selected.
b) Select Add Required Bundles