View Javadoc

1   /*
2    * $Id: HistorySession.java 132 2004-11-28 13:35:12Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.filters.validator;
24  
25  import java.io.Serializable;
26  import java.util.HashMap;
27  import java.util.LinkedList;
28  import java.util.Map;
29  
30  import javax.servlet.http.HttpSessionBindingEvent;
31  import javax.servlet.http.HttpSessionBindingListener;
32  
33  /**
34   * Object used to store request history.<br>
35   * Part of experimental extended validator filter. Work in progress.
36   *
37   * @author  Jose M. Palomar
38   * @version $Revision: 132 $
39   * @todo finish extended validator architecture.
40   */
41  public final class HistorySession implements HttpSessionBindingListener, Serializable {
42  
43      // Fields
44      /**
45       * Requests queue.
46       */
47      private LinkedList requestsQueue;
48  
49      /**
50       * Requests map.
51       */
52      private Map requestsMap;
53  
54      /**
55       * History size.
56       */
57      private int historySize;
58  
59      // Constructors
60      /**
61       * Creates a new <code>HistorySession</code>.
62       *
63       * @param historySize int history size.
64       */
65      public HistorySession(int historySize) {
66          this.historySize = historySize;
67          this.requestsQueue = new LinkedList();
68          this.requestsMap = new HashMap();
69      }
70  
71      // Methods
72      /**
73       * Adds a request to requests history.
74       *
75       * @param request HistoricRequest historic request.
76       */
77      public void add(HistoricRequest request) {
78  
79          requestsQueue.addFirst(request);
80          requestsMap.put(request.getTicket(), request);
81  
82          if (requestsQueue.size() >= historySize) {
83              HistoricRequest removed = (HistoricRequest) requestsQueue.removeLast();
84              requestsMap.remove(removed.getTicket());
85          }
86  
87      }
88  
89      /**
90       * Finds a historic request in history.
91       *
92       * @param ticket String request's ticket.
93       * @return HistoricRequest found historic request or <code>null</code>.
94       */
95      public HistoricRequest find(String ticket) {
96          return (HistoricRequest) requestsMap.get(ticket);
97      }
98  
99      /**
100      * Notifies the object that it is being bound to a session and identifies the
101      * session. This method do nothing.
102      *
103      * @param httpSessionBindingEvent HttpSessionBindingEvent the event that
104      * identifies the session.
105      * @see javax.servlet.http.HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
106      */
107     public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
108     }
109 
110     /**
111      * Notifies the object that it is being unbound from a session and identifies the
112      * session.
113      *
114      * @param httpSessionBindingEvent HttpSessionBindingEvent the event that
115      * identifies the session.
116      * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(HttpSessionBindingEvent)
117      */
118     public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
119     }
120 
121 }