FileDocCategorySizeDatePackage
CounterBean.javaAPI DocExample2584Thu Jun 28 16:14:16 BST 2001com.ora.jsp.beans.counter

CounterBean

public class CounterBean extends Object implements Serializable
This class maintains page counters. Note that this class is intended to be used in session and application scope so regular setter and getter methods for pageContext can not be used; all info needed to get and increment the counter value must be provided as parameters to the corresponding method to avoid multi-threading issues.
author
Hans Bergsten, Gefion software
version
1.0.1

Fields Summary
private Hashtable
counters
Constructors Summary
Methods Summary
public intgetCurrentValue(java.lang.String uri)
Returns the current value of the counter for this page.

param
uri the URI for the page, as provided by HttpServletRequest.getRequestURI()
return
the current counter value, or -1 if it's not found


                                                     
        
        int value = -1;
        int[] counter = (int[]) counters.get(uri);
        if (counter != null) {
            value = counter[0];
        }
        return value; 
    
public intgetNextValue(java.lang.String uri)
Increments the counter for this page and returns the new value.

param
uri the URI for the page, as provided by HttpServletRequest.getRequestURI()
return
the current counter value incremented by one

        incrementValue(uri);
        return getCurrentValue(uri);
    
public voidincrementValue(java.lang.String uri)
Increments the counter for this page. If the permanent is true, the new value is also saved in a file.

param
uri the URI for the page, as provided by HttpServletRequest.getRequestURI()

        /*
         * Synchronized block for incrementing the counter to handle
         * concurrent calls from multiple threads
         */
        synchronized (counters) {
            int[] counter = (int[]) counters.get(uri);
            if (counter == null) {
                counter = new int[1];
                counter[0] = 0;
            }
            counter[0]++;
            counters.put(uri, counter);
        }