KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > javacoding > jspider > core > storage > jdbc > CookieDAOImpl


1 package net.javacoding.jspider.core.storage.jdbc;
2
3 import net.javacoding.jspider.core.storage.CookieDAO;
4 import net.javacoding.jspider.core.storage.spi.CookieDAOSPI;
5 import net.javacoding.jspider.core.storage.spi.StorageSPI;
6 import net.javacoding.jspider.core.logging.LogFactory;
7 import net.javacoding.jspider.core.logging.Log;
8 import net.javacoding.jspider.api.model.Cookie;
9
10 import java.util.*;
11 import java.sql.*;
12
13 /**
14  * $Id: CookieDAOImpl.java,v 1.3 2003/04/11 16:37:05 vanrogu Exp $
15  */

16 class CookieDAOImpl implements CookieDAOSPI {
17
18     protected Log log;
19
20     protected StorageSPI storage;
21     protected DBUtil dbUtil;
22     protected Map cookies;
23
24     public CookieDAOImpl ( StorageSPI storage, DBUtil dbUtil ) {
25         this.log = LogFactory.getLog(CookieDAO.class);
26         this.storage = storage;
27         this.dbUtil = dbUtil;
28         this.cookies = new HashMap ( );
29     }
30
31     public Cookie[] find(int id) {
32         ArrayList al = new ArrayList ( );
33         Statement st = null;
34         ResultSet rs = null;
35         try {
36             Connection connection = dbUtil.getConnection();
37             st = connection.createStatement();
38             rs = st.executeQuery("select * from jspider_cookie where site='" + id + "'" );
39             while ( rs.next() ) {
40                 al.add(createCookieFromRecord(rs));
41             }
42         } catch (SQLException e) {
43             log.error("SQLException", e);
44         } finally {
45             dbUtil.safeClose(rs, log);
46             dbUtil.safeClose(st, log);
47         }
48         return (Cookie[]) al.toArray(new Cookie[al.size()]);
49     }
50
51     public void save(int id, Cookie[] cookies) {
52         Connection connection = dbUtil.getConnection();
53
54         Statement st = null;
55         ResultSet rs = null;
56
57         for (int i = 0; i < cookies.length; i++) {
58             Cookie cookie = cookies[i];
59             try {
60                 st = connection.createStatement();
61                 rs = st.executeQuery("select count(*) as count from jspider_cookie where id='" + id + "' and name='" + cookie.getName() + "'");
62                 rs.next();
63                 int count = rs.getInt("count");
64                 if ( count == 0 ) {
65                     st = connection.createStatement();
66                     st.executeUpdate("insert into jspider_cookie ( site, name, value, path, expires, domain ) values ( '" + id + "', '" + cookie.getName() + "', '" + cookie.getValue() + "', '"+ cookie.getPath() + "', '" + cookie.getExpires() + "', '" + cookie.getDomain() + "')");
67                 } else {
68                     st = connection.createStatement();
69                     st.executeUpdate("update jspider_cookie set value='" + cookie.getValue() + "', path='" + cookie.getPath() + "', domain='"+ cookie.getDomain() + "', expires='" + cookie.getExpires() + "' where site='" + id + "' and name='" + cookie.getName() + "'");
70                 }
71             } catch (SQLException e) {
72                 log.error("SQLException", e);
73             } finally {
74                 dbUtil.safeClose(rs, log);
75                 dbUtil.safeClose(st, log);
76             }
77         }
78     }
79
80     protected static Cookie createCookieFromRecord ( ResultSet rs ) throws SQLException {
81         String JavaDoc name = rs.getString("name") ;
82         String JavaDoc value = rs.getString("value") ;
83         String JavaDoc path = rs.getString("path") ;
84         String JavaDoc domain = rs.getString("domain") ;
85         String JavaDoc expires = rs.getString("expires") ;
86         return new Cookie(name, value, domain, path, expires);
87     }
88
89 }
90
Popular Tags