MAIN MENU           BIODATA              ASSIGNMENTS                 TUTORIALS                EMAILS

DISTRIBUTED SYSTEMS (TCT 2034)

ASSIGNMENT 5

( CORBA )

QUESTION:

Insert 3 things in the interface that is Stock Code, Stock Name and Price.

 

Answer 1:

1. Create IDL file

 

//Stock.idl

module Stock { 

       readonly attribute string code;

       attribute string name;

       attribute double price;

   }; 

}; 

------------------------------------------------------------------------------------------------------------

2. Implement the interfaces

 

class StockItemServant extends _StockItemImplBase

{

            //Declare the initialize instance variable…

            private String code = “”;

            private String name = “ ”;

            private double price = “”;

 

            //Constructor…

            public StockItemServer(String newcode, Sring newname, double newprice)

            {

                        code = newCode;

                        name = newName;

                        price = newPrice;

            }

 

            //Must supply the following ‘get’ and ‘set’ methods…

            //Accessor method(‘get’ method) for stock code…

            public String code();

            {

                        return code;

            }

 

            //Accessor method(‘get’ method) for stock name…

            public String name();

            {

                        return name;

            }

           

            //Accessor method(‘get’ method) for stock price…

            public double price();

            {

                        return price;

            }

 

}

 

class StockItemFactoryServant

                                    extends _StockItemFactoryImplBase

{

            //Method to create a StockItemServant object and return a

            //reference to this object (allowing clients to create

            //StockItem objects on the servant)…

            public StockItem CreateItemServant(newcode, newName, newPrice)

            {

                        return (new StockItemServant(newCode, newName, newPrice));

            }

}

-----------------------------------------------------------------------------------------------------------

3. Create the server

 

import Stock.*;

 

import org.omg.CosNaming.*;

import org.omg.CosNaming.NamingContextPackage.*;

import org.omg.CORBA.*;

 

public class StockItemServer

{

     public static void main(String[] args)

     {

            try

            {

                 //Create and initialize the ORB…

                 ORB orb = ORB.init(args, null);

 

                 //Create a StockItemServant object…

                StockItemServant stockServant =

                                    new  StockItemServant(“S0001”, “The Unforgiven”, “15.90” );

 

                 //Register the object with the ORB…

                orb.connect(StockServant);

 

                 //Create a StockItemFactoryServant object…

                 StockItemFactoryServant factoryServant =

                                                new StockItemFactoryServant();

 

                 //Register the object with the ORB…

                 orb.connect(factoryServant);

 

                 //Get a reference to the root naming context…

                 org.omg.CORBA.Object objectRef =

                 orb.resolve_initial_references(“NameService”);

 

                 //’Narrow’ (‘downcast’) the context reference…

                 NamingContext namingContext =

                                    NamingContextHelper.narrow(objectRef);

 

                 //Create a NameComponent object for the StockItem interface

                NameComponent nameComp =

                                                new NameComponent(“Stock”,  “”);

 

                //Specify the path to the interface…

                NameComponent[] stockPath = {nameComp};

 

                //Bind the servant to the interface path…

                namingContext.rebind(stockPath, stockServant);

 

                //Create a NameComponent object for the StockFactory interface…

                NameComponent factoryNameComp =

                                                new NameComponent(“StockFactory”, “”);

 

                //Specify the path to the interface…

                NameComponent[] factoryPath = {factoryNameComp};

           

                 //Bind the servant to the interface path…

                 namingContext.rebind(factoryPath, factoryServant);

 

                 System.out.print(“\n Server running…”);

           

                 java.lang.Object syncObj = new java.lang.Object();

                 synchronized(syncObj)

                {

                 syncObj.wait();

                }

         }

         catch(Exception e)

        {

                 System.out.println(“*** Server error! ***”);

                 e.printStackTrace();

        }

    }

}

 

class StockItemServant extends _StockItemImplBase

{

            //Declare the initialize instance variable…

            private String code = “”;

            private String name = “ ”;

            private double price = “”;

 

            //Constructor…

            public StockItemServer(String newcode, Sring newname, double newprice)

            {

                        code = newCode;

                        name = newName;

                        price = newPrice;

            }

 

            //Must supply the following ‘get’ and ‘set’ methods…

            //Accessor method(‘get’ method) for stock code…

            public String code();

            {

                        return code;

            }

 

            //Accessor method(‘get’ method) for stock name…

            public String name();

            {

                        return name;

            }

           

            //Accessor method(‘get’ method) for stock price…

            public double price();

            {

                        return price;

            }

 

}

 

class StockItemFactoryServant

                                    extends _StockItemFactoryImplBase

{

            //Method to create a StockItemServant object and return a

            //reference to this object (allowing clients to create

            //StockItem objects on the servant)…

            public StockItem CreateItemServant(newcode, newName, newPrice)

            {

                        return (new StockItemServant(newCode, newName, newPrice));

            }

}

------------------------------------------------------------------------------------------------------------

4. Create a client

 

import Stock.*;

import org.omg.CORBA.*;

import org.omg.CosNaming.*;

 

public class StockItemClient extends HttpServlet

{

     public static void main(String[] args)

     {

         try

        {

            //Create and initialize the ORB…

            ORB orb = ORB.init(args, null);

 

            //Get a reference to the root naming context…

            org.omg.CORBA.Object objectRef =

                        orb.resolve_initial_references(“NameService”);

 

            //’Downcast’ the context reference…

            NamingContext namingContext =

                                    NamingContextHelper.narrow(objectRef);

 

            //Create a NameComponent object for the StockItem interface

            nameComponent nameComp =

                                                new NameComponent(“Stock”, “”);

 

            //Specify the path to the interface…

            NameComponent[] stockPath = {nameComp};

 

            //Get a reference to the interface (reusing existing reference)…

            objectRef = namingContext.resolve(stockPath);

 

            //’Downcast’ the reference…

            Stockitem stockRef1 =

                                    StockItemHelper.narrow(objectRef);

 

            //now use this reference to call methods of the StockItem object…

            System.out.println(

                                    “\nStock code: “+ stockRef1.code());

            System.out.println(

                                    “\nStock name: “+ stockRef1.name());

           

            System.out.println(

                                    “\nStock price: RM “+ stockRef1.price());

 

            //Create a NameComponent object for the Stockfactory interface…

            NameComponent factoryNameComp =

                                    new NameComponent(“StockFactory”, “ “);

 

            //Specify the path to the interface…

            NameComponent[] factoryPath = {factoryNameComp};

 

            //Get a reference to the interface(reusing existing reference)…

            objectRef = namingContext.resolve(factoryPath);

 

            //’Downcast’ the reference…

            StockItemFactory stockFactoryRef =

                                    StockItemFactoryHelper.narrow(objectRef);

 

            //Use factory reference to create a StockItem object

            //on the server and return a reference to this

            //StockItem (using method CreateItem  within the

            //StockItemFactory interface)…

            StockItem  stockRef2 =

                        stockFactoryref.createitem(“S0002”, “Biography of Dr.M”, “45.95”);

 

            //Now use this reference to call methods of the new StockItem object…

            System.out.println(

                                    “\nStock code: “ + stockRef2.code());

            System.out.println(

                                    “\nStock name: “ + stockRef2.name());

            System.out.println(

                                    “\nStock price: “ + stockRef2.price());

         }

    }

}

 

-----------------------------------------------------------------------------------------------------------

request.html <html>
<head>
<title>Servlet <->CORBA </title>
</head>
<body bgcolor="#ffffcc" text="#006666">
<center>
<h3>Servlet <==> CORBA Communication</h3> </center>
<form action="/developer/technicalArticles/Servlets/corba/..\servlet\MathServlet" method="POST">
<center>
Stock Code: <input type=text size=10 name="code">
<br>
Stock Name: <input type=text size=10 name="name">
<br>

Stock Price: RM <input type=text size=10 name="price">
</center>
</form>
</body>

-----------------------------------------------------------------------------------

Answer 2:

1. Create IDL file

 

//Stock.idl

module Stock { 

       readonly attribute string code;

       attribute string name;

       attribute double price;

   }; 

}; 

------------------------------------------------------------------------------------------------------------

2. Implement the interfaces

 

class StockItemServant extends _StockItemImplBase

{

            //Declare the initialize instance variable…

            private String code = “”;

            private String name = “ ”;

            private double price = “”;

 

            //Constructor…

            public StockItemServer(String newcode, Sring newname, double newprice)

            {

                        code = newCode;

                        name = newName;

                        price = newPrice;

            }

 

            //Must supply the following ‘get’ and ‘set’ methods…

            //Accessor method(‘get’ method) for stock code…

            public String code();

            {

                        return code;

            }

 

            //Accessor method(‘get’ method) for stock name…

            public String name();

            {

                        return name;

            }

           

            //Accessor method(‘get’ method) for stock price…

            public double price();

            {

                        return price;

            }

 

}

 

class StockItemFactoryServant

                                    extends _StockItemFactoryImplBase

{

            //Method to create a StockItemServant object and return a

            //reference to this object (allowing clients to create

            //StockItem objects on the servant)…

            public StockItem CreateItemServant(newcode, newName, newPrice)

            {

                        return (new StockItemServant(newCode, newName, newPrice));

            }

}

-----------------------------------------------------------------------------------------------------------

3. Create the server

 

import Stock.*;

 

import org.omg.CosNaming.*;

import org.omg.CosNaming.NamingContextPackage.*;

import org.omg.CORBA.*;

 

public class StockItemServer

{

     public static void main(String[] args)

     {

            try

            {

                 //Create and initialize the ORB…

                 ORB orb = ORB.init(args, null);

 

                 //Create a StockItemServant object…

                StockItemServant stockServant =

                                    new  StockItemServant(“S0001”, “The Unforgiven”, “15.90” );

 

                 //Register the object with the ORB…

                orb.connect(StockServant);

 

                 //Create a StockItemFactoryServant object…

                 StockItemFactoryServant factoryServant =

                                                new StockItemFactoryServant();

 

                 //Register the object with the ORB…

                 orb.connect(factoryServant);

 

                 //Get a reference to the root naming context…

                 org.omg.CORBA.Object objectRef =

                 orb.resolve_initial_references(“NameService”);

 

                 //’Narrow’ (‘downcast’) the context reference…

                 NamingContext namingContext =

                                    NamingContextHelper.narrow(objectRef);

 

                 //Create a NameComponent object for the StockItem interface

                NameComponent nameComp =

                                                new NameComponent(“Stock”,  “”);

 

                //Specify the path to the interface…

                NameComponent[] stockPath = {nameComp};

 

                //Bind the servant to the interface path…

                namingContext.rebind(stockPath, stockServant);

 

                //Create a NameComponent object for the StockFactory interface…

                NameComponent factoryNameComp =

                                                new NameComponent(“StockFactory”, “”);

 

                //Specify the path to the interface…

                NameComponent[] factoryPath = {factoryNameComp};

           

                 //Bind the servant to the interface path…

                 namingContext.rebind(factoryPath, factoryServant);

 

                 System.out.print(“\n Server running…”);

           

                 java.lang.Object syncObj = new java.lang.Object();

                 synchronized(syncObj)

                {

                 syncObj.wait();

                }

         }

         catch(Exception e)

        {

                 System.out.println(“*** Server error! ***”);

                 e.printStackTrace();

        }

    }

}

 

class StockItemServant extends _StockItemImplBase

{

            //Declare the initialize instance variable…

            private String code = “”;

            private String name = “ ”;

            private double price = “”;

 

            //Constructor…

            public StockItemServer(String newcode, Sring newname, double newprice)

            {

                        code = newCode;

                        name = newName;

                        price = newPrice;

            }

 

            //Must supply the following ‘get’ and ‘set’ methods…

            //Accessor method(‘get’ method) for stock code…

            public String code();

            {

                        return code;

            }

 

            //Accessor method(‘get’ method) for stock name…

            public String name();

            {

                        return name;

            }

           

            //Accessor method(‘get’ method) for stock price…

            public double price();

            {

                        return price;

            }

 

}

 

class StockItemFactoryServant

                                    extends _StockItemFactoryImplBase

{

            //Method to create a StockItemServant object and return a

            //reference to this object (allowing clients to create

            //StockItem objects on the servant)…

            public StockItem CreateItemServant(newcode, newName, newPrice)

            {

                        return (new StockItemServant(newCode, newName, newPrice));

            }

}

------------------------------------------------------------------------------------------------------------

4. Create a client

 

import Stock.*;

import org.omg.CORBA.*;

import org.omg.CosNaming.*;

 

public class StockItemClient extends HttpServlet

{

     public static void main(String[] args)

     {

         try

        {

            //Create and initialize the ORB…

            ORB orb = ORB.init(args, null);

 

            //Get a reference to the root naming context…

            org.omg.CORBA.Object objectRef =

                        orb.resolve_initial_references(“NameService”);

 

            //’Downcast’ the context reference…

            NamingContext namingContext =

                                    NamingContextHelper.narrow(objectRef);

 

            //Create a NameComponent object for the StockItem interface

            nameComponent nameComp =

                                                new NameComponent(“Stock”, “”);

 

            //Specify the path to the interface…

            NameComponent[] stockPath = {nameComp};

 

            //Get a reference to the interface (reusing existing reference)…

            objectRef = namingContext.resolve(stockPath);

 

            //’Downcast’ the reference…

            Stockitem stockRef1 =

                                    StockItemHelper.narrow(objectRef);

 

            //now use this reference to call methods of the StockItem object…

            System.out.println(

                                    “\nStock code: “+ stockRef1.code());

            System.out.println(

                                    “\nStock name: “+ stockRef1.name());

           

            System.out.println(

                                    “\nStock price: RM “+ stockRef1.price());

 

            //Create a NameComponent object for the Stockfactory interface…

            NameComponent factoryNameComp =

                                    new NameComponent(“StockFactory”, “ “);

 

            //Specify the path to the interface…

            NameComponent[] factoryPath = {factoryNameComp};

 

            //Get a reference to the interface(reusing existing reference)…

            objectRef = namingContext.resolve(factoryPath);

 

            //’Downcast’ the reference…

            StockItemFactory stockFactoryRef =

                                    StockItemFactoryHelper.narrow(objectRef);

 

            //Use factory reference to create a StockItem object

            //on the server and return a reference to this

            //StockItem (using method CreateItem  within the

            //StockItemFactory interface)…

            StockItem  stockRef2 =

                        stockFactoryref.createitem(“S0002”, “Biography of Dr.M”, “45.95”);

 

            //Now use this reference to call methods of the new StockItem object…

            System.out.println(

                                    “\nStock code: “ + stockRef2.code());

            System.out.println(

                                    “\nStock name: “ + stockRef2.name());

            System.out.println(

                                    “\nStock price: “ + stockRef2.price());

         }

    }

}

 -----------------------------------------------------------------------------------

OUTPUT