Vous êtes sur la page 1sur 41

Submitted by:

K.VENKATESH
160110742002
CBIT
1
Contents
1. Develop a Remote Object with method for summation of range of given
numbers using RMI................................................................................................ 3
2. rite a program to develop a remote object for the student details having a
method to !nd the student details. Implement the client and server for the same.
.............................................................................................................................. "
3. rite a program to demonstrate the #OR$% application for %ddition of two
numbers................................................................................................................ &
'.rite an (M) program to displa* +mplo*ee details using +,ternal D-D..........11
".rite a program to connect to the database using .D$#..................................12
/.Implementation of a remote object for an emplo*ee application having a
remote method to !nd emplo*ee details such as empid0 ename0 age0 salar*0 and
service using RMI................................................................................................. 1'
1.rite a #OR$% ID) interface for 2nowing the status of a reservation in a railwa*
reservation s*stem. Implement the ID) interface using .ava. #reate a .ava client
to test the above. 3se .D$# to connect to database...........................................11
Types................................................................................................................................21
There are commercial and free drivers available for most relational database servers.
These drivers fall into one of the following types:..........................................................21
JDBC Architectre...........................................................................................................21
!lti"tier #etwor$ Compting: C%&BA is an e'ample of what is termed (middleware)""a
technology that enables the separation of applications into three distinct sections *see
+igre ,-:................................................................................................................................
&. rite a #OR$% ID) Interface for the $an2 Object. -he Interface shall contain
the following Methods4........................................................................................ 3'
2
PART-A
.
1. Develop a Remote Object with method for summation
of range of given numbers using RMI.
//Remte i!te"#$%e #" &umm$ti! # &e"ie& # !umbe"&
import 0ava.rmi.12
pblic interface 3mmation e'tends &emote
4
pblic int add*int first5 int last- throws &emote6'ception2
7
//Im'(eme!t$ti! %($&& #" Summ$ti! $''(i%$ti!
import 0ava.rmi.12
import 0ava.rmi.server.12
import 0ava.io.12
import 0ava.lang.12
pblic class 3m8mp e'tends 9nicast&emote%b0ect implements 3mmation
4
static 3tring str:; ;2
pblic 3m8mp*- throws 6'ception47
pblic int add*int first5 int last- throws &emote6'ception
4
int res:<2
for*int i:first2 i=:last2 i>>-
4
res:res>i2
7
retrn res2
7
72
//Se")e" '"*"$m
import 0ava.rmi.12
import 0ava.rmi.server.12
pblic class 3erver
4
pblic static void main*3tring arg?@- throws 6'ception
4
&emote r:new 3m8mp*-2
#aming.bind*;rmi://localhost:1<AA/addob0;5r-2
B
3ystem.ot.println*;3erver is p and &nning.....;-2
7
72
//Client Crogram
import 0ava.rmi.12
import 0ava.io.12
pblic class Client
4
pblic static void main*3tring arg?@- throws 6'ception
4
3ystem.set3ecrity!anager*new &!83ecrity!anager*--2
Bffered&eader br:new Bffered&eader*new 8npt3tream&eader*3ystem.in--2
int first5 last5 reslt2
3ystem.ot.println*;6nter the +irst #mber...;-2
first:8nteger.parse8nt*br.readDine*--2
3ystem.ot.println*;6nter the last #mber....;-2
last:8nteger.parse8nt*br.readDine*--2
&emote s:#aming.loo$p*;rmi://localhost:1<AA/addob0;-2
3mmation r:*3mmation- s2
3ystem.ot.println*;The 3mmation of series...;>first>; ...... ;>last> ; is
;>r.add*first5last--2
7
7
%tpt:
D:EasmtechE&!8F 0avac 1.0ava
D:EasmtechE&!8F rmic 3m8mp
D:EasmtechE&!8F start rmiregistry
D:EasmtechE&!8F start 0ava 3erver
D:EasmtechE&!8F 0ava Client
6nter the +irst #mberG. 1
6nter the Dast #mberG.. ,
The smmation of series G 1G.., is.. 1,
,
2. rite a program to develop a remote object for the
student details having a method to !nd the student
details. Implement the client and server for the same.
I!te"#$%e '"*: &tdI!te".+$)$
import 0ava.rmi.12
pblic interface std8nter e'tends &emote
4
pblic 3tdent getDetails*- throws &emote6'ception2
7
I!te"#$%e Im'(eme!t$ti! P"*"$m : &tdI!te"Im'.+$)$
import 0ava.rmi.12
import 0ava.rmi.server.12
pblic class std8nter8mp e'tends 9nicast&emote%b0ect implements std8nter
4
pblic std8nter8mp*- throws &emote6'ception472
pblic 3tdent getDetails*- throws &emote6'ception
4
try
4
3tdent std:new 3tdent*B<5;0ohn;5;83;5;Hradation;5;!8T;-2
retrn std2
7
catch*6'ception e-
4
3ystem.ot.println*(6'ception &aisedG..)>e-2
retrn nll2
7
7
7
I
Stude!t.+$)$
import 0ava.rmi.12
import 0ava.io.12
pblic class 3tdent implements 3erialiJable
4
3tring sname2
int rno2
3tring dept2
3tring corse2
3tring cname2
pblic 3tdent*- throws &emote6'ception472
pblic 3tdent*int rno53tring sname53tring dept53tring corse53tring cname- throws
&emote6'ception
4
this.rno:rno2
this.sname:sname2
this.dept:dept2
this.corse:corse2
this.cname:cname2
7
7
Se")e" '"*: &tdSe")e".+$)$
import 0ava.rmi.12
pblic class std3erver
4
pblic static void main*3tring args?@- throws 6'ception
4
std8nter st:new std8nter8mp*-2
#aming.bind*;rmi://localhost:1<AA/stdob0;5st-2
3ystem.ot.println*;server is ready;-2
7
K
7
C(ie!t '"*: &tdC(ie!t.+$)$
import 0ava.rmi.12
pblic class stdClient
4
pblic static void main*3tring args?@- throws 6'ception
4
&emote 31:#aming.loo$p*;rmi://localhost:1<AA/stdob0;-2
std8nter si:*std8nter-312
3tdent s:si.getDetails*-2
3ystem.ot.println*;En stdent rollno:;>s.rno-2
3ystem.ot.println*;En stdent name:;>s.sname-2
3ystem.ot.println*;En stdent dept:;>s.dept-2
3ystem.ot.println*;En stdent corse:;>s.corse-2
3ystem.ot.println*;En stdent collegename:;>s.cname-2
7
7
,ut'ut:
D:EasmtechE&!8F 0avac 1.0ava
D:EasmtechE&!8F rmic std8nter8mp
D:EasmtechE&!8F start rmiregistry
D:EasmtechE&!8F start 0ava std3erver
D:EasmtechE&!8F 0ava stdClient
3tdent rollno: B<
3tdent name: 0ohn
3tdent dept: 83
3tdent corse: gradation
3tdent collegename: !8T
L
". rite a program to demonstrate the COR#$
application for $ddition of two numbers
//8DD file
modle libMadd
4
interface addition
4
long sm*in long n15 in long n2-2
72
72
//3ervant Crogram
//implemenation class
import libMadd.12
pblic class 3ervant e'tends Maddition8mplBase
4
pblic 3ervant*- throws 6'ception 47
pblic int sm*int n15int n2-
4
int res:n1>n22
retrn res2
7
72
//3erver Crogram
import libMadd.12
import org.omg.C%&BA.12
import org.omg.Cos#aming.12
import org.omg.Cos#aming.#amingConte'tCac$age.12
pblic class 3erver
4
pblic static void main*3tring args?@- throws 6'ception
A
4
%&B orb:%&B.init*args5nll-2
org.omg.C%&BA.%b0ect o2
3ervant as:new 3ervant*-2
orb.connect*as-2
o:orb.resolveMinitialMreferences*;#ame3ervice;-2
#amingConte't ncref:#amingConte'tNelper.narrow*o-2
#ameComponent nc:new #ameComponent*;ob0;5; ;-2
#ameComponent?@ path:4nc72
ncref.rebind*path5as-2
0ava.lang.%b0ect ob0:new 0ava.lang.%b0ect*-2
synchroniJed*ob0-
4
ob0.wait*-2
7
7
7
//CDient Crogram
import libMadd.12
import org.omg.C%&BA.12
import org.omg.Cos#aming.12
import org.omg.Cos#aming.#amingConte'tCac$age.12
import 0ava.io.12
pblic class Client
4
pblic static void main*3tring args?@- throws 6'ception
4
%&B orb:%&B.init*args5nll-2
org.omg.C%&BA.%b0ect o2
3ervant as:new 3ervant*-2
orb.connect*as-2
#amingConte't
ncref:#amingConte'tNelper.narrow*orb.resolveMinitialMreferences*;#ame3ervice;--2
#ameComponent nc:new #ameComponent*;ob0;5; ;-2
#ameComponent?@ path:4nc72
o:ncref.resolve*path-2
addition a:additionNelper.narrow*o-2
int n15 n22
Bffered&eader br:new Bffered&eader*new 8npt3tream&eader*3ystem.in--2
3ystem.ot.println*;6#ter the first nmber.....;-2
n1:8nteger.parse8nt*br.readDine*--2
3ystem.ot.println*;6nter the second nmber....;-2
n2:8nteger.parse8nt*br.readDine*--2
1<
3ystem.ot.println*;The sm of two nmbers is...;>a.sm*n15n2--2
7
7
,ut'ut:
D:EasmtechE0dbcEC%&BAEaddFcd libMadd
D:EasmtechE0dbcEC%&BAEaddElibMaddF0avac 1.0ava
#ote: Maddition8mplBase.0ava ses nchec$ed or nsafe operations.
#ote: &ecompile with "Olint:nchec$ed for details.
D:EasmtechE0dbcEC%&BAEaddElibMaddFcd ..
D:EasmtechE0dbcEC%&BAEaddF0avac 1.0ava
D:EasmtechE0dbcEC%&BAEaddFstart orbd
D:EasmtechE0dbcEC%&BAEaddFstart 0ava 3erver
D:EasmtechE0dbcEC%&BAEaddF0ava Client
6#ter the first nmber.....
12
6nter the second nmber....
2.
The sm of two nmbers is....,
D:EasmtechE0dbcEC%&BAEaddF0ava Client
6#ter the first nmber.....
.B
6nter the second nmber....
.B
The sm of two nmbers is...IL
D:EasmtechE0dbcEC%&BAEaddF0ava Client
6#ter the first nmber.....
,<
6nter the second nmber....
,<
The sm of two nmbers is...1<<
11
%.rite an &M' program to displa( )mplo(ee details
using )*ternal D+D
-"..m(
=P'ml version:;1.<;PF
=QD%CTRC6 hr 3R3T6! ;hr2;F
=hrF
=employeeF
=firstnameFter=/firstnameF
=lastnameFasdf=/lastnameF
=positionFger=/positionF
=departmentFgh0=/departmentF
=/employeeF
=employeeF
=firstnameFabc=/firstnameF
=lastnameF'yJ=/lastnameF
=positionFpSr=/positionF
=departmentFABC=/departmentF
=/employeeF
=/hrF
-"2.dtd
=Q6D6!6#T employee *firstname5lastname5position5department-F
=Q6D6!6#T firstname *TCCDATA-F
=Q6D6!6#T lastname *TCCDATA-F
=Q6D6!6#T position *TCCDATA-F
=Q6D6!6#T department *TCCDATA-F
12
,.rite a program to connect to the database using
-D#C.
import 0ava.sSl.12
import 0ava.io.12
import 0ava.lang.12
class 0dbc1
4
pblic static void main*3tring arg?@-
4
Connection con2
Crepared3tatement ps2
&eslt3et rs2
3tring str:;#9DD;2
try
4
Bffered&eader br:new Bffered&eader*new
8npt3tream&eader*3ystem.in--2
3ystem.ot.println*;6#ter the &oll nmber of the stdent....;-2
int rollno:8nteger.parse8nt*br.readDine*--2
Class.for#ame*;sn.0dbc.odbc.Jdbc%dbcDriver;-2
con:Driver!anager.getConnection*;Jdbc:%dbc:asrar;-2
ps:con.prepare3tatement*;select 1from mar$s where rollno:P;-2
ps.set8nt*15rollno-2
rs:ps.e'ecteUery*-2
if*rs.ne't*--
str:;Nost name is:;>rs.get3tring*;name;-2
else
str:;invalid rollnmber;2
.close*-2
3ystem.ot.println*;The answer is: ;>str-2
7
catch*6'ception e-
4
3ystem.ot.println*;6'ception raised in main method....;-2
7
7
7
,ut'ut:
D:EasmtechE0dbcF0avac 0dbc1.0ava
D:EasmtechE0dbcF0ava 0dbc1
1.
6#ter the &oll nmber of the stdent....
1<
The answer is: Nost name is:asrar
D:EasmtechE0dbcF0ava 0dbc1
6#ter the &oll nmber of the stdent....
2<
The answer is: Nost name is:ahmed
D:EasmtechE0dbcF0ava 0dbc1
6#ter the &oll nmber of the stdent....
.<
The answer is: Nost name is:mohammed
D:EasmtechE0dbcF0ava 0dbc1
6#ter the &oll nmber of the stdent....
B<
The answer is: invalid rollnmber
1B
..Implementation of a remote object for an emplo(ee
application having a remote method to !nd emplo(ee
details such as empid/ ename/ age/ salar(/ and
service using RMI.
/$t$b$&e t$b(e:
P"*"$m:
I!te"#$%e '"*"$m: em'i!t.+$)$
import 0ava.rmi.12
pblic interface empint e'tends &emote
4
3tring getDetails*int id- throws 6'ception2
7
Im'(eme!t$ti! '"*"$m: em'im'0+$)$
import 0ava.rmi.server.12
import 0ava.sSl.12
pblic class empimp e'tends 9nicast&emote%b0ect implements empint
4
Connection c2
Crepared3tatement ps2 &eslt3et rs2
3tring res2
pblic empimp*- throws 6'ception
4
Class.for#ame *;sn.0dbc.odbc.Jdbc%dbcDriver;-2
c:Driver!anager.getConnection*;0dbc:odbc:oracle;5;scott;5;tiger;-2
7
pblic 3tring getDetails*int id- throws 6'ception
4
1,
ps:c.prepare3tatement *;select 1 from myemp where empid:P;-2
ps.set8nt*15id-2
rs:ps.e'ecteUery*-2
if*rs.ne't*--
4
res:rs.get3tring*1->;:;>rs.get8nt*2->;:;>rs.get8nt*.->;:;>rs.get3tring*B->;:;>rs.get8nt*,-2
retrn res2
7
else retrn ;V;2
7
protected void finaliJe*- throws throwable
4
c.close*-2
sper.finaliJe*-2
7
7
Se")e" '"*"$m: &e".+$)$
import 0ava.rmi.12
pblic class ser
4
pblic static void main*3tring args?@- throws 6'ception
4
empimp ob0:new empimp*-2
#aming.rebind*;6!C;5ob0-2
7
7
C(ie!t '"*"$m: %(i.+$)$
import 0ava.til.12
import 0ava.io.12
import 0ava.rmi.12
pblic class cli
4
1I
pblic static void main*3tring args?@- throws 6'ception
4
Bffered&eader br:new Bffered&eader*new 8npt3tream&eader*3ystem.in--2
3tring rval2
empint ob0:*empint- #aming.loo$p*;rmi://localhost:1<AA/6!C;-2
3ystem.ot.print*;enter empid:;-2
rval:ob0.getDetails*8nteger.parse8nt*br.readDine*---2
if*Qrval.eSals*;V;--
4
3tringTo$eniJer st:new 3tringTo$eniJer*rval5;:;-2
3ystem.ot.println*;Et"""employee details:"""En;-2
3ystem.ot.println*;Etname:;>st.ne'tTo$en*--2
3ystem.ot.println*;Etage:;>st.ne'tTo$en*--2
3ystem.ot.println*;Etsalary:;>st.ne'tTo$en*--2
3ystem.ot.println*;Etservice:;>st.ne'tTo$en*--2
7
else
3ystem.ot.println*;Et8nvalid 8D;-2
7
7
,ut'ut:
C:E<1.Ermi>0dbcEemp appF0avac 1.0ava
C:E<1.Ermi>0dbcEemp appFstart rmiregistry
C:E<1.Ermi>0dbcEemp appFstart 0ava ser
C:E<1.Ermi>0dbcEemp appF0ava cli
6nter emp id: 1
"""" 6mployees Details """"
6mpMid:1
6mpM#ame:ra0
DateMofM0oin:2<<L"<L"1<
3ervice:manager
3alary:1<<<<
1K
0.rite a COR#$ ID' interface for 1nowing the status of
a reservation in a railwa( reservation s(stem.
Implement the ID' interface using -ava. Create a -ava
client to test the above. 2se -D#C to connect to
database
I/1 P"*"$m: '!"2i!t#.id(
modle pnr
4
interface pnrMintf
4
string get3tats*in long id-2
72
72
Im'(eme!t$ti! P"*"$m: im'(&e")e!t.+$)$
import pnr.12
import 0ava.sSl.12
class implservent e'tends MpnrMintf8mplBase
4
Connection c2
Crepared3tatement ps2
&eslt3et rs2
pblic implservent*- throws 6'ception
4
Class.for#ame*;sn.0dbc.odbc.Jdbc%dbcDriver;-2
c:Driver!anager.getConnection*;0dbc:odbc:oracle;5;scott;5;tiger;-2
7
pblic 3tring get3tats*int id-
4
try
4
ps:c.prepare3tatement*;select 1 from mypnr where pnrMid:P;-2
ps.set8nt*15id-2
1L
rs : ps.e'ecteUery*-2
if*rs.ne't*--
retrn rs.get3tring*2-2
else
retrn ;V;2
7
catch*6'ception e-47
retrn ;V;2
7
7
Se")e" P"*"$m: &e")e".+$)$
import pnr.12
import org.omg.C%&BA.12
import org.omg.Cos#aming.12
import org.omg.Cos#aming.#amingConte'tCac$age.12
pblic class server
4
pblic static void main*3tring args?@- throws 6'ception
4
%&B orb :%&B.init*args5nll-2
implservent ob0:new implservent*-2
orb.connect*ob0-2
#amingConte'tn ob0:#amingConte'tNelper.narrow
*orb.resolveMinitialMreferences*;#ame3ervice;--2
#ameComponent nc:new #ameComponent*;C#&;5; ;-2
#ameComponent?@ path :4nc72
nob0.rebind*path5ob0-2
0ava.lang.%b0ect oob0:new 0ava.lang.%b0ect*-2
synchroniJed*oob0-
4
oob0.wait*-2
7
7
1A
7
C(ie!t P"*"$m: %(ie!t.+$)$
import pnr.12
import org.omg.C%&BA.12
import org.omg.Cos#aming.12
import org.omg.Cos#aming.#amingConte'tCac$age.12
import 0ava.io.12
import 0ava.til.12
pblic class client
4
pblic static void main*3tring args?@- throws 6'ception
4
Bffered&eader br:new Bffered&eader
*new 8npt3tream&eader*3ystem.in--2
%&B orb :%&B.init*args5nll-2
implservent ob0:new implservent*-2
orb.connect*ob0-2
#amingConte'tn ob0:#amingConte'tNelper.narrow
*orb.resolveMinitialMreferences*;#ame3ervice;--2
#ameComponent nc:new #ameComponent*;C#&;5; ;-2
#ameComponent?@ path : 4nc72
pnrMintf pi:pnrMintfNelper.narrow*nob0.resolve*path--2
3ystem.ot.print*;6nter C#& id:;-2
3tringrval:ob0.get3tats*8nteger.parse8nt*br.readDine*---2
if*Qrval.eSals*;V;--
3ystem.ot.println*;Crrent 3tats is: ; > rval-2
else
3ystem.ot.println*;8nvalid C#& #mber;-2
7
7
,ut'ut:
D:EasmtechEC%&BAE&63WF start orbd
D:EasmtechEC%&BAE&63WF start 0ava 3erver
2<
D:EasmtechEC%&BAE&63WF 0ava Client
6nter C#& 8D:1<<
Crrent 3tats is: Xaiting
PART B
21
3AVA /ATABASE C,NNECTIVIT4 53/BC6
Java Database Connectivity *JDBC- is an AC8 for the Java programming langage that
defines how a client may access a database. 8t provides methods for Serying and pdating
data in a database. JDBC is oriented towards relational databases.
JDBC allows mltiple implementations to e'ist and be sed by the same application. The AC8
provides a mechanism for dynamically loading the correct Java pac$ages and registering
them with the JDBC Driver !anager. The Driver !anager is sed as a connection factory for
creating JDBC connections.
JDBC connections spport creating and e'ecting statements. These may be pdate
statements sch as 3UDYs C&6AT65 8#36&T5 9CDAT6 and D6D6T65 or they may be Sery
statements sch as 36D6CT. Additionally5 stored procedres may be invo$ed throgh a
JDBC connection. JDBC represents statements sing one of the following classes:
3tatement Z the statement is sent to the database server each and every time.
Crepared3tatement Z the statement is cached and then the e'ection path is pre
determined on the database server allowing it to be e'ected mltiple times in an
efficient manner.
Callable3tatement Z sed for e'ecting stored procedres on the database.
JDBC Drivers are client"side adaptors *they are installed on the client machine5 not on the
server- that convert reSests from Java programs to a protocol that the DB!3 can
nderstand.
Ty'e&
There are commercial and free drivers available for most relational database servers. These
drivers fall into one of the following types:
Type 15 the JDBC"%DBC bridge
Type 25 the #ative"AC8 driver
Type .5 the networ$"protocol driver
Type B5 the native"protocol drivers
8nternal JDBC driver5 driver embedded with J&6 in Java"enabled 3UD databases. 9sed for
Java stored procedres. This does not belong to the above classification.
22
3/BC A"%-ite%tu"e
T7-tie" $!d T-"ee-tie" P"%e&&i!* 8de(&
The JDBC AC8 spports both two"tier and three"tier processing models for database access.
+igre 1: Two-tie" A"%-ite%tu"e #" /$t$ A%%e&&.
+igre 2: T-"ee-tie" A"%-ite%tu"e #" /$t$ A%%e&&.
3/BC P"du%t Cm'!e!t&
3/BC i!%(ude& #u" %m'!e!t&:
1. T-e-3/BC-API 9
The JDBC[ AC8 provides programmatic access to relational data from the Java[
programming langage. 9sing the JDBC AC85 applications can e'ecte 3UD
statements5 retrieve reslts5 and propagate changes bac$ to an nderlying data sorce.
The JDBC AC8 can also interact with mltiple data sorces in a distribted5
heterogeneos environment. The JDBC B.< AC8 is divided into two pac$ages: 0ava.sSl
and 0ava'.sSl. Both pac$ages are inclded in the"Java"36"and"Java"66"platforms.
2. 3/BC-/"i)e"-8$!$*e" 9
The JDBC Driver!anager class defines ob0ects which can connect Java applications
to a JDBC driver. Driver!anager has traditionally been the bac$bone of the JDBC
2.
architectre. 8t is Site"small"and"simple.
The 3tandard 6'tension pac$ages 0ava'.naming and 0ava'.sSl let yo se a
Data3orce ob0ect registered with a Java #aming and Directory 8nterface[ *J#D8-
naming service to establish a connection with a data sorce. Ro can se either
connecting mechanism5 bt sing a Data3orce ob0ect is recommended whenever
possible.
.. 3/BC-Te&t-Suite \
The JDBC driver test site helps yo to determine that JDBC drivers will rn yor
program. These tests are not comprehensive or e'hastive5 bt they do e'ercise many
of the important featres in"JDBC"AC8.
B. 3/BC-,/BC-B"id*e 9
The Java 3oftware Bridge provides JDBC access via %DBC drivers. #ote that yo
need to load %DBC binary code onto each client machine that ses this driver. As a
reslt5 the %DBC driver is most appropriate on a corporate networ$ where client
installations are not a ma0or problem5 or for application server code written in Java in
a three"tier architectre.
2B
PR,CE/:RE ;,R CREATIN< /ATA S,:RCE NA8E 5/SN6:
Ste' 1: 3elect start 3ettings Control
Canel
Ste' 2: 3elect Administrative Tools icon
2,
Ste' =: 3elect Data3orces %DBC
Ste' 4: 3elect !icrosoft Driver for %DBC which rns D3# for application
2I
Ste' >: Crovide the D3# name and server in !icrosoft %DBC for %racle 3etp dialoge
bo'.
Ste' 6: 3elect Driver name and clic$ on o$ btton.
2K
A new Data 3orce will be created and will be listed in %DBC Data 3orce Administrator
dialog bo'.
C,88,N ,B3ECT RE?:EST BR,KER ARCHITECT:RE:
The Common %b0ect &eSest Bro$er Architectre *C%&BA- from the %b0ect !anagement
Hrop *%!H- provides a platform"independent5 langage"independent architectre for
writing distribted5 ob0ect"oriented applications. C%&BA ob0ects can reside in the same
process5 on the same machine5 down the hall5 or across the planet. The Java langage is an
e'cellent langage for writing C%&BA programs. 3ome of the featres that accont for this
poplarity inclde the clear mapping from %!H 8DD to the Java programming langage5 and
the Java rntime environmentYs bilt"in garbage collection.
T-e Be!e#it& # /i&t"ibuted Cm'uti!*
The motivations for strctring a system as a set of distribted components inclde:
An increased ability to collaborate throgh better connectivity and networ$ing
The potential to increase performance and efficiency throgh the se of parallel
processing
8ncreased reliability5 scalability and availability throgh replication
A greater cost"effectiveness throgh the sharing of compting resorces *for e'ample5
printers5 dis$ and processing power- and the implementation of heterogeneos5 open
systems
Along with these impressive advantages comes additional comple'ity to the development
process. The programmer now has to handle sitations li$e the following:
#etwor$ delay and latency
Doad balancing between the different processing elements in the system
6nsring the correct ordering of events
2L
Cartial failre of commnications lin$s5 in particlar with regard to immtable
transactions *i.e.5 transactions which mst yield the same reslt if repeated\for
e'ample5 the accidental processing of a ban$ transaction twice shold not erroneosly
affect the accont balance-
%ne of the earlier techniSes to develop the distribted applications was the remote
procedre call *&CC-.
Remte P"%edu"e C$((&
The motivation behind &CCs is to ma$e networ$ programming as easy as ma$ing a
traditional fnction call. Din' provides the %pen #etwor$ Compting flavor of remote
procedre calls *%#C"&CC-5 which was originally developed by 3n !icrosystems for se in
its #etwor$ +ile 3ystem *#+3- implementation.
&CCs do nothing more than hide the implementation details of creating soc$ets *networ$
endpoints-5 sending data and closing the soc$ets as reSired. They are also responsible for
converting the *remote- procedreYs *local data- parameters into a format sitable for networ$
transportation and again into a format that can be sed on the remote host. This networ$"
transparent transmission of data ensres both end machines can interpret the information
content correctly.
Uite often5 different representation isses are involved in this process *for e'ample5 byte
ordering5 A3C88 to 6BCD8C5 etc.-. Additionally5 pointers *references to address locations in a
virtal memory space- cannot be passed directly from one process/machine to another.
8nstead5 a (deep copy) needs to be performed5 copying the data pointed to by the pointer
which is then sent (across the wire). The entire procedre of transferring the data from the
native format of a machine into a networ$"transparent format is called (marshalling). The
reverse procedre is called (de"marshaling) or (nmarshaling). The %#C"&CC ses a special
type of marshaling called e'ternal data representation *OD&-. Below +igre shows the
seSence of events involved in a remote procedre call. The mechanisms and semantics of
%#C"&CCs are discssed in mch greater detail in &+C 1L.15 &+C 1L.2 and &+C 1L...
2A
A! ,b+e%t-,"ie!ted A''"$%-
As mentioned5 &CCs follow the traditional fnctional model. 3tate information may need to
be maintained independently at both client and server *depending on the type of application-.
This data is often repeatedly re"transferred across the networ$ on each remote fnction call.
An alternative architectre is to se techniSes from ob0ect"oriented development and to
partition the system into a set of independent ob0ects. 6ach ob0ect is responsible for
maintaining its own internal state information.
By sing an ob0ect"oriented approach to networ$ software development5 we can promote
certain beneficial traits in or code:
6ncapslation: ensring a clear separation between the interfaces *throgh which the
ob0ects in yor system interact with one another- and their implementations
!odlarity5 scalability and e'tensibility
&e"sability *of code and5 perhaps more importantly5 of design-
8nheritance and specialiJation of fnctionality and polymorphism
The act of sending a message from one entity on a networ$ to another is remar$ably similar
to one ob0ect invo$ing a method on another ob0ect. The integration of distribted networ$
technology and ob0ect"orientation nites the featres of a basic commnications
infrastrctre with a high"level abstraction of these interfaces and a framewor$ for
.<
encapslation and modlarity\throgh this5 developing applications which inter"wor$ is
significantly more intitive.
T-e ,b+e%t 8$!$*eme!t A"%-ite%tu"e
8n 1AA15 a grop of interested parties 0oined to form the %b0ect !anagement Hrop *%!H-""
a consortim dedicated to the standardiJation of distribted ob0ect compting. The %!H
spports heterogeneity in its architectres5 providing the mechanisms for applications written
in any langage *rnning on any operating system5 any hardware platform- to commnicate
and collaborate with each other\in essence5 the development of a (software bs) to allow
for implementation diversity5 as a hardware bs does for e'pansion cards.
The %!H architectre which permits this distribted collaboration of ob0ects is called
the Object Management Architecture *%!A-. Below +igre shows the ob0ect management
architectre.
;i*u"e 2: ,b+e%t 8$!$*eme!t A"%-ite%tu"e 5,8A6
CORBAservices provide the basic fnctionality for the management of ob0ects dring their
lifetime\for e'ample5 this incldes:
#aming *niSely specifying a particlar ob0ect instance-
3ecrity *providing aditing5 athentication5 etc.-
Cersistence *allowing ob0ect instances to be (flattened) to or created from a seSence
of bytes-
Trading *providing ob0ects and %&Bs a mechanism to (advertise) particlar
fnctionality-
6vents *allows an ob0ect to dynamically register or nregister an interest in a
particlar type of event5 essentially decopling the commnication from the ob0ect-
Dife"cycle *allows ob0ects to be created5 copied5 moved5 deleted-
Common Facilities provide the framewor$s necessary for application development sing
distribted ob0ects. These framewor$s are classified into two distinct grops: horiJontal
facilities *commonly sed in all applications5 sch as ser"interface management5 information
management5 tas$ management and system management-5 and vertical facilities *related more
to a particlar indstry5 for e'ample telecommnications or health care-.
The C%&BA standard specifies an entity called the %b0ect &eSest Bro$er *%&B-5 which is
the (gle) that binds ob0ects together to enable higher"level distribted collaboration. 8t
.1
enables the e'change of C%&BA reSests between local and remote ob0ects. +igre . shows
the architectre of C%&BA. +igre B shows the invocation of methods on different remote
ob0ects via the %&B.
;i*u"e =. Cmm! ,b+e%t Re@ue&t B"Ae" A"%-ite%tu"e 5C,RBA6
;i*u"e 4. C$((i!* $ met-d 7it-i! $ &'e%i#i% b+e%t i!&t$!%e
.2
An ob0ect reference does not describe the interface of an ob0ect. Before an application can
ma$e se of an ob0ect *reference-5 it mst somehow determine/$now what services an ob0ect
provides.
8nterfaces to ob0ects are defined via the Interface Description Language *8DD-. The %!H
8DD defines the interface of an ob0ect by means of the varios methods they spport and the
parameters these methods accept. Warios langage mappings e'ist for the 8DD *for e'ample5
C5 C>>5 Java5 C%B%D5 etc.-. The generated langage stbs provide the application with
compile"time $nowledge which allows these interfaces to be accessed.
The interfaces5 alternatively5 can be added to a special database5 called the interface
repository. The interface repository contains a dynamic copy of the interface information of
an ob0ect5 which is generated statically via the 8DD. The Dynamic Invocation Interface *D88-
is the facility by which an ob0ect client can probe an ob0ect for the methods it spports and5
pon discovering a particlar method5 can invo$e it at rntime. This involves loo$ing p the
ob0ect interface5 generating the method parameters5 invo$ing the method on the remote ob0ect
and retrning the reslts.
%n the (server) side5 the Dynamic Skeleton Interface *D38- allows the %&B to invo$e ob0ect
implementations that do not have static *i.e.5 compile time- $nowledge of the type of ob0ect it
is implementing. All reSests to a particlar ob0ect are handled by having the %&B invo$e the
..
same single call"p rotine5 called the Dynamic Interface outine *D8&-. The Implementation
epository *as opposed to 8nterface &epository- is a rntime database of information abot
the classes the %&B $nows of5 its instantiated ob0ects and additional implementation
information *logging5 secrity aditing5 etc.-.
The %b0ect Adapter sits above the core %&B networ$ fnctionality. 8t acts as a mediator
between the %&B and the ob0ect5 accepting method reSests on the ob0ectYs behalf. 8t helps
alleviate (bloated) ob0ects or %&Bs. The %b0ect Adapter enables the instantiation of new
ob0ects5 reSests passing between the %&B and an ob0ect5 the assignment of ob0ect references
to an ob0ect *niSely naming the ob0ect-5 and the registering of classes of ob0ects with the
8mplementation &epository. Crrently5 all %&B implementations mst spport one ob0ect
adapter5 the !asic Object A"apter *B%A-.
This interoperability is not sefl nless %&Bs from different developers/vendors can
commnicate with one another. The #eneral InterO! $rotocol *H8%C- is a bridge
specifying standard transfer synta' and a set of message formats for the networ$ing of %&Bs.
The H8%C is independent of any networ$ transport. The Internet InterO! $rotocol *88%C-
specifies a mapping between H8%C and TCC/8C. That is5 it details how H8%C information is
e'changed sing TCC/8C connections. 8n this way5 it enables (ot"of"the"bo') interoperability
with 88%C"compatible %&Bs based on the worldYs most poplar prodct and vendor netral
networ$ transport\TCC/8C.
8u(ti-tie" Net7"A Cm'uti!*: C%&BA is an e'ample of what is termed (middleware)""a
technology that enables the separation of applications into three distinct sections *see
+igre ,-:
1. The presentation5 or ser interface5 tier
2. The bsiness logic5 or control5 tier
.. The data storage tier
;i*u"e >. T-"ee-Tie" Net7"A Cm'uti!*
.B
3. rite a COR#$ ID' Interface for the #an1 Object. +he
Interface shall contain the following Methods4
A. Balance 6nSiry
B. Deposit %peration
C. Xithdraw %peration
8mplement the 8DD 8nterface sing Java. Create a Java Client and 3erver for the above
application and se JDBC to connect to the Database.
//modle for ban$ application
modle ban$Mmod
4
interface ban$
4
string balance*in long accno5 in string pwd-2
string withdraw*in long acno5 in string pwd5 in long amt-2
string deposit*in long acno5 in string pwd5 in long amt-2
72
72
//servant class
import ban$Mmod.12
import 0ava.sSl.12
pblic class servant e'tends Mban$8mplBase
4
Connection con2
Crepared3tatement ps2
&eslt3et rs5rsn2
pblic servant*- throws 6'ception
4
Class.for#ame*;sn.0dbc.odbc.Jdbc%dbcDriver;-2
con:Driver!anager.getConnection*;Jdbc:%dbc:ban$corba;-2
7
pblic 3tring balance*int acno53tring pwd-
4
3tring str:;#9DD;2
try
4
ps:con.prepare3tatement*;select 1from ban$ where acno:P;-2
ps.set8nt*15acno-2
.,
rs:ps.e'ecteUery*-2
if*rs.ne't*--
4
3tring pw:rs.get3tring*;pwd;-2
if**pw.compareTo*pwd--::<-
str:;Walid 9ser5balance is:;>rs.get8nt*;bal;-2
else
str:;8nvalid Cassword;2
7
7
catch*6'ception e-
4
3ystem.ot.println*;6'ception &aised....;-2
7
retrn str2
7
pblic 3tring withdraw*int acno53tring pwd5 int amt-
4
3tring str:;#9DD;2
int bal5amont2
try
4
ps:con.prepare3tatement*;select 1from ban$ where acno:P;-2
ps.set8nt*15acno-2
rs:ps.e'ecteUery*-2
while*rs.ne't*--
4
3tring pw:rs.get3tring*;pwd;-2
if**pw.compareTo*pwd--::<-
4
bal:rs.get8nt*;bal;-2
amont:bal"amt2
if*amont=:1<<<-
4
str:;Walid 9ser: Accont 9nder+low: cant
Xithdraw;2
7
else
4
ps:con.prepare3tatement*;pdate ban$ set bal:P
where acno:P;-2
ps.set8nt*15amont-2
ps.set8nt*25acno-2
ps.e'ecte9pdate*-2
.I
ps:con.prepare3tatement*;select 1from ban$
where acno:P;-2
ps.set8nt*15acno-2
rsn:ps.e'ecteUery*-2
if*rsn.ne't*--
4
amont:rsn.get8nt*;bal;-2
str:;Walid 9ser: padated balance
is;>amont2
7
7
7
else
4
str:;8nvalid 9ser:;2
7
7
7
catch*6'ception e-
4
3ystem.ot.println*;6'ceptin raised in Xithdraw method....;-2
7
retrn str2
7
pblic 3tring deposit*int acno53tring pwd5 int amt-
4
3tring str:;#9DD;2
int bal5amont2
try
4
ps:con.prepare3tatement*;select 1from ban$ where acno:P;-2
ps.set8nt*15acno-2
rs:ps.e'ecteUery*-2
while*rs.ne't*--
4
3tring pw:rs.get3tring*;pwd;-2
if**pw.compareTo*pwd--::<-
4
3ystem.ot.println*; ser details validated;-2
bal:rs.get8nt*;bal;-2
amont:bal>amt2
.K
ps:con.prepare3tatement*;pdate ban$ set bal:P where
acno:P;-2
ps.set8nt*15amont-2
ps.set8nt*25acno-2
ps.e'ecte9pdate*-2
ps:con.prepare3tatement*;select 1from ban$ where
acno:P;-2
ps.set8nt*15acno-2
rsn:ps.e'ecteUery*-2
while*rsn.ne't*--
4
str:;new balance is:;>rsn.get8nt*;bal;-2
3ystem.ot.println*;str in Dep:;>str-2
7
7
else
4
str:;8nvalid 9ser:;2
7
7
7
catch*6'ception e-
4
3ystem.ot.println*;6'ceptin raised in Deposit method....;-2
7
retrn str2
7
7
//server class
import ban$Mmod.12
import org.omg.C%&BA.12
import org.omg.Cos#aming.12
import org.omg.Cos#aming.#amingConte'tCac$age.12
pblic class server
4
pblic static void main*3tring args?@- throws 6'ception
4
%&B orb:%&B.init*args5nll-2
org.omg.C%&BA.%b0ect o2
servant bs:new servant*-2
.L
orb.connect*bs-2
o:orb.resolveMinitialMreferences*;#ame3ervice;-2
#amingConte't ncref:#amingConte'tNelper.narrow*o-2
#ameComponent nc:new #ameComponent*;ob0;5; ;-2
#ameComponent?@ path:4nc72
ncref.rebind*path5bs-2
0ava.lang.%b0ect ob0:new 0ava.lang.%b0ect*-2
synchroniJed*ob0-
4
ob0.wait*-2
7
7
7
,ut'ut:
/:B$&mte%-B-meBC,RBABb$!AC%d b$!A2md
/:B$&mte%-B-meBC,RBABb$!ABb$!A2mdC+$)$% D.+$)$
Nte: 2b$!AIm'(B$&e.+$)$ u&e& u!%-e%Aed " u!&$#e 'e"$ti!&.
Nte: Re%m'i(e 7it- -E(i!t:u!%-e%Aed #" det$i(&.
/:B$&mte%-B-meBC,RBABb$!ABb$!A2mdCdi"
V(ume i! d"i)e / i& Ne7 V(ume
V(ume Se"i$( Numbe" i& =C/;-406>
/i"e%t"y # /:B$&mte%-B-meBC,RBABb$!ABb$!A2md
07F1GF2011 01:== P8 H/IRC .
07F1GF2011 01:== P8 H/IRC ..
07F26F2011 10:26 A8 1G7 b$!A.%($&&
07F1GF2011 12:4I A8 =11 b$!A.+$)$
07F26F2011 10:26 A8 20=I= b$!AHe('e".%($&&
07F1GF2011 12:4I A8 20==1 b$!AHe('e".+$)$
07F26F2011 10:26 A8 I61 b$!AH(de".%($&&
07F1GF2011 12:4I A8 762 b$!AH(de".+$)$
07F26F2011 10:26 A8 2>I b$!A,'e"$ti!&.%($&&
07F1GF2011 12:4I A8 40I b$!A,'e"$ti!&.+$)$
07F26F2011 10:26 A8 20111 2b$!AIm'(B$&e.%($&&
07F1GF2011 12:4I A8 20622 2b$!AIm'(B$&e.+$)$
07F26F2011 10:26 A8 20G60 2b$!AStub.%($&&
07F1GF2011 12:4I A8 =0G21 2b$!AStub.+$)$
12 ;i(e5&6 1G012> byte&
2 /i"5&6 =60=0=01100144 byte& #"ee
.A
/:B$&mte%-B-meBC,RBABb$!ABb$!A2mdC%d ..
/:B$&mte%-B-meBC,RBABb$!AC+$)$% D.+$)$
/:B$&mte%-B-meBC,RBABb$!AC&t$"t "bd
/:B$&mte%-B-meBC,RBABb$!AC&t$"t +$)$ &e")e"
/:B$&mte%-B-meBC,RBABb$!AC+$)$ %(ie!t
Se(e%t A!y ,!e # t-e ,'e"$ti!...
1. B$($!%e E!@ui"y....
2. /e'&it.....
=. Jit-d"$7....
E!te" yu" %-i%e....
1
E!te" t-e $%%u! !umbe" t A!7 t-e b$($!%e....
100
E!te" t-e P$&&7"d......
$b%d
Re&u(t i& V$(id :&e"0b$($!%e i&:24000
/ yu 7i&- t %!ti!ue......4FN
ye&
Se(e%t A!y ,!e # t-e ,'e"$ti!...
1. B$($!%e E!@ui"y....
2. /e'&it.....
=. Jit-d"$7....
E!te" yu" %-i%e....
2
T-e 'e"$ti! %-&e! i&...../e'&ite...
E!te" t-e $%%u!t !umbe"....
200
E!te" t-e P$&&7"d......
e#*-
E!te" t-e $mu!t t be de'&ited......
1000
/e'&it 8et-d: !e7 b$($!%e i&:7000
/ yu 7i&- t %!ti!ue......4FN
ye&
Se(e%t A!y ,!e # t-e ,'e"$ti!...
1. B$($!%e E!@ui"y....
2. /e'&it.....
=. Jit-d"$7....
E!te" yu" %-i%e....
=
T-e 'e"$ti! %-&e! i&.....Jit-d"$7(...
B<
E!te" t-e $%%u!t !umbe"....
1
E!te" t-e P$&&7"d......
$b%d
E!te" t-e $mu!t t be 7it-d"$7!......
1000
T-e 'e"$ti! %-&e! i&..... Jit-d"$7(....
Jit-d"$7 met-d:N:11
/ yu 7i&- t %!ti!ue......4FN
!
B1

Vous aimerez peut-être aussi