B.E. 4/4 - I Semester: Bit 432 Middleware Technlogies Laboratory

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 46

B.E.

4/4 – I semester
BIT 432 MIDDLEWARE TECHNLOGIES LABORATORY
List of Experiments Prescribed by Osmania University

1. Creation of a Distributed Name Server, using RMI


2. Creation of a Java Bean application for displaying graphical shapes
3. Developing an EJB for Student Information System.
4. Developing an EJB for Library Information System.
5. Creation of an ActiveX control for Time Table.
6. Developing a component for converting currency values using COM/.NET.
7. Developing a component for browsing the CD catalog using COM/.NET
8. Developing a component for retrieving information from message box using
DCOM/.NET.
9. Developing a Middleware component for retrieving Stock Market Exchange
information using CORBA
10. Developing a Middleware component for retrieving Bank Balance using CORBA.

1
MIDDLEWARE TECHNOLOOGIES LAB
CONTENTS
S No Name Of the Experiment Page No
1 Creation of a Distributed Name Server, Using RMI. 4
2 Creation of a Java Bean application for displaying graphical shapes 10
3 Developing an EJB for Student Information System. 15
4 Developing an EJB for Library Information System. 20
5 Creation of an ActiveX control for Time Table. 25
6 Developing a component for converting currency values using COM/.NET. 28
7 Developing a component for browsing the CD catalog using COM/.NET. 30
Developing a component for retrieving information from message box using
8 33
DCOM/.NET.
Developing a Middleware component for retrieving Stock Market Exchange
9 35
information using CORBA
Developing a Middleware component for retrieving Bank Balance using
10 40
CORBA.

1. Creation of a Distributed Name Server, Using RMI.


AIM: Creation of a Distributed Name Server, Using RMI
Apparatus: J2SDK 1.5 Software

2
rmic tool
java compiler, java tool, notepad/Edit plus
Flow:
Remote Method Invocation (RMI)
 In Java distributed object model, a remote object is one whose methods can be
invoked from another Java Virtual Machine.
 Allows object-to-object communication between Java Virtual Machines.
 RMI is the action of invoking a method of a remote interface on a remote object.
 A remote object is always accessed via its remote interface.
 In RMI the terms ‘server’ and ‘client’ refer to objects, but not physical machines.
Client invoking method on Remote Object
the remote object

Stub Skeleton

Remote Reference Layer Remote Reference Layer

TCP TCP

IP IP

Physical Layer
Hardware Interfaces Hardware Interfaces

 The stubs/skeletons layer intercepts method calls made by the client to the interface
reference and redirects these calls to a remote object. Stubs are specific to the
client side, whereas, skeletons are found on the server side.

3
 The remote reference layer handles the details relating to interpreting and managing
references made by clients on the remote objects. It connects clients to remote
objects that are running and exported to a server by a one-to-one connection link.
 The transport layer is based on TCP/IP connections between machines in a
network.
Procedure:
The basic steps involved in writing client-server applications using RMI :
1. Defining a remote interface
2. Implementing the remote interface
3. Writing the client that uses the remote objects
4. Connecting stubs and skeletons
5. Starting the registry and registering the object
6. Running the server and the client.
Defining the Remote interface:
o The remote interface must be declared public
o The remote interface must extend java.rmi.Remote interface
o Each method must throw a java.rmi.RemoteException
o If the remote methods have any remote objects as parameters or return
types, they must be interface types not the implementation classes.
o java.rmi.Remote interface has no methods

Programming:
// Program to create a remote interface
import java.rmi.*;

4
public interface MyRemoteInterface extends java.rmi.Remote
{
public String displayDate() throws RemoteException;
}

Implementing the Remote Interface:


 The implementation class provides the implementation for methods defined in the
remote interface.
 The generic java.rmi.server.RemoteObject is an abstract class and describes the
behavior for remote objects.
 The abstract class java.rmi.server.RemoteServer describes the behavior
associated with the server implementation and provides the basic semantics to
support remote references.
 java.rmi.server.RemoteServer has two subclasses :
 java.rmi.server.UnicastRemoteObject
 java.rmi.activation.Activatable

// Program for implementation of the Rremote Interface


import java.io.*;
import java.util.Date;
import java.rmi.server.*;
import java.rmi.*;
public class MyImplementor extends UnicastRemoteObject implements
MyRemoteInterface
{
public MyImplementor() throws RemoteException
{
super();
}
public String displayDate() throws RemoteException

5
{
System.out.println(“Hello! The date is “ + new Date());
}
}

 A remote class can define any methods, but only methods in th remote interface
can be invoked remotely.

The Client application


// Program to generate the client
import java.rmi.*;
import java.io.*;
public class MyRemoteClient
{
public static void main(String[] args)
{
/*if (System.getSecurityManager() == null)
{
System.setSecurityManager(new SecurityManager());
}*/
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try
{
System.out.println("Enter IP Address: ");
String ipaddr = br.readLine();
/*System.out.println("Enter value2 : ");
int value2 = Integer.parseInt(br.readLine());
System.out.println("The values are : " + value1 + " " + value2);
MyRemoteServer mobj = new MyRemoteServer();

6
Naming.rebind("/MyRemoteServer",mobj);*/
MyRemoteInterface mobj = (MyRemoteInterface)
Naming.lookup("rmi://localhost:1099/RemoteService");
String msg1 = mobj.displayDate();
String msg2 = mobj.displayHostname(ipaddr);
System.out.println(msg1);
System.out.println(msg2);
}
catch (RemoteException re)
{
System.err.println("Client Exception : " + re);
}
catch (IOException ioe)
{
System.err.println("The exception is : " + ioe);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

The Server application


// Program to implement the remote interface
import java.rmi.*;
import java.rmi.server.*;

7
import java.util.*;
import java.rmi.Naming;
public class MyRemoteServer
{
public MyRemoteServer()
{
try
{
MyRemoteInterface c = new MyRemoteImplementer();
Naming.rebind("rmi://localhost:1099/RemoteService", c);
}
catch (Exception e)
{
System.out.println("Trouble: " + e);
}
}

public static void main(String args[])


{
new MyRemoteServer();
}
}
 javac *.java – to compile all the java files
 rmic Myimplementor – to generate stubs and skeletons
 start rmiregistry
 java MyRemoteServer
 java MyRemoteClient
Observations:
 The actual DNS application involves the following modifications to the above
mentioned sample program:

8
1) The services to be provided are to be declared in the remote interface declared by the
user. The services could be: getIPAddress, getDomainName, returnIPAddress,
returnDomainName
2) The above-mentioned services are to be implemented in the implementer class. As
part of implementation the database is to be accessed to retrieve the DNS information,
for which JDBC is to be made use of.
3) The client program would have code to get connected to the server and make the
remote calls to the server for the required services.
4) The server program would create an instance of the remote implementer and would
provide the services requested services to the client.

Steps to be followed for execution:


1) All the programs are to be compiled using the command javac.
2) The stub is to be generated by compiling the implementer class using the
command rmic <implementerclassname>.
3) The RMI registry service is then started using the command :
start rmiregistry.
4) Then the server is registered with the RMI registry by running the server program
using the command java.
5) The client is also then run using the command java.

Result: If the input to the application is an IP Address, then, it should return back the
Domain Name and vice versa.

2. Creation Of A Java Bean Application For Displaying Graphical


Shapes
AIM: Creation of a Java Bean Application for Displaying Graphical Shapes

9
Apparatus: J2SDK 1.5 Software
AWT tool
java compiler, java tool, notepad/Edit plus
Flow:
Java Bean
 A Java Bean is an independent and reusable software component that can be
manipulated visually in a builder tool.
 A bean is represented by an interface that is seen by the users. The environment
must connect to the interface, if it wants to interact with this bean. Beans consist of
three general-purpose interfaces: events, properties and methods.
 Bean events are the mechanism for sending asynchronous messages between beans,
and, between beans and containers.
 The Java Beans architecture communicates primarily using event listener interfaces
that extend EventListener
 The properties of a bean can be changed .at runtime through their get and set
methods.
 The JavaBean introspection process exposes the properties, methods and events of
a bean and also any global information about a bean. This is done through the
BeanInfo interface, provided by the Java Beans API.
Programming:
The following is a sample application that is used to display colors using a bean:
// Program to test beans
import java.beans.*;
public class MyColorsBeanInfo extends SimpleBeanInfo
{
public PropertyDescriptor[] getPropertyDescriptors()
{
try
{
PropertyDescriptor rectangular = new
PropertyDescriptor("rectangular",MyColors.class);

10
PropertyDescriptor pd[] = {rectangular};
return pd;
}
catch (Exception e)
{}
return null;
}
}

// Program to test Java Beans


import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
public class MyColors extends Canvas implements Serializable
{
transient private Color color;
private boolean rectangular;

public MyColors()
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
change();
}
} );
rectangular = false;
setSize(200,100);
change();
}

11
public boolean getRectangular()
{
return rectangular;
}
public void setRectangular(boolean flag)
{
this.rectangular = flag;
repaint();
}

public void change()


{
color = randomColor();
repaint();
}

private Color randomColor()


{
int r = (int) (255 * Math.random() );
int g = (int) (255 * Math.random() );
int b = (int) (255 * Math.random() );
return new Color(r,g,b);
}
public void paint(Graphics g)
{
Dimension d = getSize();
int h = d.height;
int w = d.width;
g.setColor(color);
if (rectangular)
{

12
g.fillRect(0,0,w-1,h-1);
}
else
{
g.fillOval(0,0,w-1,h-1);
}
}
}

// Program to test beans


import java.awt.*;
import java.beans.*;
public class IntrospectorDemo
{
public static void main(String[] args)
{
try
{
Class c = Class.forName("MyColors");
BeanInfo beanInfo = Introspector.getBeanInfo(c);
System.out.println("properties:");
PropertyDescriptor propertyDescriptor[] =
beanInfo.getPropertyDescriptors();
for( int i=0; i < propertyDescriptor.length; i++)
{
System.out.println("\t" + propertyDescriptor[i].getName());
}
System.out.println("events:");
EventSetDescriptor eventsetDescriptor[] =
beanInfo.getEventSetDescriptors();
for(int i =0; i < eventsetDescriptor.length; i++)

13
{
System.out.println("\t" + eventsetDescriptor[i].getName());
}
}
catch (Exception e)
{
System.out.println("Exception caught. " + e);
}
}
}

Procedure:
 For displaying the various graphical shapes, the classes from the AWT package can
be used.
 The code relevant to this can be put as part of an applet and can be executed in a
Java enabled web browser.
 The application can also be implemented using any of the builder tools like the
Eclipse or the NetBeans.
 The expected output from the application to be done is that it should display
various graphical shapes in the browser or in the tool used.

Steps for execution:


1) Compile all the files
2) Execute the introspector from the prompt or run the applet in a Java enabled web
browser.

Observations & Result: The expected output from the application to be done is that it
should display various graphical shapes in the browser or in the tool used.
3. Developing an EJB for Student Information System
AIM: Developing an EJB for Student Information System

14
Apparatus: J2SDK 1.5 Software, Myeclipse workbench Installer
Weblogic application server 8.1, NetBeans IDE
java compiler, java tool, notepad/Edit plus

Flow:
Enterprise Java Beans (EJB)
 EJB components are designed to encapsulate business logic, and to protect the
application developer from having to worry about many system-level issues like
transactions, security, etc.
 An EJB is just a collection of Java classes and XML file, bundled into a single unit.
 An EJB runs in an EJB container. The EJB container runs within an application
server and takes responsibility for system-level issues.
 EJB specification is intended to provide enterprise-level services.
 EJBs provide persistence services.
 There are three types of EJBs. Session Beans – stateful and stateless, message
driven beans and, Entity beans.
 The Session and Entity beans must have two interfaces: Home and Remote
Interface
 .
 The Remote and Home interfaces can be used to access the EJB either remotely or
locally within the same container.

EJB Server

EJB Container
Client EJBHome
Interface EJB

EJBRemote
Interface
Programming:
The following is a sample application using EJB:

15
// Program to declare the Remote interface
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Hello extends EJBObject
{
public String sayHello(String str)throws RemoteException;
}

// Program to declare the Home interface


import java.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
public interface HelloHome extends EJBHome
{
public Hello create() throws RemoteException,CreateException;
}

// Program to implement the bean


import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
public class HelloBean implements SessionBean
{
private SessionContext ctx;
public void ejbActivate()
{
System.out.println("Hello Bean:Activated");
}
public void ejbPassivate()

16
{
System.out.println("Hello Bean:Passivated");
}
public void setSessionContext(SessionContext sc)
{
ctx=sc;
System.out.println("Hello Bean:Session Context");
}
public void ejbRemove()
{
System.out.println("Hello Bean:Removed");
}
public void ejbCreate() throws CreateException
{
System.out.println("Hello Bean:Created");
}
public String sayHello(String str)throws RemoteException
{
System.out.println("Hello Bean:Say");
return "Hello:"+str;
}
}

// Program to develop the client application


import java.rmi.*;
import java.util.*;
import javax.ejb.*;
import javax.rmi.*;
import javax.naming.*;

public class HelloClient

17
{
public static void main(String args[])
{
Properties p=new Properties();

p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactor
y");
p.put(Context.PROVIDER_URL,"t3://localhost:7001");
try
{
InitialContext ctx=new InitialContext(p);
Object obj=ctx.lookup("HelloBean");
Class cls=HelloHome.class;
Object o=PortableRemoteObject.narrow(obj,cls);
HelloHome h=(HelloHome)o;
Hello r=h.create();
String result=r.sayHello(args[0]);
System.out.println(result);
r.remove();
}
catch (Exception e)
{
e.printStackTrace();
}

}
}

Procedure:

18
 For the execution of the above application the following steps are to be carried out:
 Compile all the java programs.
 For generating the component , open the Weblogic Builder tool
 Open the directory where all the .class files are stored. The builder tool
will automatically recognize the bean class and would prompt for creation
of the deployment descriptor.
 Click on save and close. After this the tool will create a folder META-INF
with the files ejb-jar.xml and weblogic-ejb-jar.xml.
 Combine all the .class files and the files in the META-INF folder , using
the tool ‘jar ’to create a file with an extension .jar.
 Copy the .jar file into the application folder of WebLogic
 Run the client program.
Observations:
 For the Student and Library information systems, the services to be provided are to be
declared in the remote interface.
 The services are to be implemented in the bean classes.
 For the implementation part, the data retrieval is to be done by getting connected to
the database using JDBC.
 For the Student Information System, the input would be the student ID and the output
would be the relevant information of the student.

Results: In case of the Library System, the book-ID would be given as input and the
relevant information of the book is to be retrieved as output.

19
4. Developing an EJB for Library Information System.
AIM: Developing an EJB for Student Information System

Apparatus: J2SDK 1.5 Software, Myeclipse workbench Installer


Weblogic application server 8.1, NetBeans IDE
java compiler, java tool, notepad/Edit plus

Flow:
Enterprise Java Beans (EJB)
 EJB components are designed to encapsulate business logic, and to protect the
application developer from having to worry about many system-level issues like
transactions, security, etc.
 An EJB is just a collection of Java classes and XML file, bundled into a single unit.
 An EJB runs in an EJB container. The EJB container runs within an application
server and takes responsibility for system-level issues.
 EJB specification is intended to provide enterprise-level services.
 EJBs provide persistence services.
 There are three types of EJBs. Session Beans – stateful and stateless, message
driven beans and, Entity beans.
 The Session and Entity beans must have two interfaces: Home and Remote Interface
 .
 The Remote and Home interfaces can be used to access the EJB either remotely or
locally within the same container.

EJB Server

EJB Container
Client EJBHome
Interface EJB

EJBRemote
Interface

20
Programming:
The following is a sample application using EJB:
// Program to declare the Remote interface
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Hello extends EJBObject
{
public String sayHello(String str)throws RemoteException;
}

// Program to declare the Home interface


import java.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
public interface HelloHome extends EJBHome
{
public Hello create() throws RemoteException,CreateException;
}

// Program to implement the bean


import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
public class HelloBean implements SessionBean
{
private SessionContext ctx;
public void ejbActivate()

21
{
System.out.println("Hello Bean:Activated");
}
public void ejbPassivate()
{
System.out.println("Hello Bean:Passivated");
}
public void setSessionContext(SessionContext sc)
{
ctx=sc;
System.out.println("Hello Bean:Session Context");
}
public void ejbRemove()
{
System.out.println("Hello Bean:Removed");
}
public void ejbCreate() throws CreateException
{
System.out.println("Hello Bean:Created");
}
public String sayHello(String str)throws RemoteException
{
System.out.println("Hello Bean:Say");
return "Hello:"+str;
}
}

// Program to develop the client application


import java.rmi.*;
import java.util.*;

22
import javax.ejb.*;
import javax.rmi.*;
import javax.naming.*;
public class HelloClient
{
public static void main(String args[])
{
Properties p=new Properties();

p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactor
y");
p.put(Context.PROVIDER_URL,"t3://localhost:7001");
try
{
InitialContext ctx=new InitialContext(p);
Object obj=ctx.lookup("HelloBean");
Class cls=HelloHome.class;
Object o=PortableRemoteObject.narrow(obj,cls);
HelloHome h=(HelloHome)o;
Hello r=h.create();
String result=r.sayHello(args[0]);
System.out.println(result);
r.remove();
}
catch (Exception e)
{
e.printStackTrace();
}

}
}

23
Procedure:
 For the execution of the above application the following steps are to be carried out:
 Compile all the java programs.
 For generating the component , open the Weblogic Builder tool
 Open the directory where all the .class files are stored. The builder tool
will automatically recognize the bean class and would prompt for creation
of the deployment descriptor.
 Click on save and close. After this the tool will create a folder META-INF
with the files ejb-jar.xml and weblogic-ejb-jar.xml.
 Combine all the .class files and the files in the META-INF folder , using
the tool ‘jar ’to create a file with an extension .jar.
 Copy the .jar file into the application folder of WebLogic
 Run the client program.

Observations:
 For the Student and Library information systems, the services to be provided are to be
declared in the remote interface.
 The services are to be implemented in the bean classes.
 For the implementation part, the data retrieval is to be done by getting connected to
the database using JDBC.
 For the Student Information System, the input would be the student ID and the output
would be the relevant information of the student.

Results:
In case of the Library System, the book-ID would be given as input and the relevant
information of the book is to be retrieved as output.

24
5. Creation Of An Active-x Control For Time Table
AIM: Creation of an Active-x Control for Time Table
Apparatus: Microsoft Visual Studio 2005
Programming:

TIME TABLE PROGRAM


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace TimeTable
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.SelectedIndex)
{

25
case 0:
comboBox2.Items.Clear();
comboBox2.Items.Add("1styear");
comboBox2.Items.Add("2ndyear");
comboBox2.Items.Add("3rdyear");
break;
case 1:
comboBox2.Items.Clear();
comboBox2.Items.Add("1styear");
comboBox2.Items.Add("2ndyear");
break;
case 2:
comboBox2.Items.Clear();
comboBox2.Items.Add("1styear");
comboBox2.Items.Add("2ndyear");
comboBox2.Items.Add("3rdyear");
comboBox2.Items.Add("4thyear");
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
OleDbConnection cn = new OleDbConnection("user
id=scott;password=tiger;provider=msdaora.1");
string t = "select * from " + comboBox1.SelectedItem +
comboBox2.SelectedItem;
//MessageBox.Show(t);
OleDbDataAdapter da = new OleDbDataAdapter(t, cn);

26
DataSet ds = new DataSet();
da.Fill(ds, "timetable");
dataGridView1.DataSource = ds.Tables["timetable"];
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}

Result & Observation:

27
6. Developing A Component For Converting Currency Values Using
COM/.NET
AIM: Developing a component for converting currency values using com .net
Apparatus: Microsoft Visual Studio 2005
Programming:

CURRENCY TRANSLATOR PROGRAM


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace currencytranslator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = ((Double.Parse(textBox1.Text))/43.50).ToString();
}
private void button2_Click(object sender, EventArgs e)

28
{
textBox4 .Text = ((Double.Parse(textBox3.Text)) * 43.50).ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}

Result& Observations:

29
7. Developing A Component For Browsing The CD Catalog Using
COM/.NET
AIM: Developing a component for browsing the CD catalog using com .net
Apparatus: Microsoft Visual Studio 2005
Programming:

CD CATALOGUE PROGRAM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CDCatalogue
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

30
{
string s = comboBox1.SelectedItem.ToString();
listBox1.Items.Clear();
label2.Text = s;
try
{
string[] x = Directory.GetDirectories(s);
for (int i = 0; i < x.Length; i++)
{
DirectoryInfo di = new DirectoryInfo(x[i]);
listBox1.Items.Add(di.Name );
}
}
catch (IOException io)
{
MessageBox.Show(io.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
string x = "D:\\";
string[] f = Directory.GetDirectories(x);
for (int i = 0; i < f.Length; i++)
{
comboBox1.Items.Add(f[i]);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox2.Items.Clear();
string t = comboBox1.SelectedItem + "\\" + listBox1.SelectedItem;

31
label3.Text = t;
string[] x = Directory.GetFiles(t);
for (int i = 0; i < x.Length; i++)
{
FileInfo f = new FileInfo(x[i]);
listBox2.Items.Add(f.Name);
}
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}

Result & Observations:

32
8. Developing A Component For Retrieving Information From
Message Box Using DCOM/.NET
AIM: developing a component for retrieving information from message box using
dcom .net
Apparatus: Microsoft Visual Studio 2005
Programming:

Message Box Program…….


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;

33
using System.Text;
using System.Windows.Forms;
namespace msgbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(textBox1.Text);
MessageBox.Show("Successfully submitted.");
}
}
}
Result& Observations:

34
9. Developing A Middleware Component For Retrieving Stock
Market Exchange Information Using CORBA

35
AIM: developing a middleware component for retrieving stock market exchange
information using CORBA
Apparatus: Microsoft Visual Studio 2005
Flow:

CORBA (Common Object Request Broker Architecture)


 CORBA is a specification that defines how distributed objects can interoperate.
 CORBA specification is controlled by OMG.
 CORBA objects can be written in almost any language and can exist on almost any
platform.
 Language Independence is possible using the construction of interfaces to objects
using the Interface Definition Language (ICL). The only requirement is a bridge
between the natural language and IDL.
 ORB (Object Request Broker) is at the core of CORBA. This is the principal
component for the transmission of information between the client and the server of
the CORBA application.
 The Java IDL enables distributed Java applications to transparently invoke
operations or remote network services, using the industry standard IDL and IIOP.

Programming:
The following is a sample application using CORBA:

module hello
{
// this is a interface and all the method which are called from client and executed
at the server are define in it.
interface Hello
{
// the return type and parameter (in , out , inout ) are idl data type
string displayDate();
float displayResult(in float f1, in float f2);

36
};
// an interface can have more than one method
};
// A module can have more than one interface
// implementation class
import hello.*;
import org.omg.CORBA.*;
import java.util.*;
public class HelloImpl extends _HelloImplBase
{
public String displayDate()
{
return " " + new Date();
}
public float displayResult(float f1, float f2)
{
return f1 * f2;
}
}

// Program to implement the Client


import hello.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;
public class HelloClient
{
static Hello helloImpl;
public static void main(String args[])
{

37
try
{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// resolve the Object Reference in Naming
NameComponent nc = new NameComponent("Date", "");
NameComponent path[] = {nc};
Hello helloImpl = HelloHelper.narrow(ncRef.resolve(path));
System.out.println(helloImpl.displayDate());
BufferedReader br = new BufferedReader( new
InputStreamReader(System.in));
System.out.println("Enter the first float value : ");
float f_val1 = Float.parseFloat(br.readLine());
System.out.println("Enter the second float value : ");
float f_val2 = Float.parseFloat(br.readLine());
System.out.println("The result is : " +
helloImpl.displayResult(f_val1,f_val2));
}
catch (Exception e)
{
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
}
// Program to implement the Server
import hello.*;

38
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.util.Properties;

public class HelloServer


{
public static void main(String args[])
{
try
{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// create servant and register it with the ORB
HelloImpl helloImpl = new HelloImpl();
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
Hello href = HelloHelper.narrow(helloImpl);
// bind the Object Reference in Naming
NameComponent nc = new NameComponent("Date", "");
NameComponent path[] = {nc};
ncRef.rebind(path, href);
System.out.println("HelloServer ready and waiting ...");
// wait for invocations from clients
orb.run();
}
catch (Exception e)
{
System.err.println("ERROR: " + e);

39
e.printStackTrace(System.out);
}
System.out.println("HelloServer Exiting ...");
}
}
Procedure:
The following steps are to be carried out to execute the above application:
1) Firstly, the IDL file is to be converted into a Java format. For doing so the following
command is used - idlj -fall -oldImpBase hello.idl.
2) After this a set of Java classes are created in a separate folder by name Hello.
3) All the java files, including those in the Hello folder, are to be compiled using the
following command - avac *.java hello\*.java.
4) The ORB directory service is now started using the following command - start orbd
-ORBIntitialPort 1200.
5) The server is then started using the command - java HelloServer -ORBIntitialPort
1200.
6) The client is then started using the command - java HelloCli -ORBIntitialPort 1200.

Observations& results:
 For the applications of Stock Market Exchange information and Bank balance
retrieval , the services are to be declared in the IDL nad to be implemented in the
implementation class.
 A connection to the database is to be made, using JDBC, in the implementation
class, to retrieve the data.
 Clients may be written in any of the natural languages and are to converted into the
IDL form using the compatible tools.

10. Developing a Middleware Component for Retrieving Bank Balance


Using CORBA

40
AIM: Developing a Middleware Component for Retrieving Bank Balance Using
CORBA
Apparatus: Microsoft Visual Studio 2005
Flow:

CORBA (Common Object Request Broker Architecture)

 CORBA is a specification that defines how distributed objects can interoperate.


 CORBA specification is controlled by OMG.
 CORBA objects can be written in almost any language and can exist on almost any
platform.
 Language Independence is possible using the construction of interfaces to objects
using the Interface Definition Language (ICL). The only requirement is a bridge
between the natural language and IDL.
 ORB (Object Request Broker) is at the core of CORBA. This is the principal
component for the transmission of information between the client and the server of
the CORBA application.
 The Java IDL enables distributed Java applications to transparently invoke
operations or remote network services, using the industry standard IDL and IIOP.

Programming:
The following is a sample application using CORBA:

module hello
{
// this is a interface and all the method which are called from client and executed
at the server are define in it.
interface Hello
{
// the return type and parameter (in , out , inout ) are idl data type
string displayDate();

41
float displayResult(in float f1, in float f2);
};
// an interface can have more than one method
};
// A module can have more than one interface

// implementation class

import hello.*;
import org.omg.CORBA.*;
import java.util.*;

public class HelloImpl extends _HelloImplBase


{
public String displayDate()
{
return " " + new Date();
}

public float displayResult(float f1, float f2)


{
return f1 * f2;
}
}

// Program to implement the Client


import hello.*;

42
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;

public class HelloClient


{
static Hello helloImpl;
public static void main(String args[])
{
try
{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// resolve the Object Reference in Naming
NameComponent nc = new NameComponent("Date", "");
NameComponent path[] = {nc};
Hello helloImpl = HelloHelper.narrow(ncRef.resolve(path));
System.out.println(helloImpl.displayDate());
BufferedReader br = new BufferedReader( new
InputStreamReader(System.in));
System.out.println("Enter the first float value : ");
float f_val1 = Float.parseFloat(br.readLine());
System.out.println("Enter the second float value : ");
float f_val2 = Float.parseFloat(br.readLine());
System.out.println("The result is : " +
helloImpl.displayResult(f_val1,f_val2));

43
}
catch (Exception e)
{
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
}

// Program to implement the Server


import hello.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.util.Properties;
public class HelloServer
{
public static void main(String args[])
{
try
{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// create servant and register it with the ORB
HelloImpl helloImpl = new HelloImpl();
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
Hello href = HelloHelper.narrow(helloImpl);

44
// bind the Object Reference in Naming
NameComponent nc = new NameComponent("Date", "");
NameComponent path[] = {nc};
ncRef.rebind(path, href);
System.out.println("HelloServer ready and waiting ...");
// wait for invocations from clients
orb.run();
}
catch (Exception e)
{
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
System.out.println("HelloServer Exiting ...");
}
}

Procedure:
The following steps are to be carried out to execute the above application:
1) Firstly, the IDL file is to be converted into a Java format. For doing so the following
command is used - idlj -fall -oldImpBase hello.idl.
2) After this a set of Java classes are created in a separate folder by name Hello.
3) All the java files, including those in the Hello folder, are to be compiled using the
following command - avac *.java hello\*.java.
4) The ORB directory service is now started using the following command - start orbd
-ORBIntitialPort 1200.
5) The server is then started using the command - java HelloServer -ORBIntitialPort
1200.
6) The client is then started using the command - java HelloCli -ORBIntitialPort 1200.
Observations& result:

45
 For the applications of Stock Market Exchange information and Bank balance
retrieval, the services are to be declared in the IDL and to be implemented in the
implementation class.
 A connection to the database is to be made, using JDBC, in the implementation
class, to retrieve the data.
 Clients may be written in any of the natural languages and are to converted into the
IDL form using the compatible tools.

46

You might also like