KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > http > cookie > MemoryCookieManager


1 package net.matuschek.http.cookie;
2
3 /*********************************************
4     Copyright (c) 2001 by Daniel Matuschek
5 *********************************************/

6
7 import java.util.Vector JavaDoc;
8 import java.net.URL JavaDoc;
9                                          
10 /**
11  * This class is a container for storing cookies in
12  * the memory. It will automatically expire old cookies.
13  *
14  * @author Daniel Matuschek
15  * @version $Id $
16  */

17 public class MemoryCookieManager implements CookieManager
18 {
19
20   /**
21    * an internal thread that will be used to clean up old cookies
22    */

23   class CleanupThread extends Thread JavaDoc {
24     MemoryCookieManager cm = null;
25     boolean finished = false;
26
27     public CleanupThread(MemoryCookieManager cm) {
28       this.cm = cm;
29       this.setDaemon(true);
30     }
31
32     /** stop cleanup and finish this thread */
33     public void finish() {
34       this.finished = true;
35     }
36
37     public void run() {
38       while (! finished) {
39     // cleanup every minute
40
try {
41       sleep(60000);
42     } catch (InterruptedException JavaDoc e) {
43       this.finished=true;
44     }
45     cm.cleanUpExpired();
46       }
47     }
48   }
49
50
51   /** List of stored cookies */
52   private Vector JavaDoc<Cookie> cookies;
53
54   /** The background thread for cookie expiration */
55   private CleanupThread ct = null;
56
57
58
59   /**
60    * Default constructor, initializes a new CookieManager
61    * that has no cookies stored.
62    * It also starts a CleanUp thread that will periodically delete
63    * expired cookies.
64    */

65   public MemoryCookieManager() {
66     cookies = new Vector JavaDoc<Cookie>();
67
68     /* Insiders BugFix
69      * This code fragment must be executed at the add(Cookie) method.
70      * If we do it here then dead threads will be created.
71     // start cleanup thread as a daemon
72     ct = new CleanupThread(this);
73     ct.start();
74     */

75   }
76
77
78   /**
79    * Add this cookie. If there is already a cookie with the same name and
80    * path it will be owerwritten by the new cookie.
81    * @param cookie a Cookie that will be stored in this cookie manager
82    */

83   public void add(Cookie cookie) {
84     if (ct == null) {
85         synchronized (this) {
86             if (ct == null) {
87                 // start cleanup thread as a daemon
88
ct = new CleanupThread(this);
89                 ct.start();
90             }
91         }
92     }
93     for (int i=0; i<cookies.size(); i++) {
94       Cookie oldcookie = (Cookie)(cookies.elementAt(i));
95       if (cookie.overwrites(oldcookie)) {
96     cookies.removeElementAt(i);
97     i--;
98       }
99     }
100     cookies.add(cookie);
101   }
102
103
104   /**
105    * How many cookies are currently stored in this CookieManager ?
106    * @return the number of stored Cookies
107    */

108   public int countCookies() {
109     return this.cookies.size();
110   }
111
112
113   /**
114    * Get the cookie values for the given URL.
115    * @return a String containing a list of NAME=VALUE pairs (separated by
116    * semicolon) containing all cookie values that are valid for the
117    * given URL, <br/ >
118    * null if no cookies can be found for this URL
119    */

120   public String JavaDoc cookiesForURL(URL JavaDoc u) {
121     final String JavaDoc SEP = "; ";
122     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
123     
124     for (int i=0; i<cookies.size(); i++) {
125       Cookie c = (Cookie)(cookies.elementAt(i));
126       if (c.isValid(u)) {
127     sb.append(c.getNameValuePair());
128     sb.append(SEP);
129       }
130     }
131
132     if (sb.length() > 0) {
133       // ignore the last "; "
134
return sb.substring(0,sb.length()-SEP.length());
135     } else {
136       return null;
137     }
138   }
139
140
141   /**
142    * Remove all stored cookies
143    */

144   public void clear() {
145     this.cookies.clear();
146   }
147
148
149   /**
150    * Cleans up expired cookies
151    */

152   protected void cleanUpExpired() {
153     for (int i=0; i<cookies.size(); i++) {
154       Cookie c = (Cookie)(cookies.elementAt(i));
155
156       // if this cookie has expired, remove it
157
if (! c.isValid()) {
158     cookies.removeElementAt(i);
159     i--;
160       }
161     }
162   }
163   
164   /**
165    * <b>Insiders BugFix</b>
166    * This method finishes the CleanUpThread.
167    */

168   public void finish() {
169     if (ct != null) {
170         ct.finished = true;
171         ct.interrupt();
172     }
173   }
174   
175   
176   /**
177    * Show all cookies
178    */

179   public String JavaDoc toString() {
180     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
181     for (int i=0; i<cookies.size(); i++) {
182         sb.append(i+": ");
183         sb.append(cookies.elementAt(i).toString());
184         sb.append("\n");
185     }
186     return sb.toString();
187   }
188
189 }
190
Popular Tags