KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > internal > caches > AbstractIDItemCache


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * Abstract IDItem cache.
28  */

29 package net.killingar.forum.internal.caches;
30
31 import it.unimi.dsi.fastutil.longs.Long2ObjectAVLTreeMap;
32 import net.killingar.forum.internal.IDItem;
33 import net.killingar.forum.internal.managers.AbstractManager;
34
35 import java.sql.*;
36
37 public abstract class AbstractIDItemCache
38 {
39     protected Timestamp latestUpdate = new Timestamp(0);
40     protected Long2ObjectAVLTreeMap objects = new Long2ObjectAVLTreeMap();
41
42     public synchronized IDItem get(long id) throws SQLException
43     {
44         update();
45
46         if (!(objects.get(id) instanceof IDItem) && objects.get(id) != null)
47             throw new ClassCastException JavaDoc("cast failed from "+objects.get(id).getClass()+" to IDItem");
48
49         return (IDItem)objects.get(id);
50     }
51
52     protected abstract PreparedStatement createStatement(Connection c) throws SQLException;
53     protected abstract IDItem getObjectFromRow(ResultSet result) throws SQLException;
54
55     public synchronized void add(IDItem inO)
56     {
57         objects.put(inO.getId(), inO);
58     }
59
60     protected long getUpdateTimeout()
61     {
62         return 1500;
63     }
64
65     protected synchronized void update() throws SQLException
66     {
67         if (System.currentTimeMillis()-latestUpdate.getTime() < getUpdateTimeout())
68             return;
69         /*
70     get timeNow
71         get all new messages since lastUpdateTime
72         update cache with all new messages
73         set lastUpdateTime = timeNow
74         */

75
76         Timestamp now = new Timestamp(System.currentTimeMillis()-1001); // subtract one second to make sure database rounding doesn't screw us up
77

78         Connection c = null;
79         PreparedStatement statement = null;
80         ResultSet result = null;
81
82         try
83         {
84             c = AbstractManager.getNewConnection();
85             statement = createStatement(c);
86
87             result = statement.executeQuery();
88             while (result.next())
89                 add(getObjectFromRow(result));
90         }
91         finally { AbstractManager.closeAll(c, statement, result); }
92
93         latestUpdate = now;
94     }
95 }
96
Popular Tags