KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > security > PasswordNotUsed


1 package dinamica.security;
2
3 import java.sql.Connection JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6 import javax.sql.DataSource JavaDoc;
7
8 import dinamica.*;
9
10 /**
11  * This validator can be used to detect if there are
12  * any related records in another table before trying to
13  * delete a certain record. Returns TRUE if there are no
14  * related records meaning that the record can be deleted.
15  * You will need to call this validator for every referential
16  * integrity check before deleting a record.
17  * <br><br>
18  * Rrequires the following custom attributes:<br>
19  * <ul>
20  * <li> sql: query to find any related record. You may use field makers
21  * that will be replaced by the corresponding request parameters passed
22  * as a recordset to the isValid method.
23  * </ul>
24  *
25  * (c) 2004 Martin Cordova<br>
26  * This code is released under the LGPL license<br>
27  * Dinamica Framework - http://www.martincordova.com
28  * @author Martin Cordova (dinamica@martincordova.com)
29  * */

30 public class PasswordNotUsed extends AbstractValidator
31 {
32
33     /* (non-Javadoc)
34      * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.HashMap)
35      */

36     public boolean isValid(
37         HttpServletRequest JavaDoc req,
38         Recordset inputParams,
39         HashMap JavaDoc attribs)
40         throws Throwable JavaDoc
41     {
42
43         boolean flag = true;
44
45         //get security datasource name
46
String JavaDoc jndiName = (String JavaDoc)getContext().getAttribute("dinamica.security.datasource");
47         if (jndiName==null)
48             throw new Throwable JavaDoc("Context attribute [dinamica.security.datasource] is null, check your security filter configuration.");
49         
50         //get datasource and DB connection
51
DataSource JavaDoc ds = Jndi.getDataSource(jndiName);
52         Connection JavaDoc conn = ds.getConnection();
53         this.setConnection(conn);
54         
55         try
56         {
57             //get db channel
58
Db db = getDb();
59
60             //detect if sql parameter was passed to the validator
61
boolean bSql = attribs.containsKey("sql");
62
63             if (!bSql)
64             {
65                 throw new Throwable JavaDoc("[" + this.getClass().getName() + "] Missing attribute [sql] in validator.xml");
66             }
67             else
68             {
69
70                 //read config
71
String JavaDoc query = (String JavaDoc)attribs.get("sql");
72             
73                 //load template and replace parameter values
74
String JavaDoc sql = getResource(query);
75                 sql = getSQL(sql, inputParams);
76
77                 //execute sql
78
Recordset rs = db.get(sql);
79                 if (rs.getRecordCount()>0)
80                     flag = false;
81
82             }
83         }
84         catch (Throwable JavaDoc e)
85         {
86             throw e;
87         }
88         finally
89         {
90             if (conn!=null)
91                 conn.close();
92         }
93         
94         return flag;
95
96     }
97
98 }
99
Popular Tags