8/4/2019 Middle Ware Lab Doc
1/45
REMOTE METHOD INVOCATION
FileServerIntf.java
import java.rmi.*;
import java.io.*;
public interface FileServerIntf extends Remote
{
void fileDownload(String d1,String d2)throws RemoteException,IOException;
}
FileServerImpl.javaimport java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class FileServerImpl extends UnicastRemoteObject implements FileServerIntf {
public FileServerImpl() throws RemoteException { }
public void fileDownload(String d1,String d2)throws RemoteException,IOException{
String str;
FileReader fr=new FileReader(d1);
BufferedReader br =new BufferedReader(fr);
FileWriter fw=new FileWriter(d2);
while((str=br.readLine())!=null)
{
str=str+"\r\n";
char buff[]=new char[str.length()];
str.getChars(0,str.length(),buff,0);
fw.write(buff);
}
fr.close();
fw.close();
System.out.println("File Downloaded Successfully");
}
}
8/4/2019 Middle Ware Lab Doc
2/45
FileServer.java
import java.rmi.*;
public class FileServer
{
public static void main(String args[])
{
try{
FileServerImpl fileServerImpl=new FileServerImpl();
Naming.rebind("FileServer",fileServerImpl);
System.out.println("Server successfully started !");
}catch(Exception e)
{
System.out.println("Exception"+e);
}
}
}
FileClient.java
import java.rmi.*;
public class FileClient
{
public static void main(String args[]) {
try {
String fileServerURL="rmi://"+args[0]+"/FileServer";
FileServerIntf fileServerIntf=(FileServerIntf)
Naming.lookup(fileServerURL);
System.out.println("The source file is : "+args[1]);
System.out.println("The destination file is : "+args[2]);
fileServerIntf.fileDownload(args[1],args[2]);
} catch(Exception e) {
System.out.println("Exception:"+ e);
}
}
}
8/4/2019 Middle Ware Lab Doc
3/45
OUTPUT
8/4/2019 Middle Ware Lab Doc
4/45
BEAN DEVELOPER KIT
Colors.java
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
public class Colors extends Canvas
{
transient private Color color;
private boolean rectangular;
public Colors()
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
change();
}});
rectangular=false;
setSize(200,200);
change();
}
public boolean getRectangular()
{
return rectangular;
}
public void setRectangular(boolean flag)
{
rectangular=flag;
repaint();
}
8/4/2019 Middle Ware Lab Doc
5/45
public void change()
{
color=randomColor();
repaint();
}
public void changeShape()
{
if(rectangular)
rectangular=false;
else
rectangular=true;
repaint();
}
public 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 w=d.width;
int h=d.height;
g.setColor(color);
if(rectangular)
g.fillRect(0,0,w-1,h-1);
else
g.fillOval(0,0,w-1,h-1);
}
}
8/4/2019 Middle Ware Lab Doc
6/45
sams.mft
Name: Colors.class
Java-Bean: True
OUTPUT
8/4/2019 Middle Ware Lab Doc
7/45
ENTERPRISE JAVA BEAN
Remote.java
import java.rmi.*;
import javax.ejb.*;
public interface Remote extends EJBObject
{
public String getMessage(String str) throws RemoteException;
}
Home.java
import java.rmi.*;
import javax.ejb.*;
public interface Home extends EJBHome
{
public Remote create() throws RemoteException,CreateException;
}
Bean.java
import java.rmi.*;
import javax.ejb.*;
public class Bean implements SessionBean
{
public void setSessionContext(SessionContext set) { }
public void ejbpassivate() { }
public void ejbActivate() { }
public void ejbRemove() { }
public void ejbPassivate() { }
public void ejbCreate() { }
public String getMessage(String str)
{ return str;
}
}
8/4/2019 Middle Ware Lab Doc
8/45
Client.java
import javax.naming.*;
import java.util.*;
class Client
{
public static void main(String args[])
{
try{
Properties p=new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://localhost:7001/");
InitialContext ic=new InitialContext(p);
Home h=(Home)ic.lookup("mysession");
Remote r=h.create();
System.out.println(r.getMessage("Fear of the lord is begining of the
knowldge"));
}catch(Exception e){}
}
}
8/4/2019 Middle Ware Lab Doc
9/45
OUTPUT
8/4/2019 Middle Ware Lab Doc
10/45
DISPLAY THE MESSAGE USING SESSION BEAN
HelloRemote.java
import javax.ejb.*;
import java.rmi.*;
public interface HelloRemote extends EJBObject
{
public String sayHello(String str)throws RemoteException;
}
HelloHome.java
import javax.ejb.*;
import java.rmi.*;
public interface HelloHome extends EJBHome
{
public HelloRemote create() throws RemoteException,CreateException;
}
HelloBean.java
import javax.ejb.*;
public class HelloBean implements SessionBean
{
SessionContext ctx;
public void ejbCreate() { }
public String sayHello(String str){
return "Hello, "+str;
}
public void ejbRemove() { }
public void ejbPassivate() { }
public void ejbActivate() { }
public void setSessionContext(SessionContext ctx)
{
8/4/2019 Middle Ware Lab Doc
11/45
this.ctx=ctx;
}
}
HelloClient.java
import java.io.*;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import java.util.Properties;
public class HelloClient
{
public static void main(String args[])
{
try{
Properties h=new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL,"t3://localhost:7001/");
InitialContext ic=new InitialContext(h);
HelloHome ch=(HelloHome)ic.lookup("msgSession");
HelloRemote c=ch.create();
System.out.println(c.sayHello("Have a nice day !!"));
}catch(Exception e)
{
e.printStackTrace();
}
}
}
8/4/2019 Middle Ware Lab Doc
12/45
OUTPUT:
8/4/2019 Middle Ware Lab Doc
13/45
LIBRARY INFORMATION USING SESSION BEAN
Book.java
import javax.ejb.*;
import java.rmi.*;
import java.util.*;
public interface Book extends EJBObject
{
public void addBook(String title)throws RemoteException;
public void removeBook(String title)throws RemoteException,BookException;
public Vector getContents()throws RemoteException;
}
BookHome.java
import java.io.*;
import java.rmi.*;
import javax.ejb.*;
public interface BookHome extends EJBHome{
public Book create(String person)throws RemoteException,CreateException;
public Book create(String person,String id)throws RemoteException,CreateException;
}
BookEJB.java
import java.util.*;
import javax.ejb.*;
public class BookEJB implements SessionBean
{
String MemberName;
String memberId;
Vector contents;
public BookEJB() { }
public void ejbCreate(String person)throws CreateException
8/4/2019 Middle Ware Lab Doc
14/45
{
if(person==null)
throw new CreateException("\n Null bean not allowed");
else
MemberName=person;
System.out.println("\n MemberName:"+person);
memberId="0";
contents=new Vector();
}
public void ejbCreate(String person,String id)throws CreateException
{
if(person==null)
throw new CreateException("\n Member name"+person);
memberId="0";
contents=new Vector();
}
public void addBook(String title)
{
contents.addElement(title);
}
public void removeBook(String title)throws BookException
{
boolean result=contents.removeElement(title);
if(result==false)
throw new BookException(title+"Book is not in cart");
}
public Vector getContents()
{
return contents;
}
public void ejbActivate() { }
public void ejbPassivate() { }
public void ejbRemove() { }
public void setSessionContext(SessionContext ctx) { } }
8/4/2019 Middle Ware Lab Doc
15/45
BookException.java
public class BookException extends Exception
{
public BookException()
{
}
public BookException(String msg)
{
super(msg);
}
}
BookClient.java
import javax.naming.*;
import javax.rmi.*;
import java.rmi.*;
import java.util.*;
import java.io.*;
public class BookClient
{
public static void main(String args[])
{
Enumeration enum;
Vector bookList;
Book shoppingCart;
try{
Properties p=new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://localhost:7001/");
p.put(Context.SECURITY_PRINCIPAL,"weblogic");
p.put(Context.SECURITY_CREDENTIALS,"weblogic");
InitialContext ict=new InitialContext(p);
8/4/2019 Middle Ware Lab Doc
16/45
Object t=ict.lookup("library");
System.out.println("Lookup Successfull");
BookHome home=(BookHome)
PortableRemoteObject.narrow(t,BookHome.class);
shoppingCart=home.create("shriram","123");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("\nEnter Member Name : ");
String memb=br.readLine();
System.out.println("\nEnter the book name u want : ");
String book1=br.readLine();
shoppingCart.addBook(book1);
System.out.println("Enter the book name u want : ");
String book2=br.readLine();
shoppingCart.addBook(book2);
System.out.println("Enter book name name u want : ");
String book3=br.readLine();
shoppingCart.addBook(book3);
System.out.println("MemberName:"+memb);
System.out.println("Selected books are listed below:");
bookList=new Vector();
bookList=shoppingCart.getContents();
enum=bookList.elements();
while(enum.hasMoreElements())
{
String title=(String)enum.nextElement();
System.out.println(title);
}
System.out.println("Do u want to remove any book from
cart (y/n):");
String ch=br.readLine();
if(ch.equals("y"))
{ try{
System.out.println("ENter the book name:");
8/4/2019 Middle Ware Lab Doc
17/45
String remBook=br.readLine();
shoppingCart.removeBook(remBook);
System.out.println("Book has been removed
from the card:");
System.out.println("Member name:"+memb);
System.out.println("Selected books
are listed below:");
bookList=shoppingCart.getContents();
enum=bookList.elements();
while(enum.hasMoreElements())
{
String title1=(String)enum.nextElement();
System.out.println(title1);
}
System.out.println ("thanks for dealing with
us:");
}catch(Exception ex)
{
System.out.println("caught a book
Exception"+ex.getMessage());
}
}
else
System.out.println("thanks for dealing with us");
shoppingCart.remove();
}catch(Exception ex)
{ ex.printStackTrace();
}
}
}
8/4/2019 Middle Ware Lab Doc
18/45
OUTPUT:
8/4/2019 Middle Ware Lab Doc
19/45
BANKING OPERATIONS ( ENTITY BEAN)
BankRemote.java
import javax.ejb.*;import java.rmi.*;
import java.util.*;
public interface BankRemote extends EJBObject
{
public Integer getCusid()throws RemoteException;
public void setBal(double balance)throws RemoteException;
public double getBal()throws RemoteException;
public void debit(double amount)throws RemoteException;
public void credit(double amount)throws RemoteException;
}
BankHome.java
import java.io.*;
import java.rmi.RemoteException;
import javax.ejb.*;
public interface BankHome extends EJBHome
{
public BankRemote create(Integer empid,double balance)throws
RemoteException,CreateException;
public BankRemote findByPrimaryKey(Integer cusid)throws
FinderException,RemoteException;
}
BankBean.java
import javax.rmi.*;
import javax.ejb.*;
public abstract class BankBean implements EntityBean
{
public double cbalance;
public double amount;
8/4/2019 Middle Ware Lab Doc
20/45
public double balance;
public EntityContext context,ctx;
public Integer id;
public void ejbActivate(){}
public void ejbPassivate(){}
public void ejbRemove(){}
public void ejbLoad(){}
public void ejbStore(){}
public void setEntityContext(EntityContext ctx){}
public void unsetEntityContext(){}
public abstract void setCusid(Integer cusid);
public abstract Integer getCusid();
public abstract void setBal(double balance);
public abstract double getBal();
public Integer ejbCreate(Integer cusid,double balance)
{
setCusid(cusid);
setBal(balance);
return null;
}
public void ejbPostCreate(Integer cusid,double balance){}
public void debit(double amount)
{
balance=getBal();
balance-=amount;
setBal(balance);
}
public void credit(double amount)
{
balance=getBal();
balance+=amount;
setBal(balance);
}
}
8/4/2019 Middle Ware Lab Doc
21/45
EmployeeClient.java
import javax.naming.*;
import javax.rmi.*;
import java.rmi.*;
import java.util.*;
import java.io.*;
import javax.ejb.*;
public class EmployeeClient
{
public static void main(String args[])
{
int cid;
double cbalance;
int opt;
double camount;
try{
Properties p=new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://localhost:7001");
p.put(Context.SECURITY_PRINCIPAL,"weblogic");
p.put(Context.SECURITY_CREDENTIALS,"weblogic");
InitialContext ict=new InitialContext(p);
Object t=ict.lookup("moses-jemimah");
System.out.println("Lookup Successfull");
BankHome h=(BankHome)
PortableRemoteObject.narrow(t,BankHome.class);
System.out.println("\nWelcome to Banking Operation -
Middleware Lab ");
do{
System.out.println("\n1.Create an Account\n2.Credit\n3.Debit
\n4.Balance Enquiry\n5.Exit\nEnter your choice : ");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
8/4/2019 Middle Ware Lab Doc
22/45
opt=Integer.parseInt(br.readLine());
switch(opt)
{
case 1:
System.out.println("\nEnter account number : ");
cid=Integer.parseInt(br.readLine());
System.out.println("\nEnter the inital amount : ");
camount=Integer.parseInt(br.readLine());
BankRemote b1=h.create(new Integer(cid),camount);
System.out.println("Successfully started !!");
break;
case 2:
System.out.println("\nEnter the account number :");
cid=Integer.parseInt(br.readLine());
BankRemote b2=h.findByPrimaryKey(new
Integer(cid));
System.out.println("\nEnter the amount : ");
camount=Integer.parseInt(br.readLine());
b2.credit(camount);
cbalance=b2.getBal();
System.out.println("\nAvailable balance : "+cbalance);
break;
case 3:
System.out.println("\nEnter the account number : );
cid=Integer.parseInt(br.readLine());
BankRemote b3=h.findByPrimaryKey(new
Integer(cid));
System.out.println("\nEnter the amount : ");
camount=Integer.parseInt(br.readLine());
b3.debit(camount);
cbalance=b3.getBal();
System.out.println ("\nAvailable balance : "+cbalance);
break;
case 4:
8/4/2019 Middle Ware Lab Doc
23/45
ystem.out.println("\nEnter the account number :");
cid=Integer.parseInt(br.readLine());
BankRemote b4=h.findByPrimaryKey(new
Integer(cid));
cbalance=b4.getBal();
System.out.println("\nAvailable balance : "+cbalance);
break;
default:
System.out.println("\nPlease read the above options , Try
again");
break;
}
}while(opt!=5);
System.out.println("\nThanks!! , Visit Again ");
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
8/4/2019 Middle Ware Lab Doc
24/45
8/4/2019 Middle Ware Lab Doc
25/45
8/4/2019 Middle Ware Lab Doc
26/45
8/4/2019 Middle Ware Lab Doc
27/45
8/4/2019 Middle Ware Lab Doc
28/45
CONVERT PROGRAM
Public Class convert
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If ComboBox1.SelectedItem = "DOLLAR" And
ComboBox2.SelectedItem = "RUPEES" Then
TextBox2.Text = Val(TextBox1.Text) * 48.77
ElseIf ComboBox1.SelectedItem = "RUPEES" And
ComboBox2.SelectedItem = "DOLLAR" Then
TextBox2.Text = Val(TextBox1.Text) / 48.77
ElseIf ComboBox1.SelectedItem = "EURO" And
ComboBox2.SelectedItem = "RUPEES" Then
TextBox2.Text = Val(TextBox1.Text) * 62.42
ElseIf ComboBox1.SelectedItem = "RUPEES" And
ComboBox2.SelectedItem = "EURO" Then
TextBox2.Text = Val(TextBox1.Text) / 62.42
ElseIf ComboBox1.SelectedItem = "DOLLAR" AndComboBox2.SelectedItem = "EURO" Then
TextBox2.Text = Val(TextBox1.Text) * 0.78
ElseIf ComboBox1.SelectedItem = "EURO" And
ComboBox2.SelectedItem = "DOLLAR" Then
TextBox2.Text = Val(TextBox1.Text) / 0.78
Else
MsgBox("Enter Currect Currency Type")
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox2.Text = " "
End Sub
8/4/2019 Middle Ware Lab Doc
29/45
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
End
End Sub
End Class
OUTPUT:
8/4/2019 Middle Ware Lab Doc
30/45
ENCRYPTION AND DECRYPTION
Imports System.Data
8/4/2019 Middle Ware Lab Doc
31/45
Imports System
Imports System.IO
Imports System.Security.AccessControl
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("Enter File Path")
Else
Try
If RadioButton1.Checked = True Then
MsgBox("Encryption" + TextBox1.Text)
File.Encrypt(TextBox1.Text)
MsgBox("Encryption was Done")
RadioButton2.Checked = False
ElseIf RadioButton2.Checked = True Then
MsgBox("Decryption" + TextBox1.Text)
File.Decrypt(TextBox1.Text)
MsgBox("Decryption was done")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
End
End Sub
End Class
OUTPUT:
8/4/2019 Middle Ware Lab Doc
32/45
MESSAGE CONTROL
Public Class Form1
8/4/2019 Middle Ware Lab Doc
33/45
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles PictureBox1.Click
Dim d As String = InputBox("Enter u r data of birth(mm/dd/yyyy)")
If d Nothing Then
Dim dd As Date = DateTime.Parse(d)
Dim uday = System.DateTime.Today.Day - dd.Day
Dim uyy = System.DateTime.Today.Year - dd.Year
Dim umm = System.DateTime.Today.Month - dd.Month
MsgBox(uyy & ":years" & umm & ":months" & uday & ":days" & vbCrLf &
"WHAT DID YOU DONE IN THOSE YEARS?")
End If
End Sub
End Class
OUTPUT:
8/4/2019 Middle Ware Lab Doc
34/45
STOCK MARKET EXCHANGE INFORMATION
USING CORBA
StockMarket.idl
8/4/2019 Middle Ware Lab Doc
35/45
module SimpleStocks
{
interface StockMarket
{
float get_price(in string symbol);
};
};
StockMarketImpl.java
import org.omg.CORBA.*;
import SimpleStocks.*;
public class StockMarketImpl extends _StockMarketImplBase
{
StockMarketImpl()
{
super();
}
public float get_price(String symbol)
{
float price=0;
for(int i=0;i
8/4/2019 Middle Ware Lab Doc
36/45
public class StockMarketServer
{
public static void main(String args[])
{
try{
ORB orb=ORB.init(args,null);
StockMarketImpl smi=new StockMarketImpl();
orb.connect(smi);
org.omg.CORBA.Object objRef=
orb.resolve_initial_references("NameService");
NamingContext ncref=NamingContextHelper.narrow(objRef);
NameComponent nc=new NameComponent("NASDAQ","");
NameComponent path[]={nc};
ncref.rebind(path,smi);
System.out.println("\nThe stock market server is ready and up");
Thread.currentThread().join();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
StockmarketClient.java
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import SimpleStocks.*;
public class StockmarketClient
8/4/2019 Middle Ware Lab Doc
37/45
{
public static void main(String args[])
{
try{
ORB orb=ORB.init(args,null);
StockMarketImpl smi=new StockMarketImpl();
NamingContext ncref=NamingContextHelper.narrow
(orb.resolve_initial_references("NameService"));
NameComponent path[]={new
NameComponent("NASDAQ","")};
StockMarket market=StockMarketHelper
.narrow(ncref.resolve(path));
System.out.println("\nPrice of my company is $ "
+market.get_price("j-soft"));
}catch(Exception e)
{
e.printStackTrace();
}
}
}
OUTPUT:
8/4/2019 Middle Ware Lab Doc
38/45
8/4/2019 Middle Ware Lab Doc
39/45
8/4/2019 Middle Ware Lab Doc
40/45
WEATHER FORECASTING USING CORBA
WeatherForecastServer.idl
module WeatherForecast
{
interface WeatherForecastServer
{
string getWeather();
};
};
WeatherForecastImpl.java
import org.omg.CORBA.*;
import WeatherForecast.*;
import java.util.*;
import java.sql.*;
public class WeatherForecastImpl extends _WeatherForecastServerImplBase
{Connection con;
Statement stat;
ResultSet res;
String details="";
int i=1;
WeatherForecastImpl()
{
super();
}
public String getWeather()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:cicils");
stat=con.createStatement();
8/4/2019 Middle Ware Lab Doc
41/45
res=stat.executeQuery("select * from weatherdata");
details="";
while(res.next())
{
for(i=1;i
8/4/2019 Middle Ware Lab Doc
42/45
ncref.rebind(path,smi);
System.out.println("\nThe weather forecast server is ready and up");
Thread.currentThread().join();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
WeatherClient.java
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import WeatherForecast.*;
import java.util.*;
public class WeatherClient
{
public static void main(String args[])
{
try{
int i=0;
String details;
StringTokenizer st;
ORB orb=ORB.init(args,null);
WeatherForecastImpl smi=new WeatherForecastImpl();
NamingContext ncref=NamingContextHelper.narrow
(orb.resolve_initial_references("NameService"));
NameComponent path[]={new
NameComponent("NASDAQ","")};
WeatherForecastServer data=WeatherForecastServerHelper
.narrow(ncref.resolve(path));
details=data.getWeather();
st=new StringTokenizer(details,";");
System.out.println("\nWeather Report by Prof.PR");
8/4/2019 Middle Ware Lab Doc
43/45
System.out.println("_____________________________________\n");
System.out.println("City\t\tMax\t\tMin\t\tHummidity");
System.out.println("____________________________________\n");
while(st.hasMoreTokens())
{
System.out.print(st.nextToken()+"\t\t");
if(((i+1)%4)==0)
System.out.println();
i++;
}
System.out.println("_____________________________________");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
8/4/2019 Middle Ware Lab Doc
44/45
OUTPUT:
8/4/2019 Middle Ware Lab Doc
45/45
Recommended