Mozart de Melo Paulo Tenório Universidade Federal de Alagoas As Origens Limitações da orientação a objetos Programação Orientada a Aspectos AspectJ Histórico Paradigma Estruturado Paradigma Orientado a Objeto O nível de complexidade dos programas vem sendo ampliado progressivamente, parte por conta de novas tecnologias e parte motivada pela crescente complexidade das soluções. Essa constatação tornou-se especialmente verdadeira a partir da segunda metade de década de 1980, quando alguns autores (Brooks, 1987; Cox, 1990; Wentzel, 1993) começaram a prever uma crise que logo recairia sobre a produção mundial de software: a demanda por software estaria crescendo mais rapidamente do que a capacidade de fornecimento, visto que os meios produtivos disponíveis eram ineficazes. Por muito tempo o objetivo da engenharia de software... “Decomposição IDEAL dos conceitos” importantes do problema a ser solucionado... Manutenibilidade, reuso, modularidade, evolução Software com qualidade Esse objetivo motivou a evolução dos paradigmas de programação (procedimental, estruturado, etc) até chegar na orientação a objetos Decomposição em procedimentos ou funções Banco Abrir Contacorrente Abrir Poupança Depositar Ver o saldo Sacar Tornar cliente especial Fechar conta Decomposição em classes de objetos Conta Cliente possui nome tipo 1 tornarEspecial() número valor abrir() 1..* fechar() sacar() depositar() verSaldo() conta-corrente A OO possibilita um nível maior de abstração quando comparada às metodologias anteriores, permitindo uma melhor representação dos elementos que compõem a solução de um problema. A orientação a objetos introduziu conceitos como encapsulamento, herança e polimorfismo, permitindo criar uma hierarquia de objetos que modelam um conjunto comum de comportamentos. Poupança limiteEspecial diaRendimento verSaldo() verSaldo() Dificuldade em determinar o que fará parte de uma classe ou de outra. Um objeto não deveria ter acesso a informações que não fazem parte do seu escopo. Todas as mudanças de estado de um objeto deveriam ocorrer somente pela da interface revelada por esse mesmo objeto. Uma classe deveria implementar apenas um elemento específico do domínio, executando a função com perfeição. As relações de herança entre as classes deveriam manter a coerência conceituai. O entrelaçamento entre classes deveria ser minimizado. Estas Dificuldades ocorrem porque o conjunto de abstrações disponibilizado pelo paradigma orientado a objetos apresenta limitações ao tratar comportamentos que se espalham pelos diversos objetos. Parte desses entraves poderia ser contornada utilizando-se padrões de projeto ou algumas extensões da OO, como é o caso da programação orientada a aspectos (POA). A Figura exemplifica a quebra de dois dos principais princípios da OO. O exemplo mostra uma parte do diagrama de classes de um editor de imagens. As classes Line e Point desse editor deveriam conter apenas código para implementação da linha e ponto. Display O desenvolvimento desta aplicação começou com dois interesses: Figure makePoint(..) makeLine(..) Point getX() getY() setX(int) setY(int) FigureElement moveBy(int, int) Line getP1() getP2() setP1(Point) setP2(Point) 1. 2. Representar os elementos gráficos Atualizar o Display quando esses elementos se movem class Line { private Point p1, p2; Point getP1() { return p1; } Point getP2() { return p2; } void setP1(Point p1) { this.p1 = p1; } void setP2(Point p2) { this.p2 = p2; } } class Point { private int x = 0, y = 0; int getX() { return x; } int getY() { return y; } void setX(int x) { this.x = x; } void setY(int y) { this.y = y; } } Adicionar o comportamento para atualizar o display. class Line { private Point p1, p2; Point getP1() { return p1; } Point getP2() { return p2; } void setP1(Point p1) { this.p1 = p1; Display.Updating() } void setP2(Point p2) { this.p2 = p2; Display.Updating() } } class Point { private int x = 0, y = 0; int getX() { return x; } int getY() { return y; } void setX(int x) { this.x = x; Display.Updating() } void setY(int y) { this.y = y; Display.Updating(); } } Adicionar o comportamento para atualizar o display. Caso fosse necessário implementar interesses adicionais no sistema, como persistência de dados, autenticação ou distribuição, haveria ocorrência de todos esses interesses dentro das classes Line e Point. Os interesses adicionais são forçados a fazer parte da implementação de um outro interesse, quebrando, desta forma, os preceitos da OO. A junção desses interesses leva o sistema à condição denominada código espalhado (scattering). Além do código espalhado, pode haver ainda a ocorrência do código emaranhado (tangled). Outro problema da OO consiste na característica essencialmente estática da classe. A OO prevê que, antes de se implementar qualquer elemento, deve-se ter clareza quanto aos requisitos necessários a serem atendidos. Sendo assim, é complicado lidar com mudanças de requisitos de um projeto, uma vez que eles podem ocasionar profundos impactos nele e, por conseguinte, no tempo necessário para o seu desenvolvimento. Imagine a hipótese de se ter de criar um registro de operações dos usuários em um sistema OO até então estável. Por uma mudança de requisitos, necessita-se que a essa classe seja adicionada a capacidade de geração de registros para auditoria (log). Interesse funcional É formado pelos aspectos relacionados ao domínio da aplicação e denomina-se interesses funcionais. Interesse sistêmico É formado por características de suporte à aplicação, como, por exemplo, o tratamento de exceções, que não fazem parte daquilo que pode ser chamado de lógica de negócio No caso do tratamento de exceções, quando implementado utilizando a OO, o código responsável por implementar esse interesse é inseparável do código onde a exceção poderá ser gerada, impedindo o agrupamento desse interesse em uma unidade elementar. Sendo assim, o código fica disperso na implementação. Os interesses sistêmicos também podem ser chamados de ortogonais ou transversais. A tentativa de implementar um interesse transversal tem como resultado códigos que se espalham por todo o programa. A implementação de vários interesses sistêmicos e funcionais em um mesmo módulo resulta no código chamado de emaranhado. Tradução Termo original Scattering Code Código espalhado Tangled Code Código emaranhado Descrição Código necessário para cumprir um interesse propagado em classes que precisam cumprir outros interesses, 0 Código espalhado pode ser dividido em duas categorias distintas: Bloco de Código Duplicado e Bloco de Código Complementar. Utilização de uma única classe ou método para implementar múltiplos interesses. 0 código emaranhado ocorre quando um módulo é implementado com múltiplos interesses simultaneamente. Um desenvolvedor freqüentemente considera os interesses como lógica do negócio, performance, sincronização, segurança, dentre outros, durante a implementação do módulo, Isso resulta na implementação de vários interesses em um mesmo módulo. Implementação do parser XML no Tomcat Para a implementação do parser XML, não há ocorrência de código espalhado, tendo em vista que o código para implementação do interesse encontra-se bem centralizado em uma única unidade, conforme demonstrado na Figura Implementação da funcionalidade de auditoria(log) no Tomcat Já para o desenvolvimento do interesse de auditoria (log), essa modularização não é alcançada, pois o código encontra-se espalhado por diversas classes no sistema, como podemos observar na Figura StandardSession 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 "); thro w new Ille galSt ateEx cept ion(m sg); package org. apac he.to mcat. sessi on; import java. io.I OExce ption ; import java. io.O bject Input Strea m; import java. io.O bject Outpu tStre am; import java. io.S erial izabl e; import java. util .Enum erati on; import java. util .Hash table ; import java. util .Vect or; import javax .ser vlet. Servl etExc eptio n; import javax .ser vlet. http. HttpS essio n; import javax.s ervle t.ht tp.Ht tpSes sionB indin gEve nt; import javax.s ervle t.ht tp.Ht tpSes sionB indin gLis tener ; import javax .ser vlet. http. HttpS essio nCon text; import org.a pach e.tom cat.c atali na.*; import org.a pach e.tom cat.u til.S tring Mana ger; } 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); } 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 ); /** * Stan dard impl ement ation of t he <b>Sess ion</ b> i nterf ace. This obje ct i s * 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 session supp ort. * <p> * <b>I MPLEM ENTA TION NOTE< /b>: An i nsta nce o f this cl ass r epre sents 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 declare d pub lic, Java logi c out side * of t he <code>o rg.ap ache .tomc at.se ssion </cod e> p ackag e cannot cast an * 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 $ */ 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 this ob ject * is th e fa cade. */ pub lic H ttpS essio n get Sessi on() { * 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); } // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- Sessi on Pu blic Meth ods /** * Updat e th e acc essed time info rmat ion f or th is session . Th is m ethod * shoul d be call ed by the conte xt w hen a requ est comes i n for a p artic ular * sessi on, even if th e app licat ion does not referen ce it . */ pub lic v oid acces s() { /** * 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 invalid ate t his sessi on, * witho ut t rigge ring an ex cepti on i f the sess ion has alr eady expi red. */ pub lic v oid expir e() { 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) ); } } // R emov e thi s ses sion from our manag er's active sessi ons if ( (man ager != nu ll) & & (ma nage r ins tance of Manager Base) ) ((Ma nager Base) mana ger). remo ve(th is); // U nbin d any obje cts a ssoci ated with this } session remo veVa lue(n ame); // 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) ; ((Ht tpSes sionB indin gList ener )valu e).va lueBo und(e ); } valu es.p ut(na me, v alue) ; } /** * @depr ecat ed */ pub lic O bjec t get Value (Stri ng na me) { retu rn g etAtt ribut e(nam e); } 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. *; 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 "); /** * 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") ; // ----- ---- ----- ----- ----- ----- ---- ----- ------- ----- ---- ---- Const ructo rs retu rn v alues .get( name) ; } /** * @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(); whil e (e .hasM oreEl ement s()) { name s.add Eleme nt(e. nextE leme nt()) ; } 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; Stri ng[] valu eName s = n ew St ring [name s.siz e()]; name s.co pyInt o(val ueNam es); retu rn v alueN ames; // ----- ---- ----- ----- ----- ----- ---- ----- ------- ----- -- I nstan ce Va riabl es /** * 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 Hashtab 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; /** * 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" ; this .ina ctive Inter val = cont ext. getSe ssion TimeO ut(); } 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); /** * 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) { } } 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; } // M ark this sessi on as inva lid setV alid (fals e); supe r(); this .man ager = man ager; } thro w new Ille galAr gumen tExc eptio n(msg ); public class App licat ionSe ssion impl emen ts Ht tpSes sion { 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 spe cifie d Ma nager . * * @para m ma nager The manag er wi th w hich this Se ssion is assoc iated */ pub lic S tand ardSe ssion (Mana ger m anag er) { /** * The l ast acces sed t ime f or th is Session . */ pri vate long last Acces sedTi me = creatio nTime ; /** * Relea se a ll ob ject refer ences , an d ini tiali ze instanc e var iabl es, i n * prepa rati on fo r reu se of this obj ect. */ pub lic v oid recyc le() { // R eset the insta nce v ariab les assoc iated with this Se ssion 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; remo veAt tribu te(na me); Hash tabl e val uesCl one = (Has htab le)va lues. clone (); 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( ); /** * 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; /** * @depr ecat ed */ pub lic v oid remov eValu e(Str ing n ame) { remo veAt tribu te(na me); } vali date (); } 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; thro w new Ille galSt ateEx cept ion(m sg); } if ( thisI nterv al > inact iveI nterv al) { inval idate (); } if ( name == n ull) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.va lue. iae") ; /** * The m axim um ti me in terva l, in sec onds, between clie nt r eques ts be fore * the s ervl et co ntain er ma y inv alid ate this se ssion . A nega tive time * indic ates that the sessi on sh ould neve r time ou t. */ pri vate int maxIn activ eInte rval = -1 ; /** * 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) { recycle d if ( (man ager != nu ll) & & (ma nage r ins tance of Manager Base) ) ((Ma nager Base) mana ger). recy cle(t his); } // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----Session Pack age Metho ds /** * Retur n th e <co de>is Valid </cod e> f lag f or th is session . */ boo lean isVa lid() { 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" )); 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) ); } retu rn ( this. isVal id); } /** * Flag indi catin g whe ther this sess ion i s new or not. */ pri vate bool ean i sNew = tru e; /** * Set t he < code> isNew </cod e> fl ag f or th is session . * * @para m is New T he ne w val ue fo r th e <code>i sNew< /cod e> fl ag */ voi d set New( boole an is New) { // ----- ---- ----- ----- ----- ----- ---- ----- ----- - 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 { /** * Set t he < code> isVal id</c ode> flag for this session . * * @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) { this .isV alid = isV alid; // 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); ((Ht tpSes sionB indin gList ener )o).v alueU nboun d(e); } thro w new Ille galSt ateEx cept ion(m sg); } /** * Flag indi catin g whe ther this sess ion i s valid o r not . */ pri vate bool ean i sVali d = f alse; /** * The s trin g man ager for t his p acka ge. */ pri vate Stri ngMan ager sm = valu es.r emove (name ); StringM anage r.ge tMana ger(" org.a pache .tom cat.s e ssion") ; } } 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 "); 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 "); 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(); } } /** * The H TTP sessi on co ntext asso ciat ed with th is se ssio n. */ pri vate stat ic Ht tpSes sionC ontex t session Conte xt = null ; /** * The c urre nt ac cesse d tim e for thi s session . */ pri vate long this Acces sedTi me = creatio nTime ; retu rn i nacti veInt erval ; // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- HttpS essio n Pr opert ies /** * Retur n th e tim e whe n thi s ses sion was creat ed, in mill iseco nds since * midni ght, Janu ary 1 , 197 0 GMT . * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is call ed on an * inva lida ted s essio n */ pub lic l ong getCr eatio nTime () { /** * Retur n th e ses sion conte xt wi th w hich this session is a ssoc iated . * * @depr ecat ed As of V ersio n 2.1 , th is me thod is depreca ted a nd h as no * repl acem ent. It w ill b e rem oved in a futu re version of t he * Java Ser vlet API. */ pub lic H ttpS essio nCont ext g etSes sion Conte xt() { } 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 "); } // ----- ---- ----- ----- ----- ----- ---- ----- ------- ----- -- S essio n Pro perti es //----- ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- ----- ---/** * Set t he c reati on ti me fo r thi s se ssion . This me thod is c alled by t he * Manag er w hen a n exi sting Sess ion instanc e is reus ed. * * @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() { retu rn ( this. id); if ( sess ionCo ntext == n ull) sess ionCo ntext = ne w Standar dSess ionC ontex t(); retu rn ( sessi onCon text) ; // ----- ---- ----- ----- ----- ----- ---- ----- ----- --HttpSes sion Publ ic Me thods 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; } 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 ); } } } } 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 ; 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; } /** * 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" ; /** 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 . .. /** * 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 ); } } /** * 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" ); /** * 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; retu rn ( attri butes .get( name) ); ServerSession if ( (thi s.id != nu ll) & & (ma nage r != /** * 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 this se ssion . * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is call ed on an * inva lida ted s essio n */ pub lic E nume ratio n get Attri buteN ames () { 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 ( 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 ] */ Serve rSess ionMa nager ssm = S erver Sessi onMan ager .getM anage r(); ssm.r emove Sessi on(th is); if ( (man ager != nu ll) & & (ma nage r instanc eof M anag erBas e)) ((Ma nager Base) mana ger). add( this) ; } /** * Retur n de scrip tive infor matio n ab out this Se ssion imp lemen tatio n and * the c orre spond ing v ersio n num ber, in the for mat * <code>& lt;de scri ption >/ <v ersio n> ;</co d e>. */ pub lic S trin g get Info( ) { 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") ); } } } retu rn ( getAt tribu te(na me)); } retu rn ( this. info) ; } 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; } } 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 ); appS essio n.inv alida te(); } 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; } thro w new Ille galAr gumen tExc eptio n(msg ); /** * Retur n th e las t tim e the clie nt s ent a request asso ciat ed wi th th is * sessi on, as th e num ber o f mil lise conds since m idnig ht, Janua ry 1, 1970 * GMT. Act ions that your appli cati on takes, such as g ettin g or setti ng * a val ue a ssoci ated with the s essi on, d o not aff ect t he a ccess time . */ pub lic l ong getLa stAcc essed Time( ) { retu rn ( this. lastA ccess edTim e); /** * 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 returne d. * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is call ed on an * inva lida ted s essio n * * @depr ecat ed As of V ersio n 2.2 , th is me thod is replace d by * <cod e>ge tAttr ibute Names ()</c ode> */ pub lic S trin g[] g etVal ueNam es() { pub lic l ong getCr eatio nTime () { retu rn c reati onTim e; 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; } 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") ; 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 ); 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 ); retu rn ( this. manag er); 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); retu rn ( dummy .elem ents( )); retu rn ( this. check Inter val); } /** * 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 ) { /** * Set t he M anage r wit hin w hich this Session is v alid . * * @para m ma nager The new M anage r */ pub lic v oid setMa nager (Mana ger m anag er) { pub lic v oid remov eValu e(Str ing n ame) { valu es.r emove (name ); } this .man ager = man ager; /** * Inval idat es th is se ssion and unbi nds a ny ob jects bound t o it. * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is call ed on * an i nval idate d ses sion */ pub lic v oid inval idate () { 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 ] */ /** * 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) { sess ion.r eap() ; sess ion.v alida te(); 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 ); 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( ); appS essio n.val idate (); } } } } /** * Retur n th e max imum time inter val, in seconds , bet ween clie nt re quest s * befor e th e ser vlet conta iner will invalid ate t he s essio n. A nega tive * time indi cates that the sessi on s hould never t ime o ut. * * @exce ptio n Ill egalS tateE xcept ion if this me thod is c alled on * an i nval idate d ses sion */ pub lic i nt g etMax Inact iveIn terva l() { retu rn ( null) ; retu rn ( this. maxIn activ eInte rval ); } /** * Set t he m aximu m tim e int erval , in seconds , bet ween clie nt re quest s * befor e th e ser vlet conta iner will invalid ate t he s essio n. A nega tive * time indi cates that the sessi on s hould never t ime o ut. * * @para m in terva l The new maxim um interva l */ pub lic v oid setMa xInac tiveI nterv al(i nt interva l) { this .max Inact iveIn terva l = i nter val; * @para m se ssion The sessi on to be marke d */ Http Sess ion s essio n=fin dSess ion( ctx, id); if( sess ion = = nul l) re turn; if ( sess ion i nstan ceof Sessi on) * Spec ializ ed i mplem entat ion o f org.apa che.t omca t.cor e.Ses sionM anage r ((Se ssion ) ses sion) .acce ss() ; * that adap ts t o the new compo nent- base d Man ager impleme ntati on. // 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 } coded, * and lifec ycle conf igura tion is no t su pport ed. // XXX s houl d we throw exce ption or just retur n nul l ?? * <p> pub lic H ttpS essio n fin dSess ion( Cont ext c tx, S tring id ) { * <b>I MPLEM ENTA TION NOTE< /b>: Manager /Sess ion Once we commi t to the n ew try { Sess ion s essio n = m anage r.fi ndSes sion( id); * para digm, I w ould sugge st mo ving the logic impl ement ed he re back in to * the core leve l. more li ke a if(s essio n!=nu ll) T he To mcat. Next "Man ager" inte rface acts retur n ses sion. getSe ssio n(); } ca tch (IOEx cepti on e) { * coll ectio n cl ass, and h as mi nimal kno wledg e of the d etail ed request } retu rn ( null) ; * proc essin g se manti cs of hand ling sess ions. } * <p> * XXX - At pres ent, there is n o way (vi a the Sess ionMa nager interfa ce) f or pub lic H ttpS essio n cre ateSe ssion (Con text ctx) { * a Co ntext to tell the M anage r tha t we crea te wh at th e def ault session retu rn manag er.cr eateS essio n(). getSe ssion (); } * time out f or t his w eb ap plica tion (spe cifie d in the d eploy ment descrip tor) /** * shou ld be . * Remov e al l ses sions beca use o ur a ssoci ated Conte xt is being s hut d own. * * @aut hor C raig R. M cClan ahan * */ * @para m ct x The cont ext t hat i s be ing s hut d own public final cla ss St andar dSess ionMa nage r pub lic v oid remov eSess ions( Conte xt c tx) { imp lemen ts S essio nMana ger { // X XX X XX a manag er ma y be shar ed by mult iple /** try { * Creat e a new S essio nMana ger t hat adapt s to the corresp ondin g Ma nager ((Lif ecycl e) ma nager ).st op(); } ca tch ( Lifec ycleE xcept ion e) { * imple ment ation . throw new Illeg alSta teEx cepti on("" + e) ; */ } pub lic S tand ardSe ssion Manag er() { } mana ger = new Stan dardM anage r(); } if ( mana ger i nstan ceof Lifec ycle ) { try { /** ((Lif ecycl e) ma nager ).co nfigu re(nu ll); ((Lif ecycl e) ma nager ).st art() ; * Used by c ontex t to confi gure the sessi on ma nager 's inactiv ity t imeo ut. } ca tch ( Lifec ycleE xcept ion e) { throw new Illeg alSta teEx cepti on("" + e) ; } } * * The S essi onMan ager may h ave s ome defau lt se ssion time out, th e * Conte xt o n the othe r han d has it' s tim eout set b y the deploym ent * descr ipto r (we b.xml ). Th is me thod lets the Conte xt conforg ure t he } * sessi on m anage r acc ordin g to this valu e. * // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----Instanc e Var iabl es * @para m mi nutes The sessi on in acti vity timeo ut in minu tes. } */ /** * 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 started * @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 error * that nee ds to be r eport ed */ pub lic v oid stop( ) thr ows L ifecy cleE xcept ion { // 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; this .che ckInt erval = ch eckIn terv al; // S top the b ackgr ound reape r th read thre adSt op(); // 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 (); } /** * 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 >< ;desc ripti on> ;/< ;ver sion& gt;</ code> . */ pub lic S trin g get Info( ) { } } /** * 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( ) { // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- --- Priva te Methods /** * Inval idat e all sess ions that have expi red. */ pri vate void proc essEx pires () { retu rn ( this. maxAc tiveS essio ns); 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()) / /** * 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) { 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 (); 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 ); 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 ); } } /** * Retur n <c ode>t rue</ code> if t he c lient does not yet kno w abo ut t he * sessi on, or if the clien t cho oses not to jo in the ses sion. Fo r * examp le, if th e ser ver u sed o nly cooki e-bas ed session s, an d th e cli ent * has d isab led t he us e of cooki es, then a ses sion would b e new on each * reque st. * * @exce ptio n Ill egalS tateE xcept ion if th is me thod is call ed on an * inva lida ted s essio n */ pub lic b oole an is New() { // S tart the backg round reap er t hread thre adSt art() ; } pro tecte d in t ina ctive Inter val = -1; sta tic { mana ger = new Serv erSes sionM anag er(); } // C ause this sess ion t o exp ire expi re() ; 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; } // 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; } } // 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 acces sed(C ontex t ctx , Re quest req, Stri ng id ) { /** // c onne ction open } } pub lic s tati c Ser verSe ssion Manag er g etMan ager( ) { retu rn m anage r; } } } } retu rn v alues .get( name) ; } // X XX // s ync t o ens ure v alid? } voi d val idat e() /** * Retur n th e Man ager withi n whi ch t his Session is v alid . */ pub lic M anag er ge tMana ger() { } if ( appS essio n == null && cr eate ) { /** * 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() { } } } /** * Retur n th e che ck in terva l (in sec onds) for this Manag er. */ pub lic i nt g etChe ckInt erval () { 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) ; if(- 1 != inac tiveI nterv al) { sess ion. setMa xInac tiveI nterv al(i nacti veInt erval ); 1000L); if ( timeI dle > = max Inact iveI nterv al) sessi on.ex pire( ); } } // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- 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() { /** * 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() { 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 ")); retu rn ( super .crea teSes sion( )); } try { Thre ad.sl eep(c heckI nterv al * 1000 L); } ca tch (Inte rrupt edExc eptio n e) { ; } } /** * 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( ); } } } retu rn s essio n.get Appli catio nSes sion( ctx, true ); } 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; Thi s * calle d fo r eac h req uest by a Requ estIn terce ptor. 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; if ( mana ger i nstan ceof Lifec ycle ) { * Prepa re f or th e beg innin g of acti ve us e of the p ublic met hods of this * compo nent . Th is me thod shoul d be call ed af ter <code>c onfig ure( )</co de>, * and b efor e any of t he pu blic meth ods o f the comp onent are utilize d. * * @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 error * that pre vents this comp onent fro m bei ng us ed */ pub lic v oid start () th rows Lifec ycle Excep tion { retu rn ( this. info) ; pri vate Vect or du mmy = new Vecto r(); /** * Mark the speci fied sessi on's last acce ssed time. should be import org.a pach e.tom cat.c ore.R eques t; import org.a pach e.tom cat.c ore.R espon se; */ /** // ---- ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- - Pri vate Class final c lass Stan dardS essio nCont ext i mple ments Http Sessi onCon text { import org.a pach e.tom cat.c atali na.*; import org.a pach e.tom cat.c ore.C ontex t; pub lic v oid setSe ssion TimeO ut(in t mi nutes ) { } /** * 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. */ import javax .ser vlet. http. Cooki e; import javax .ser vlet. http. HttpS essio n; // 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 -- Cons truct ors databas e } // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- ---- Prope rties 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)); retu rn ( attri butes .keys ()); /** * Retur n th e obj ect b ound with the speci fied name in this sess ion, or * <code >nul l</co de> i f no objec t is boun d wit h that na me. * * @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 call ed on an * inva lida ted s essio n * * @depr ecat ed As of V ersio n 2.2 , th is me thod is replace d by * <cod e>ge tAttr ibute ()</c ode> */ pub lic O bjec t get Value (Stri ng na me) { node = a ttrib utes. getNa medIt em(" maxIn activ eInte rval" ); if ( node != n ull) { try { setMaxI nacti veIn terva l(Int eger. parse Int( node. getNo deVal ue()) ); } ca tch ( Throw able t) { ; // XXX - Thr ow e 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"; } this .id = id; 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. *; setMaxA ctive Sess ions( Integ er.pa rseIn t(no de.ge tNode Value ())); } ca tch ( Throw able t) { ; // XXX - Thr ow e xcept ion? } } /** * The b ackg round thre ad co mplet ion semap hore. */ pri vate bool ean t hread Done = fal se; null) & & (m anag er in stanc eof M anage rBas e)) ((Ma nager Base) manager ).rem ove( this) ; // ---- ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----Public Metho ds 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 } } /** * Set t he s essio n ide ntifi er fo r th is session . * * @para m id The new s essio n ide ntif ier */ pub lic v oid setId (Stri ng id ) { // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- - Li fecyc le Methods /** been * Stan dard impl ement ation of t he <b >Man ager< /b> i nterf ace t hat provi des * conf igur ed an d/or start ed * no s essio n pe rsist ence or di strib utab le ca pabil ities , but doe s sup port * @exce ptio n Lif ecycl eExce ption if this compo nent detec ts a fata l * an o ption al, confi gurab le, m aximu m nu mber of ac tive sessi ons allow ed. error * <p> * in t he c onfig urati on pa ramet ers it wa s giv en * Life cycle con figur ation of t his c ompo nent assum es an XML node */ * in t he fo llow ing f ormat : pub lic v oid confi gure( Node param eter s) * <cod e> thro ws L ifecy cleEx cepti on { * <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" // V alid ate a nd up date our c urre nt co mpone nt st ate * maxIn activ eInte rval= "-1" /> if ( conf igure d) * </co de> thro w new Life cycle Excep tion * wher e you can adju st th e fol lowin g pa ramet ers, with defau lt v alues (sm.g etStr ing(" stand ardM anage r.alr eadyC onfig ured ")); * in s quare bra ckets : conf igur ed = true; * <ul> if ( para meter s == null) * <li> <b>ch eckI nterv al</b > - T he in terv al (i n sec onds) betw een backg round retu rn; * 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 // P arse and proce ss ou r con figu ratio n par amete rs * be ac tive at o nce, or -1 for no l imit. [-1 ] if ( !("M anage r".eq uals( param eter s.get NodeN ame() ))) * <li> <b>ma xIna ctive Inter val</ b> - The defau lt ma ximum numb er o f sec onds of retu rn; * inact ivit y bef ore w hich the s ervl et co ntain er is allo wed to ti me ou t Name dNod eMap attri butes = pa rame ters. getAt tribu tes() ; * a ses sion , or -1 fo r no limit . T his v alue shoul d be over ridde n fro m Node nod e = n ull; * 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 ] node = a ttrib utes. getNa medIt em(" check Inter val") ; * </ul > if ( node != n ull) { * try { * @aut hor C raig R. M cClan ahan setCh eckIn terva l(Int eger .pars eInt( node. getNo deVa lue() )); * @ver sion $Rev ision : 1.1 .1.1 $ $Da te: 2000/ 05/02 21:2 8:30 $ } ca tch ( Throw able t) { */ ; // XXX - Thr ow e xcept ion? } public final cla ss St andar dMana ger } ext ends Mana gerBa se imp lemen ts L ifecy cle, Runna ble { node = a ttrib utes. getNa medIt em(" maxAc tiveS essio ns"); if ( node != n ull) { try { // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- Ins tance Vari ables /** * Has t his compo nent been confi gure d yet ? */ pri vate bool ean c onfig ured = fal se; // 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 )); } } 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 /** * The i nter val ( in se conds ) bet ween chec ks fo r exp ired sess ions. */ pri vate int check Inter val = 60; // 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. // 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) ; } /** * Retur n th e obj ect b ound with the speci fied name in this sess ion, or * <code >nul l</co de> i f no objec t is boun d wit h that na me. * * @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 call ed on an * inva lida ted s essio n */ pub lic O bjec t get Attri bute( Strin g na me) { 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; } // 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)); } // 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); } /** * 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 { } // 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 { } retu rn ( this. creat ionTi me); 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. *; // 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() ; thro w new Ille galAr gumen tExc eptio n(msg ); } StandardManage package org. apac he.to mcat. reque st; pub lic S essi onInt ercep tor() { } } this .isN ew = isNew ; } } } } SessionInterceptor } // T ell our M anage r tha t thi s Se ssion has been } /** * 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. */ Gerenciamento de Sessões setA ttri bute( name, valu e); } this .las tAcce ssedT ime = this .thi sAcce ssedT ime; this .thi sAcce ssedT ime = System. curre ntTi meMil lis() ; this .isN ew=fa lse; } /** * 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() { if ( thre ad == null ) retu rn; retu rn s Sessi on.ge tAppl icati onSe ssion (ctx, fals e); } thre adDo ne = true; thre ad.i nterr upt() ; try { thre ad.jo in(); } ca tch (Inte rrupt edExc eptio n e) { ; } retu rn ( this. isNew ); } thre ad = null ; } // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- - Ba ckgro und Thread } /** * The b ackg round thre ad th at ch ecks for sessi on ti meout s an d shutdow n. */ pub lic v oid run() { // L oop until the termi natio n se mapho re is set whil e (! threa dDone ) { thre adSle ep(); proc essEx pires (); } } if(- 1 != minu tes) { /** // T he ma nager work s wit h se conds ... * The M anag er im pleme ntati on we are actu ally using . mana ger.s etMax Inact iveIn terv al(mi nutes * 60 ); */ } pri vate Mana ger m anage r = n ull; } } StandardSesin ApplicationSession privat e lon g la stAcc essed = cr eatio nTim e; pri vate int inact iveIn terva l = - 1; /* * ==== ===== ==== ===== ===== ===== ===== ==== ===== ===== ===== ===== ==== ===== == * * 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 "); thro w new Ille galSt ateEx cept ion(m sg); package org. apac he.to mcat. sessi on; import java. io.I OExce ption ; import java. io.O bject Input Strea m; import java. io.O bject Outpu tStre am; import java. io.S erial izabl e; import java. util .Enum erati on; import java. util .Hash table ; import java. util .Vect or; import javax .ser vlet. Servl etExc eptio n; import javax .ser vlet. http. HttpS essio n; import javax.s ervle t.ht tp.Ht tpSes sionB indin gEve nt; import javax.s ervle t.ht tp.Ht tpSes sionB indin gLis tener ; import javax .ser vlet. http. HttpS essio nCon text; import org.a pach e.tom cat.c atali na.*; import org.a pach e.tom cat.u til.S tring Mana ger; } 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); } if ( name == n ull) { Stri ng ms g = s m.get Strin g("a pplic ation Sessi on.va lue. iae") ; /** * 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 this cl ass r epre sents 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 declare d pub lic, Java logi c out side * 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 $ */ * 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); } // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- Session Publ ic M ethod s /** * Perfo rm t he in terna l pro cessi ng r equir ed to invalid ate t his sessi on, * 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() { 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); } final c lass Stan dardS essio n imp lemen ts H ttpSe ssion , Ses sion { thro w new Ille galAr gumen tExc eptio n(msg ); remo veVa lue(n ame); // remov e an y exi sting bind ing ((Ht tpSes sionB indin gList ener )valu e).va lueBo und(e ); } valu es.p ut(na me, v alue) ; } /** * @depr ecat ed */ pub lic O bjec t get Value (Stri ng na me) { retu rn g etAtt ribut e(nam e); } 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. *; 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 "); /** * 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") ; // ----- ---- ----- ----- ----- ----- ---- ----- --------- ----- ---- -- Co nstru ctors /** * Const ruct a ne w Ses sion assoc iate d wit h the spe cifie d Ma nager . * * @para m ma nager The manag er wi th w hich this Se ssion is assoc iated */ pub lic S tand ardSe ssion (Mana ger m anag er) { supe r(); this .man ager = man ager; } 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; private bool ean valid = tr ue; } /** * @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(); whil e (e .hasM oreEl ement s()) { name s.add Eleme nt(e. nextE leme nt()) ; } 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; Stri ng[] valu eName s = n ew St ring [name s.siz e()]; name s.co pyInt o(val ueNam es); this .ina ctive Inter val = cont ext. getSe ssion TimeO ut(); /** * Relea se a ll ob ject refer ences , an d ini tiali ze instanc e var iabl es, i n * prepa rati on fo r reu se of this obj ect. */ pub lic v oid recyc le() { /** * 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 Hashtab le(); recycle d if ( (man ager != nu ll) & & (ma nage r ins tance of Manager Base) ) ((Ma nager Base) mana ger). recy cle(t his); } /** * 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" ; } 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); } /** * 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. */ Hash tabl e val uesCl one = (Has htab le)va lues. clone (); /** * The l ast acces sed t ime f or th is S essio n. */ /** * 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; retu rn ( Enume ratio n)val uesCl one. keys( ); } // HTTP SESS ION I MPLEM ENTAT ION M ETHO DS /** 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 "); * @depr ecat ed */ pub lic v oid remov eValu e(Str ing n ame) { remo veAt tribu te(na me); /** * The m axim um ti me in terva l, in sec onds, between clie nt r eques ts be fore * 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 time ou t. */ pri vate int maxIn activ eInte rval = -1 ; } thro w new Ille galSt ateEx cept ion(m sg); } 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 "); } 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 "); thro w new Ille galSt ateEx cept ion(m sg); /** * Flag indi catin g whe ther this sess ion i s new or not. */ pri vate bool ean i sNew = tru e; } 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 galSt ateEx cept ion(m sg); } thro w new Ille galAr gumen tExc eptio n(msg ); } } /** * * @depr ecat ed */ Obje ct o = va lues. get(n ame); 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); pub lic H ttpS essio nCont ext g etSes sion Conte xt() { retu rn n ew Se ssion Conte xtImp l(); } ((Ht tpSes sionB indin gList ener )o).v alueU nboun d(e); } /** * Flag indi catin g whe ther this sess ion i s valid o r not . */ pri vate bool ean i sVali d = f alse; /** * The s trin g man ager for t his p acka ge. */ pri vate Stri ngMan ager sm = StringM anage r.ge tMana ger(" org.a pache .tom cat.s ess ion"); valu es.r emove (name ); } 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 "); /** * The H TTP sessi on co ntext asso ciat ed wi th this se ssion . */ pri vate stat ic Ht tpSes sionC ontex t session Conte xt = null ; retu rn ( this. isVal id); } /** * 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 <code>i sNew< /cod e> fl ag */ voi d set New( boole an is New) { 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" )); thro w new Ille galSt ateEx cept ion(m sg); } 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> isVal id</c ode> flag for this session . * * @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) { inac tive Inter val = inte rval; 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 c urre nt ac cesse d tim e for thi s session . */ pri vate long this Acces sedTi me = creatio nTime ; thro w new Ille galSt ateEx cept ion(m sg); } retu rn i nacti veInt erval ; // ----- ---- ----- ----- ----- ----- ---- ----- --------- ----- Ses sion Prope rties } } //----- ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- ----- ---- /** * Set t he c reati on ti me fo r thi s se ssion . This me thod is c alled by t he * Manag er w hen a n exi sting Sess ion insta nce is reus ed. * * @para m ti me Th e new crea tion time */ pub lic v oid setCr eatio nTime (long tim e) { this .isV alid = isV alid; // ----- ---- ----- ----- ----- ----- ---- ----- ----- - 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 { // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- HttpSes sion Prop ertie s /** * 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 () { retu rn ( this. creat ionTi me); } retu rn ( this. id); /** * 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 { } // ----- ---- ----- ----- ----- ----- ---- ----- ----- --HttpSes sion Publ ic Me thods /** * Retur n th e obj ect b ound with the speci fied name in this se ssion , or * <code >nul l</co de> i f no objec t is boun d wit h tha t name. * * @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) { // 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); stream. write Obje ct(ne w Int eger( maxIn acti veInt erval )); stre am.w riteO bject (new Boole an(i sNew) ); stre am.w riteO bject (new Boole an(i sVali d)); // 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) ); /** * 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 () { // 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 )); } if ( (thi s.id != nu ll) & & (ma nage r != null) & & (m anag er in stanc eof M anage rBas e)) ((Ma nager Base) manager ).rem ove( this) ; this .id = id; if ( (man ager != nu ll) & & (ma nage r instanc eof M anag erBas e)) ((Ma nager Base) mana ger). add( this) ; 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 ); appS essio n.inv alida te(); } /** * 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 ] */ } 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") ; thro w new Ille galAr gumen tExc eptio n(msg ); 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; private int inac tiveI nterv al = -1; remo veVa lue(n ame); // remov e an y exi sting bind ing valu es.p ut(na me, v alue) ; } /** * Retur n th e Man ager withi n whi ch t his Session is v alid . */ pub lic M anag er ge tMana ger() { thro w new Ille galAr gumen tExc eptio n(msg ); Ser verSe ssio n(Str ing i d) { this .id = id; } } retu rn v alues .get( name) ; } pub lic S trin g get Id() { retu rn i d; } pub lic E nume ratio n get Value Names () { retu rn v alues .keys (); } pub lic l ong getCr eatio nTime () { retu rn c reati onTim e; } pub lic v oid remov eValu e(Str ing n ame) { valu es.r emove (name ); } /** * Set t he M anage r wit hin w hich this Sess ion is vali d. * * @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 ); pub lic v oid setMa xInac tiveI nterv al(i nt in terva l) { inac tive Inter val = inte rval; } pub lic i nt g etMax Inact iveIn terva l() { retu rn i nacti veInt erval ; } if ( appS essio n == null && cr eate ) { // X XX // s ync t o ens ure v alid? // 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. 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); /** * Retur n th e max imum time inter val, in seconds , bet ween clie nt re quest s * befor e th e ser vlet conta iner will invalid ate t he s essio n. A nega tive * time indi cates that the sessi on s hould never t ime o ut. * * @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() { } } 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 ); retu rn a ppSes sion; appS essio n.val idate (); /** * 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. */ retu rn ( this. maxIn activ eInte rval ); syn chron ized void reap () { Enum erat ion e num = appS essio ns.k eys() ; // 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 } voi d rem oveA pplic ation Sessi on(Co ntex t con text) { appS essi ons.r emove (cont ext); } 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 ); retu rn ( this. manag er); 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 aximu m tim e int erval , in seconds , bet ween clie nt re quest s * befor e th e ser vlet conta iner will invalid ate t he s essio n. A nega tive * time indi cates that the sessi on s hould never t ime o ut. * * @para m in terva l The new maxim um i nterv al */ pub lic v oid setMa xInac tiveI nterv al(i nt interva l) { */ Http Sess ion s essio n=fin dSess ion( ctx, id); if( sess ion = = nul l) re turn; if ( sess ion i nstan ceof Sessi on) ((Se ssion ) ses sion) .acce ss() ; // c ache the HttpS essio n - a void anot her f ind req. setS essio n( se ssion ); StandardManager SessionIntercepto 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. *; // 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 { // 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 ; 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; } 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; } 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 ); } } final c lass Stan dardS essio nCont ext i mple ments Http Sessi onCon text { 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() { } 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; /** * 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> * <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 $ */ retu rn ( dummy .elem ents( )); // 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; // 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; 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? } } node = a ttrib utes. getNa medIt em(" maxAc tiveS essio ns"); if ( node != n ull) { try { // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- 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 ; /** * The i nter val ( in se conds ) bet ween chec ks fo r exp ired sess ions. */ pri vate int check Inter val = 60; /** * Has t his compo nent been confi gure d yet ? */ pri vate bool ean c onfig ured = fal se; } /** * 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" ; // 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. setMaxA ctive Sess ions( Integ er.pa rseIn t(no de.ge tNode Value ())); } ca tch ( Throw able t) { ; // XXX - Thr ow e xcept ion? } } node = a ttrib utes. getNa medIt em(" maxIn activ eInte rval" ); if ( node != n ull) { try { setMaxI nacti veIn terva l(Int eger. parse Int( node. getNo deVal ue()) ); } ca tch ( Throw able t) { ; // XXX - Thr ow e xcept ion? } } } /** * 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) ; } } import javax .ser vlet. http. Cooki e; /** * 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 ); } } /** * 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" ); /** * 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)) { request .setR eque stedS essio nId(s essio nId) ; if( d ebug> 0 ) c m.log (" F inal sessi on id " + sess ionId ); retur n ses sionI d; } } retu rn n ull; } * Prepa re f or th e beg innin g of acti ve us e of the p ublic met hods of * compo nent . Th is me thod shoul d be call ed af ter <code>c onfig ure( )</co de>, * 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 error * that pre vents this comp onent fro m bei ng us ed */ pub lic v oid start () th rows Lifec ycle Excep tion { retu rn ( this. check Inter val); // S tart the backg round reap er t hread thre adSt art() ; if(s essio n!=nu ll) retur n ses sion. getSe ssio n(); } retu rn ( null) ; } /** pub lic H ttpS essio n cre ateSe ssion (Con text ctx) { * Spec ializ ed i mplem entat ion o f org.apa che.t omca t.cor e.Ses sionM anage r retu rn * that adap ts t o the new compo nent- base d Man ager imple menta tion . manag er.cr eateS essio n(). getSe ssion (); } * <p> * XXX - At pres ent, use o f <co de>St anda rdMan ager< /code > is hard coded, * and lifec ycle conf igura tion is no t su pport ed. /** * Remov e al l ses sions beca use o ur a ssoci ated Conte xt is bei ng shut do wn. * <p> * * <b>I MPLEM ENTA TION NOTE< /b>: Manager /Sess ion Once we commi t to the n ew * @para m ct x The cont ext t hat i s be ing s hut d own */ * para digm, I w ould sugge st mo ving the logic impl ement ed he re back in to * the core leve l. like a pub lic v oid remov eSess ions( Conte xt c tx) { T he To mcat. Next "Man ager" inte rface acts mor e // X XX X XX a manag er ma y be shar ed by mult iple * coll ectio n cl ass, and h as mi nimal kno wledg e of the d etail ed request * proc essin g se manti cs of hand ling sess ions. // 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 databas e * <p> // c onne ction open * XXX - At pres ent, there is n o way (vi a the Sess ionMa nager interfa ce) f or if ( mana ger i nstan ceof Lifec ycle ) { try { * a Co ntext to tell the M anage r tha t we crea te wh at th e def ault session ((Lif ecycl e) ma nager ).st op(); } ca tch ( Lifec ycleE xcept ion e) { * time out f or t his w eb ap plica tion (spe cifie d in the d eploy ment descrip tor) throw new Illeg alSta teEx cepti on("" + e) ; * shou ld be . } * } * @aut hor C raig R. M cClan ahan */ } public final cla ss St andar dSess ionMa nage r imp lemen ts S essio nMana ger { /** * Used by c ontex t to confi gure the sessi on ma nager 's inactiv ity t imeo ut. * * The S essi onMan ager may h ave s ome defau lt se ssion time out , * descr ipto r (we b.xml ). Th is me thod lets the Conte xt conforg ure t he /** * Creat e a new S essio nMana ger t hat adapt s to the corresp ondin g Ma nager // 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; // S top the b ackgr ound reape r th read thre adSt op(); // 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 (); } this .che ckInt erval = ch eckIn terv al; } /** * 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 >< ;desc ripti on> ;/< ;ver sion& gt;</ code> . */ pub lic S trin g get Info( ) { retu rn ( this. info) ; // 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(); } // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- --- Priva te Methods } /** * 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() { /** * 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( ) { try { Thre ad.sl eep(c heckI nterv al * 1000 L); } ca tch (Inte rrupt edExc eptio n e) { ; } 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) { pro tecte d in t ina ctive Inter val = -1; } /** * 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() { 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 (); 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; } 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) ; } } public HttpS essi on cr eateS essio n(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) ; 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 ); } 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( ); // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- 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() { } /** * 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() { if ( thre ad == null ) retu rn; 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 ")); thre adDo ne = true; thre ad.i nterr upt() ; try { thre ad.jo in(); } ca tch (Inte rrupt edExc eptio n e) { ; } retu rn ( super .crea teSes sion( )); } thre ad = null ; } } retu rn s Sessi on.ge tAppl icati onSe ssion (ctx, fals e); // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- - Ba ckgro und Thread /** * 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() { this .max Inact iveIn terva l = i nter val; 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 (); } } } * sessi on m anage r acc ordin g to this valu e. * * imple ment ation . * @para m mi nutes The sessi on in acti vity timeo ut in minu tes. */ */ pub lic S tand ardSe ssion Manag er() { pub lic v oid setSe ssion TimeO ut(in t mi nutes ) { if(- 1 != minu tes) { mana ger = new Stan dardM anage r(); // T he ma nager work s wit h se conds ... if ( mana ger i nstan ceof Lifec ycle ) { mana ger.s etMax Inact iveIn terv al(mi nutes * 60 ); try { } ((Lif ecycl e) ma nager ).co nfigu re(nu ll); ((Lif ecycl e) ma nager ).st art() ; } ca tch ( Lifec ycleE xcept ion e) { throw new Illeg alSta teEx cepti on("" + e) ; } } } // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----Instanc e Var iabl es /** * 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 started * @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 error * that nee ds to be r eport ed */ pub lic v oid stop( ) thr ows L ifecy cleE xcept ion { /** * 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 } Sess ion s essio n = m anage r.fi ndSes sion( id); } ca tch (IOEx cepti on e) { 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; // 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; /** * The b ackg round thre ad co mplet ion semap hore. */ pri vate bool ean t hread Done = 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 () { } try { import org.a pach e.tom cat.c ore.R eques t; import org.a pach e.tom cat.c ore.R espon se; } 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; pub lic H ttpS essio n fin dSess ion( Cont ext c tx, S tring id ) { import org.a pach e.tom cat.c atali na.*; import org.a pach e.tom cat.c ore.C ontex t; // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- -the Constru ctors /** this /** * The b ackg round thre ad. */ pri vate Thre ad th read = nul l; /** * Name to r egist er fo r the back grou nd th read. */ pri vate Stri ng th readN ame = "Sta ndar dMana ger"; package org. apac he.to mcat. sessi on; // XXX shoul d we thro w exc eptio n or just retu rn nu ll ?? import javax .ser vlet. http. HttpS essio n; * Conte xt o n the othe r han d has it' s tim eout set b y the deploym ent /** 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 . .. 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. *; // ---- ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----Public Metho ds 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 error * 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 { } sta tic { mana ger = new Serv erSes sionM anag er(); } } // C ause this sess ion t o exp ire expi re() ; } retu rn ( this. isNew ); // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- - Li fecyc le Methods } /** * Inval idat es th is se ssion and unbi nds a ny ob jects bound t o 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 () { /** * Retur n <c ode>t rue</ code> if t he c lient does not yet kno w abo ut 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 session s, an d th e cli ent * has d isab led t he us e of cooki es, then a ses sion would b e 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() { 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 public final cla ss St andar dMana ger ext ends Mana gerBa se imp lemen ts L ifecy cle, Runna ble { } // ---- ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- - Pri vate Class /** * 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. */ StandardSessionManager package org. apac he.to mcat. reque st; } 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") ); } } } /** * 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 returne 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 replace d by * <cod e>ge tAttr ibute Names ()</c ode> */ pub lic S trin g[] g etVal ueNam es() { } * * @para m se ssion The sessi on to be marke d // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----- ----- ---- ---- Prope rties retu rn ( getAt tribu te(na me)); /** * 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 >/ <v ersio n> ;</co de> . */ pub lic S trin g get Info( ) { Thi s * calle d fo r eac h req uest by a Requ estIn terce ptor. pub lic v oid acces sed(C ontex t ctx , Re quest req, Stri ng id ) { } 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)); } synchro nized voi d inv alida te() { Enum erat ion e num = appS essio ns.k eys() ; * Mark the speci fied sessi on's last acce ssed time. should be lastAc cesse dTim e = 0 L; lastAc cesse dTim e = ( (Long ) stream. readO bjec t()). longV alue( ); maxI nact iveIn terva l = ( (Inte ger) stream. readO bjec t()). intVa lue() ; isNe w = ((Boo lean) stream. readO bjec t()). boole anVal ue(); } /** * Retur n th e obj ect b ound with the speci fied name in this se ssion , or * <code >nul l</co de> i f no objec t is boun d wit h tha t name. * * @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 replace d by * <cod e>ge tAttr ibute ()</c ode> */ pub lic O bjec t get Value (Stri ng na me) { } 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. *; /** this .las tAcce ssedT ime = this .thi sAcce ssedT ime; this .thi sAcce ssedT ime = System. curre ntTi meMil lis() ; this .isN ew=fa lse; } 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 retu rn ( attri butes .keys ()); } ServerSession last Acce ssed = thi sAcce ssTim e; this Acce ssTim e = S ystem .curr entT imeMi llis( ); } } if ( sess ionCo ntext == n ull) sess ionCo ntext = ne w Standar dSess ionC ontex t(); retu rn ( sessi onCon text) ; } /** * Set t he s essio n ide ntifi er fo r th is session . * * @para m id The new s essio n ide ntif ier */ pub lic v oid setId (Stri ng id ) { /** * Updat e th e acc essed time info rmat ion f or th is session . Th is m ethod * shoul d be call ed by the conte xt w hen a requ est comes i n for a p artic ular * sessi on, even if th e app licat ion does not referen ce it . */ pub lic v oid acces s() { void a ccess ed() { // 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 // 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); } /** * Retur n th e ses sion conte xt wi th w hich this sessi on is asso ciate d. * * @depr ecat ed As of V ersio n 2.1 , th is me thod is depreca ted a nd h as no * repl acem ent. It w ill b e rem oved in a futu re version of t he * Java Ser vlet API. */ pub lic H ttpS essio nCont ext g etSes sion Conte xt() { this .cre ation Time = tim e; this .thi sAcce ssedT ime = time ; /** * Retur n th e ses sion ident ifier for this session . */ pub lic S trin g get Id() { for (int i = 0; i < ses sions .len gth; i++) { Stan dardS essio n ses sion = (S tanda rdSes sion) if ( !sess ion.i sVali d()) conti nue; int maxIn activ eInte rval = session .getM axIn activ eInte 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()) / 1000L); if ( timeI dle > = max Inact iveI nterv al) sessi on.ex pire( ); } } this .las tAcce ssedT ime = time ; privat e lon g la stAcc essed = cr eatio nTim e; // 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( ); isValid = (( Bool ean) strea m.rea dObje ct() ).boo leanV alue( ); } } long tim eNow = Sys tem.c urren tTim eMill is(); Sess ion sessi ons[] = fi ndSes sion s(); session s[i]; } public long get LastA ccess edTim e() { retu rn l astAc cesse d; } pub lic S essi onInt ercep tor() { } } } } * Inval idat e all sess ions that have expi red. */ pri vate void proc essEx pires () { remo veAt tribu te(na me); /** * 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) { this .isN ew = isNew ; } /** retu rn ( this. lastA ccess edTim e); thro w new Ille galSt ateEx cept ion(m sg); } } } /** * Retur n th e <co de>is Valid </cod e> f lag f or th is session . */ boo lean isVa lid() { retu rn v alueN ames; if ( this .inac tiveI nterv al != -1) { this .inac tiveI nterv al *= 60; } } public long get LastA ccess edTim e() { if ( vali d) { retu rn la stAcc essed ; } el se { Stri ng ms g = sm.getS tring ("ap plica tionS essio n.ses sion .ise" ); // ----- ---- ----- ----- ----- ----- ---- ----- ----- ----Session Pack age Metho ds /** * 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; /** * 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 getting or s etti ng * a val ue a ssoci ated with the s essi on, d o not affe ct the acc ess t ime. */ pub lic l ong getLa stAcc essed Time( ) { /** * 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) { // T ell our M anage r tha t thi s Se ssion has been // c ache it - no n eed t o com pute it a gain req. setS essio n( ap S ); } if ( thisI nterv al > inact iveI nterv al) { inval idate (); } } // R eset the insta nce v ariab les assoc iated with this Se ssion attr ibut es.cl ear() ; crea tion Time = 0L; id = nul l; manager = nu ll; maxI nact iveIn terva l = - 1; isNe w = true; isVa lid = fal se; // ----- ---- ----- ----- ----- ----- ---- ----- --------- ----- Ins tance Vari ables thro w new Ille galAr gumen tExc eptio n(msg ); } public class App licat ionSe ssion impl emen ts Ht tpSes sion { } Serv erSe ssion serv S=apS .getS erve rSess ion() ; serv S.ac cesse d(); apS. acce ssed( ); } } } privat e lon g la stAcc essed Time = cre atio nTime ; } 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) ); } } // M ark this sessi on as inva lid setV alid (fals e); } 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) ; ssm.r emove Sessi on(th is); setA ttri bute( name, valu e); // U nbin d any obje cts a ssoci ated with this sM.acc essed (ctx , req uest, sess ionId ); public void acc essed ( Con text ctx, Requ est r eq, S tring id ) { Appl icat ionSe ssion apS= (Appl icat ionSe ssion )find Sessi on( ctx, id ); if( apS= =null ) ret urn; Serve rSess ionMa nager ssm = S erver Sessi onMan ager .getM anage r(); 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 exceede d it if ( inac tiveI nterv al != -1) { int thisI nterv al = (int) (Syst em.cu rrent Time Milli s() - last Acces sed) / 1000; /** * 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) { session stream. write Obje ct(ne w Lon g(las tAcce ssed Time) ); if ( thisI nterv al > inact iveI nterv al) { inval idate (); vali date (); } } // R emov e thi s ses sion from our manag er's activ e session s if ( (man ager != nu ll) & & (ma nage r ins tance of Manager Base) ) ((Ma nager Base) mana ger). remo ve(th is); if (ina ctive Inte rval != -1 ) { int thisI nterv al = (int) (Syst em.cu rrent Time Milli s() lastAcc essed ) / 1000; void a ccess ed() { // 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( ); /** /** * 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() { /** * The M anag er im pleme ntati on we are actu ally using . */ pri vate Mana ger m anage r = n ull; } } Replicação de código: Um mesmo tratamento pode ser necessário em classes diferentes, que não componham a mesma árvore de heranças. Dificuldade de manutenção: A cada alteração ou correção, a implementação dos interesses sistêmicos implicará em uma varredura em todos os locais onde há lógica destinada a implementação. Redução da capacidade de reutilização de código: Um tratamento destinado à implementação de um interesse sistêmico em uma classe genérica pode impedir o uso do método genérico em uma classe especializada. Aumento da dificuldade de compreensão: Esse aumento ocorre tanto no contexto geral do programa, quanto no interesse sistêmico implementado, Sincronização de Objetos Concorrentes Distribuição Tratamento de exceções Coordenação de múltiplos objetos Persistência Auditoria Conforme visto anteriormente, os paradigmas atuais (como o POO) não atendem às necessidades para a implementação dos requisitos de um sistema completo sem que parte dos conceitos desses paradigmas sejam quebrados. Na tentativa de resolver esses problemas, o desenvolvimento orientado a aspectos emerge, promovendo a separação avançada de perspectivas, desde o nível da implementação até os outros estágios do processo de desenvolvimento, incluindo especificação de requisitos, análise e projeto. O que é ? Resolvendo Velhos Problemas Simplificando a Engenharia de Software Composição do sistema Combinação Aspectual Conceitos Fundamentais Pontos de Junção Pontos de Atuação Adendo Aspecto Onde e por que aplicar POA A programação orientada a aspectos foi criada no ano de 1997, em Paio Alto, nos laboratórios da Xerox, por Gregor Kiczales O objetivo era construir uma abordagem que fosse um conjunto não necessariamente homogêneo, que permitisse à linguagem de programação, composta por linguagem central e várias linguagens específicas de domínio, expressar de forma ideal as características sistêmicas do comportamento do programa. Compiladores destinados a orientação a Aspecto Não geram um produto final (um programa compilado e executável ou interpretável), mas um novo código. Nessa primeira compilação são acrescidos elementos ao código, para dar suporte às novas abstrações. O código resultante necessita ser novamente compilado para que seja, então, gerado o produto final. O principal objetivo da POA consiste em separar o código referente ao negócio do sistema dos interesses transversais, de uma forma bem definida e centralizada. Como já visto anteriormente, interesses são as características relevantes de uma aplicação. Um interesse pode ser dividido em uma série de aspectos que representam os requisitos da aplicação. Os aspectos que podem ser agrupados no domínio da aplicação compõem os interesses funcionais, referenciados também como lógica de negócio. Na Figura abaixo, cada nuvem representa um interesse sistêmico implementado no sistema, como auditoria (log), tratamento de exceções, persistência, distribuição, dentre outros. Por estarem bem separados e em locais bem definidos, os componentes podem ser melhor reutilizados e a sua manutenção e legibilidade torna-se mais agradável. Supondo uma classe para implementação do controle de produtos, conforme a Figura abaixo, atende apenas a um interesse, não infringindo nenhum conceito da orientação a objetos. Posteriormente, surge a necessidade de implementação do controle de auditoria (Log) e o controle de exceções. Sem utilizar POA, o interesse seria implementado na própria classe Produto, como pode ser observado nas Figuras abaixo. Implementação do controle de auditoria com OO Controle de exceção da classe Produto com OO Na programação orientada a aspectos, por sua vez, os interesses são programados em módulos separados (classes e aspectos, por exemplo). Após a programação, ocorre a combinação entre as classes e os aspectos, conforme indicado pela Figura abaixo. Isso leva à simplificação do problema, pois o programador pode focar cada interesse de forma independente. Segundo Eduardo Piveta (2001) a POA é composta pelos seguintes componentes: Linguagem de componentes (Java, C++, C#, PHP) Linguagem de aspectos ▪ deve suportar a implementação das propriedades desejadas de forma clara e concisa. ▪ fornecer construções necessárias para que o programador crie estruturas que descrevam o comportamento dos aspectos Combinador de aspectos (aspect weaver) ▪ Sua função é combinar os programas escritos em linguagem de componentes com os escritos em linguagem de aspectos. (AspectJ) Programas escritos em linguagem de Aspecto Aspectos Funcionalidade Tempo de projeto Combinador Weaver Tempo de compilação Tempo de execução (Sistema completo) É o processo responsável por combinar os elementos escritos em linguagem de componentes com os elementos escritos em linguagem de aspectos. É um processo que antecede a compilação, gerando um código intermediário na linguagem de componentes capaz de produzir a operação desejada, ou de permitir a sua realização durante a execução do programa. Combinação Aspectual Pontos de junção (join points) Pontos de atuação (pointcuts) Adendo (advice) Aspectos (Aspects) Combinador (weaving) Os pontos de junção são locais bem definidos da execução de um programa, como, por exemplo, uma chamada a um método ou a ocorrência de uma exceção, dentre muitos outros. A partir dos pontos de junção, são criadas as regras que darão origem aos pontos de atuação. Todos os pontos de junção possuem um contexto associado. Por exemplo, a chamada para um método possui um objeto chamador, o objeto alvo e os argumentos do método disponível como contexto. É uma construção lingüística que permite identificar os pontos de junção e capturar o contexto de execução desse ponto de junção, ex: Parâmetros Objetos alvo Após a identificação dos pontos de junção para um determinado interesse, é necessário agrupá-los em um ponto de atuação. A combinação do ponto de junção e atuação torna evidente a ocorrência de interesses sistêmicos, já que esse interesse muito provavelmente estará propagado em diversas classes no sistema, e estas classes, estarão, portanto, implementando mais de um interesse. Adendos são partes da implementação de um aspecto executados em pontos bem definidos do programa principal (pontos de junção). Os adendos são compostos de duas parles: A primeira delas é o ponto de atuação, que define as regras de captura dos pontos de junção; a segunda é o código que será executado quando ocorrer o ponto de junção definido pela primeira parte. O adendo é um mecanismo bastante similar a um método (quando comparamos com a programação orientada a objetos), cuja função é declarar o código que deve ser executado a cada ponto de junção em um ponto de atuação. Um aspecto é o mecanismo disponibilizado pela programação orientada a aspectos para agrupar fragmentos de código referente aos componentes não-funcionais em uma unidade no sistema. Em tempo de compilação O código fonte da parte funcional e dos aspectos é composto antes que o compilador produza o bytecode. AspectJ 1.0.x Em tempo de linkedição A composição ocorre depois que os códigos base e aspectuais foram transformados em bytecode. AspectJ 1.1.x Load-time A composição ocorre quando as classes são carregadas pelo ClassLoader. Ocorre também no nível de bytecode. AspectJ 1.2.x Utiliza uma variável de ambiente ASPECTPATH Run-time A composição ocorre com o sistema em operação – aspectos são criados em tempo de execução e passam a atuar a partir desse momento Preocupação com a Compatibilidade Divisão do AspectJ Elementos do AspectJ Ponto de Junção Pontos de Atuação Adendo Inserção Declaração em Tempo de Compilação Aspecto Exemplo Compatibilidade total: todo programa Java válido é também um programa AspectJ válido. Compatibilidade de plataforma: todo programa AspectJ pode ser executado em uma máquina virtual Java (JVM). Compatibilidade de ferramentas: deve ser possível estender ferramentas existentes para suportar o AspectJ de uma forma natural; isso inclui IDEs (Integrated Development Environments), ferramentas de documentação e ferramentas de projeto. Compatibilidade para o programador: Ao programar com AspectJ, o programador deve sentir-se como se estivesse utilizando uma extensão da linguagem Java. Pontos de junção ou entrada são identificadores bem definidos na execução de um programa. No AspectJ tudo gira em torno deles e é nesses pontos que os interesses sistêmicos serão combinados. Os pontos de junção em destaque na classe Conta são a execução do método crédito e o acesso ao atributo saldo nesse mesmo método. É importante observar que o programador não codifica o ponto de junção em uma linguagem de aspectos, uma vez que esses pontos já existem no sistema, codificados na linguagem de componentes. Pontos de atuação são elementos do programa usados para definir um ponto de junção, como uma espécie de regra criada pelo programador para especificar eventos que serão considerados como um ponto de junção. Para escrever um ponto de atuação que possa capturar a execução do método sacar O na classe Conta, podemos fazer algo semelhante ao apresentado a seguir: execution (void Conta.sacar(double)); O ponto de atuação execution (void Conta.sacar(double)); está capturando a execução do método sacar da classe Conta, que informe como parâmetro algum valor do tipo dlouble e que não tenha retorno (void). Dessa forma, se tivermos outro método sacar na classe Conta, mas que não apresente as características definidas no ponto de atuação, ele será desconsiderado, já que não satisfaz todas as características definidas no ponto de atuação para o ponto de junção. Os conjuntos de atuação mais comuns são: call captura chamadas a métodos/construtores execution captura execução de métodos/construtores set captura modificação do valor de um atributo get captura leitura do valor de um atributo Podem ser compostos utilizando filtros de composição semântica e/ou caracteres coringa call (Point.setX(int) || Point.setY(int)) execution (void Figure.make*(..)) execution (public * Figure.*(*,..)) execution (PersistentRoot+.new(..)) set (int Figure.point); O adendo é um mecanismo similar a um método, usado para declarar o código que deve ser executado a cada ponto de junção em um ponto de atuação. No adendo, o programador pode definir em que momento o código deverá ser executado, se durante o ponto de junção (around), após (after) ou, ainda, antes (before) de sua execução. /* * Adendo para gerar registro de auditoria * Antes da execução do método saque() da classe Conta */ after() : execution (void Conta.sacar(double)){ System.out.println(“Operação de saque da classe Conta Realizada”); } A inserção é uma instrução sistêmica estática que permite efetuar mudanças em classes e aspectos do sistema. Ela é um subgrupo das declarações intertipos. A inserção provoca mudanças nos módulos de forma a não alterar diretamente o seu comportamento. É uma instrução sistémica (ortogonal) estática que permite adicionar warnings e errors em cima da detecção da utilização de certos padrões. O comando a seguir informa para o compilador definir um warning se qualquer parte do sistema chamar o método salvar() na classe Persistência. Observe a utilização do ponto de atuação(pointcut call) para capturar uma chamada de método. declare warning : call (void Persistência.salvar(Object)) : "Considere a utilização de Persistência.Salvar.OtimizadoO"; O aspecto é a unidade central do AspectJ, da mesma forma que a classe é a unidade central em Java. O aspecto contém códigos que expressam as regras para combinação estática e dinâmica. Pontos de atuação, adendos, instruções e declarações são combinadas no aspecto. Além dos elementos do AspectJ, o aspecto pode conter dados, métodos, classes aninhadas, como uma classe Java. A junção dos códigos das seções anteriores pode ser observada a seguir A aplicação começa pela classe Comunicador, java,que contém dois métodos para impressão de mensagens na tela . Um para entregar uma mensagem sem especificar o destinatário e outro em que o destinatário é especificado. A próxima etapa da aplicação será a construção de uma classe que teste a funcionalidade da classe Comunicador Conforme já visto anteriormente, todo programa Java válido é também um programa AspectJ válido, as classes Comunicador e ExecutaComunicador podem ser compiladas com o compilador do AspectJ (o ajc) e o resultado obtido será o mesmo se as classes fossem compiladas com o javac (compilador Java). Caso fosse necessário exibir a mensagem "Olá", antes dos métodos de entrega, no modelo convencional, seria necessário alterar o código dos dois métodos, incluindo o trecho de código referente à nova mensagem. Já com a POA e o AspectJ é possível efetuar essa implementação sem alterar as classes Comunicador e ExecutaComunicador. As classes Comunicador e ExecutaComunicador precisam ser compiladas juntamente com o aspecto AspectoComunicador, para que o aje produza o código combinado. A compilação deve ser efetuada da seguinte forma: > ajc com/aopbook/Comunicador.Java com/aopbook/ExecutaComunicador.java com/aopbook/AspectoComunicador.java > java com/aopbook/ExecutaComunicador Após a compilação, a classe ExecutaComunicador deve ser executada como comando java. A saída deve ser similar a apresentada pela Figura abaixo > java com/aopbook/ExecutaComunicador 1 -Declaração de um aspecto, bastante similar à declaração de uma classe. 3 - Definição da parte do adendo a ser executada antes do ponto de atuação entregaMensagemO 2 - O aspecto define um pointcut, o entregaMensagem ( ), que captura as chamadas para todos os métodos com o nome entrega ( ) na classe Comunicador. O símbolo * indica que não importa o tipo de retorno do método, e o ".." dentro dos parênteses, após entrega, não faz qualquer especificação quanto ao número de parâmetros e os respectivos tipos. Certamente a Orientação a Aspectos (OA) ainda não é o paradigma ideal, e acreditamos que em breve surgirão outros paradigmas capazes de melhor abstrair / expressar soluções, e por tal, permitindo a criação de soluções melhores. Mas o que fica claro para nós é que, a OA é um grande passo rumo ao paradigma ideal. GOETTEN Junior, WINCK Diogo, 2006. AspectJ - Programação Orientada a Aspectos com Java. São Paulo - SP: Novatec Editora, 2006. ASPECTJ Team. Disponível em: & lt; http: / / www.eclipse.org / aspectj & gt;. BÖLLERT, Kai. On Weaving Aspects. In the Proceedings of the Aspect - Oriented Programming Workshop at ECOOP ' 99, 1999. CHAVEZ, Christina. GARCIA, Alessandro. LUCENA, Carlos. Desenvolvimento Orientado a Aspectos. Anais do XVII Simpósio Brasileiro de Engenharia de Software, Manaus - Amazonas. Universidade Federal do Amazonas. 2003. CHAVEZ, Christina. LUCENA, Carlos. A Theory of Aspects for Aspect-Oriented Software Development. Anais do XVII Simpósio Brasileiro de Engenharia de Software. Manaus - Amazonas. Universidade Federal do Amazonas. 2003. GOETTEN Junior, WINCK Diogo e MACHADO Caio, 2004. Programação Orientada a Aspectos - Abordando Java e aspectJ. Workcomp Sul - SBC. KICZALES, G.; LAMPING, J.; MENDHEKAR, A.; MAEDA, C. et al. Aspect-oriented programming. Para apresentação em: ECOOP ' 97, LNCS 1241. Springer, 1997. PIVETA, Eduardo. Um modelo de suporte a programação orientada a aspectos. UFSC. Dissertação submetida como parte dos requisitos para a obtenção do grau de Mestre em Ciência da Computação, 2001. VACCARE Rosana Teresinha Braga, Apresentação Programação Orientada a Aspectos e a linguagem AspectJ