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

From CDOT Wiki
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 405: Line 405:
 
*Save and close both files and now open the manifest file
 
*Save and close both files and now open the manifest file
 
*Click on the dependencies tab then click on add in the "import packages" panel.
 
*Click on the dependencies tab then click on add in the "import packages" panel.
*In the search field, enter "ecl", then click on "ecl.team2.lab2.weather", and afterwords click okay
+
*In the search field, enter "ecl", then click on "ecl.team2.lab2.weather"
 +
*Afterwords ensure you only have "metaInf" on the binary build panel checked and ensure that "out..= bin/" is included in your build.properties tab
 +
[[File:MetafestcT2.png]]
 +
[[File:Metafestc2T2.png]]
 
==Run Configuration Setting==
 
==Run Configuration Setting==
 
*After completing all previous steps, on the menu bar click on run, then click "run configuration.."
 
*After completing all previous steps, on the menu bar click on run, then click "run configuration.."
Line 420: Line 423:
 
*Most of the values are randomly generated and shows an excellent example of OSGI
 
*Most of the values are randomly generated and shows an excellent example of OSGI
 
*This concludes the tutorial
 
*This concludes the tutorial
 
 
 
==INCOMPLETE CURRENT CODE DOESN'T WORK, MUST RESOLVE==
 
 
 
==End==
 
==End==

Latest revision as of 17:51, 18 February 2011

Tutorial

Prerequisites

Creating and Setting the Interface Bundle

  • Run Eclipse
  • Select/Enter your preferred workspace in the text box provided and click okay

Workspace2T2.png

  • After Eclipse has completed loaded, create a plug-in by clicking on "File" on the menu bar then scroll to New->Project->Plug-in
  • Enter the following information that is seen in the image below

PluginT2.png

  • Click Next->Finish
  • Now open the src folder, open the package inside, remove the activator file currently residing in it, and create a class called "Weather" and an interface called "WeatherSystem"
  • Copy and paste the following code to their respective file

Weather

package ecl.team2.lab2.weather;

public class Weather {
    private String city;
    private float tempInCelcius;
    private float rainInMM;
    private float snowInMM;
    private float windspeedInKM;
    private char windDirection;
   
    public Weather(String pcity, float ptemp, float prain, float psnow, float pwspeed, char pwdirection){
        this.setCity(pcity);
        this.setTempInCelcius(ptemp);
        this.setRainInMM(prain);
        this.setSnowInMM(psnow);
        this.setWindspeedInKM(pwspeed);
        this.setWindDirection(pwdirection);
    }
   
   
    public void setWindDirection(char windDirection) {
        this.windDirection = windDirection;
    }

    public char getWindDirection() {
        return windDirection;
    }



    public void setWindspeedInKM(float windspeedInKM) {
        this.windspeedInKM = windspeedInKM;
    }



    public float getWindspeedInKM() {
        return windspeedInKM;
    }



    public void setSnowInMM(float snowInMM) {
        this.snowInMM = snowInMM;
    }



    public float getSnowInMM() {
        return snowInMM;
    }



    public void setRainInMM(float rainInMM) {
        this.rainInMM = rainInMM;
    }



    public float getRainInMM() {
        return rainInMM;
    }



    public void setTempInCelcius(float tempInCelcius) {
        this.tempInCelcius = tempInCelcius;
    }



    public float getTempInCelcius() {
        return tempInCelcius;
    }



    public void setCity(String city) {
        this.city = city;
    }



    public String getCity() {
        return city;
    }
}

WeatherSystem

package ecl.team2.lab2.weather;

public interface WeatherSystem {
    Weather getWeather(String city) throws Exception;
}
  • Save and close both files and now open the manifest file

ManiT2.png

  • Click on the runtime tab then click on add in the "export packages" panel.
  • click on the ecl.... package and click okay

Mani2T2.png

  • Now close the manifest and save all changes

Creating and Setting the Provide Bundle

  • Follow the same steps for creating a new plug-in projects but instead supply the name "ecl.team2.lab2.weathersystem.provider"
  • Remove all contents within the src->current package and add a class file called "ProviderActivator"
  • Add a new package to the current project we're dealing with and name it "ecl.team2.lab2.weathersystem.provider.internal" and within it create a class called "SimpleWeatherSystem"
  • Copy and paste the following code to their respective file

ProviderActivator

package ecl.team2.lab2.weathersystem.provider;

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

import ecl.team2.lab2.weather.WeatherSystem;
import ecl.team2.lab2.weathersystem.provider.internal.SimpleWeatherSystem;

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;
       
        SimpleWeatherSystem sws = new SimpleWeatherSystem();
        context.registerService(WeatherSystem.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 !");
    }

}

SimpleWeatherSystem

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

import ecl.team2.lab2.weather.WeatherSystem;
import ecl.team2.lab2.weather.Weather;
import java.util.ArrayList;
import java.util.Random;


public class SimpleWeatherSystem implements WeatherSystem {
    ArrayList<Weather> weathers = new ArrayList<Weather>();
    public SimpleWeatherSystem(){
        ArrayList<String> cities = new ArrayList<String>();
        cities.add("Toronto");
        cities.add("New York");
        cities.add("Calgary");
        cities.add("Ottawa");
        cities.add("Vancouver");
        cities.add("Regina");
        cities.add("Winnipeg");
        cities.add("St. John");
        cities.add("Fredericton");
        cities.add("Halifax");
        cities.add("Quebec City");
        cities.add("Whitehorse");
        cities.add("Yellowknife");
        for(String c:cities){
            Random randomGenerator = new Random();
            float temp = randomGenerator.nextFloat()+35;
            float rain = randomGenerator.nextFloat()+10;
            float snow = randomGenerator.nextFloat()+10;
            float windspeed = randomGenerator.nextFloat()+50;
            char winddirection = '.';
            int r = randomGenerator.nextInt(4);
            switch(r){
            case 0:
                winddirection = 'E';
                break;
            case 1:
                winddirection = 'W';
                break;
            case 2:
                winddirection = 'N';
                break;
            case 3:
                winddirection = 'S';
                break;
                   
            }
           
            Weather w =  new Weather(c, temp, rain, snow, windspeed, winddirection);
            this.weathers.add(w);
            System.out.println(w.toString());
        }   
    }
    public Weather getWeather(String city) throws Exception {
        // TODO Auto-generated method stub
        Weather found = null;
        for(Weather w:this.weathers){
            city = city.toLowerCase();
            String wcity =w.getCity().toLowerCase();
            if(city.equals(wcity)){
                found = w;
                return found;
            }
        }
        return found;
    }

}
  • Save and close both files and now open the manifest file
  • Click on the dependencies tab then click on add in the "import packages" panel.
  • In the search field, enter "ecl", then click on "ecl.team2.lab2.weather", and afterwords click okay

Manifest3T2.png

Creating and Implementing the Client Bundle

  • Follow the same steps for creating a new plug-in projects but instead supply the name "ecl.team2.lab2.weathersystem.client"
  • Remove all contents within the src->current package and add a class file called "Activator" and another class file called "
  • Copy and paste the following code to their respective file

Activator

package ecl.team2.lab2.weathersystem.client;

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

import ecl.team2.lab2.weather.WeatherSystem;

public class Activator implements BundleActivator {

    private static BundleContext context;
    private WeatherSystem 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(WeatherSystem.class.getName());
        if(reference != null){
            sws = (WeatherSystem) 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 !");
    }
}

WeatherDialog

package ecl.team2.lab2.weathersystem.client;
import javax.swing.*;

import ecl.team2.lab2.weather.WeatherSystem;

import java.awt.*;
import java.awt.event.*;


public class WeatherDialog extends JFrame{
    private static final long serialVersionUID = 1L;
    private  JPanel panel;
    private  JButton btnGo;
    private  JComboBox combo;
    private  JLabel labelTitle;
    private  JLabel labelTemperature;
    private  JLabel labelRain;
    private  JLabel labelSnow;
    private  JLabel labelWindDirection;
    private  JLabel labelWindSpeed;
   
    private WeatherSystem sws;
    String[] cities = {"Toronto","New York","Calgary","Ottawa","Vancouver",
                        "Regina","Winnipeg","St. John","Fredericton",
                        "Halifax","Quebec City", "Whitehorse","Yellowknife"
                        };
   

    public WeatherDialog(){
        panel = new JPanel();
        panel.setPreferredSize(new Dimension(200,200));
        panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        combo = new JComboBox();
        for(int i=0;i<cities.length;i++){
            combo.addItem(cities[i]);
        }
        panel.add(combo);
       
       
        btnGo = new JButton("Go");
        panel.add(btnGo);
       
        btnGo.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                String chosencity = (String)combo.getSelectedItem();
                ShowWeather(chosencity);
            }
        }
        );
        this.labelTitle = new JLabel();
        this.labelTemperature = new JLabel();
        this.labelRain = new JLabel();
        this.labelSnow = new JLabel();
        this.labelWindDirection = new JLabel();
        this.labelWindSpeed = new JLabel();
       
        panel.add(labelTitle);panel.add(labelTemperature);panel.add(labelRain);
        panel.add(labelSnow);panel.add(labelWindDirection);panel.add(labelWindSpeed);
        this.setTitle("Todays Weather");
       
        // Display the window.
        this.setContentPane(panel);
        this.pack();
       
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
    }
    public void ShowWeather(String city){
        try {       
            float temp = sws.getWeather(city).getTempInCelcius();
            float rain =sws.getWeather(city).getRainInMM();
            float snow = sws.getWeather(city).getSnowInMM();
            char direction = sws.getWeather(city).getWindDirection();
            float speed = sws.getWeather(city).getWindspeedInKM();
           
           
            this.labelTitle.setText("Todays weather in "+ city);
            this.labelTemperature.setText("Temperature: "+ String.format("%.2f", temp) + " Celcius");
            this.labelRain.setText("Rain: " + String.format("%.2f", rain) + " mm");
            this.labelSnow.setText("Snow: " + String.format("%.2f", snow)  + " mm");
            this.labelWindDirection.setText("Wind Direction: " + direction);
            this.labelWindSpeed.setText("Speed: " + String.format("%.2f", speed) + " km/hr");
        }
        catch (Exception ex){
            System.err.println(ex.getMessage());
        }
    }
   
    private static void createAndShowGUI(WeatherSystem ws) {
        WeatherDialog frame = new WeatherDialog();
        frame.sws = ws;
    }
 
    public static void runDialog(final WeatherSystem ws) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(ws);
            }
        });
    }
}
  • Save and close both files and now open the manifest file
  • Click on the dependencies tab then click on add in the "import packages" panel.
  • In the search field, enter "ecl", then click on "ecl.team2.lab2.weather"
  • Afterwords ensure you only have "metaInf" on the binary build panel checked and ensure that "out..= bin/" is included in your build.properties tab

MetafestcT2.png Metafestc2T2.png

Run Configuration Setting

  • After completing all previous steps, on the menu bar click on run, then click "run configuration.."
  • A window should have popped up, on the left panel, double click on "OSGI Framework"
  • The right panel should have altered, click on deselect all button to deactivate all bundles.
  • click on the check box in the table dubbed "workspace", this is essentially what we have been working on throughout the tutorial.
  • After click on the button "add required bundles" and click on the check box labeled "only show selected"
  • This reveals all the bundles that are required for our small program to work. Now under "start level" column, place the items in the following order, click apply and then run the code by clicking the run button

RunConfT2.png

  • Once executed, a java window should have popped up

Gui1T2.png

  • Click Go and you should see something similar to this

Gui2T2.png

  • Most of the values are randomly generated and shows an excellent example of OSGI
  • This concludes the tutorial

End