KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > db > CategoryCache


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/CategoryCache.java,v 1.10 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.10 $
5  * $Date: 2006/04/14 17:05:26 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.db;
42
43 import java.util.*;
44
45 import net.myvietnam.mvncore.exception.DatabaseException;
46 import net.myvietnam.mvncore.exception.ObjectNotFoundException;
47 import net.myvietnam.mvncore.util.DateUtil;
48
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51
52 import com.mvnforum.MVNForumConfig;
53 import com.whirlycott.cache.*;
54
55 public class CategoryCache {
56
57     public static final long TIME_OUT = DateUtil.HOUR;
58
59     private static Log log = LogFactory.getLog(ForumCache.class);
60
61     // static singleton variable
62
static private CategoryCache instance = new CategoryCache();
63
64     // instance variable
65
private Cache cache;
66
67     public CategoryCache() {
68         //Use the cache manager to create the default cache
69
try {
70             if (MVNForumConfig.getEnableCacheCategory()) {
71                 cache = CacheManager.getInstance().getCache("category");
72             }
73         } catch (CacheException ex) {
74             log.error("Cannot get the WhirlyCache. Category caching is disabled.", ex);
75         } catch (LinkageError JavaDoc e) {
76             // @todo: Should be never throw
77
log.error("Cannot get the WhirlyCache caused by Package Conflict. Category caching is disabled.", e);
78         }
79     }
80
81     /**
82      * Returns the single instance
83      * @return CategoryCache : the singleton instance.
84      *
85      * NOTE: if use normal singleton pattern, this method should be synchronized
86      */

87     static public CategoryCache getInstance() {
88         return instance;
89     }
90
91     public String JavaDoc getEfficiencyReport() {
92         String JavaDoc result = "No report";
93         if (cache == null) {
94             if (MVNForumConfig.getEnableCacheCategory() == false) {
95                 result = "Cache is disabled.";
96             } else {
97                 result = "Cache cannot be inited";
98             }
99         } else if (cache instanceof CacheDecorator) {
100             result = ((CacheDecorator) cache).getEfficiencyReport();
101         }
102         return result;
103     }
104
105     public void clear() {
106         if (cache != null) {
107             cache.clear();
108         }
109     }
110
111     // instance variable
112
// private List beanArray = null;
113

114     /**
115      * This is a private method, and a util method If a method call ensureNewData(), then it MUST be synchronized
116      */

117     /*
118      * private synchronized void ensureNewData() throws DatabaseException { if ( CategoryDAOImplJDBC.isDirty() ||
119      * (beanArray == null) ) { CategoryDAOImplJDBC.setDirty(false); beanArray =
120      * (List)DAOFactory.getCategoryDAO().getCategories(); } }
121      */

122
123     /**
124      * IMPORTANT NOTE: The caller must not alter the returned collection.
125      * Any attempt to modify it will throw an <code>UnsupportedOperationException</code>.
126      */

127     public List getBeans() throws DatabaseException {
128         List result = null;
129         if (cache != null) {
130             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
131             buffer.append("getBeans");
132             String JavaDoc key = buffer.toString();
133             result = (List) cache.retrieve(key);
134             if (result == null) {
135                 result = (List) DAOFactory.getCategoryDAO().getCategories();
136                 cache.store(key, result, TIME_OUT);
137             }
138         } else {
139             result = (List) DAOFactory.getCategoryDAO().getCategories();
140         }
141         return Collections.unmodifiableList(result);
142     }
143
144     public CategoryBean getBean(int categoryID) throws DatabaseException, ObjectNotFoundException {
145
146         // ensureNewData();
147
List beans = getBeans(); // We do not want the list to change in the process.
148

149         int size = beans.size();
150         for (int i = 0; i < size; i++) {
151             CategoryBean bean = (CategoryBean) beans.get(i);
152             if (bean.getCategoryID() == categoryID) {
153                 return bean;
154             }
155         }
156         // @todo : localize me
157
throw new ObjectNotFoundException("Cannot find the row in table Category " + "where primary key = (" + categoryID + ").");
158     }
159
160     public CategoryBean getBean(String JavaDoc categoryName) throws DatabaseException, ObjectNotFoundException {
161
162         // ensureNewData();
163
List beans = getBeans(); // We do not want the list to change in the process.
164

165         for (Iterator it = beans.iterator(); it.hasNext();) {
166             CategoryBean bean = (CategoryBean) it.next();
167             if (bean.getCategoryName().equals(categoryName)) {
168                 return bean;
169             }
170         }
171         // @todo : localize me
172
throw new ObjectNotFoundException("Cannot find a category with the given name: " + categoryName);
173     }
174
175 }
176
Popular Tags