Chapter 1. Welcome to EJB
Table of Contents Chapter 1. Welcome to EJB.......................................................................... 1 Section 1.1. OBJECTIVES............................................................................................................................................................... 2 Section 1.2. What is EJB all about?................................................................................................................................................. 3 Section 1.3. What does EJB really give me?................................................................................................................................... 4 Section 1.4. No more vendor lock-in!............................................................................................................................................. 5 Section 1.5. There are no Dumb Questions.................................................................................................................................... 6 Section 1.6. How does it all work?................................................................................................................................................... 7 Section 1.7. Behind the scenes........................................................................................................................................................ 8 Section 1.8. Sharpen your pencil................................................................................................................................................... 10 Section 1.9. Beans come in three flavors....................................................................................................................................... 11 Section 1.10. Session beans can be stateless or stateful................................................................................................................ 12 Section 1.11. There are no Dumb Question................................................................................................................................... 12 Section 1.12. Sharpen your pencil.................................................................................................................................................. 13 Section 1.13. Overheard at! THE TIKIBEAN LOUNGE................................................................................................................ 14 Section 1.14. Example: The Advice Guy bean................................................................................................................................ 15 Section 1.15. Five things you do to build a bean:.......................................................................................................................... 16 Section 1.16. EJB Roles and Responsibilities............................................................................................................................... 26 Section 1.17. Tutorial:.................................................................................................................................................................... 28 Section 1.18. Organize your project directory............................................................................................................................... 29 Section 1.19. Compile the two interfaces and the bean class....................................................................................................... 30 Section 1.20. Start the server......................................................................................................................................................... 31 Section 1.21. You'll see something like this................................................................................................................................... 31 Section 1.22. Start deploytool....................................................................................................................................................... 32 Section 1.23. you'll see something like this................................................................................................................................... 32 Section 1.24. Make a new Application.......................................................................................................................................... 33 Section 1.25. There are no Dumb Questions................................................................................................................................ 33 Section 1.26. Name and save the new application........................................................................................................................ 34 Section 1.27. What you'll see af ter you create and name the application................................................................................... 35 Section 1.28. Now let's make the new enterprise bean (the ejb-jar and the DD)........................................................................ 36 Section 1.29. Now we're in the really cool New Enterprise Bean Wizard.................................................................................... 37 Section 1.30. Create the new ejb-jar............................................................................................................................................. 38 Section 1.31. Add the three class files (including their package directory) to the JAR............................................................... 39 Section 1.32. Confirm that you added ONLY the package directory and the class files.............................................................. 40 Section 1.33. Make it a Stateless Session bean.............................................................................................................................. 41 Section 1.34. Tell it which of the three class files in the JAR is the actual BEAN class............................................................... 42 Section 1.35. Tell it which is the Home interface, and which is the Component interface......................................................... 43 Section 1.36. Verify everything on this screen!............................................................................................................................ 44 Section 1.37. You're done, click Finish.......................................................................................................................................... 45 Section 1.38. Meanwhile back on the main deploytool screen.................................................................................................... 46 Section 1.39. Run your bean through the deploytool verifier...................................................................................................... 47 Section 1.40. Close your eyes and click OK.................................................................................................................................. 48 Section 1.41. Whew! No failed tests.............................................................................................................................................. 49 Section 1.42. Time to Deploy........................................................................................................................................................ 50 Section 1.43. Make it Return a Client Jar...................................................................................................................................... 51 Section 1.44. Give it a name, so clients can look it up.................................................................................................................. 52 Section 1.45. Watch the progress bars go up, then celebrate....................................................................................................... 53 Section 1.46. Now you'll see the AdviceApp inside the server..................................................................................................... 54 Section 1.47. Now all we need is a client....................................................................................................................................... 55 Section 1.48. Organizing your project directory for the client..................................................................................................... 56 Section 1.49. The Client Code (AdviceClient.java)....................................................................................................................... 57 Section 1.50. Run the client!......................................................................................................................................................... 58 Section 1.51. COFFEE CRAM........................................................................................................................................................ 59 Section 1.52. COFFEE CRAM....................................................................................................................................................... 60
Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID:
[email protected] Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.
Chapter 1. Welcome to EJB
Return to Table of Contents
Page 1
Intro to EJB
5
4 :HOFRPHWR(-%
4
@DUJV ^ QHZ$GYLFH&OLHQW JR ` InitialContext
is our entry point ming service, into the JNDI na SXEOLFYRLGJR ^ we do the lookup. ere wh DI WU\^ e bean using the JN &RQWH[WLF QHZ,QLWLDO&RQWH[W . Lookup the Aditvicdu ent ym plo de g rin 2EMHFWR LFORRNXS³$GYLVRU´ name we gave
$GYLFH+RPHKRPH $GYLFH+RPH 3RUWDEOH5HPRWH2EMHFWQDUURZR$GYLFH+RPHFODVV $GYLFHDGYLVRU KRPHFUHDWH Just go with it for now 6\VWHPRXWSULQWOQDGYLVRUJHW$GYLFH make perfect sense a litt. ItÕll all `FDWFK([FHSWLRQH[ ^ le later. H[SULQW6WDFN7UDFH ` Finally we get to the act ` The method declared in ual business method! ` the component
(Advice) and implement
erface ed in the bean claint ss.
&RPSLOHWKHFOLHQW )LOH(GLW:LQGRZ+HOS$OO7KLV)RU-XVW7KDW
FGaSURMHFWVDGYLFH
The client JAR Þle
The client class
MDYDFFODVVSDWK^&/$663$7+`$GYLFH$SS&OLHQWMDU$GYLFH&OLHQWMDYD 4HECLIENTNEEDSACCESSTOTHETWOINTERFACES!DVICE !DVICE(OME ANDTHETWOSTUB CLASSESTHATIMPLEMENTTHOSEINTERFACES4HEYREBOTHINTHECLIENT*!2THESERVERMADE BUTWECANTCOMPILETHECLIENTWITHOUTTHEM4HECLEANESTWAYISTOADDTHEMTOTHE CLASSPATHATCOMPILE TIME USINGTHEFODVVSDWKCOMPILERmAG
\RXDUHKHUH
Chapter 1. Welcome to EJB Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID:
[email protected] Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.
Chapter 1. Welcome to EJB
Return to Table of Contents
Page 58
UXQQLQJWKHFOLHQW
5XQWKHFOLHQW )LOH(GLW:LQGRZ+HOS$OO7KLV)RU-XVW7KDW
FGaSURMHFWVDGYLFH MDYDFS^&/$663$7+`$GYLFH$SS&OLHQWMDU$GYLFH&OLHQW !TRUNTIME THECLIENTSTILLNEEDSACCESSTOTHETWOINTERFACES!DVICE !DVICE(OME ANDTHETWOSTUBCLASSESTHATIMPLEMENTTHOSEINTERFACES 4HEYREBOTHINTHECLIENT*!2THESERVERMADE WEHAVETOADDTHEMTOTHE CLASSPATH4HEBESTWAYISTOUSETHEFSCOMPILERmAG .OTE&ORNOW THECLIENTMUSTBEONTHESAMEPHYSICALMACHINEASTHESERVER ,ATER WELLSEEHOWTOCHANGETHIS ANDRUNTHECLIENTAGAINSTA*%%SERVERON ADIFFERENTMACHINE
UROOVH\HV@)LUVW LIWKHVHFXULW\FKHFNVDUHFRGHGLQWRPHWKHQ,·PQRW DVUHXVDEOH7KHZKROHSRLQWRI(-%LVWRFRQÀJXUHDQG FXVWRPL]HEHDQVDWUXQWLPHZLWKRXWUHZULWLQJWKHFRGH ,I,·YHJRWVHFXULW\SURJUDPPLQJLQP\-DYDFRGHWKHQ LWFDQ·WEHFKDQJHGZLWKRXWWRXFKLQJWKHVRXUFH$QG ZKRZDQWVWKDW"
(-%2EMHFW³ZDLW³E\SHUVLVWHQWVWRUHGRQ·W\RXMXVW PHDQ'$7$%$6(":K\GRQ·W\RXMXVWVD\GDWDEDVH"
(-%2EMHFW%HFDXVH %HDQ%HFDXVH,KDYHWRORDGLQ7RP'XII·V LQIRUPDWLRQIURPWKHGDWDEDVHÀUVW (-%2EMHFW%HFDXVH %HDQ%HFDXVH,·GUHWXUQDQDGGUHVVWKDWPLJKWQRW HYHQEHYDOLG8QOHVV,·PVWLOOLQDSUHYLRXVWUDQVDFWLRQ ZLWKWKLVFOLHQWWKHQWKHVHUYHUKDVWRWHOOPHWR ORDGP\VHOIZLWK7RP'XII·VGDWDEDVHLQIR%()25( WKHVHUYHUWHOOVPHWRUXQWKHJHW$GGUHVV PHWKRG 2WKHUZLVHZKRNQRZVZKDW,·GUHWXUQ"2.ZHOOWKDW·V ODVWFDOOVRZH·OOKDYHWRFRQWLQXHWKLVVRPHRWKHUWLPH \RXDUHKHUH
Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID:
[email protected] Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.
Chapter 2. EJB Architecture
Return to Table of Contents
Page 31
(-%DUFKLWHFWXUH
there are no
Dumb Questions
Q:
(OWANDWHENDOESTHE CONTAINERCREATETHE%*"/BJECTAND THE(OMEANDTHESTUBS
A:
7HENYOUDEPLOYABEAN THECONTAINERLOOKSATTHE$$AND TAKESITFROMTHERE2EMEMBER THE $$GIVESTHEFULLYQUALIFIEDNAMEOF YOUR2EMOTE#OMPONENT%*"/BJECT INTERFACEANDYOUR2EMOTE(OME INTERFACE3O ONCETHECONTAINERGETS THOSEINTERFACES ITGENERATESCODE FORTHETWOCLASSESIMPLEMENTING THOSEINTERFACES!NDBECAUSETHEYRE 2EMOTE THECONTAINERALSOCREATES THECLIENTSTUBSTHATKNOWHOWTO COMMUNICATEBACKTOTHE2EMOTE OBJECTS
Q:
!RETHESEALWAYSPLAINOLD2-) STUBS)SEETHATWHENWEDEPLOYED USINGTHE2) ITPRINTEDOUTAMESSAGE THATITWASRUNNINGRMIC
A:
4HECONTAINERCANDOWHAT EVERITWANTSTOCREATETHESTUBSTHE REQUIREMENTISTHATTHESTUBSBE2-) ))/0COMPLIANT)NFACT ASERVERCAN USESOMETHINGCALLEDDYNAMICPROXIES TOIMPLEMENTTHESTUBFUNCTIONAL ITY BUTWEDONTCARE7HENWESAY hSTUBvWEMEANSOMETHINGWITHSTUB BEHAVIOR7HETHERITSAN2-)STUBOR SOMETHINGELSE ISANIMPLEMENTATION DETAILFORTHESERVER7ELLLOOKATTHAT INMOREDETAILINTHENEXTCHAPTER4HE BOTTOMLINEISTHATYOUREALLYDONT KNOWWHATTHESTUBCLASSCODELOOKS LIKE&ORTHATMATTER YOUREALLYDONT KNOWHOWTHE%*"/BJECTANDTHE (OMEAREIMPLEMENTED9OURENOT SUPPOSEDTOKNOW 9OUMIGHTHAVEAN%*"CONTAINERTHAT
LETSYOUVIEWPOSSIBLYEVENHACK THEGENERATEDSOURCECODE BUTDONT COUNTONIT!NDIFWEWEREYOU WE JUSTWOULDNTGOTHERE EVENIFWE COULD
Q:
3OTHESECLASSESAREUPTOTHE VENDORSIMPLEMENTATION
A:
9ES4HEVENDORHASALLSORTS OFCHOICESFORIMPLEMENTINGTHESE CLASSESANDMIGHTUSETHESTUBSAND OR(OMEAND%*"/BJECTSTOGETSOME PERFORMANCEADVANTAGES"UTAGAIN THATSNOTUPFORYOUTOMESSWITH OR EVENKNOWABOUT 4HEONLYREQUIREMENTINTHESPEC THATYOUREGUARANTEEDANDREQUIRED TOADHERETO ISTHAT2EMOTEOBJECTS MUSTFOLLOWTHERULESFOR2-) ))/0 WHICHMEANS*AVAS2EMOTE-ETHOD )NVOCATIONUSINGTHE))/0#/2"! STANDARD WIREPROTOCOL
Q:
9OUBROUGHTITUPHOWIS2-) ))/0DIFFERENTFROMREGULAR2-)
A:
0LAINOLD2-)USES*2-0ASITS WIREPROTOCOL"UT))/0LETS2EMOTE OBJECTSINTEROPERATETHROUGH#/2"! WEWONTBESAYINGMUCHATALLABOUT #/2"!INTHISBOOKITSDEFINITELYOUT OFSCOPEFORTHEEXAMANDTHEBOOK EXCEPTINAFEWTINYCASESWELLSEE SCATTEREDTHROUGHOUTTHECHAPTERS 4HATGIVESYOUROBJECTSACHANCETOBE ACCESSED FOREXAMPLE BYEVENNON *AVACLIENTS/NETHING))/0SPECIFIESIS THEWAYINFORMATIONFORTRANSACTIONS ANDSECURITYCANBEPROPAGATED ALONGWITHTHEMETHODCALL ANDYOUR CONTAINERMIGHTBETAKINGADVANTAGE OFTHAT
&ORTHEMOSTPART YOULLBARELYNOTICE THEDIFFERENCEBETWEENPLAINOLD2-) AND2-) ))/0"UTTHEREAREACOUPLE OFPLACESWHEREITSDIFFERENT ANDONE OFTHESEDIFFERENCESISDEFINITELYON THEEXAMTHENEEDTONARROWASTUB 7ELLCOVERNARROWINGINDETAILINTHE NEXTCHAPTERON#LIENT6IEW&ORNOW JUSTKNOWTHATITSSOMETHINGACLIENT MUSTDOWITHAN%*"STUBTHATTHEY DONTHAVETODOWITHAPLAIN*AVA STUBBECAUSETHE%*"SPECTELLSYOUTO ASSUMETHATTHESTUBISUSING))/0AND THUSMIGHTBEADIFFERENTKINDOFSTUB
Q:
7HENAREWEGOINGTOTALK ABOUTTHE(OME
A: Q:
.EXTPAGE
7HYDIDYOUTAKESOLONG )SNTTHE(OMEIMPORTANT
A:
!SCRUCIALASTHE(OMEIS ITSUSUALLYJUSTTHEWAYYOUGET AREFERENCETOSOMETHINGTHAT IMPLEMENTSTHE#OMPONENTINTERFACE )NOTHERWORDS YOUUSETHE(OMETO GETAN%*"/BJECTSTUBFOR2EMOTE CLIENTS WHICHISALLWEVETALKEDABOUT SOFAR &OR%NTITYBEANS THE(OMECANHAVE AMOREIMPORTANTROLE ANDWELLSEE THAT BUTEVENWITH%NTITYBEANS THE (OMESPRIMARYUSEISSTILLTOGET %*"/BJECTSTUBS!FTERTHAT MOSTOF THECOMMUNICATIONBETWEENTHE CLIENTANDTHEBEANCOMESTHROUGH THE%*"/BJECTANDNOTTHE(OME -OSTOFTHETIME INFACT CLIENTSUSE THE(OMEJUSTTOGETTHE%*"/BJECT REFERENCE ANDTHENTHE(OME REFERENCEISTOSSEDOUT NOTNEEDED
&KDSWHU
Chapter 2. EJB Architecture Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID:
[email protected] Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.
Chapter 2. EJB Architecture
Page 32
Return to Table of Contents
DUFKLWHFWXUDORYHUYLHZ
@DUJV ^ QHZ$GYLFH/RFDO&OLHQW JR ` SXEOLFYRLGJR ^ 2EMHFWR QXOO WU\^ &RQWH[WLF QHZ,QLWLDO&RQWH[W R LFORRNXS³$GYLVRU/RFDO´
e lookup the DI, and do the to a real JN h ug ro th to go ferenc You still have ly this time you get a cere of EJBLocalHome), usual way, onon the heap (an instan Home object.. Java object a stub to a Remote EJB instead of
`FDWFK1DPLQJ([FHSWLRQQH[ ^ QH[SULQW6WDFN7UDFH ` $GYLFH+RPH/RFDOKRPH $GYLFH+RPH/RFDO R $GYLFH/RFDODGYLVRU QXOO WU\^ DGYLVRU KRPHFUHDWH `FDWFK&UHDWH([FHSWLRQFH[ ^ FH[SULQW6WDFN7UDFH `
od still declares a ception The create() methbut not a RemoteEx , ion CreateExcept
6\VWHPRXWSULQWOQDGYLVRUJHW$GYLFH ` `
HereÕs a big ch ge! No narrowing old cast (we stan ! Just a pla return type of illloohave to cast because the in ku p is Object, t we have to narrow it donÕt since it isnÕt a bu stub.)
The business method call. method call is no longer and since getAJust a normal local met a Remote hod call, dvice() doesnÕ excpetions, th t de have to be w e business method caclllare any rapped in a tr do y/catch. esnÕt
&KDSWHU
Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID:
[email protected] Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.
Chapter 3. Exposing Yourself
Return to Table of Contents
Page 51
WKHFOLHQWYLHZ
YiX`e gfn\i ?PI\PI[\WKPIVOMQV[QLM\PMJMIVKTI[[' 7EVESEENHOWTHEINTERFACESCHANGE ANDHOWTHECLIENTCODEHASTO CHANGE WHENYOUGOFROMA2EMOTETOLOCALCLIENTVIEW"UTWHATABOUTTHE BEANCLASSITSELF7HATDOYOUTHINK$OESTHEBEANCODENEEDTOCHANGE IFYOUREGOINGTODEPLOYITWITHALOCALCLIENTVIEWINSTEADOFA2EMOTECLIENT VIEW7HATIFYOUPLANTODEPLOYITWITHBOTHALOCALAND2EMOTECLIENTVIEW &ORNOW LETSASSUMETHATTHEONLYMETHODTHATMATTERSISTHEBEANS BUSINESSMETHOD(ERESHOWITLOOKSINTHEORIGINALBEANCLASS SXEOLF6WULQJJHW$GYLFH ^ 6\VWHPRXWSULQWOQ³LQJHWDGYLFH´ LQWUDQGRP LQW 0DWKUDQGRP DGYLFH6WULQJVOHQJWK UHWXUQDGYLFH6WULQJV>UDQGRP@ `
$OYOUSEEANYTHINGINTHATMETHODTHATLOOKSSPECIlCTOA2EMOTECLIENT VIEW7OULDYOUNEEDTODOANYTHINGDIFFERENTWITHALOCALCLIENT .O DONTTHINKSO !NYTHINGTHATWORKSASARETURNTYPEORARGUMENTFORA2EMOTEMETHODIS GUARANTEEDTOWORKFORALOCALMETHODASWELL SOWERE/+THERE+IND OFANO BRAINERWHENTHERETURNTYPEIS3TRING THOUGH /+ THEREISONE EXCEPTIONREMEMBER ACCORDINGTO"EANLAWYOUMUSTNOTRETURNABEANS 2EMOTEINTERFACEFROMALOCALINTERFACEMETHOD 3OITLOOKSLIKEATLEASTWITHTHISBEAN WESHOULDNEVERHAVETOKNOWOR CARE7ESHOULDBEABLETODEPLOYTHEBEANASWRITTEN ANDTHEBEANSHOULD BEKEPTUNAWAREOFWHETHERITSCLIENTSARE2EMOTEORLOCAL 3OUNDSGOOD DOESNTIT3IMPLE CLEAN OBJECT ORIENTED "UTTHINKABOUTITSOMEMORE)MAGINEABEANWITHMORECOMPLEXLOGIC -OREBUSINESSMETHODS!RGUMENTSTOTHOSEMETHODS!RGUMENTSTHE METHODMIGHTEVENNEEDTOACTON (MMMMMCANYOUTHINKOFANYTHINGTHATTHEBEANMIGHTWANTTOTREAT DIFFERENTLY IFITKNEWTHECLIENTWERELOCALINSTEADOF2EMOTE
\RXDUHKHUH
Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID:
[email protected] Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.
Chapter 3. Exposing Yourself
Return to Table of Contents
Page 52
SDVVLQJREMHFWVORFDOO\
:DLWDPLQXWH-DYD SDVVHVREMHFWVORFDOO\E\SDVVLQJ DFRS\RIWKHREMHFWUHIHUHQFHQRW WKHREMHFWLWVHOI%XWZHNQRZWKDW 5HPRWHPHWKRGDUJXPHQWVDQGUHWXUQ YDOXHVDUHSDVVHGDVD6HULDOL]HG FRS\RIWKHDFWXDOREMHFW
,QRUGLQDU\ORFDOPHWKRGFDOOV-DYDSDVVHV DQREMHFWUHIHUHQFHE\YDOXHDVDFRS\RIWKH UHIHUHQFHYDULDEOH 7KHREMHFWLWVHOILVQHYHUSDVVHG %XWZLWK5HPRWHFDOOVWKHREMHFWLWVHOILVFRSLHG :LWK5HPRWHFDOOVWKHFDOOHGPHWKRGLVDOZD\V ZRUNLQJRQDFRS\RIWKHFDOOHU¶VREMHFW :LWKORFDOFDOOVWKHFDOOHGPHWKRGLVDOZD\VZRUNLQJ ZLWKWKHFDOOHU¶VRULJLQDOREMHFW²QRWDFRS\
&KDSWHU
Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID:
[email protected] Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.
Chapter 3. Exposing Yourself
Return to Table of Contents
Page 53
WKHFOLHQWYLHZ
)ZO]UMV\[\W:MUW\M^[TWKITUM\PWL[ /2&$/PHWKRGFDOO 'RJ REMHFW
'RJ¿GR QHZ'RJ
WKLVWUDLQ3HW¿GR ^a\g
a copy of the reference (the remote control) not the Dog object
'RJ
%()25(GRJ UHIHUHQFH
YRLGWUDLQ3HW'RJDUJ ^`
'RJ REMHFW
r and the ÔÞdoÕ Now the ÔargÕ paramel te ies. Both cop ica nt ide variable are object. g Do e reference the sam
Yj_ ^a\g 'RJ
%()25(GRJ UHIHUHQFHV
'RJ
5(027(PHWKRGFDOO 'RJ¿GR QHZ'RJ UHPRWH6WXEWUDLQ3HW¿GR
'RJ REMHFW
ue! NOT the reference val ItÕs a serialized copy tof the actual Dog objec
'RJ REMHFW ^a\g 'RJ
%()25(GRJ UHIHUHQFH
y The server Dog is a cop g Do nt clie e th of
YRLGWUDLQ3HW'RJDUJ ^`
'RJ REMHFW
'RJ REMHFW
^a\g 'RJ
Yj_
&OLHQW
$)7(5GRJV UHIHUHQFHV
'RJ
6HUYHU \RXDUHKHUH
Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID:
[email protected] Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.
Chapter 3. Exposing Yourself
Return to Table of Contents
Page 54
5HPRWHYVORFDO
WKHUH DUH QR
'XPE 4XHVWLRQV
Q:
3O )MSTILLNOTCLEARIFTHE BEAN-534ALWAYSKNOWIFTHECLIENT IS2EMOTEORLOCAL
A:
)TSNOTTHATTHEBEANMUST BUTRATHERTHATTHEBEANMIGHTHAVE TOKNOW)FITMATTERSTHATTHEBEAN ISWORKINGONATHECALLERSOBJECT VIAACOPYOFTHECALLERSREFERENCE ASOPPOSEDTOACOPYOFTHECALLERS OBJECT YOURBEANCODEMIGHTHAVE TOCHANGE!NDTHATGOESFORRETURN VALUESTOO)FITMATTERSTHATTHECALLING METHODGETSBACKACOPYOFAREFER ENCEVSACOPYOFANOBJECT THEBEAN CODEMIGHTHAVETOCHANGE
Q:
"UT)THOUGHTTHATCHOOSINGTO DEPLOYABEANWITHLOCALVS2EMOTE CLIENTVIEWSWASJUSTAMATTEROF SWITCHINGASWITCHATDEPLOYTIME
A:
./././,ETSIMAGINETHAT YOUDIDWRITETWOSETSOFINTERFACES ONEFORALOCALCLIENTVIEWANDONE FORA2EMOTECLIENTVIEW)TISTRUE THATATDEPLOYMENTYOUCOULDDECIDE WHICHOFTHETWOVIEWSYOUWANTEDTO EXPOSEORBOTH "UTTHATWORKSONLY IFTHEBEANCODEDOESNTCAREWHERE THECLIENTIS!BEANMETHODWITHNO ARGUMENTSORRETURNVALUESMIGHTBE SAFEREGARDLESSOFHOWTHECLIENTIS ACCESSINGIT /NESOLUTIONMIGHTBETOWRITETHE BEANCODEASSUMINGTHEBEANIS ALWAYSGETTINGACOPY ANDTHENIFTHE CLIENTISLOCAL HAVETHECLIENTMAKE ACOPYCLONE OFTHEOBJECTBEFORE PASSINGIT/R FORRETURNVALUES YOU MIGHTALWAYSHAVETHEBEANMAKEA COPYBEFOREHANDINGITBACK4HATWAY
THEBEANNEVERHASTOWORRYTHATA LOCALCLIENTMIGHTBEMODIFYINGTHE BEANSOBJECT
!NDALOCALCLIENTWONTHAVECODE TOHANDLETHE2EMOTE%XCEPTIONSOR NARROWTHESTUBS
Q:
4HEBOTTOMLINEISTHATDEPLOYINGA BEANWITHA2EMOTEVSLOCALCLIENT VIEWISA"IG$EAL)TSACOMMITMENT 4HECLIENTHASTOKNOWINADVANCE
4HENITSJUSTABOUT ARGUMENTSANDRETURNVALUES)S THEREANYOTHERREASONYOUCOULDNT DEPLOYABEANANDMAKETHE DECISIONFOR2EMOTEVSLOCALVIEWAT DEPLOYTIME
A:
4HEREISANOTHERREASON4HE CLIENTCODE%VENIFTHEBEANDOESNT NEEDTOKNOWHOWITSCLIENTIS ACCESSINGIT THECLIENTMUSTKNOW! CLIENTWRITTENTOACCESSABEANLOCALLY WOULDNTWORKIFTHEBEANS2EMOTE CLIENTINTERFACES ANDVICE VERSA
Q: A:
7HYNOT
4HECLIENTMUSTKNOWIN ADVANCEWHETHERITSACCESSINGA BEANS2EMOTEORLOCALCLIENTVIEW BECAUSETHEINTERFACESTHEMSELVES AREDIFFERENT0OLYMORPHICALLY YOU CANTUSETHE2EMOTEANDLOCAL VIEWSINTERCHANGEABLY BECAUSETHE INTERFACESTHEMSELVESAREDIFFERENT 4HERESNOWAYTHECLIENTCANBE KEPTBLISSFULLYIGNORANT BECAUSE THEBEHAVIOROFTHEBEANISDIFFERENT 2EMEMBER A2EMOTECLIENTMUST HANDLE2EMOTE%XCEPTIONS AND NARROWTHE2EMOTESTUBCOMINGBACK FROMTHELOOKUP!NDA2EMOTECLIENT ISEXPOSEDTOMETHODSINTHEBEANS CLIENTINTERFACESMETHODSTHAT DONTEXISTINTHELOCALINTERFACES3OA 2EMOTECLIENTMIGHT FOREXAMPLE TRY TOCALLAGET(ANDLE METHODONTHE LOCALCOMPONENTINTERFACE AMETHOD CALLTHATWOULDNEVERWORK
Q:
#ANYOUGETAROUNDTHISBY DECLARINGYOUR2EMOTE%XCEPTIONS ONYOURLOCALINTERFACE!NDCOULD YOUMAKEANINTERFACETHATISBOTH 2EMOTEANDLOCALBYHAVING YOURCOMPONENTINTERFACELIKE !DVICE EXTENDBOTH2EMOTEAND %*",OCAL/BJECT7HATSTHEHARM IFTHECLIENTSIMPLYALWAYSHANDLES 2EMOTE%XCEPTIONS ANDALWAYSDOES THENARROW 4HATWAYTHECLIENT SHOULDNTHAVETOKNOW
A:
3TILLWONTWORK&ORONETHING ACCORDINGTOBEANLAW YOURENOT ALLOWEDTODECLARE2EMOTE%XCEPTIONS ONLOCALMETHODS3OTHERESNO GUARANTEETHATYOURSERVERWOULD EVENLETYOUDEPLOYABEANWITH ALOCALINTERFACETHATDECLARES 2EMOTE%XCEPTIONS!NDTHEREISNO GUARANTEETHATTHENARROW METHOD WOULDNOTCAUSEPROBLEMS!NDTHEN THEREAREHANDLESANDALLTHATOTHER STUFF9OUNEEDTOJUSTLETTHISGO
&KDSWHU
Chapter 3. Exposing Yourself Head First EJB™ By Bert Bates, Kathy Sierra ISBN: 0596005717 Publisher: O'Reilly Prepared for Linda Martin, Safari ID:
[email protected] Print Publication Date: 2003/10/01 User number: 896963 Copyright 2008, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.
Chapter 3. Exposing Yourself
Return to Table of Contents
Page 55
WKHFOLHQWYLHZ
%(WKH&RQWDLQHU (DFKRIWKHFRGHVQLSSHWVRQWKLVSDJHUHSUHVHQWVFRGHIURPHLWKHU DQLQWHUIDFHRUDFOLHQW