@@ -0,0 +1,2236 @@
+package com.justjournal;%0A%0Aimport com.justjournal.db.SQLHelper;%0Aimport org.apache.log4j.Category;%0Aimport sun.jdbc.rowset.CachedRowSet;%0A%0A/**%0A * Count access attempts to a resource%0A *%0A * TODO: Lock the insert/update code at the database level.  stored proc?%0A *%0A * @author Lucas Holt%0A * @version $Id: HitCounter.java,v 1.1 2008/07/27 11:17:02 laffer1 Exp $%0A *          User: laffer1%0A *          Date: Jul 27, 2008%0A *          Time: 6:33:49 AM%0A */%0Apublic class HitCounter %7B%0A%0A    private static final Category log = Category.getInstance(HitCounter.class.getName());%0A%0A    /**%0A     * Create a resource record%0A     * @param resource url%0A     * @throws Exception Database access or creation error%0A     */%0A    public void insert(String resource) throws Exception %7B%0A        SQLHelper.executeNonQuery(%22Insert into hitcount (resource) VALUES(%22 + resource + %22);%22);%0A    %7D%0A%0A    /**%0A     * Update a resource record%0A     * @param resource url%0A     * @param count counter value to save%0A     * @throws Exception database access error%0A     */%0A    public void update(String resource, int count) throws Exception %7B%0A        SQLHelper.executeNonQuery(%22UPDATE hitcount set count= %22 + count + %22 WHERE resource=%5C%22%22 + resource + %22%5C%22);%22);%0A    %7D%0A%0A    /**%0A     * Log a hit%0A     * @param resource url%0A     * @return New value of the counter (assumes it worked)%0A     */%0A    public int increment(String resource) %7B%0A        int count = count(resource);%0A%0A        try %7B%0A            // probably not in the database yet.%0A            if (count %3C 0)%0A                insert(resource);%0A            else%0A                update(resource, count + 1);%0A        %7D catch (Exception e) %7B%0A            log.error(e.getMessage());%0A        %7D%0A%0A        return count + 1;%0A    %7D%0A%0A    /**%0A     * Retrieve the counter value%0A     * @param resource url%0A     * @return current counter value%0A     */%0A    public int count(String resource) %7B%0A        int count = -1;%0A        CachedRowSet rs;%0A%0A        try %7B%0A            rs = SQLHelper.executeResultSet(%22SELECT count FROM hitcount WHERE resource=%5C%22%22 + resource + %22%5C%22 LIMIT 1;%22);%0A%0A            if (rs.next())%0A                count = rs.getInt(1);%0A%0A        %7D catch (Exception e) %7B%0A            log.error(e.getMessage());%0A        %7D%0A%0A        return count;%0A    %7D%0A%0A%7D%0A

Add hit counter backend.