Open main menu

CDOT Wiki β

Teams Winter 2011/team2/project

Revision as of 16:36, 16 April 2011 by Thupten (talk | contribs)

Tutorial

In this tutorial, we will be elaborating on how to replicate our OSGI project application. We will provide a brief explanation of each plug-in project needed, the packages that corresponds to those projects, and of course the java source files that will be used to create the program.

Project

For this project, we are going to create a weather service program using the osgi framework. This project will contain three bundles: Consumer, Interface and Provider bundles. The Interface bundle will contain a set of interface and classes used by both Consumer and Provider. The Provider bundle will provide a service to return weather information for a selected city. The Consumer bundle will use the service reference to get weather information from the Provider and display in a GUI window based on Java Swing Library.

Prerequisites


Steps

Prepare eclipse

1. In Eclipse, create a new workspace. 2. Create a new Plug-in project following settings, name it consumer.
 

 

 

3. Likewise create 2 more plug-in projects called interface and provider. Uncheck the 'Generate Activator' option for interface.

Interface bundle

1. In the interface bundle, we create a package, ecl.team2.project.weather. We will create classes and interface java files that are used by consumer and provider bundles.
 

2. The default MANIFEST.MF of interface bundle looks like this. It does not require any modification.

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Weather
Bundle-SymbolicName: interface
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: ecl.team2.project.weather

3. Create interface WeatherInterface.java

package ecl.team2.project.weather;

import java.util.ArrayList;

public interface WeatherInterface {
	WeatherAndCurrent getWeather(String city) throws Exception;
    ArrayList<Location> getCity (String city) throws Exception;
}

4. Create WeatherAndCurrent.java

package ecl.team2.project.weather;

import java.util.ArrayList;

public  class WeatherAndCurrent {
	CurrentCondition Current;
	public WeatherAndCurrent() {
		super();
		// TODO Auto-generated constructor stub
	}
	public WeatherAndCurrent(CurrentCondition current, ArrayList<Weather> weathers) {
		super();
		Current = current;
		this.weathers = weathers;
	}
	public CurrentCondition getCurrent() {
		return Current;
	}
	public void setCurrent(CurrentCondition current) {
		Current = current;
	}
	public ArrayList<Weather> getWeathers() {
		return weathers;
	}
	public void setWeathers(ArrayList<Weather> weathers) {
		this.weathers = weathers;
	}
	ArrayList<Weather> weathers;
}

5. Create Weather class

package ecl.team2.project.weather;


public class Weather {
	private String date;
	private String tempMaxC;
	private String tempMaxF;
	private String tempMinC;
	private String tempMinF;
	private String windspeedMiles;
	private String windspeedKmph;
	private String winddirection;
	private String winddir16Point;
	private String winddirDegree;
	private String weatherCode;
	private String weatherIconUrl;
	private String weatherDesc;
	private String precipMM;


	public Weather(){
		super();
	}
	public Weather(String date, String tempMaxC, String tempMaxF,
			String tempMinC, String tempMinF, String windspeedMiles,
			String windspeedKmph, String winddirection, String winddir16Point,
			String winddirDegree, String weatherCode, String weatherIconUrl,
			String weatherDesc, String precipMM) {
		super();
		this.date = date;
		this.tempMaxC = tempMaxC;
		this.tempMaxF = tempMaxF;
		this.tempMinC = tempMinC;
		this.tempMinF = tempMinF;
		this.windspeedMiles = windspeedMiles;
		this.windspeedKmph = windspeedKmph;
		this.winddirection = winddirection;
		this.winddir16Point = winddir16Point;
		this.winddirDegree = winddirDegree;
		this.weatherCode = weatherCode;
		this.weatherIconUrl = weatherIconUrl;
		this.weatherDesc = weatherDesc;
		this.precipMM = precipMM;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getTempMaxC() {
		return tempMaxC;
	}
	public void setTempMaxC(String tempMaxC) {
		this.tempMaxC = tempMaxC;
	}
	public String getTempMaxF() {
		return tempMaxF;
	}
	public void setTempMaxF(String tempMaxF) {
		this.tempMaxF = tempMaxF;
	}
	public String getTempMinC() {
		return tempMinC;
	}
	public void setTempMinC(String tempMinC) {
		this.tempMinC = tempMinC;
	}
	public String getTempMinF() {
		return tempMinF;
	}
	public void setTempMinF(String tempMinF) {
		this.tempMinF = tempMinF;
	}
	public String getWindspeedMiles() {
		return windspeedMiles;
	}
	public void setWindspeedMiles(String windspeedMiles) {
		this.windspeedMiles = windspeedMiles;
	}
	public String getWindspeedKmph() {
		return windspeedKmph;
	}
	public void setWindspeedKmph(String windspeedKmph) {
		this.windspeedKmph = windspeedKmph;
	}
	public String getWinddirection() {
		return winddirection;
	}
	public void setWinddirection(String winddirection) {
		this.winddirection = winddirection;
	}
	public String getWinddir16Point() {
		return winddir16Point;
	}
	public void setWinddir16Point(String winddir16Point) {
		this.winddir16Point = winddir16Point;
	}
	public String getWinddirDegree() {
		return winddirDegree;
	}
	public void setWinddirDegree(String winddirDegree) {
		this.winddirDegree = winddirDegree;
	}
	public String getWeatherCode() {
		return weatherCode;
	}
	public void setWeatherCode(String weatherCode) {
		this.weatherCode = weatherCode;
	}
	public String getWeatherIconUrl() {
		return weatherIconUrl;
	}
	public void setWeatherIconUrl(String weatherIconUrl) {
		this.weatherIconUrl = weatherIconUrl;
	}
	public String getWeatherDesc() {
		return weatherDesc;
	}
	public void setWeatherDesc(String weatherDesc) {
		this.weatherDesc = weatherDesc;
	}
	public String getPrecipMM() {
		return precipMM;
	}
	public void setPrecipMM(String precipMM) {
		this.precipMM = precipMM;
	}
}

6. CurrentCondition.java class will return current weather.

package ecl.team2.project.weather;

public class CurrentCondition {
	private String observation_time;
	private String temp_C;
	private String temp_F;
	private String weatherCode;
	private String weatherIconUrl;
	private String weatherDesc;
	private String windspeedMiles;
	private String windspeedKmph;
	private String winddirDegree;
	private String winddir16Point;
	private String precipMM;
	private String humidity;
	private String visibility;
	private String pressure;
	private String cloudcover;
	
	public CurrentCondition(String observation_time, String temp_C, String temp_F,
			String weatherCode, String weatherIconUrl, String weatherDesc,
			String windspeedMiles, String windspeedKmph, String winddirDegree,
			String winddir16Point, String precipMM, String humidity,
			String visibility, String pressure, String cloudcover) {
		super();
		this.observation_time = observation_time;
		this.temp_C = temp_C;
		this.temp_F = temp_F;
		this.weatherCode = weatherCode;
		this.weatherIconUrl = weatherIconUrl;
		this.weatherDesc = weatherDesc;
		this.windspeedMiles = windspeedMiles;
		this.windspeedKmph = windspeedKmph;
		this.winddirDegree = winddirDegree;
		this.winddir16Point = winddir16Point;
		this.precipMM = precipMM;
		this.humidity = humidity;
		this.visibility = visibility;
		this.pressure = pressure;
		this.cloudcover = cloudcover;
	}
	
	public CurrentCondition() {
		super();
	}

	public String getObservation_time() {
		return observation_time;
	}
	public void setObservation_time(String observation_time) {
		this.observation_time = observation_time;
	}
	public String getTemp_C() {
		return temp_C;
	}
	public void setTemp_C(String temp_C) {
		this.temp_C = temp_C;
	}
	public String getTemp_F() {
		return temp_F;
	}
	public void setTemp_F(String temp_F) {
		this.temp_F = temp_F;
	}
	public String getWeatherCode() {
		return weatherCode;
	}
	public void setWeatherCode(String weatherCode) {
		this.weatherCode = weatherCode;
	}
	public String getWeatherIconUrl() {
		return weatherIconUrl;
	}
	public void setWeatherIconUrl(String weatherIconUrl) {
		this.weatherIconUrl = weatherIconUrl;
	}
	public String getWeatherDesc() {
		return weatherDesc;
	}
	public void setWeatherDesc(String weatherDesc) {
		this.weatherDesc = weatherDesc;
	}
	public String getWindspeedMiles() {
		return windspeedMiles;
	}
	public void setWindspeedMiles(String windspeedMiles) {
		this.windspeedMiles = windspeedMiles;
	}
	public String getWindspeedKmph() {
		return windspeedKmph;
	}
	public void setWindspeedKmph(String windspeedKmph) {
		this.windspeedKmph = windspeedKmph;
	}
	public String getWinddirDegree() {
		return winddirDegree;
	}
	public void setWinddirDegree(String winddirDegree) {
		this.winddirDegree = winddirDegree;
	}
	public String getWinddir16Point() {
		return winddir16Point;
	}
	public void setWinddir16Point(String winddir16Point) {
		this.winddir16Point = winddir16Point;
	}
	public String getPrecipMM() {
		return precipMM;
	}
	public void setPrecipMM(String precipMM) {
		this.precipMM = precipMM;
	}
	public String getHumidity() {
		return humidity;
	}
	public void setHumidity(String humidity) {
		this.humidity = humidity;
	}
	public String getVisibility() {
		return visibility;
	}
	public void setVisibility(String visibility) {
		this.visibility = visibility;
	}
	public String getPressure() {
		return pressure;
	}
	public void setPressure(String pressure) {
		this.pressure = pressure;
	}
	public String getCloudcover() {
		return cloudcover;
	}
	public void setCloudcover(String cloudcover) {
		this.cloudcover = cloudcover;
	}	
}

7. Location.java class will contain location information. This class contains detailed information about a location like city, country, coordinates, population. We will only be using the city name for this project.

package ecl.team2.project.weather;

public class Location {
	private String areaName;
	private String country;
	private String region;
	private String latitude;
	private String longitude;
	private String population;
	public Location(String areaName, String country, String region,
			String latitude, String longitude, String population) {
		super();
		this.areaName = areaName;
		this.country = country;
		this.region = region;
		this.latitude = latitude;
		this.longitude = longitude;
		this.population = population;
	}
	public Location() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getAreaName() {
		return areaName;
	}
	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getRegion() {
		return region;
	}
	public void setRegion(String region) {
		this.region = region;
	}
	public String getLatitude() {
		return latitude;
	}
	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}
	public String getLongitude() {
		return longitude;
	}
	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}
	public String getPopulation() {
		return population;
	}
	public void setPopulation(String population) {
		this.population = population;
	}
	
}

Provider bundle

1. Provider Activator

package ecl.team2.project.weathersystem.provider;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import ecl.team2.project.weather.WeatherInterface;
import ecl.team2.project.weathersystem.provider.internal.WeatherImplementation;

public class ProviderActivator implements BundleActivator {

	private static BundleContext context;

	static BundleContext getContext() {
		return context;
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext bundleContext) throws Exception {
		ProviderActivator.context = bundleContext;
		
		WeatherInterface sws = new WeatherImplementation();
		context.registerService(WeatherInterface.class.getName(), sws, null);
		System.out.println("Weather Station On !");
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		ProviderActivator.context = null;
		System.out.println("Weather Station Off !");
	}

}

2. Provider's implementation. The provider's implementation class WeatherImplementation.java uses a webservice from http://www.worldweatheronline.com/ get weather information. You will need to subscribe to their website to get an apikey to request and get weather information. Worldweatheronline.com provides data in both json and xml. We will use xml format in this project.

package ecl.team2.project.weathersystem.provider.internal;

import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import ecl.team2.project.weather.CurrentCondition;
import ecl.team2.project.weather.Location;
import ecl.team2.project.weather.Weather;
import ecl.team2.project.weather.WeatherAndCurrent;
import ecl.team2.project.weather.WeatherInterface;

public class WeatherImplementation implements WeatherInterface {
	//	ArrayList<Weather> weathers = new ArrayList<Weather>();
	String url="";
	String str_location="";

	public WeatherImplementation(){

	}
	public WeatherAndCurrent getWeather(String city) throws Exception {

		WeatherAndCurrent weatherAndCurrentTemp = new WeatherAndCurrent();
		ArrayList<Weather> weathers = new ArrayList<Weather>();
		CurrentCondition current = new CurrentCondition();

		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse("http://free.worldweatheronline.com/feed/weather.ashx?q="+city+"&num_of_days=4&date=today&format=xml&key=4aa3418d0a204342110904");
			doc.getDocumentElement().normalize();

			weathers = this.setWeathers(doc);
			current = this.setCurrent(doc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		weatherAndCurrentTemp.setCurrent(current);
		weatherAndCurrentTemp.setWeathers(weathers);
		return weatherAndCurrentTemp;
	}


	private ArrayList<Weather> setWeathers(Document doc) {

		ArrayList<Weather> weathers = new ArrayList<Weather>();



		NodeList nodeLst = doc.getElementsByTagName("weather");
		//System.out.println("Information of all employees");

		for (int s = 0; s < nodeLst.getLength(); s++) {
			Weather w = new Weather();
			Node fstNode = nodeLst.item(s);

			if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

				Element fstElmnt = (Element) fstNode;

				NodeList dateNmElmntLst = fstElmnt.getElementsByTagName("date");
				Element dateNmElmnt = (Element) dateNmElmntLst.item(0);
				NodeList dateNm = dateNmElmnt.getChildNodes();
				w.setDate(((Node) dateNm.item(0)).getNodeValue());

				NodeList tempMaxCNmElmntLst = fstElmnt.getElementsByTagName("tempMaxC");
				Element tempMaxCNmElmnt = (Element) tempMaxCNmElmntLst.item(0);
				NodeList tempMaxCNm = tempMaxCNmElmnt.getChildNodes();
				w.setTempMaxC(((Node) tempMaxCNm.item(0)).getNodeValue());

				NodeList tempMaxFNmElmntLst = fstElmnt.getElementsByTagName("tempMaxF");
				Element tempMaxFNmElmnt = (Element) tempMaxFNmElmntLst.item(0);
				NodeList tempMaxFNm = tempMaxFNmElmnt.getChildNodes();
				w.setTempMaxF(((Node) tempMaxFNm.item(0)).getNodeValue());

				NodeList tempMinCNmElmntLst = fstElmnt.getElementsByTagName("tempMinC");
				Element tempMinCNmElmnt = (Element) tempMinCNmElmntLst.item(0);
				NodeList tempMinCNm = tempMinCNmElmnt.getChildNodes();
				w.setTempMinC(((Node) tempMinCNm.item(0)).getNodeValue());

				NodeList tempMinFNmElmntLst = fstElmnt.getElementsByTagName("tempMinF");
				Element tempMinFNmElmnt = (Element) tempMinFNmElmntLst.item(0);
				NodeList tempMinFNm = tempMinFNmElmnt.getChildNodes();
				w.setTempMinF(((Node) tempMinFNm.item(0)).getNodeValue());

				NodeList windspeedMilesNmElmntLst = fstElmnt.getElementsByTagName("windspeedMiles");
				Element windspeedMilesNmElmnt = (Element) windspeedMilesNmElmntLst.item(0);
				NodeList windspeedMilesNm = windspeedMilesNmElmnt.getChildNodes();
				w.setWindspeedMiles(((Node) windspeedMilesNm.item(0)).getNodeValue());

				NodeList windspeedKmphNmElmntLst = fstElmnt.getElementsByTagName("windspeedKmph");
				Element windspeedKmphNmElmnt = (Element) windspeedKmphNmElmntLst.item(0);
				NodeList windspeedKmphNm = windspeedKmphNmElmnt.getChildNodes();
				w.setWindspeedKmph(((Node) windspeedKmphNm.item(0)).getNodeValue());

				NodeList winddirectionNmElmntLst = fstElmnt.getElementsByTagName("winddirection");
				Element winddirectionNmElmnt = (Element) winddirectionNmElmntLst.item(0);
				NodeList winddirectionNm = winddirectionNmElmnt.getChildNodes();
				w.setWinddirection(((Node) winddirectionNm.item(0)).getNodeValue());

				NodeList winddir16PointNmElmntLst = fstElmnt.getElementsByTagName("winddir16Point");
				Element winddir16PointNmElmnt = (Element) winddir16PointNmElmntLst.item(0);
				NodeList winddir16PointNm = winddir16PointNmElmnt.getChildNodes();
				w.setWinddir16Point(((Node) winddir16PointNm.item(0)).getNodeValue());

				NodeList winddirDegreeNmElmntLst = fstElmnt.getElementsByTagName("winddirDegree");
				Element winddirDegreeNmElmnt = (Element) winddirDegreeNmElmntLst.item(0);
				NodeList winddirDegreeNm = winddirDegreeNmElmnt.getChildNodes();
				w.setWinddirDegree(((Node) winddirDegreeNm.item(0)).getNodeValue());

				NodeList weatherCodeNmElmntLst = fstElmnt.getElementsByTagName("weatherCode");
				Element weatherCodeNmElmnt = (Element) weatherCodeNmElmntLst.item(0);
				NodeList weatherCodeNm = weatherCodeNmElmnt.getChildNodes();
				w.setWeatherCode(((Node) weatherCodeNm.item(0)).getNodeValue());

				NodeList weatherIconUrlNmElmntLst = fstElmnt.getElementsByTagName("weatherIconUrl");
				Element weatherIconUrlNmElmnt = (Element) weatherIconUrlNmElmntLst.item(0);
				NodeList weatherIconUrlNm = weatherIconUrlNmElmnt.getChildNodes();
//				System.out.println(((Node) weatherIconUrlNm.item(0)).getNodeValue());
				w.setWeatherIconUrl(((Node) weatherIconUrlNm.item(0)).getNodeValue());

				NodeList weatherDescNmElmntLst = fstElmnt.getElementsByTagName("weatherDesc");
				Element weatherDescNmElmnt = (Element) weatherDescNmElmntLst.item(0);
				NodeList weatherDescNm = weatherDescNmElmnt.getChildNodes();
				w.setWeatherDesc(((Node) weatherDescNm.item(0)).getNodeValue());

				NodeList precipMMNmElmntLst = fstElmnt.getElementsByTagName("precipMM");
				Element precipMMNmElmnt = (Element) precipMMNmElmntLst.item(0);
				NodeList precipMMNm = precipMMNmElmnt.getChildNodes();
				w.setPrecipMM(((Node) precipMMNm.item(0)).getNodeValue());

			}
			weathers.add(w);
		}
		return weathers;


	}

	private CurrentCondition setCurrent(Document doc) {

		CurrentCondition current = new CurrentCondition();

		NodeList nodeLst = doc.getElementsByTagName("current_condition");
		//System.out.println("Information of all employees");

		for (int s = 0; s < nodeLst.getLength(); s++) {

			Node fstNode = nodeLst.item(s);

			if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

				Element fstElmnt = (Element) fstNode;



				NodeList observation_timeNmElmntLst = fstElmnt.getElementsByTagName("observation_time");
				Element observation_timeNmElmnt = (Element) observation_timeNmElmntLst.item(0);
				NodeList observation_timeNm = observation_timeNmElmnt.getChildNodes();

				current.setObservation_time(((Node) observation_timeNm.item(0)).getNodeValue());

				NodeList temp_CNmElmntLst = fstElmnt.getElementsByTagName("temp_C");
				Element temp_CNmElmnt = (Element) temp_CNmElmntLst.item(0);
				NodeList temp_CNm = temp_CNmElmnt.getChildNodes();
				current.setTemp_C(((Node) temp_CNm.item(0)).getNodeValue());

				NodeList temp_FNmElmntLst = fstElmnt.getElementsByTagName("temp_F");
				Element temp_FNmElmnt = (Element) temp_FNmElmntLst.item(0);
				NodeList temp_FNm = temp_FNmElmnt.getChildNodes();
				current.setTemp_F(((Node) temp_FNm.item(0)).getNodeValue());

				NodeList weatherCodeNmElmntLst = fstElmnt.getElementsByTagName("weatherCode");
				Element weatherCodeNmElmnt = (Element) weatherCodeNmElmntLst.item(0);
				NodeList weatherCodeNm = weatherCodeNmElmnt.getChildNodes();
				current.setWeatherCode(((Node) weatherCodeNm.item(0)).getNodeValue());

				NodeList weatherIconUrlNmElmntLst = fstElmnt.getElementsByTagName("weatherIconUrl");
				Element weatherIconUrlNmElmnt = (Element) weatherIconUrlNmElmntLst.item(0);
				NodeList weatherIconUrlNm = weatherIconUrlNmElmnt.getChildNodes();
				current.setWeatherIconUrl(((Node) weatherIconUrlNm.item(0)).getNodeValue());

				NodeList weatherDescNmElmntLst = fstElmnt.getElementsByTagName("weatherDesc");
				Element weatherDescNmElmnt = (Element) weatherDescNmElmntLst.item(0);
				NodeList weatherDescNm = weatherDescNmElmnt.getChildNodes();
				current.setWeatherDesc(((Node) weatherDescNm.item(0)).getNodeValue());

				NodeList windspeedMilesNmElmntLst = fstElmnt.getElementsByTagName("windspeedMiles");
				Element windspeedMilesNmElmnt = (Element) windspeedMilesNmElmntLst.item(0);
				NodeList windspeedMilesNm = windspeedMilesNmElmnt.getChildNodes();
				current.setWindspeedMiles(((Node) windspeedMilesNm.item(0)).getNodeValue());

				NodeList windspeedKmphNmElmntLst = fstElmnt.getElementsByTagName("windspeedKmph");
				Element windspeedKmphNmElmnt = (Element) windspeedKmphNmElmntLst.item(0);
				NodeList windspeedKmphNm = windspeedKmphNmElmnt.getChildNodes();
				current.setWindspeedKmph(((Node) windspeedKmphNm.item(0)).getNodeValue());

				NodeList winddirDegreeNmElmntLst = fstElmnt.getElementsByTagName("winddirDegree");
				Element winddirDegreeNmElmnt = (Element) winddirDegreeNmElmntLst.item(0);
				NodeList winddirDegreeNm = winddirDegreeNmElmnt.getChildNodes();
				current.setWinddirDegree(((Node) winddirDegreeNm.item(0)).getNodeValue());

				NodeList winddir16PointNmElmntLst = fstElmnt.getElementsByTagName("winddir16Point");
				Element winddir16PointNmElmnt = (Element) winddir16PointNmElmntLst.item(0);
				NodeList winddir16PointNm = winddir16PointNmElmnt.getChildNodes();
				current.setWinddir16Point(((Node) winddir16PointNm.item(0)).getNodeValue());

				NodeList precipMMNmElmntLst = fstElmnt.getElementsByTagName("precipMM");
				Element precipMMNmElmnt = (Element) precipMMNmElmntLst.item(0);
				NodeList precipMMNm = precipMMNmElmnt.getChildNodes();
				current.setPrecipMM(((Node) precipMMNm.item(0)).getNodeValue());

				NodeList humidityNmElmntLst = fstElmnt.getElementsByTagName("humidity");
				Element humidityNmElmnt = (Element) humidityNmElmntLst.item(0);
				NodeList humidityNm = humidityNmElmnt.getChildNodes();
				current.setHumidity(((Node) humidityNm.item(0)).getNodeValue());

				NodeList visibilityNmElmntLst = fstElmnt.getElementsByTagName("visibility");
				Element visibilityNmElmnt = (Element) visibilityNmElmntLst.item(0);
				NodeList visibilityNm = visibilityNmElmnt.getChildNodes();
				current.setVisibility(((Node) visibilityNm.item(0)).getNodeValue());

				NodeList pressureNmElmntLst = fstElmnt.getElementsByTagName("pressure");
				Element pressureNmElmnt = (Element) pressureNmElmntLst.item(0);
				NodeList pressureNm = pressureNmElmnt.getChildNodes();
				current.setPressure(((Node) pressureNm.item(0)).getNodeValue());

				NodeList cloudcoverNmElmntLst = fstElmnt.getElementsByTagName("cloudcover");
				Element cloudcoverNmElmnt = (Element) cloudcoverNmElmntLst.item(0);
				NodeList cloudcoverNm = cloudcoverNmElmnt.getChildNodes();
				current.setCloudcover(((Node) cloudcoverNm.item(0)).getNodeValue());

			}
		}
		return current;


	}

	public ArrayList <Location> getCity(String inCity) throws Exception {

		ArrayList< Location> citys = new ArrayList<Location>();

		try{

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			
			Document doc = db.parse("http://www.worldweatheronline.com/feed/search.ashx?key=4aa3418d0a204342110904&query="+inCity+"&num_of_results=3&format=xml");
			doc.getDocumentElement().normalize();

//			System.out.println("Root element " + doc.getDocumentElement().getNodeName());
			NodeList nodeLst = doc.getElementsByTagName("result");
			//System.out.println("Information of all employees");
			System.out.println("selected city is "+ inCity+ " result length is "+ nodeLst.getLength());
			for (int s = 0; s < nodeLst.getLength(); s++) {
				Location city = new Location();
				Node fstNode = nodeLst.item(s);
				
				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

					Element fstElmnt = (Element) fstNode;

					NodeList areaNameNmElmntLst = fstElmnt.getElementsByTagName("areaName");
					Element areaNameNmElmnt = (Element) areaNameNmElmntLst.item(0);
					NodeList areaNameNm = areaNameNmElmnt.getChildNodes();
					System.out.println(((Node) areaNameNm.item(0)).getNodeValue());
					city.setAreaName(((Node) areaNameNm.item(0)).getNodeValue());

					NodeList countryNmElmntLst = fstElmnt.getElementsByTagName("country");
					Element countryNmElmnt = (Element) countryNmElmntLst.item(0);
					NodeList countryNm = countryNmElmnt.getChildNodes();
					city.setCountry(((Node) countryNm.item(0)).getNodeValue());

					NodeList latitudeNmElmntLst = fstElmnt.getElementsByTagName("latitude");
					Element latitudeNmElmnt = (Element) latitudeNmElmntLst.item(0);
					NodeList latitudeNm = latitudeNmElmnt.getChildNodes();
					city.setLatitude(((Node) latitudeNm.item(0)).getNodeValue());

					NodeList longitudeNmElmntLst = fstElmnt.getElementsByTagName("longitude");
					Element longitudeNmElmnt = (Element) longitudeNmElmntLst.item(0);
					NodeList longitudeNm = longitudeNmElmnt.getChildNodes();
					city.setLongitude(((Node) longitudeNm.item(0)).getNodeValue());

					NodeList populationNmElmntLst = fstElmnt.getElementsByTagName("population");
					Element populationNmElmnt = (Element) populationNmElmntLst.item(0);
					NodeList populationNm = populationNmElmnt.getChildNodes();
					
					city.setPopulation(((Node) populationNm.item(0)).getNodeValue());
				}
				citys.add(city);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return citys;
	}
}

Consumer bundle

1. Activator.java gets a refrence to the service provided by the provider bundle. Then it passes this object to the gui view. The gui will then display the information. In this context, the view can a gui, a console or even a portable device.

package ecl.team2.project.weathersystem.client;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import ecl.team2.project.weather.WeatherInterface;

public class Activator implements BundleActivator {

	private static BundleContext context;
	private WeatherInterface sws;

	static BundleContext getContext() {
		return context;
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext bundleContext) throws Exception {
		Activator.context = bundleContext;
		
		ServiceReference reference = context.getServiceReference(WeatherInterface.class.getName());
		if(reference != null){
			sws = (WeatherInterface) context.getService(reference);
			
			if(sws != null) {
				WeatherDialog.runDialog(sws);
				context.ungetService(reference);
			} else
				System.err.println("The Weather Station is down !!!");
		} else
			System.err.println("The Weather Station can not be found !!!");
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		Activator.context = null;
		System.err.println("Client Not Interested !");
	}
}

2. WeatherDialog.java Use a gui builder to create the GUI.

WeatherDialog.java


3. DialogSetLocation.java Use a gui builder to create the Dialog box to enter a city name. DialogSetLocation.java


Running the application

1. Setup a a new run configuration. Create a new osgi run configuration. Set up the start level as show in figure below. Press Add required bundles and Run the program.