Aspect-Oriented Software Design Motivating the Problem What does OO Offer? When and How does OO Fail? Looking at the Problem Large Scale Aspect-Oriented Software Design (AOSD) Concepts, Ideas, and Benefits Aspect-Oriented Programming (AOP) and AspectJ From Design to Programming The AspectJ AOP Model Examples to Illustrate Key Ideas Concluding Remarks on AOSD/AOP Security UML -1 Recall: What does OO Offer? Classes are a Very Good Technique to Modularize the Components of the System Abstraction Classes and Objects Usually Represent Concepts from the Real World, Abstracting Unnecessary Details. Encapsulation Classes Encapsulate their Behavior into Manageable Units, by Hiding their Implementation to the Clients Classification Hierarchies Provide a Good way to Organize and Classify Classes Security UML -2 Classic OO program Survey Management example Concerns are Separated into Different Classes class Survey_List { public void Add_Survey_Header(Survey_Header sh) { } public List Survey_Title_Search(String title) { } public void Update_Survey_List() { } public void Delete_Survey_Header(Survey_Header sh) { } } class Survey_Header { public void Create_Survey_Header() { } public void Add_Question(String q) { } public void Add_Special_Question(String sq) { } } class Public_Survey_Results { public List Get_General_Statistics() { } public List Get_Questions() { } } Security UML -3 When and How does OO fail? The Object-Oriented Paradigm Provides the Class as the Main unit to Decompose a Software System A Good Designer can Decompose the Main Concerns of an Application into Classes and Hierarchies However, no Matter how Good is the Design there are still Concerns that Cannot be Well Modularized Sound Modeling for One Concern (GUI) may Lead to Poor Modeling for Another Concern (Database) This Leads to: Scattering – Code for Concern is Distributed Tangling – Code for Multiple Concerns is Present in Each Abstraction (Class, Method, etc.) Security UML -4 Survey Management Application A Visual Example Access-control Code is Scattered Through Both Classes Each Class has Tangled both Access-control Code and Domain-specific Code public void Survey_Title_Search(String title) { if (user.canSearch()){ slist.search(title); ... } public void public void Add_Special_Question(String sq) { if (user.canAddSpecialQuestion()){ Question_List.add(sq); ... } Security UML -5 What Occurs at Larger Scale? Consider Apache Tomcat as a Set of Abstractions that Represents its Broad Functionality for: XML Parsing UML Pattern Matching Logging of User Activity Many Other Capabilities Represented Pictorially as: Security UML -6 An Example of Good Modularity XML Parsing in org.apache.tomcat Red shows Relevant lines of code Nicely Located in one box – Well Defined © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -7 Another Example of Good Modularity URL Pattern Matching in org.apache.tomcat Red shows Relevant lines of code Nicely Located in two boxes – (using inheritance) © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -8 An Example of Poor Modularity Logging in org.apache.tomcat Red Shows lines of code Handle Logging Not in just one place Not even in a small number of places Just a Mess… © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -9 Another Example of Poor Modularity … ApplicationSession /* * ==== ===== ==== ===== ===== ===== ===== ==== ===== ===== ===== ===== ==== ===== == * * The Apach e So ftwar e Lic ense, Vers ion 1.1 * * Copy right (c) 1999 The Apach e Sof twar e Fou ndati on. All r ight s * rese rved. * * Redi strib utio n and use in so urce and binar y for ms, w ith o r wi thout * modi ficat ion, are permi tted provi ded that the f ollow ing c ondi tions * are met: * * 1. R edist ribu tions of s ource code mus t ret ain t he ab ove c opyr ight * n otice , th is li st of cond ition s an d the foll owing disc laim er. * * 2. R edist ribu tions in b inary form mus t rep roduc e the abov e co pyrig ht * n otice , th is li st of cond ition s an d the foll owing disc laim er in * t he do cume ntati on an d/or other mat erial s pro vided with the * d istri buti on. * * 3. T he en d-us er do cumen tatio n inc lude d wit h the redi strib utio n, if * a ny, m ust inclu de th e fol lowin g ac knowl egeme nt: * "Th is p roduc t inc ludes soft ware deve loped by t he * Ap ache Soft ware Found ation (ht tp:// www.a pache .org/ )." * A ltern atel y, th is ac knowl egeme nt m ay ap pear in th e sof twar e itself, * i f and whe rever such thir d-par ty a cknow legem ents norma lly appea r. * * 4. T he na mes "The Jakar ta Pr oject ", " Tomca t", a nd "A pache Sof tware * F ounda tion " mus t not be u sed t o en dorse or p romot e pro duct s derived * f rom t his softw are w ithou t pri or w ritte n per missi on. F or w ritte n * p ermis sion , ple ase c ontac t apa che@ apach e.org . * * 5. P roduc ts d erive d fro m thi s sof twar e may not be ca lled "Apa che" * n or ma y "A pache " app ear i n the ir n ames witho ut pr ior w ritt en * p ermis sion of t he Ap ache Group . * * THIS SOFT WARE IS P ROVID ED `` AS IS '' A ND AN Y EXP RESSE D OR IMPL IED * WARR ANTIE S, I NCLUD ING, BUT N OT LI MITE D TO, THE IMPLI ED WA RRAN TIES * OF M ERCHA NTAB ILITY AND FITNE SS FO R A PARTI CULAR PURP OSE A RE * DISC LAIME D. IN NO EVEN T SHA LL TH E AP ACHE SOFTW ARE F OUNDA TION OR * ITS CONTR IBUT ORS B E LIA BLE F OR AN Y DI RECT, INDI RECT, INCI DENT AL, * SPEC IAL, EXEM PLARY , OR CONSE QUENT IAL DAMAG ES (I NCLUD ING, BUT NOT * LIMI TED T O, P ROCUR EMENT OF S UBSTI TUTE GOOD S OR SERVI CES; LOSS OF * USE, DATA , OR PROF ITS; OR BU SINES S IN TERRU PTION ) HOW EVER CAUS ED AN D * ON A NY TH EORY OF L IABIL ITY, WHETH ER I N CON TRACT , STR ICT L IABI LITY, * OR T ORT ( INCL UDING NEGL IGENC E OR OTHE RWISE ) ARI SING IN AN Y WA Y OUT * OF T HE US E OF THIS SOFT WARE, EVEN IF ADVIS ED OF THE POSSI BILI TY OF * SUCH DAMA GE. * ==== ===== ==== ===== ===== ===== ===== ==== ===== ===== ===== ===== ==== ===== == * * This soft ware cons ists of vo lunta ry c ontri butio ns ma de by man y * indi vidua ls o n beh alf o f the Apac he S oftwa re Fo undat ion. For more * info rmati on o n the Apac he So ftwar e Fo undat ion, pleas e see * <htt p://w ww.a pache .org/ >. * * [Add ition al n otice s, if requ ired by p rior licen sing condi tion s] * */ public void inva lidat e() { serv erSe ssion .remo veApp licat ionS essio n(con text) ; // r emov e eve rythi ng in the sess ion Enum erat ion e num = valu es.ke ys() ; whil e (e num.h asMor eElem ents( )) { Stri ng na me = (Stri ng)en um.n extEl ement (); remo veVal ue(na me); } vali d = false ; } pub lic b oole an is New() { if ( ! va lid) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.se ssio n.ise "); StandardSession package org. apac he.to mcat. sessi on; import import import import import import import import import import import import import import java. io.I OExce ption ; java. io.O bject Input Strea m; java. io.O bject Outpu tStre am; java. io.S erial izabl e; java. util .Enum erati on; java. util .Hash table ; java. util .Vect or; javax .ser vlet. Servl etExc eptio n; javax .ser vlet. http. HttpS essio n; javax .ser vlet. http. HttpS essio nBin dingE vent; javax .ser vlet. http. HttpS essio nBin dingL isten er; javax .ser vlet. http. HttpS essio nCon text; org.a pach e.tom cat.c atali na.*; org.a pach e.tom cat.u til.S tring Mana ger; thro w new Ille galSt ateEx cept ion(m sg); } if ( this Acces sTime == c reati onTi me) { retu rn tr ue; } el se { retu rn fa lse; } } /** * @depr ecat ed */ pub lic v oid putVa lue(S tring name , Ob ject value ) { setA ttri bute( name, valu e); } pub lic v oid setAt tribu te(St ring name , Obj ect v alue) { if ( ! va lid) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.se ssio n.ise "); thro w new Ille galSt ateEx cept ion(m sg); /** * Stan dard impl ement ation of t he <b >Ses sion< /b> interfa ce. This obje ct is * seri aliza ble, so t hat i t can be s tore d in persist ent s tora ge or tran sferr ed * to a diff eren t JVM for distr ibuta ble sessi on support . * <p> * <b>I MPLEM ENTA TION NOTE< /b>: An i nsta nce o f thi s class r epres ents both the * inte rnal (Ses sion) and appli catio n le vel (HttpSe ssion ) vi ew of the sessi on. * Howe ver, beca use t he cl ass i tself is not d eclar ed public, Java log ic ou tside * of t he <c ode> org.a pache .tomc at.se ssio n</co de> package cann ot c ast a n * Http Sessi on v iew o f thi s ins tance bac k to a Session view . * * @aut hor C raig R. M cClan ahan * @ver sion $Rev ision : 1.2 $ $D ate: 2000 /05/1 5 17:54:1 0 $ */ } if ( name == n ull) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.va lue. iae") ; thro w new Ille galAr gumen tExc eptio n(msg ); } remo veVa lue(n ame); final c lass Stan dardS essio n imp lemen ts H ttpSe ssion , Ses sion { /** /** * Retur n th e <co de>Ht tpSes sion< /cod e> fo r whi ch th is object * is th e fa cade. */ pub lic H ttpS essio n get Sessi on() { // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- Session Publ ic M ethod s /** * Updat e th e acc essed time info rmat ion f or th is se ssion . This me thod * shoul d be call ed by the conte xt w hen a requ est c omes in for a p artic ular * sessi on, even if th e app licat ion does not r efere nce i t. */ pub lic v oid acces s() { ((Ht tpSes sionB indin gList ener )valu e).va lueBo und(e ); } package org. apac he.to mcat. sessi on; /** * Core impl emen tatio n of an ap plica tion leve l ses sion * * @aut hor J ames Dunc an Da vidso n [du ncan @eng. sun.c om] * @aut hor J ason Hunt er [j ch@en g.sun .com ] * @aut hor J ames Todd [gon zo@en g.sun .com ] */ thro w new Ille galSt ateEx cept ion(m sg); } if ( name == n ull) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.va lue. iae") ; thro w new Ille galAr gumen tExc eptio n(msg ); } public class App licat ionSe ssion impl emen ts Ht tpSes sion { retu rn v alues .get( name) ; pri vate Stri ngMan ager sm = Stri ngMa nager .getM anage r("or g.ap ache. tomca t.ses sion" ); pri vate Hash table valu es = new H asht able( ); pri vate Stri ng id ; pri vate Serv erSes sion serve rSess ion; pri vate Cont ext c ontex t; pri vate long crea tionT ime = Syst em.c urren tTime Milli s();; pri vate long this Acces sTime = cr eati onTim e; pri vate long last Acces sed = crea tion Time; pri vate int inact iveIn terva l = - 1; pri vate bool ean v alid = tru e; // U nbin d any obje cts a ssoci ated with this sess ion Vect or r esult s = n ew Ve ctor( ); Enum erat ion a ttrs = get Attri bute Names (); whil e (a ttrs. hasMo reEle ments ()) { Stri ng at tr = (Stri ng) a ttrs .next Eleme nt(); resu lts.a ddEle ment( attr) ; } Enum erat ion n ames = res ults. elem ents( ); whil e (n ames. hasMo reEle ments ()) { Stri ng na me = (Stri ng) n ames .next Eleme nt(); remo veAtt ribut e(nam e); } /** * Const ruct a ne w Ses sion assoc iate d wit h the specifi ed Ma nage r. * * @para m ma nager The manag er wi th w hich this Session is a ssoc iated */ pub lic S tand ardSe ssion (Mana ger m anag er) { // ----- ---- ----- ----- ----- ----- ---- ----- ----- --------- Inst ance Vari ables /** * The c olle ction of u ser d ata a ttri butes associa ted w ith this Sessi on. */ pri vate Hash table attr ibute s = n ew H ashta ble() ; Stri ng[] valu eName s = n ew St ring [name s.siz e()]; /** * The s essi on id entif ier o f thi s Se ssion . */ pri vate Stri ng id = nu ll; /** * Descr ipti ve in forma tion descr ibin g thi s Session impl emen tatio n. */ pri vate stat ic fi nal S tring info = "Standa rdSes sion /1.0" ; // R eset the insta nce v ariab les assoc iated with this Session attr ibut es.cl ear() ; crea tion Time = 0L; id = nul l; last Acce ssedT ime = 0L; mana ger = nul l; maxI nact iveIn terva l = - 1; isNe w = true; isVa lid = fal se; if ( this .inac tiveI nterv al != -1) { this .inac tiveI nterv al *= 60; pub lic E nume ratio n get Attri buteN ames () { if ( ! va lid) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.se ssio n.ise "); } Ser verSe ssio n get Serve rSess ion() { retu rn s erver Sessi on; } thro w new Ille galSt ateEx cept ion(m sg); } /** * The M anag er wi th wh ich t his S essi on is associa ted. */ pri vate Mana ger m anage r = n ull; } /** * Retur n th e <co de>is Valid </cod e> f lag f or th is se ssion . */ boo lean isVa lid() { retu rn ( Enume ratio n)val uesCl one. keys( ); } voi d acc esse d() { // s et l ast a ccess ed to this Acce ssTim e as it wi ll be lef t ove r // f rom the p revio us ac cess last Acce ssed = thi sAcce ssTim e; this Acce ssTim e = S ystem .curr entT imeMi llis( ); /** * @depr ecat ed */ /** * The m axim um ti me in terva l, in sec onds, betw een client reque sts befor e * the s ervl et co ntain er ma y inv alid ate t his session . A nega tive time * indic ates that the sessi on sh ould neve r tim e out. */ pri vate int maxIn activ eInte rval = -1 ; pub lic v oid remov eValu e(Str ing n ame) { remo veAt tribu te(na me); } vali date (); /** * Flag indi catin g whe ther this sess ion i s new or } pub lic v oid remov eAttr ibute (Stri ng n ame) { if ( ! va lid) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.se ssio n.ise "); voi d val idat e() { // i f we have an i nacti ve in terv al, c heck to se e if we'v e exc eeded it if ( inac tiveI nterv al != -1) { int thisI nterv al = (int) (Syst em.cu rrent Time Milli s() - last Acces sed) / 10 00; if ( (man ager != nu ll) & & man ager .getD istri butab le() && !( valu e ins tance of Se riali zabl e)) thro w new Ille galAr gumen tExc eptio n (sm.g etStr ing(" stand ardS essio n.set Attri bute. iae" )); retu rn ( this. isVal id); } sync hron ized (attr ibute s) { remo veAtt ribut e(nam e); attr ibute s.put (name , val ue); if ( value inst anceo f Htt pSes sionB indin gList ener) ((Htt pSess ionBi nding List ener) valu e).va lueBo und ( new H ttpSe ssion Bind ingEv ent(( HttpS essio n) t his, name) ); } /** * Set t he < code> isNew </cod e> fl ag f or th is se ssion . * * @para m is New T he ne w val ue fo r th e <co de>is New</ code> flag */ voi d set New( boole an is New) { Hash tabl e val uesCl one = (Has htab le)va lues. clone (); /** * Calle d by cont ext w hen r eques t co mes i n so that acces ses and * inact ivit ies c an be deal t wit h ac cordi ngly. */ /** * Bind an o bject to t his s essio n, u sing the s pecif ied n ame. If an ob ject * of th e sa me na me is alre ady b ound to t his s essio n, th e ob ject is * repla ced. * <p> * After thi s met hod e xecut es, a nd i f the obje ct im pleme nts * <code >Htt pSess ionBi nding Liste ner< /code >, th e con taine r ca lls * <code >val ueBou nd()< /code > on the objec t. * * @para m na me Na me to whic h the obj ect i s bou nd, c annot be null * @para m va lue O bject to b e bou nd, canno t be null * * @exce ptio n Ill egalA rgume ntExc epti on if an a ttemp t is made to a dd a * non- seri aliza ble o bject in a n en viron ment marke d dis trib utabl e. * @exce ptio n Ill egalS tateE xcept ion if th is me thod is ca lled on a n * inva lida ted s essio n */ pub lic v oid setAt tribu te(St ring name , Obj ect v alue) { // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- Sess ion Package Meth ods /** * The l ast acces sed t ime f or th is S essio n. */ pri vate long last Acces sedTi me = crea tionT ime; retu rn v alueN ames; } remo veAt tribu te(na me); if ( name == n ull) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.va lue. iae") ; // ----- ---- ----- ----- ----- ----- ---- ----- ----- - Htt pSess ion Priva te Me thods /** * Read a se riali zed v ersio n of this sess ion o bject from the spec ified * objec t in put s tream . * <p> * <b>IM PLEM ENTAT ION N OTE</ b>: The refer ence to th e own ing Manag er * is no t re store d by this metho d, a nd mu st be set expli citl y. * * @para m st ream The i nput strea m to read from * * @exce ptio n Cla ssNot Found Excep tion if a n unk nown class is speci fied * @exce ptio n IOE xcept ion i f an inpu t/out put e rror occur s */ pri vate void read Objec t(Obj ectIn putS tream stre am) thro ws C lassN otFou ndExc eptio n, I OExce ption { not. */ pri vate bool ean i sNew = tru e; /** * Flag indi catin g whe ther this sess ion i s val id or not. */ pri vate bool ean i sVali d = f alse; thro w new Ille galAr gumen tExc eptio n(msg ); } } // HTTP SESS ION I MPLEM ENTAT ION M ETHO DS Obje ct o = va lues. get(n ame); pub lic S trin g get Id() { if ( vali d) { retu rn id ; } el se { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.se ssio n.ise "); if ( o in stanc eof H ttpSe ssion Bind ingLi stene r) { Http Sessi onBin dingE vent e = new H ttpSe ssion Bindi ngEv ent(t his,n ame); /** * The s trin g man ager for t his p acka ge. */ pri vate Stri ngMan ager sm = this .isV alid = isV alid; } StringM anage r.ge tMana ger(" org.a pache .tom cat.s essio n") ; // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- HttpSes sion Prop ertie s retu rn ( this. creat ionTi me); pub lic v oid setMa xInac tiveI nterv al(i nt in terva l) { if ( ! va lid) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.se ssio n.ise "); thro w new Ille galSt ateEx cept ion(m sg); } thro w new Ille galSt ateEx cept ion(m sg); } inac tive Inter val = inte rval; } } /** * * @depr ecat ed */ pub lic i nt g etMax Inact iveIn terva l() { if ( ! va lid) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.se ssio n.ise "); /** * The H TTP sessi on co ntext asso ciat ed wi th th is session . */ pri vate stat ic Ht tpSes sionC ontex t se ssion Conte xt = null; /** * The c urre nt ac cesse d tim e for thi s ses sion. */ pri vate long this Acces sedTi me = crea tionT ime; } // ----- ---- ----- ----- ----- ----- ---- ----- ----- --------- Sess ion Prope rties /** * Write a s erial ized versi on of thi s ses sion objec t to the speci fied * objec t ou tput strea m. * <p> * <b>IM PLEM ENTAT ION N OTE</ b>: The ownin g Man ager will not be st ored * in th e se riali zed r epres entat ion of th is Se ssion . Af ter calli ng * <code >rea dObje ct()< /code >, yo u mu st se t the asso ciate d Ma nager * expli citl y. * <p> * <b>IM PLEM ENTAT ION N OTE</ b>: Any attri bute that is no t Se riali zable * will be s ilent ly ig nored . If you do n ot wa nt an y suc h at tribu tes, * be su re t he <c ode>d istri butab le</ code> prop erty of ou r as socia ted * Manag er i s set to < code> true< /cod e>. * * @para m st ream The o utput stre am t o wri te to * * @exce ptio n IOE xcept ion i f an inpu t/out put e rror occur s */ pri vate void writ eObje ct(Ob jectO utpu tStre am st ream) thro ws I OExce ption { if ( sess ionCo ntext == n ull) sess ionCo ntext = ne w Sta ndar dSess ionCo ntext (); retu rn ( sessi onCon text) ; } retu rn i nacti veInt erval ; } pub lic l ong getLa stAcc essed Time( ) { if ( vali d) { retu rn la stAcc essed ; } el se { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.se ssio n.ise "); } //----- ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- ----- ---- /** * Set t he c reati on ti me fo r thi s se ssion . Th is method is ca lled by t he * Manag er w hen a n exi sting Sess ion insta nce i s reused. * * @para m ti me Th e new crea tion time */ pub lic v oid setCr eatio nTime (long tim e) { thro w new Ille galSt ateEx cept ion(m sg); this .cre ation Time = tim e; this .las tAcce ssedT ime = time ; this .thi sAcce ssedT ime = time ; } } } /** * Retur n th e ses sion ident ifier for this session . */ pub lic S trin g get Id() { } // ----- ---- ----- ----- ----- ----- ---- ----- ----- --HttpSes sion Publ ic Me thods /** * Retur n th e obj ect b ound with the speci fied name in th is session , or * <code >nul l</co de> i f no objec t is boun d wit h tha t nam e. * * @para m na me Na me of the attri bute to b e ret urned * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is called on an * inva lida ted s essio n */ pub lic O bjec t get Attri bute( Strin g na me) { /** * Set t he s essio n ide ntifi er fo r th is se ssion . * * @para m id The new s essio n ide ntif ier */ pub lic v oid setId (Stri ng id ) { if ( (thi s.id != nu ll) & & (ma nage r != null) && (m anag er in stanc eof M anage rBas e)) ((Ma nager Base) mana ger). remo ve(th is); this .id = id; ServerSession package org. apac he.to mcat. sessi on; import org.a pach e.tom cat.c ore.* ; import org.a pach e.tom cat.u til.S tring Mana ger; import java. io.* ; import java. net. *; import java. util .*; import javax .ser vlet. *; import javax .ser vlet. http. *; void va lidat e() { // i f we have an i nacti ve in terv al, c heck to se e if // w e've exce eded it if ( inac tiveI nterv al != -1) { int thisI nterv al = (int) (Syst em.cu rrent Time Milli s() - last Acces sed) / 10 00; if ( (man ager != nu ll) & & (ma nage r ins tance of Manager Base) ) ((Ma nager Base) mana ger). add( this) ; } /** * Retur n de scrip tive infor matio n ab out t his Session impl emen tatio n and * the c orre spond ing v ersio n num ber, in t he format * <code>& lt;de scri ption &gt;/ &lt;v ersio n&gt ;</co de>. */ pub lic S trin g get Info( ) { retu rn 0 ; pub lic i nt r eques tMap( Reque st re ques t ) { Stri ng s essio nId = null ; } Cook ie c ookie s[]=r eques t.get Cook ies() ; // asser t !=n ull /** Noti fica tion of co ntext shut down */ pub lic v oid conte xtShu tdown ( Con text ctx ) thro ws T omcat Excep tion { if( ctx. getDe bug() > 0 ) ctx .log ("Rem oving sess ions from " + ctx ) ; ctx. getS essio nMana ger() .remo veSe ssion s(ctx ); } for( int i=0; i<co okies .leng th; i++ ) { Cook ie co okie = coo kies[ i]; if ( cooki e.get Name( ).equ als( "JSES SIONI D")) { sessi onId = coo kie.g etVa lue() ; sessi onId= valid ateSe ssio nId(r eques t, se ssion Id); if (s essio nId!= null) { r eques t.set Reque sted Sessi onIdF romCo okie( true ); } } Serve rSess ionMa nager ssm = S erver Sessi onMan ager .getM anage r(); ssm.r emove Sessi on(th is); } } public class Ser verSe ssion { } pri vate Stri ngMan ager sm = Stri ngMa nager .getM anage r("or g.ap ache. tomca t.ses sion" ); pri vate Hash table valu es = new H asht able( ); pri vate Hash table appS essio ns = new Hasht able( ); pri vate Stri ng id ; pri vate long crea tionT ime = Syst em.c urren tTime Milli s();; pri vate long this Acces sTime = cr eati onTim e; pri vate long last Acces sed = crea tion Time; pri vate int inact iveIn terva l = - 1; syn chron ized void inva lidat e() { Enum erat ion e num = appS essio ns.k eys() ; Ser verSe ssio n(Str ing i d) { this .id = id; } } appS essio n.inv alida te(); } } /** Vali date and fix t he se ssion id. If t he se ssion is n ot v alid retur n nul l. * It w ill also clean up t he se ssio n fro m loa d-bal ancin g st rings . * @retu rn s essio nId, or nu ll if not vali d */ pri vate Stri ng va lidat eSess ionId (Req uest reque st, S tring ses sionI d){ // G S, W e pig gybac k the JVM id o n top of t he se ssion coo kie // S epar ate t hem . .. sta tic a dvic e(Sta ndard Sessi on s) : in valid ate(s ) { befo re { if ( !s.is Valid ()) throw new Illeg alSta teEx cepti on ( s.sm. getSt ring( "sta ndard Sessi on." + th isJoi nPoin t.met hodNa me + ". ise") ); } } pub lic O bjec t get Value (Stri ng na me) { if ( name == n ull) { Stri ng ms g = s m.get Strin g("s erver Sessi on.va lue.i ae") ; /** * Set t he M anage r wit hin w hich this Sess ion i s valid. * * @para m ma nager The new M anage r */ pub lic v oid setMa nager (Mana ger m anag er) { this .man ager = man ager; pub lic A ppli catio nSess ion g etApp lica tionS essio n(Con text cont ext, bool ean creat e) { Appl icat ionSe ssion appS essio n = (App licat ionSe ssion )appS essi ons.g et(co ntext ); thro w new Ille galAr gumen tExc eptio n(msg ); } } /** * This clas s is a du mmy i mplem entat ion of th e <co de>Ht tpSes sion Conte xt</c ode> * inte rface , to conf orm t o the requ irem ent t hat s uch a n obj ect be re turne d * when <cod e>Ht tpSes sion. getSe ssion Cont ext() </cod e> is call ed. * * @aut hor C raig R. M cClan ahan * * @dep recat ed A s of Java Servl et AP I 2. 1 wit h no repla cemen t. The * int erfac e wi ll be remo ved i n a f utur e ver sion of th is AP I. */ final c lass Stan dardS essio nCont ext i mple ments Http Sessi onCon text { Vect or r esult s = n ew Ve ctor( ); Enum erat ion a ttrs = get Attri bute Names (); whil e (a ttrs. hasMo reEle ments ()) { Stri ng at tr = (Stri ng) a ttrs .next Eleme nt(); resu lts.a ddEle ment( attr) ; } Stri ng n ames[ ] = n ew St ring[ resu lts.s ize() ]; for (int i = 0; i < nam es.le ngth ; i++ ) name s[i] = (St ring) resu lts. eleme ntAt( i); retu rn ( names ); pri vate Vect or du mmy = new Vecto r(); /** * Retur n th e ses sion ident ifier s of all sessi ons d efine d * withi n th is co ntext . * * @depr ecat ed As of J ava S ervle t AP I 2.1 with no r eplac emen t. * This met hod m ust r eturn an e mpty <cod e>Enu merat ion</ code > * and will be r emove d in a fut ure versi on of the API. */ pub lic E nume ratio n get Ids() { retu rn ( dummy .elem ents( )); /** * Inval idat es th is se ssion and unbi nds a ny ob jects boun d to it. * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is called on * an i nval idate d ses sion */ pub lic v oid inval idate () { /** * The s trin g man ager for t his p acka ge. */ pri vate Stri ngMan ager sm = Stri ngMa nager .getM anage r("or g.ap ache. tomca t.ses sion" ); } // X XX // s ync t o ens ure v alid? pub lic E nume ratio n get Value Names () { retu rn v alues .keys (); } appS essio n = n ew Ap plica tion Sessi on(id , thi s, co ntex t); appS essio ns.pu t(con text, app Sessi on); pub lic v oid remov eValu e(Str ing n ame) { valu es.r emove (name ); } } // X XX // m ake sure that we ha ven't gon e ove r the end of ou r // i nact ive i nterv al -- if s o, i nvali date and c reate // a new appS essio n pub lic v oid setMa xInac tiveI nterv al(i nt in terva l) { inac tive Inter val = inte rval; } retu rn a ppSes sion; pub lic i nt g etMax Inact iveIn terva l() { retu rn i nacti veInt erval ; } } voi d rem oveA pplic ation Sessi on(Co ntex t con text) { appS essi ons.r emove (cont ext); // XXX // sync' d fo r saf ty -- no o ther thre ad sh ould be ge tting som ethin g // from this whil e we are r eapin g. T his i sn't the m ost o ptim al // solut ion for t his, but w e'll dete rmine some thing else lat er. } /** * Calle d by cont ext w hen r eques t co mes i n so that acces ses and * inact ivit ies c an be deal t wit h ac cordi ngly. */ syn chron ized void reap () { Enum erat ion e num = appS essio ns.k eys() ; voi d acc esse d() { // s et l ast a ccess ed to this Acce ssTim e as it wi ll be lef t ove r // f rom the p revio us ac cess whil e (e num.h asMor eElem ents( )) { Obje ct ke y = e num.n extEl emen t(); Appl icati onSes sion appSe ssio n = (Appl icati onSes sion) appS essio ns.ge t(key ); last Acce ssed = thi sAcce ssTim e; this Acce ssTim e = S ystem .curr entT imeMi llis( ); /** * Retur n th e max imum time inter val, in s econd s, between clie nt r eques ts * befor e th e ser vlet conta iner will inva lidat e the ses sion. A negat ive * time indi cates that the sessi on s hould neve r time ou t. * * @exce ptio n Ill egalS tateE xcept ion if th is method is ca lled on * an i nval idate d ses sion */ pub lic i nt g etMax Inact iveIn terva l() { retu rn ( this. maxIn activ eInte rval ); } /** * Set t he m aximu m tim e int erval , in seco nds, between clie nt r eques ts * befor e th e ser vlet conta iner will inva lidat e the ses sion. A negat ive * time indi cates that the sessi on s hould neve r time ou t. * * @para m in terva l The new maxim um i nterv al */ pub lic v oid setMa xInac tiveI nterv al(i nt in terva l) { /** * Retur n th e <co de>Ht tpSes sion< /cod e> as socia ted w ith t he * speci fied sess ion i denti fier. * * @para m id Sess ion i denti fier for which to l ook u p a s essi on * * @depr ecat ed As of J ava S ervle t AP I 2.1 with no r eplac emen t. * This met hod m ust r eturn null and will be r emove d in a * futu re v ersio n of the A PI. */ pub lic H ttpS essio n get Sessi on(St ring id) { } retu rn ( null) ; /** * Retur n <c ode>t rue</ code> if t he c lient does not yet k now about t he * sessi on, or if the clien t cho oses not to jo in th e session . Fo r * examp le, if th e ser ver u sed o nly cooki e-bas ed se ssion s, and the clie nt * has d isab led t he us e of cooki es, then a ses sion would be new on each * reque st. * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is called on an * inva lida ted s essio n */ pub lic b oole an is New() { if( sess ion = = nul l) re turn; if ( sess ion i nstan ceof Sessi on) /** ((Se ssion ) ses sion) .acce ss() ; * Spec ializ ed i mplem entat ion o f org .apa che.t omcat .core .Sess ionM anage r * that adap ts t o the new compo nent- base d Man ager imple menta tion . // c ache the HttpS essio n - a void anot her f ind * <p> req. setS essio n( se ssion ); * XXX - At pres ent, use o f <co de>St anda rdMan ager< /code > is hard code d, } * and lifec ycle conf igura tion is no t su pport ed. * <p> * <b>I MPLEM ENTA TION NOTE< /b>: Manager /Sess ion // XXX s houl d we throw exce ption or just retur n nul l ?? Once we commi t to the n ew pub lic H ttpS essio n fin dSess ion( Cont ext c tx, S tring id ) { * para digm, I w ould sugge st mo ving the logic impl ement ed he re b ack i nto try { T he To mcat. Next "Man ager" inte rface acts mor e lik e a Sess ion s essio n = m anage r.fi ndSes sion( id); * coll ectio n cl ass, and h as mi nimal kno wledg e of the d etail ed r eques t if(s essio n!=nu ll) * proc essin g se manti cs of hand ling sess ions. retur n ses sion. getSe ssio n(); * <p> } ca tch (IOEx cepti on e) { * XXX - At pres ent, there is n o way (vi a the Sess ionMa nager int erfac e) for } retu rn ( null) ; * a Co ntext to tell the M anage r tha t we crea te wh at th e def ault sess ion } * time out f or t his w eb ap plica tion (spe cifie d in the d eploy ment descrip tor) pub lic H ttpS essio n cre ateSe ssion (Con text ctx) { * shou ld be . retu rn * manag er.cr eateS essio n(). getSe ssion (); } * @aut hor C raig R. M cClan ahan */ public final cla ss St andar dSess ionMa nage r * Remov e al l ses sions beca use o ur a ssoci ated Conte xt is bei ng sh ut down. imp lemen ts S essio nMana ger { * * @para m ct x The cont ext t hat i s be ing s hut d own */ // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- -Constru ctors pub lic v oid remov eSess ions( Conte xt c tx) { // c onte xts, we ju st wa nt to rem ove t he se ssion s of ctx! // T he m anage r wil l sti ll ru n af ter t hat ( i.e. keep dat abase * Creat e a new S essio nMana ger t hat adapt s to the c orres pond ing Manager // c onne ction open * imple ment ation . if ( mana ger i nstan ceof Lifec ycle ) { */ try { pub lic S tand ardSe ssion Manag er() { ((Lif ecycl e) ma nager ).st op(); } ca tch ( Lifec ycleE xcept ion e) { mana ger = new Stan dardM anage r(); throw new Illeg alSta teEx cepti on("" + e) ; if ( mana ger i nstan ceof Lifec ycle ) { } try { } ((Lif ecycl e) ma nager ).co nfigu re(nu ll); // V alid ate a nd up date our c urre nt co mpone nt st ate if ( !con figur ed) thro w new Life cycle Excep tion (sm.g etStr ing(" stand ardM anage r.not Confi gured ")); if ( star ted) thro w new Life cycle Excep tion (sm.g etStr ing(" stand ardM anage r.alr eadyS tarte d")) ; star ted = tru e; /** * Has t his compo nent been start ed y et? */ pri vate bool ean s tarte d = f alse; if ( sess ionId != n ull & & ses sion Id.le ngth( )!=0) { // G S, We are in a probl em h ere, we ma y act ually get // m ultip le Se ssion cook ies (one for t he ro ot // c ontex t and one for t he r eal c ontex t... or ol d se ssion // c ookie . We must check for vali dity in th e cur rent cont ext. Cont ext c tx=re quest .getC onte xt(); Sess ionMa nager sM = ctx. getS essio nMana ger() ; if(n ull ! = sM. findS essio n(ct x, se ssion Id)) { sM.ac cesse d(ctx , req uest , ses sionI d ); reque st.se tRequ ested Sess ionId (sess ionId ); if( d ebug> 0 ) c m.log (" F inal sessi on id " + sess ionId ); retur n ses sionI d; } } retu rn n ull; /** * The b ackg round thre ad. */ pri vate Thre ad th read = nul l; // S tart the backg round reap er t hread thre adSt art() ; ((Lif ecycl e) ma nager ).st art() ; } } ca tch ( Lifec ycleE xcept ion e) { throw new Illeg alSta teEx cepti on("" + e) ; } /** * Used by c ontex t to confi gure the sessi on ma nager 's in acti vity timeout . } * * The S essi onMan ager may h ave s ome defau lt se ssion time out , the } * Conte xt o n the othe r han d has it' s tim eout set b y the dep loyme nt } /** * The b ackg round thre ad co mplet ion semap hore. */ pri vate bool ean t hread Done = fal se; * descr ipto r (we b.xml ). Th is me thod lets the Conte xt co nfor gure the /** * Grace full y ter minat e the acti ve u se of the publi c met hods of t his * compo nent . Th is me thod shoul d be the last one c alled on a giv en * insta nce of th is co mpone nt. * * @exce ptio n Ill egalS tateE xcept ion if th is co mpone nt ha s no t bee n sta rted * @exce ptio n Ill egalS tateE xcept ion if th is co mpone nt ha s al ready * been sto pped * @exce ptio n Lif ecycl eExce ption if this compo nent detec ts a fata l err or * that nee ds to be r eport ed */ pub lic v oid stop( ) thr ows L ifecy cleE xcept ion { /** * Name to r egist er fo r the back grou nd th read. */ pri vate Stri ng th readN ame = "Sta ndar dMana ger"; // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- ---- Prope rties * sessi on m anage r acc ordin g to this valu e. // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- Ins tance Variabl es * * @para m mi nutes The sessi on in acti vity timeo ut in minu tes. */ /** pub lic v oid setSe ssion TimeO ut(in t mi nutes ) { * The M anag er im pleme ntati on we are actu ally using . if(- 1 != minu tes) { */ // T he ma nager work s wit h se conds ... pri vate Mana ger m anage r = n ull; // V alid ate a nd up date our c urre nt co mpone nt st ate if ( !sta rted) thro w new Life cycle Excep tion (sm.g etStr ing(" stand ardM anage r.not Start ed")) ; star ted = fal se; /** * Retur n th e che ck in terva l (in sec onds) for this Manag er. */ pub lic i nt g etChe ckInt erval () { mana ger.s etMax Inact iveIn terv al(mi nutes * 60 ); } } // S top the b ackgr ound reape r th read thre adSt op(); retu rn ( this. check Inter val); } // E xpir e all acti ve se ssion s Sess ion sessi ons[] = fi ndSes sion s(); for (int i = 0; i < ses sions .len gth; i++) { Stan dardS essio n ses sion = (S tanda rdSes sion) sess ions [i]; if ( !sess ion.i sVali d()) conti nue; sess ion.e xpire (); } /** * Set t he c heck inter val ( in se cond s) fo r thi s Man ager. * * @para m ch eckIn terva l The new chec k int erval */ pub lic v oid setCh eckIn terva l(int che ckInt erval ) { ServerSessionManager package org. apac he.to mcat. sessi on; import org.a pach e.tom cat.u til.* ; import org.a pach e.tom cat.c ore.* ; import java. io.* ; import java. net. *; import java. util .*; import javax .ser vlet. http. *; // XXX // sync' d fo r saf ty -- no o ther thre ad sh ould be ge tting som ethin g // from this whil e we are r eapin g. T his i sn't the m ost o ptim al // solut ion for t his, but w e'll dete rmine some thing else lat er. syn chron ized void reap () { Enum erat ion e num = sess ions. keys (); whil e (e num.h asMor eElem ents( )) { Obje ct ke y = e num.n extEl emen t(); Serv erSes sion sessi on = (Ser verSe ssion )sess ions. get( key); /** * * @aut hor J ames Dunc an Da vidso n [du ncan @eng. sun.c om] * @aut hor J ason Hunt er [j ch@en g.sun .com ] * @aut hor J ames Todd [gon zo@en g.sun .com ] */ sess ion.r eap() ; sess ion.v alida te(); } this .che ckInt erval = ch eckIn terv al; } // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- --- Priva te Me thods /** * Retur n de scrip tive infor matio n ab out t his M anage r imp leme ntati on an d * the c orre spond ing v ersio n num ber, in t he fo rmat * <code >&lt ;desc ripti on&gt ;/&lt ;ver sion& gt;</ code> . */ pub lic S trin g get Info( ) { /** * Inval idat e all sess ions that have expi red. */ pri vate void proc essEx pires () { long tim eNow = Sys tem.c urren tTim eMill is(); Sess ion sessi ons[] = fi ndSes sion s(); for (int i = 0; i < ses sions .len gth; i++) { Stan dardS essio n ses sion = (S tanda rdSes sion) sess ions [i]; if ( !sess ion.i sVali d()) conti nue; int maxIn activ eInte rval = se ssion .getM axIna ctive Inte rval( ); if ( maxIn activ eInte rval < 0) conti nue; int timeI dle = // T runca te, do no t rou nd up (int) ((ti meNow - se ssio n.get LastA ccess edTim e()) / 10 00L); if ( timeI dle > = max Inact iveI nterv al) sessi on.ex pire( ); } } /** * Retur n th e max imum numbe r of acti ve Se ssion s all owed, or -1 fo r * no li mit. */ pub lic i nt g etMax Activ eSess ions( ) { retu rn ( this. maxAc tiveS essio ns); } } } } public class Ser verSe ssion Manag er im plem ents Sessi onMan ager { syn chron ized void remo veSes sion( Serv erSes sion sessi on) { Stri ng i d = s essio n.get Id(); pri vate Stri ngMan ager sm = Stri ngMa nager .getM anage r("or g.ap ache. tomca t.ses sion" ); pri vate stat ic Se rverS essio nMana ger manag er; / / = n ew Se rver Sessi onMan ager( ); sess ion. inval idate (); sess ions .remo ve(id ); /** * Set t he m aximu m num ber o f act ives Sess ions allow ed, o r -1 for * no li mit. * * @para m ma x The new maxim um nu mber of s essio ns */ pub lic v oid setMa xActi veSes sions (int max) { /** * Sleep for the durat ion s pecif ied by th e <co de>ch eckIn terv al</c ode> * prope rty. */ pri vate void thre adSle ep() { try { Thre ad.sl eep(c heckI nterv al * 1000 L); } ca tch (Inte rrupt edExc eptio n e) { ; } } pro tecte d in t ina ctive Inter val = -1; this .max Activ eSess ions = max ; pub lic v oid remov eSess ions( Conte xt c ontex t) { Enum erat ion e num = sess ions. keys (); sta tic { mana ger = new Serv erSes sionM anag er(); } whil e (e num.h asMor eElem ents( )) { Obje ct ke y = e num.n extEl emen t(); Serv erSes sion sessi on = (Ser verSe ssion )sess ions. get( key); Appl icati onSes sion appSe ssio n = sessi on.ge tAppl icati onSe ssion (cont ext, false ); pub lic s tati c Ser verSe ssion Manag er g etMan ager( ) { retu rn m anage r; } } // C ause this sess ion t o exp ire expi re() ; retu rn v alues .get( name) ; if ( appS essio n == null && cr eate ) { * Prepa re f or th e beg innin g of acti ve us e of the p ublic met hods of th is * compo nent . Th is me thod shoul d be call ed af ter < code> conf igure ()</c ode>, * and b efor e any of t he pu blic meth ods o f the comp onent are util ized. * * @exce ptio n Ill egalS tateE xcept ion if th is co mpone nt ha s no t yet been * conf igur ed (i f req uired for this comp onent ) * @exce ptio n Ill egalS tateE xcept ion if th is co mpone nt ha s al ready been * star ted * @exce ptio n Lif ecycl eExce ption if this compo nent detec ts a fata l err or * that pre vents this comp onent fro m bei ng us ed */ pub lic v oid start () th rows Lifec ycle Excep tion { /** * The m axim um nu mber of ac tive Sess ions allow ed, o r -1 for no li mit. */ pro tecte d in t max Activ eSess ions = -1 ; if( debu g>0 ) cm.l og(" Orig sess ionId " + sess ionId ); if ( null != s essio nId) { int idex = ses sionI d.las tInd exOf( SESSI ONID_ ROUTE _SEP ); if(i dex > 0) { sessi onId = ses sionI d.su bstri ng(0, idex ); } } } } } remo veVa lue(n ame); // remov e an y exi sting bind ing valu es.p ut(na me, v alue) ; } pub lic l ong getLa stAcc essed Time( ) { retu rn l astAc cesse d; } Http Sess ion s essio n=fin dSess ion( ctx, id); // X XX X XX a manag er ma y be shar ed by mult iple /** retu rn ( this. info) ; retu rn ( this. manag er); */ /** // ---- ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- - Pri vate Class /** * Retur n th e Man ager withi n whi ch t his S essio n is vali d. */ pub lic M anag er ge tMana ger() { * @para m se ssion The sessi on to be marke d pub lic v oid acces sed(C ontex t ctx , Re quest req, Stri ng id ) { /** node = a ttrib utes. getNa medIt em(" maxIn activ eInte rval" ); if ( node != n ull) { try { setMa xInac tiveI nterv al(I ntege r.par seInt (node .get NodeV alue( ))); } ca tch ( Throw able t) { ; // XXX - Thr ow e xcept ion? } } /** * The d escr iptiv e inf ormat ion a bout this impl ement ation . */ pri vate stat ic fi nal S tring info = " Stand ardMa nager /1.0" ; cro sscut inv alida te(St andar dSess ion s): s & (i nt ge tMaxI nact iveIn terva l() | l ong g etCre atio nTime () | O bject getA ttri bute( Strin g) | E numer ation get Attri buteN ames( ) | S tring [] ge tVal ueNam es() | v oid i nvali date () | b oolea n isN ew() | v oid r emove Attr ibute (Stri ng) | v oid s etAtt ribu te(St ring, Obje ct)); /** * Retur n th e obj ect b ound with the speci fied name in th is session , or * <code >nul l</co de> i f no objec t is boun d wit h tha t nam e. * * @para m na me Na me of the value to be re turne d * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is called on an * inva lida ted s essio n * * @depr ecat ed As of V ersio n 2.2 , th is me thod is re place d by * <cod e>ge tAttr ibute ()</c ode> */ pub lic O bjec t get Value (Stri ng na me) { retu rn ( getAt tribu te(na me)); Thi s sh ould be * import org.a pach e.tom cat.c ore.S essio nMan ager; import org.a pach e.tom cat.u til.S essio nUti l; } // XXX w hat is th e cor rect behav ior if th e ses sion is in vali d ? // We ma y st ill s et it and just retu rn se ssion inva lid. } retu rn ( attri butes .keys ()); } pub lic l ong getCr eatio nTime () { retu rn c reati onTim e; node = a ttrib utes. getNa medIt em(" maxAc tiveS essio ns"); if ( node != n ull) { try { setMa xActi veSes sions (Int eger. parse Int(n ode.g etNo deVal ue()) ); } ca tch ( Throw able t) { ; // XXX - Thr ow e xcept ion? } } * Mark the speci fied sessi on's last acce ssed time. * calle d fo r eac h req uest by a Requ estIn terce ptor. import org.a pach e.tom cat.c ore.R eques t; import org.a pach e.tom cat.c ore.R espon se; * the core leve l. node = a ttrib utes. getNa medIt em(" check Inter val") ; if ( node != n ull) { try { setCh eckIn terva l(Int eger .pars eInt( node. getNo deVa lue() )); } ca tch ( Throw able t) { ; // XXX - Thr ow e xcept ion? } } /** import org.a pach e.tom cat.c atali na.*; import org.a pach e.tom cat.c ore.C ontex t; } } /** * Retur n th e set of n ames of ob ject s bou nd to this session . If the re * are n o su ch ob jects , a z ero-l engt h arr ay is retu rned. * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is called on an * inva lida ted s essio n * * @depr ecat ed As of V ersio n 2.2 , th is me thod is re place d by * <cod e>ge tAttr ibute Names ()</c ode> */ pub lic S trin g[] g etVal ueNam es() { import javax .ser vlet. http. Cooki e; import javax .ser vlet. http. HttpS essio n; } // S eria lize the a ttrib ute c ount and the attri bute valu es stre am.w riteO bject (new Integ er(r esult s.siz e())) ; Enum erat ion n ames = res ults. elem ents( ); whil e (n ames. hasMo reEle ments ()) { Stri ng na me = (Stri ng) n ames .next Eleme nt(); stre am.wr iteOb ject( name) ; stre am.wr iteOb ject( attri bute s.get (name )); } thro w new Ille galAr gumen tExc eptio n(msg ); } // P arse and proce ss ou r con figu ratio n par amete rs if ( !("M anage r".eq uals( param eter s.get NodeN ame() ))) retu rn; Name dNod eMap attri butes = pa rame ters. getAt tribu tes() ; Node nod e = n ull; /** * Has t his compo nent been confi gure d yet ? */ pri vate bool ean c onfig ured = fal se; retu rn ( this. info) ; pub lic v oid putVa lue(S tring name , Ob ject value ) { if ( name == n ull) { Stri ng ms g = s m.get Strin g("s erver Sessi on.va lue.i ae") ; pub lic S trin g get Id() { retu rn i d; } // V alid ate a nd up date our c urre nt co mpone nt st ate if ( conf igure d) thro w new Life cycle Excep tion (sm.g etStr ing(" stand ardM anage r.alr eadyC onfig ured ")); conf igur ed = true; if ( para meter s == null) retu rn; /** * The i nter val ( in se conds ) bet ween chec ks fo r exp ired sess ions. */ pri vate int check Inter val = 60; } /** * Retur n th e las t tim e the clie nt s ent a requ est associa ted w ith this * sessi on, as th e num ber o f mil lise conds sinc e midnigh t, Ja nuar y 1, 1970 * GMT. Act ions that your appli cati on ta kes, such as gett ing or se tting * a val ue a ssoci ated with the s essi on, d o not affect the a cces s tim e. */ pub lic l ong getLa stAcc essed Time( ) { retu rn ( this. lastA ccess edTim e); whil e (e num.h asMor eElem ents( )) { Obje ct ke y = e num.n extEl emen t(); Appl icati onSes sion appSe ssio n = (Appl icati onSes sion) appS essio ns.ge t(key ); /** * Stan dard impl ement ation of t he <b >Man ager< /b> i nterf ace t hat provi des * no s essio n pe rsist ence or di strib utab le ca pabil ities , but doe s sup port * an o ption al, confi gurab le, m aximu m nu mber of ac tive sessi ons allow ed. * <p> * Life cycle con figur ation of t his c ompo nent assum es an XML node * in t he fo llow ing f ormat : * <cod e> * &lt;M anag er cl assNa me="o rg.ap ache .tomc at.se ssion .Stan dard Manag er" * check Inter val=" 60" m axAc tiveS essio ns="- 1" * maxIn activ eInte rval= "-1" /> * </co de> * wher e you can adju st th e fol lowin g pa ramet ers, with defau lt v alues * in s quare bra ckets : * <ul> * <li> <b>ch eckI nterv al</b > - T he in terv al (i n sec onds) betw een backg round * threa d ch ecks for e xpire d ses sion s. [ 60] * <li> <b>ma xAct iveSe ssion s</b> - Th e ma ximum numb er of sess ions allo wed t o * be ac tive at o nce, or -1 for no l imit. [-1 ] * <li> <b>ma xIna ctive Inter val</ b> - The defau lt ma ximum numb er o f sec onds of * inact ivit y bef ore w hich the s ervl et co ntain er is allo wed to ti me ou t * a ses sion , or -1 fo r no limit . T his v alue shoul d be over ridde n fro m * the d efau lt se ssion time out s peci fied in th e web appl icat ion d eploy ment * descr ipto r, if any. [-1 ] * </ul > * * @aut hor C raig R. M cClan ahan * @ver sion $Rev ision : 1.1 .1.1 $ $Da te: 2000/ 05/02 21:2 8:30 $ */ // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- Ins tance Vari ables Stri ng s ig="; jsess ionid ="; int foun dAt=- 1; if( debu g>0 ) cm.l og(" XXX R URI= " + r eques t.get Reque stUR I()); if ( (fou ndAt= reque st.ge tRequ estU RI(). index Of(si g))!= -1){ sess ionId =requ est.g etReq uest URI() .subs tring (foun dAt+ sig.l ength ()); // r ewrit e URL , do I nee d to do a nythi ng mo re? requ est.s etReq uestU RI(re ques t.get Reque stURI ().su bstr ing(0 , fou ndAt) ); sess ionId =vali dateS essio nId( reque st, s essio nId); if ( sessi onId! =null ){ reque st.se tRequ ested Sess ionId FromU RL(tr ue); } } retu rn 0 ; // ---- ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- Pub lic Methods import java. io.I OExce ption ; /** * Confi gure this comp onent , bas ed o n the spec ified conf igur ation * param eter s. T his m ethod shou ld b e cal led i mmedi ately aft er th e * compo nent inst ance is cr eated , an d bef ore < code> start ()</ code> * is ca lled . * * @para m pa ramet ers C onfig urati on p arame ters for t his c ompo nent * (<B> FIXM E: Wh at ob ject type shou ld th is re ally be?) * * @exce ptio n Ill egalS tateE xcept ion if th is co mpone nt ha s al ready been * conf igur ed an d/or start ed * @exce ptio n Lif ecycl eExce ption if this compo nent detec ts a fata l err or * in t he c onfig urati on pa ramet ers it wa s giv en */ pub lic v oid confi gure( Node param eter s) thro ws L ifecy cleEx cepti on { public final cla ss St andar dMana ger ext ends Mana gerBa se imp lemen ts L ifecy cle, Runna ble { } } if ( thisI nterv al > inact iveI nterv al) { inval idate (); /** * Core impl emen tatio n of a ser ver s essi on * * @aut hor J ames Dunc an Da vidso n [du ncan @eng. sun.c om] * @aut hor J ames Todd [gon zo@en g.sun .com ] */ // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- - Li fecyc le Me thods java. io.I OExce ption ; java. util .Enum erati on; java. util .Hash table ; java. util .Vect or; org.a pach e.tom cat.c atali na.*; javax .ser vlet. http. Cooki e; javax .ser vlet. http. HttpS essio n; org.a pach e.tom cat.u til.S tring Mana ger; org.w 3c.d om.Na medNo deMap ; org.w 3c.d om.No de; } /** * Retur n an <cod e>Enu merat ion</ code > of <code>S tring </co de> o bject s * conta inin g the name s of the o bjec ts bo und t o thi s session . * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is called on an * inva lida ted s essio n */ pub lic E nume ratio n get Attri buteN ames () { StandardSessionManager package org. apac he.to mcat. sessi on; package org. apac he.to mcat. sessi on; import import import import import import import import import import } // A ccum ulate the names of s eria lizab le at tribu tes Vect or r esult s = n ew Ve ctor( ); Enum erat ion a ttrs = get Attri bute Names (); whil e (a ttrs. hasMo reEle ments ()) { Stri ng at tr = (Stri ng) a ttrs .next Eleme nt(); Obje ct va lue = attr ibute s.ge t(att r); if ( value inst anceo f Ser iali zable ) resul ts.ad dElem ent(a ttr) ; } retu rn ( attri butes .get( name) ); } resp onse .addH eader ( Coo kieTo ols. getCo okieH eader Name( cook ie), Coo kieTo ols. getCo okieH eader Value (coo kie)) ; cook ie.s etVer sion( 0); resp onse .addH eader ( Coo kieTo ols. getCo okieH eader Name( cook ie), Coo kieTo ols. getCo okieH eader Value (coo kie)) ; pub lic v oid setCo ntext Manag er( C onte xtMan ager cm ) { this .cm= cm; } // W rite the scala r ins tance var iable s (ex cept Manag er) stre am.w riteO bject (new Long( crea tionT ime)) ; stre am.w riteO bject (id); stre am.w riteO bject (new Long( last Acces sedTi me)); stre am.w riteO bject (new Integ er(m axIna ctive Inter val)) ; stre am.w riteO bject (new Boole an(i sNew) ); stre am.w riteO bject (new Boole an(i sVali d)); retu rn ( this. id); } Cook ie c ookie = ne w Coo kie(" JSES SIONI D", r eqSe ssion Id); cook ie.s etMax Age(- 1); cook ie.s etPat h(ses sionP ath); cook ie.s etVer sion( 1); pub lic v oid setDe bug( int i ) { Syst em.o ut.pr intln ("Set debu g to " + i); debu g=i; } } /** * Retur n th e ses sion conte xt wi th w hich this sessi on is associa ted. * * @depr ecat ed As of V ersio n 2.1 , th is me thod is de preca ted and has no * repl acem ent. It w ill b e rem oved in a futu re ve rsion of the * Java Ser vlet API. */ pub lic H ttpS essio nCont ext g etSes sion Conte xt() { thro w new Ille galSt ateEx cept ion(m sg); pub lic H ttpS essio nCont ext g etSes sion Conte xt() { retu rn n ew Se ssion Conte xtImp l(); } // G S, p iggyb ack t he jv m rou te o n the sess ion i d. if(! sess ionPa th.eq uals( "/")) { Stri ng jv mRout e = r reque st.g etJvm Route (); if(n ull ! = jvm Route ) { reqSe ssion Id = reqSe ssio nId + SESS IONID _ROUT E_SE P + j vmRou te; } } // GS, s epar ates the s essio n id from the jvm r oute sta tic f inal char SESS IONID _ROUT E_SE P = ' .'; int debu g=0; Con textM anag er cm ; // D eser ializ e the attr ibute cou nt an d att ribut e val ues int n = ((Int eger) stre am.re adOb ject( )).in tValu e(); for (int i = 0; i < n; i++) { Stri ng na me = (Stri ng) s trea m.rea dObje ct(); Obje ct va lue = (Obj ect) stre am.re adObj ect() ; attr ibute s.put (name , val ue); } ((Ht tpSes sionB indin gList ener )o).v alueU nboun d(e); valu es.r emove (name ); } pub lic l ong getCr eatio nTime () { if ( vali d) { retu rn cr eatio nTime ; } el se { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.se ssio n.ise "); // G S, s et th e pat h att ribut e to the cooki e. Th is wa y // m ulti ple s essio n coo kies can be us ed, o ne fo r eac h // c onte xt. Stri ng s essio nPath = rr eques t.ge tCont ext() .getP ath() ; if(s essi onPat h.len gth() == 0 ) { sess ionPa th = "/"; } /** * Will proc ess the r eques t and dete rmin e the sess ion I d, an d se t it * in t he Re ques t. * It a lso m arks the sessi on as acce ssed . * * This impl emen tatio n onl y han dles Cook ies s essio ns, p lease ext end o r * add new i nter cepto rs fo r oth er me thod s. * */ public class Ses sionI nterc eptor exte nds Base Inter cepto r imp leme nts R eques tInte rcept or { // D eser ializ e the scal ar in stan ce va riabl es (e xcept Man ager) crea tion Time = ((L ong) strea m.re adObj ect() ).lon gValu e(); id = (St ring) stre am.re adObj ect( ); last Acce ssedT ime = ((Lo ng) s trea m.rea dObje ct()) .long Valu e(); maxI nact iveIn terva l = ( (Inte ger) stre am.re adObj ect() ).in tValu e(); isNe w = ((Boo lean) stre am.re adOb ject( )).bo olean Value (); isVa lid = ((B oolea n) st ream. read Objec t()). boole anVal ue() ; /** * Retur n th e tim e whe n thi s ses sion was creat ed, i n millise conds sin ce * midni ght, Janu ary 1 , 197 0 GMT . * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is called on an * inva lida ted s essio n */ pub lic l ong getCr eatio nTime () { } thro w new Ille galSt ateEx cept ion(m sg); } } pub lic i nt b efore Body( Requ est r requ est, Respo nse r espon se ) { Stri ng r eqSes sionI d = r espon se.g etSes sionI d(); if( debu g>0 ) cm.l og("B efore Bod y " + reqS essio nId ) ; if( reqS essio nId== null) retu rn 0; import org.a pach e.tom cat.c ore.* ; import org.a pach e.tom cat.u til.* ; import java. io.* ; import java. net. *; import java. util .*; import javax .ser vlet. http. *; pub lic S essi onInt ercep tor() { } } StandardManager package org. apac he.to mcat. reque st; this .isN ew = isNew ; } /** * Set t he < code> isVal id</c ode> flag for this sessi on. * * @para m is Valid The new v alue for the <code>i sVali d</c ode> flag */ voi d set Vali d(boo lean isVal id) { thro w new Ille galSt ateEx cept ion(m sg); } if ( thisI nterv al > inact iveI nterv al) { inval idate (); } } SessionInterceptor } // T ell our M anage r tha t thi s Se ssion has been recyc led if ( (man ager != nu ll) & & (ma nage r ins tance of Manager Base) ) ((Ma nager Base) mana ger). recy cle(t his); name s.co pyInt o(val ueNam es); this .ina ctive Inter val = cont ext. getSe ssion TimeO ut(); } /** * Remov e th e obj ect b ound with the speci fied name from this sess ion. If * the s essi on do es no t hav e an obje ct bo und w ith t his n ame, this meth od * does noth ing. * <p> * After thi s met hod e xecut es, a nd i f the obje ct im pleme nts * <code >Htt pSess ionBi nding Liste ner< /code >, th e con taine r ca lls * <code >val ueUnb ound( )</co de> o n th e obj ect. * * @para m na me Na me of the objec t to remo ve fr om th is se ssio n. * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is ca lled on a n * inva lida ted s essio n * * @depr ecat ed As of V ersio n 2.2 , th is me thod is re place d by * <cod e>re moveA ttrib ute() </cod e> */ pub lic v oid remov eValu e(Str ing n ame) { /** * Relea se a ll ob ject refer ences , an d ini tiali ze in stanc e variabl es, i n * prepa rati on fo r reu se of this obj ect. */ pub lic v oid recyc le() { /** * The t ime this sessi on wa s cre ated , in millise conds sin ce mi dnigh t, * Janua ry 1 , 197 0 GMT . */ pri vate long crea tionT ime = 0L; /** * @depr ecat ed */ pub lic S trin g[] g etVal ueNam es() { Enum erat ion e = ge tAttr ibute Name s(); Vect or n ames = new Vect or(); App licat ionS essio n(Str ing i d, Se rver Sessi on se rverS essio n, Cont ext conte xt) { this .ser verSe ssion = se rverS essi on; this .con text = con text; this .id = id; } // M ark this sessi on as inva lid setV alid (fals e); } } } whil e (e .hasM oreEl ement s()) { name s.add Eleme nt(e. nextE leme nt()) ; } sync hron ized (attr ibute s) { Obje ct ob ject = att ribut es.g et(na me); if ( objec t == null) retur n; attr ibute s.rem ove(n ame); // S ystem .out. print ln( "Remo ving attri bute " + name ); if ( objec t ins tance of Ht tpSe ssion Bindi ngLis tener ) { ((Htt pSess ionBi nding List ener) obje ct).v alueU nbou nd ( new H ttpSe ssion Bind ingEv ent(( HttpS essio n) t his, name) ); } } if ( (man ager != nu ll) & & (ma nage r ins tance of Manager Base) ) ((Ma nager Base) mana ger). remo ve(th is); supe r(); this .man ager = man ager; pub lic O bjec t get Attri bute( Strin g na me) { if ( ! va lid) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.se ssio n.ise "); Throughout the Software // R emov e thi s ses sion from our manag er's activ e session s valu es.p ut(na me, v alue) ; import org.a pach e.tom cat.c ore.* ; import org.a pach e.tom cat.u til.S tring Mana ger; import java. io.* ; import java. net. *; import java. util .*; import javax .ser vlet. *; import javax .ser vlet. http. *; setA ttri bute( name, valu e); /** * Remov e th e obj ect b ound with the speci fied name from this sess ion. If * the s essi on do es no t hav e an obje ct bo und w ith t his n ame, this meth od * does noth ing. * <p> * After thi s met hod e xecut es, a nd i f the obje ct im pleme nts * <code >Htt pSess ionBi nding Liste ner< /code >, th e con taine r ca lls * <code >val ueUnb ound( )</co de> o n th e obj ect. * * @para m na me Na me of the objec t to remo ve fr om th is se ssio n. * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is ca lled on a n * inva lida ted s essio n */ pub lic v oid remov eAttr ibute (Stri ng n ame) { /** * Perfo rm t he in terna l pro cessi ng r equir ed to inva lidat e this se ssion , * witho ut t rigge ring an ex cepti on i f the sess ion h as already expi red. */ pub lic v oid expir e() { // remov e an y exi sting bind ing if ( valu e != null && va lue i nsta nceof Http Sessi onBin ding Liste ner) { Http Sessi onBin dingE vent e = new H ttpSe ssion Bindi ngEv ent(t his, name) ; /** * @depr ecat ed */ pub lic O bjec t get Value (Stri ng na me) { retu rn g etAtt ribut e(nam e); } Session Expiration is Distributed } this .las tAcce ssedT ime = this .thi sAcce ssedT ime; this .thi sAcce ssedT ime = Syst em.c urren tTime Milli s(); this .isN ew=fa lse; } // ----- ---- ----- ----- ----- ----- ---- ----- ----- --------- ----- - Co nstru ctors } * Bind an o bject to t his s essio n, u sing the s pecif ied n ame. If an ob ject * of th e sa me na me is alre ady b ound to t his s essio n, th e ob ject is * repla ced. * <p> * After thi s met hod e xecut es, a nd i f the obje ct im pleme nts * <code >Htt pSess ionBi nding Liste ner< /code >, th e con taine r ca lls * <code >val ueBou nd()< /code > on the objec t. * * @para m na me Na me to whic h the obj ect i s bou nd, c annot be null * @para m va lue O bject to b e bou nd, canno t be null * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is ca lled on a n * inva lida ted s essio n * * @depr ecat ed As of V ersio n 2.2 , th is me thod is re place d by * <cod e>se tAttr ibute ()</c ode> */ pub lic v oid putVa lue(S tring name , Ob ject value ) { retu rn ( (Http Sessi on) t his); } pri vate Hash table sess ions = new Has htabl e(); pri vate Reap er re aper; if ( appSe ssion != n ull) { appSe ssion .inva lidat e(); } pri vate Serv erSes sionM anage r() { reap er = Reap er.ge tReap er(); reap er.s etSer verSe ssion Manag er(t his); reap er.s tart( ); } } } /** * Used by c ontex t to confi gure the sessi on ma nager 's in acti vity timeo ut. * * The S essi onMan ager may h ave s ome defau lt se ssion time out , the * Conte xt o n the othe r han d has it' s tim eout set b y the dep loyme nt * descr ipto r (we b.xml ). Th is me thod lets the Conte xt co nfor gure the * sessi on m anage r acc ordin g to this valu e. * * @para m mi nutes The sessi on in acti vity timeo ut in minu tes. */ pub lic v oid setSe ssion TimeO ut(in t mi nutes ) { if(- 1 != minu tes) { // T he ma nager work s wit h se conds ... inac tiveI nterv al = (minu tes * 60) ; } } pub lic v oid acces sed( Conte xt ct x, R eques t req , Str ing i d ) { Appl icat ionSe ssion apS= (Appl icat ionSe ssion )find Sessi on( ctx, id); if( apS= =null ) ret urn; Serv erSe ssion serv S=apS .getS erve rSess ion() ; serv S.ac cesse d(); apS. acce ssed( ); } } // c ache it - no n eed t o com pute it a gain req. setS essio n( ap S ); } pub lic H ttpS essio n cre ateSe ssion (Con text ctx) { Stri ng s essio nId = Sess ionId Gene rator .gene rateI d(); Serv erSe ssion sess ion = new Serv erSes sion( sessi onId) ; sess ions .put( sessi onId, sess ion) ; } // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- Publ ic Me thods } /** * Const ruct and retur n a n ew se ssio n obj ect, based on t he d efaul t * setti ngs speci fied by th is Ma nage r's p roper ties. The ses sion * id wi ll b e ass igned by t his m etho d, an d ava ilabl e via the getI d() * metho d of the retur ned s essio n. If a new s essio n can not be cr eated * for a ny r eason , ret urn < code> null </cod e>. * * @exce ptio n Ill egalS tateE xcept ion if a new s essio n can not be * inst anti ated for a ny re ason */ pub lic S essi on cr eateS essio n() { /** * Start the back groun d thr ead t hat will perio dical ly ch eck for * sessi on t imeou ts. */ pri vate void thre adSta rt() { if ( thre ad != null ) retu rn; thre adDo ne = false ; thre ad = new Threa d(thi s, th read Name) ; thre ad.s etDae mon(t rue); thre ad.s tart( ); if ( (max Activ eSess ions >= 0) && (s essi ons.s ize() >= m axAct iveS essio ns)) thro w new Ille galSt ateEx cept ion (sm.g etStr ing(" stand ardM anage r.cre ateSe ssion .ise ")); } /** * Stop the backg round thre ad th at i s per iodic ally check ing for * sessi on t imeou ts. */ pri vate void thre adSto p() { retu rn ( super .crea teSes sion( )); } if ( thre ad == null ) retu rn; } thre adDo ne = true; thre ad.i nterr upt() ; try { thre ad.jo in(); } ca tch (Inte rrupt edExc eptio n e) { ; } if(- 1 != inac tiveI nterv al) { sess ion. setMa xInac tiveI nterv al(i nacti veInt erval ); } retu rn s essio n.get Appli catio nSes sion( ctx, true ); retu rn ( this. isNew ); } } thre ad = null ; pub lic H ttpS essio n fin dSess ion(C onte xt ct x, St ring id) { Serv erSe ssion sSes sion= (Serv erSe ssion )sess ions. get(i d); if(s Sess ion== null) retu rn nu ll; } retu rn s Sessi on.ge tAppl icati onSe ssion (ctx, fals e); // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- - Ba ckgro und T hread } appS essio n.val idate (); this .max Inact iveIn terva l = i nter val; } } /** * The b ackg round thre ad th at ch ecks for sessi on ti meout s an d shu tdown . */ pub lic v oid run() { } } } voi d val idat e() // L oop until the termi natio n se mapho re is set whil e (! threa dDone ) { thre adSle ep(); proc essEx pires (); } } } © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -10 AOSD Promotes Separation of Concerns For Improved SWD&D, AOSD Promotes Separation of All Important Concerns of an Application. Design and Implementation Languages Should Provide Decomposition Mechanisms Understand the Requirements By Partitioning Problem into Pieces Composition Mechanisms Provide Infrastructure to Assemble Pieces of System into a Whole More Powerful Decomposition Mechanisms Needed on Top of OOP! Security UML -11 Separation of Concerns Separation of Concerns Approaches have Driven Towards Formalism since Early Days R&D Efforts include: William Harrison , Harold Ossher, Subject-oriented programming: a critique of pure objects, Proc. of OOPSLA, 1993, Washington, D.C. Siobhán Clarke, Composition of object-oriented software design models, Dublin City Univ., 2001. Gregor Kiczales et al. Aspect-Oriented Programming. Proc. Of ECOOP, 1997. E. Gamma, R. Helm, R. Johnson, J. Vlissides. Design Patterns. Addison Wesley,1995. Security UML -12 Model Composition New approach for Designing Software Systems Based in previous work in “Subject-Oriented Programming” System decomposed into Subjects Each Subject represents a Particular “Subjective” View of the System Subjects Akin to Roles Decompose Software into Different views that may Represent Specific Concerns Integrate all Views into one Final Model by using Specific Model Composition Rules UML Extensions are Subjects and Composition Relationships Security UML -13 +save() +load() +save() +log() +load() Model Composition Pictorially <<subject>> Persistence Student Course +save() +load() +save() +load() match[name] +log() match[name] <<subject>> Logging <<subject>> courseware match[name] Student Student +log() Course +log() +getName() +getAddress() Course +getCredits() +getInstructor() match[name] match[name] <<subject>> courseware Student +getName() +getAddress() Course +getCredits() +getInstructor() Student +getName() +getAddress() +save() +load() +log() Student Course Course +getCredits() +getInstructor() +save() +load() +log() Security UML -14 Applying AOP to Large Application Java Code with 10,000 Files and 500 Developers Existing Policy Implementations for Logging, Error Handling, and Profiling Affect Every File 5-30 Page Policy Documents Applied By Developers Affect Every Developer Must Understand Policy Document Repeated for New Code Assets Awkward to Support Variants Complicates Product Line Don’t Even Think About Changing the Policy Policy Evolution Not Supported © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -15 Policies Re-implemented with AOP AOP Captures Logging, Error Handling, And Profiling Policies Compatible with Existing Code Base and Platform Support Policy Evolution Result: One Reusable Crosscutting Module Policy Captured Explicitly Applies Policy Uniformly For All Time Written By Central Team No Burden On Other 492 Developers Automatically Applied to New Code Easy Plug/Unplug - Simplifies Product Line Issues Changes to Policy Happen in One Place © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -16 Existing Implementation Problem: session tracking is not modularized HTTPRequest getCookies() getRequestURI()(doc) getSession() getRequestedSessionId() SessionInterceptor requestMap(request) beforeBody(req, resp) ... Session HTTPResponse getAttribute(name) setAttribute(name, val) invalidate() ... getRequest() setContentType(contentType) getOutptutStream() setSessionId(id) ... Servlet © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -17 Result – High Cost of Tangled Code Redundant Code Same Fragment of Code in Many Places Difficult To Reason About Non-explicit Structure Big Picture of the Tangling isn’t Clear Difficult To Change Have to Find All The Code Involved And Be Sure to Change it Consistently And Be Sure Not to Break it By Accident © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -18 Establish a Crosscutting Concern HTTPRequest getCookies() getRequestURI()(doc) getSession() getRequestedSessionId() ... SessionInterceptor requestMap(request) beforeBody(req, resp) ... Session HTTPResponse getAttribute(name) setAttribute(name, val) invalidate() ... getRequest() setContentType(contentType) getOutptutStream() setSessionId(id) ... Servlet © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -19 What are Cross-Cutting Concerns? Critical Aspects of Large Systems Don’t Fit Traditional Modules Logging and Error Handling Synchronization & Security Memory Management Performance Optimizations Tangled Code Has A Cost Difficult To Understand and Change Increases With Size of System Maintenance Costs Are Huge Programmers Work Hard to Eliminate Tangled Code Last 10% of Tangled Code Causes 90% of Development and Maintenance Headaches © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -20 What is AOSD? AOSD seeks to Provide a Means to Compartmentalize Cross-Cutting Concerns which are Tangled/Distributed Across the Application Critical to Application w.r.t. Indispensable Features Security, Logging, Synchronization Performance Tracking and Optimization AOSD Must Provide Means to Identify, Separate, Represent, and Compose Cross-Cutting Concerns Ability to Model Cross Cutting Concerns Describe the Integration and Interplay of Concerns Abstractions Consistent with OO Design Security UML -21 Symptoms of Cross-Cutting Concerns Scattering: Occurs when a Concern is Implemented in Several Separated Places (Different Classes) in the Code Code Related to a Single Concern is Spread Across Several Modules Tangling: Occurs when a Modular Unit of an Application Implements Several Requirements Code Unrelated to Primary Concern of a Module is Mixed In with Other Code Interface Pollution: Interface Requirements of Several Clients are Combined into A Single Interface Public Interfaces are Combinations of All Requirements for All Users – A Muddle of Concerns Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Security UML -22 Consider Another Example Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Security UML -23 Consider Another Example Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Security UML -24 Consider Another Example Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Security UML -25 Consider Another Example //C++ Solution Would Require Specific and //Difficult to Modify Code SecurityManager security = System.getSecurityManager(); try{ security.checkXXX(argument, . . . ); } catch (SecurityException secexp){ //code for denial of access //… } Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Security UML -26 Solution Separates the Concerns Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Security UML -27 Objective of AOSD Capture Orthogonal Design Requirements Replicated Class Hierarchy Each Color is a Different Cross-Cutting Concern Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Security UML -28 Combining Cross Cutting Concerns Multiple Abstraction Models must be Superimposed with one Another to Yield the Combined Model This Combined Model Represents Conjoined Functionality of All Cross Cutting Concerns Reciprocally, Decomposition Also Available Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Security UML -29 Benefits of AOSD Increased Comprehensibility: Source Code Reduction Better Separation of Concerns: Decomposition of Modules Previously Considered Monolithic More Adaptable and Easier to Reuse (Hard to Prove) Simpler System Evolution: Increased Modularity of AOSD Solutions mean fewer modules are affected when modules Change SWE Specialization via Application Transparency Some Concerns, Such as Security, Can Be Better Implemented Act of Implementing Additional Orthogonal Concerns to an Oblivious Base Functionality via Successive Application of Overlapping Modules Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Source: Elrad and Coady Security UML -30 Benefits of AOSD/AOP Value • support variability • untangle auxiliary concerns • improve quality • reduce code size • simplify problem solving • retire legacy tools Add Aspects to Code • untangle core concerns • simple, natural code • improve agility • leverage experts • improve analysis Restructure Code for Aspects • architectural quality & flexibility • reuse • extensibility Aspect-Oriented Architecture © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -31 Other Thoughts on Aspects Tyranny of Decomposition: “…the tyranny of the dominant decomposition is the single most significant cause of the failure, to date, to achieve many of the expected benefits of separation of concerns” Ossher and Tarr, HyperJ User Manual, 2001 Principle of Obliviousness “ … allows programmers to make quantified programmatic assertions over programs written by programmer oblivious to such assertions” Filman & Friedman, “AOP is Quantification and Obliviousness” Security “As things exist today, you must touch so many disparate pieces to create a tight security policy. In reality, most individual developers cannot build concurrent, secure, distributed system. Therefore, by rallying security around an aspect you compartmentalize the problem…” Booch, UML Architect, April 2003 Source: Elrad and Coady Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Security UML -32 Aspect-Oriented Programming (AOP) Crosscutting is Inherent in Complex Systems Crosscutting Concerns have a Clear Purpose and a Natural Structure Defined Set of Methods, Module Boundary Crossings, Points of Resource Utilization, Lines of Dataflow… So, Let’s Capture the Structure of Crosscutting Concerns Explicitly... In a Modular Way With Linguistic and Tool Support Aspects Are Well-modularized Crosscutting Concerns © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -33 Aspects in AOP Requirements that Crosscut Several Viewpoints (or Concerns) are Realized as Aspects Aspects are one Category of Concern[Kiczales97]: “… an aspect is a (program) property that cannot be cleanly encapsulated in a generalized procedure (such as an object, method, procedure, or API)” Aspects are a Distributed Abstraction that can Capture the Same Concern Across Multiple Classes As Such, Aspects are Modular Entities These Module Entities are “Woven” into the Application at Specific “Join Points” Weaving Distributes the Aspect Across the Application into Targeted Areas Aspects to be Woven are Decided by SWE Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Security UML -34 Objective in Discussing AOP Using AOP And AspectJ to: Improve the Modularity of Crosscutting Concerns Design Modularity Source Code Modularity Development Process Aspects are Two Things: Concerns That Crosscut [design Level] A Programming Construct [Implementation Level] Crosscutting Concerns Captured in Modular Units AspectJ An AO Extension to Java™ that Supports Generalpurpose Aspect-Oriented Programming © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -35 AOP in AspectJ™ A Small and Well-integrated Extension to Java™ Outputs .Class Files Compatible with Any JVM All Java Programs are AspectJ Programs A General-purpose AO Language Just as Java is a General-purpose OO Language Includes IDE Support Emacs, Jbuilder, Forte 4J, Eclipse Freely Available Implementation Compiler is Open Source Active User Community Aspectj-users@eclipse.Org © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -36 AspectJ Technology (www.aspectj.org) AspectJ is a small extension to Java™ Valid Java Programs are Valid AspectJ Programs AspectJ has its own Compiler, ajc Runs on Java 2 platform (Java 1.3 and later) Produces Java platform-compatible .class files (JDK 1.1 and later) AspectJ tools support IDE extensions: Emacs, JBuilder, Forte4J, Eclipse ajdoc to parallel javadoc JPDA Debugger Integration (JSR 45 support) License Compiler, Runtime and Tools are free for any use Compiler and tools are Open Source © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -37 Modularizing Crosscutting Concerns Each Class Should Contain Only the Code that is Specifically Related to its Main Function Survey_List Class Should Only Contain Code to Manage Surveys, and Nothing Else E.g., Don’t Track Logging of Actions, Security, Database Interactions, etc. Crosscutting Code Removed from each Class Should be Factorized into a Specific Place Isolate in a Module the Access Control Code Scattered in the Survey Management Application These are the Concerns that are Identified that Impact all Application Classes Allow Select/Customized Concerns to be Added as Desired by Application Designers/Builders Security UML -38 Aspect as a Modular Unit AOSD/AOP Utilizes a New Modular Unit to Isolate Crosscutting Concerns – Referred to as an Aspect Three Components of an Aspect Join Points – Location of Code Where Application Behavior Will be Modified Pointcuts – Named Collection of Join Points with a Common Basis Advices – The Code that is Attached to a Pointcut (or set of Pointcuts) to Realize Aspect’s Function Integration of an Aspect into the Application is Achieved via a Mechanism called Weaving. Security UML -39 Join Points Specific Place in the Application where we Want to Alter the Behavior of the Application Examples Method Call, Attribute Assignment Exception Raising, Creation of an Object In Example Below, Joint Point Intercepts Call from Add_Survey_Header to Create_Survey_Header Intercept could Log, Check Security, etc. Create_Survey_Header Add_Survey_Header Survey_List Join Point Survey Header Security UML -40 Join Points key points in dynamic call graph Intercepting Calls Among Classes Instance x dispatch Instance a method execution returning or throwing a method call returning or throwing dispatch Y a method execution returning or throwing © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -41 Join Point Terminology Several Kinds of Join Points Method & Constructor Call Method & Constructor Execution Field Get & Set Exception Handler Execution Static & Dynamic Initialization Instance x dispatch method execution join points method call join points © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -42 Pointcuts A Pointcut is a Named Collection of Join Points Sharing Specific Static Properties A Pointcut is a Kind of Predicate on Join Points that: Can Match or Not Match Any Given Join Point And Optionally, Can Pull Out Some of The Values at that Join Point For example - in AspectJ … Pointcuts are Defined with Quantified Boolean Formulas Defined over Method Names, Class Names, Control Flow or Lexical Scopes Security UML -43 Pointcuts Survey Management Example call(* Survey_Header.Create_Survey_Header( )) Pointcuts can be Composed using AND (&&), OR (||), and NOT (!) For Example: call(* Survey_Header.Create_Survey_Header( )) || call(* Survey_Header.Add_Question()) Call Occurs Whenever a Survey_Header Instance Receives Either of the Two Method Calls\ Pointcuts can Also be Named: pointcut login( ): call(User SecurityAdmin.logIn(..)); User is a Return Type, SecurityAdmin a Class Security UML -44 Advice in an Aspect Advice is a Code Fragment that Implements Part of an Aspect and is Intended to be Woven with the Main Program at Specific Join Points Implements Part of the Specific Concern that is Modularized in the Aspect Three Types of Advice Before – Action (Code) Invoked Prior to a Join Point After – Action (Code) Invoked After Returning from a Join Point Around – On Arrival at Join Point, Provides Explicit Control Over When and If Program Proceeds Security UML -45 Advice in an Aspect Advices are Examples of Constraint Checking Indicate the Constraints that Must be Verified Before and After Indicate the Conditions that Must be True and Guide the Execution – Around Conceptually: Before Pre-condition – is Parameter Valid After Post-Condition – Check Whether values were Set Around Force Parameters to be Valid Before Continuing This Controls the When and If of Execution Security UML -46 Before Advice – Pre-condition aspect PointBoundsPreCondition { before(int newX): call(void Point.setX(int)) && args(newX) { assert(newX >= MIN_X); what follows the ‘:’ is assert(newX <= MAX_X); always a pointcut – } primitive or user-defined before(int newY): call(void Point.setY(int)) && args(newY) { assert(newY >= MIN_Y); assert(newY <= MAX_Y); } private void assert(boolean v) { if ( !v ) throw new RuntimeException(); } } © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -47 After Advice – Post-condition aspect PointBoundsPostCondition { after(Point p, int newX) returning: call(void Point.setX(int)) && target(p) && args(newX) { assert(p.getX() == newX); } after(Point p, int newY) returning: call(void Point.setY(int)) && target(p) && args(newY) { assert(p.getY() == newY); } private void assert(boolean v) { if ( !v ) throw new RuntimeException(); } } © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -48 Around Advice – Condition Enforcement aspect PointBoundsEnforcement { void around(int newX): call(void Point.setX(int)) && args(newX) { proceed(clip(newX, MIN_X, MAX_X)); } void around(int newY): call(void Point.setY(int)) && args(newY) { proceed(clip(newY, MIN_Y, MAX_Y)); } private int clip(int val, int min, int max) { return Math.max(min, Math.min(max, val)); } } © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -49 A Second AOP Example A Library Items Locator that Maintains a Relation Between Library Items and Their Physical Locations Security UML -50 Synchronization in Example Exclusion Constraints Concurrent Execution of Methods within a Single Object Which Operations are Allowed to Execute Concurrently and Which are Not State Constraints Conditions Under Which Operations Can Be Applied to an Object May Involve Object’s Internal State or Environment State Coordination Constraints Coordinating Operations Involving a Number of Independent Objects Security UML -51 Incorporating Synchronization without AOP public class LibraryItemLocator { protected Item items[]; protected Location locations[]; protected int nitems; protected int activeReaders = 0; protected int activeWriters = 0; public Location locate(String str) throws ItemNotFound{ Item an_item = items[0]; int i = 0; boolean found = false; Location l; } if (found == false) { synchronized(this) { --activeReaders; notifyAll(); } throw new ItemNotFound (str); } l = locations[i]; synchronized(this) { --activeReaders; notifyAll(); } return l; } synchronized(this) { while (activeWriters > 0) { try{ wait(); } catch (InterruptedException e) {} } ++activeReaders; } while (i < nitems && found == false) { if (an_item.get_title().compareTo(str) == 0) found = true; else an_item = items[++i]; } Security UML -52 Incorporating Synchronization without AOP public class BookLocator extends LibraryItemLocator{ public register() { beforewrite(); if (nitems > items.length()) { afterwrite(); throw new LocatorFull(); } else { public class JournalLocator extends LibraryItemLocator { public register() { beforewrite(); if (nitems > items.length()) { afterwrite(); throw new LocatorFull(); } else { //journal specific registration code //book specific registration code } afterwrite(); } afterwrite(); } protected synchronized void beforewrite() { while ((activeReaders > 0) || (activeWrites > 0)) { try { wait(); } catch (InterruptedException e) {} } ++activeWriters; } protected synchronized void afterwrite() { --activeWriters; notifyAll(); } } protected synchronized void beforewrite() { while ((activeReaders > 0) || (activeWrites > 0)) { try { wait(); } catch (InterruptedException e) {} } ++activeWriters; } protected synchronized void afterwrite() { --activeWriters; notifyAll(); } } } Security UML -53 AOP Modeling of Sychronization Security UML -54 Example in AOP and Aspect J public class LibraryItemLocator { protected Item items[]; protected Location locations[]; protected int nitems; public Location locate(String str) throws ItemNotFound{ Item an_item = items[0]; int i = 0; boolean found = false; Location l; while (i < nitems && found == false) { if (an_item.get_title().compareTo(str) == 0) found = true; else an_item = items[++i]; } if (found == false) { throw new ItemNotFound (str); } l = locations[i]; return l; } public class BookLocator extends LibraryItemLocator{ public register() { } //book specific registration code } public class JournalLocator extends LibraryItemLocator { public register() { } //journal specific registration code } } Security UML -55 Example in AOP and Aspect J public aspect Locator_Coordinator { private int activeReaders = 0; private int activeWriters = 0; before(): read() { pointcut write(): execution(public * *.register(..)) pointcut read(): execution(public * *.locate(..)) before(): write() { synchronized(thisJointPoint.get This()) { while ((activeReaders > 0) || (activeWrites > 0)) { try { wait(); } catch (InterruptedException e) {} } ++activeWriters; } if (nitems > items.length()) throw new LocatorFull(); } after(): write() { synchronized(thisJointPoint.get This()) { --activeWriters; notifyAll(); } } } } synchronized(thisJointPoint.getTh is()) { while (activeWrites > 0) { try{ wait (); } catch (InterruptedException e) {} } ++activeReaders; after() : read(){ } } synchronized(thisJointPoint.getTh is()) { --activeReaders; notifyAll(); } Security UML -56 Weaving Weaving Process is a Compilation Technique that Composes Multiple Concern-Specific Code into the Final Application Weaving Identifies Join Points in Pointcuts and Modifies the Code at each Site According to the Specified Advices Provides Facility to Integrate Each Aspect into the Application Code Integration is Customizable – Not all Aspects Must be Woven for Every Compilation Pick and Choose Aspects – Security, Logging, etc. Security UML -57 Weaving Process Varies Multiple AOP Models w.r.t. Weaving Jointpoint Interception (AspectJ) – Our Focus G. Kiczales AspectJ, AspectC, AspectC++ Hypermodules (HyperJ) Multidimensional Separation of Concerns Creates Hypermodules H. Ossher and P. Tarr Composition Filters (ComposeJ) AOP Technique where Different Aspects Expressed as Filters Filters are Declarative and Orthogonal Message Transformation Specifications U. Twente, Holland Slides from Jason Furlong, http://www.jasonfurlong.com/index.html Security UML -58 Concluding Remarks on AOP/AOSD AOSD Future – see aosd.net Language Design - Dynamic Crosscuts, Typing … Tools - More IDE Support, Aspect Discovery, Refactoring, Re-cutting, Crosscutting Views… Software Engineering - UML Extension, Finding Aspects, … Theory - Type System For Crosscutting, Faster Compilation, Advanced Crosscut Constructs, … AOP Future: Tools - Incremental Compilation, Refactoring Language Changes - Aspect Configuration, Extensible Pointcuts, Generic Types (Java 1.5), Dynamic Crosscut Constructs/Weaving © Copyright 1998-2002 Palo Alto Research Center Incorporated. All rights reserved. ApsectJ.org. Security UML -59