KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > security > SessionListener


1 package dinamica.security;
2
3 import dinamica.*;
4 import javax.servlet.http.*;
5 import javax.servlet.*;
6 import java.sql.*;
7 import javax.sql.*;
8
9 /**
10  * This class listens to session events. It is
11  * used to destroy the session record in the s_session table
12 */

13
14 public final class SessionListener implements HttpSessionListener
15 {
16
17
18     public void sessionCreated(javax.servlet.http.HttpSessionEvent JavaDoc sbe)
19     {
20     }
21     
22     /**
23      * Get sessionID an delete corresponding record
24      * in the security database table "s_session"
25      */

26     public void sessionDestroyed(javax.servlet.http.HttpSessionEvent JavaDoc sbe)
27     {
28         
29         //get session ID
30
HttpSession s = sbe.getSession();
31         String JavaDoc id = s.getId();
32
33         Connection conn = null;
34         
35         String JavaDoc sql = "delete from s_session where jsessionid = '" + id + "'";
36         
37         ServletContext ctx = s.getServletContext();
38
39         try
40         {
41
42             //get datasource and DB connection
43
//get security datasource
44
String JavaDoc jndiName = (String JavaDoc)ctx.getAttribute("dinamica.security.datasource");
45             if (jndiName==null)
46                 throw new Throwable JavaDoc("Context attribute [dinamica.security.datasource] is null, check your security filter configuration.");
47
48             DataSource ds = Jndi.getDataSource(jndiName);
49             conn = ds.getConnection();
50         
51             Db db = new Db(conn);
52             db.exec(sql);
53             
54         }
55         catch (Throwable JavaDoc e)
56         {
57             System.err.println( "[WARNING@" + new java.util.Date JavaDoc() + "] SessionListener: " + e.getMessage() );
58         }
59         finally
60         {
61             if (conn!=null)
62             {
63                 try { conn.close(); } catch (Throwable JavaDoc e1){}
64             }
65             
66         }
67     
68     }
69
70 }
71
72
Popular Tags