Source Code Cross Referenced for WikiCache.java in  » Wiki-Engine » JAMWiki » org » jamwiki » utils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Wiki Engine » JAMWiki » org.jamwiki.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003:         *
004:         * This program is free software; you can redistribute it and/or modify
005:         * it under the terms of the latest version of the GNU Lesser General
006:         * Public License as published by the Free Software Foundation;
007:         *
008:         * This program is distributed in the hope that it will be useful,
009:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
010:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
011:         * GNU Lesser General Public License for more details.
012:         *
013:         * You should have received a copy of the GNU Lesser General Public License
014:         * along with this program (LICENSE.txt); if not, write to the Free Software
015:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
016:         */package org.jamwiki.utils;
017:
018:        import java.io.File;
019:        import net.sf.ehcache.Cache;
020:        import net.sf.ehcache.CacheManager;
021:        import net.sf.ehcache.Element;
022:        import net.sf.ehcache.config.CacheConfiguration;
023:        import net.sf.ehcache.config.Configuration;
024:        import net.sf.ehcache.config.DiskStoreConfiguration;
025:        import org.jamwiki.Environment;
026:
027:        /**
028:         * Implement utility functions that interact with the cache and provide the
029:         * infrastructure for storing and retrieving items from the cache.
030:         */
031:        public class WikiCache {
032:
033:            private static final WikiLogger logger = WikiLogger
034:                    .getLogger(WikiCache.class.getName());
035:            private static CacheManager cacheManager = null;
036:
037:            /** Directory for cache files. */
038:            private static final String CACHE_DIR = "cache";
039:
040:            static {
041:                WikiCache.initialize();
042:            }
043:
044:            /**
045:             *
046:             */
047:            private WikiCache() {
048:            }
049:
050:            /**
051:             * Add an object to the cache.
052:             *
053:             * @param cacheName The name of the cache that the object is being added
054:             *  to.
055:             * @param key A String, Integer, or other object to use as the key for
056:             *  storing and retrieving this object from the cache.
057:             * @param value The object that is being stored in the cache.
058:             */
059:            public static void addToCache(String cacheName, Object key,
060:                    Object value) {
061:                Cache cache = WikiCache.getCache(cacheName);
062:                cache.put(new Element(key, value));
063:            }
064:
065:            /**
066:             * Add an object to the cache.
067:             *
068:             * @param cacheName The name of the cache that the object is being added
069:             *  to.
070:             * @param key An int value to use as the key for storing and retrieving
071:             *  this object from the cache.
072:             * @param value The object that is being stored in the cache.
073:             */
074:            public static void addToCache(String cacheName, int key,
075:                    Object value) {
076:                WikiCache.addToCache(cacheName, new Integer(key), value);
077:            }
078:
079:            /**
080:             * Internal method used to retrieve a cache given the cache name.  If no
081:             * cache exists with the given name then a new cache will be created.
082:             *
083:             * @param cacheName The name of the cache to retrieve.
084:             * @return The existing cache with the given name, or a new cache if no
085:             *  existing cache exists.
086:             */
087:            private static Cache getCache(String cacheName) {
088:                if (!WikiCache.cacheManager.cacheExists(cacheName)) {
089:                    int maxSize = Environment
090:                            .getIntValue(Environment.PROP_CACHE_INDIVIDUAL_SIZE);
091:                    int maxAge = Environment
092:                            .getIntValue(Environment.PROP_CACHE_MAX_AGE);
093:                    int maxIdleAge = Environment
094:                            .getIntValue(Environment.PROP_CACHE_MAX_IDLE_AGE);
095:                    Cache cache = new Cache(cacheName, maxSize, true, false,
096:                            maxAge, maxIdleAge);
097:                    WikiCache.cacheManager.addCache(cache);
098:                }
099:                return WikiCache.cacheManager.getCache(cacheName);
100:            }
101:
102:            /**
103:             * Initialize the cache, clearing any existing cache instances and loading
104:             * a new cache instance.
105:             */
106:            public static void initialize() {
107:                try {
108:                    if (WikiCache.cacheManager != null) {
109:                        WikiCache.cacheManager.removalAll();
110:                        WikiCache.cacheManager.shutdown();
111:                    }
112:                    File directory = new File(Environment
113:                            .getValue(Environment.PROP_BASE_FILE_DIR),
114:                            CACHE_DIR);
115:                    if (!directory.exists()) {
116:                        directory.mkdir();
117:                    }
118:                    Configuration configuration = new Configuration();
119:                    CacheConfiguration defaultCacheConfiguration = new CacheConfiguration();
120:                    defaultCacheConfiguration.setDiskPersistent(false);
121:                    defaultCacheConfiguration.setEternal(false);
122:                    defaultCacheConfiguration.setOverflowToDisk(true);
123:                    defaultCacheConfiguration
124:                            .setMaxElementsInMemory(Environment
125:                                    .getIntValue(Environment.PROP_CACHE_TOTAL_SIZE));
126:                    defaultCacheConfiguration.setName("defaultCache");
127:                    configuration.addDefaultCache(defaultCacheConfiguration);
128:                    DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
129:                    //			diskStoreConfiguration.addExpiryThreadPool(new ThreadPoolConfiguration("", new Integer(5), new Integer(5)));
130:                    //			diskStoreConfiguration.addSpoolThreadPool(new ThreadPoolConfiguration("", new Integer(5), new Integer(5)));
131:                    diskStoreConfiguration.setPath(directory.getPath());
132:                    configuration.addDiskStore(diskStoreConfiguration);
133:                    WikiCache.cacheManager = new CacheManager(configuration);
134:                } catch (Exception e) {
135:                    logger.severe("Initialization error in WikiCache", e);
136:                }
137:            }
138:
139:            public static void shutdown() {
140:                if (WikiCache.cacheManager != null) {
141:                    WikiCache.cacheManager.shutdown();
142:                    WikiCache.cacheManager = null;
143:                }
144:
145:            }
146:
147:            /**
148:             * Given a virtual wiki name and a topic name, generate a unique key value
149:             * that can be used to store and retrieve cache objects.
150:             *
151:             * @param virtualWiki The virtual wiki name for the key value being
152:             *  created.
153:             * @param topicName The name of the topic for the key value being created.
154:             * @return The generated key value.
155:             */
156:            public static String key(String virtualWiki, String topicName) {
157:                return virtualWiki + "/" + topicName;
158:            }
159:
160:            /**
161:             * Remove a cache with the given name from the system, freeing any
162:             * resources used by that cache.
163:             *
164:             * @param cacheName The name of the cache being removed.
165:             */
166:            public static void removeCache(String cacheName) {
167:                WikiCache.cacheManager.removeCache(cacheName);
168:            }
169:
170:            /**
171:             * Remove a value from the cache with the given key and name.
172:             *
173:             * @param cacheName The name of the cache from which the object is being
174:             *  removed.
175:             * @param key The key for the record that is being removed from the cache.
176:             */
177:            public static void removeFromCache(String cacheName, Object key) {
178:                Cache cache = WikiCache.getCache(cacheName);
179:                cache.remove(key);
180:            }
181:
182:            /**
183:             * Remove a value from the cache with the given key and name.
184:             *
185:             * @param cacheName The name of the cache from which the object is being
186:             *  removed.
187:             * @param key The key for the record that is being removed from the cache.
188:             */
189:            public static void removeFromCache(String cacheName, int key) {
190:                WikiCache.removeFromCache(cacheName, new Integer(key));
191:            }
192:
193:            /**
194:             * Retrieve a cached element from the cache.  This method will return
195:             * <code>null</code> if no matching element is cached, an element with
196:             * no value if a <code>null</code> value is cached, or an element with a
197:             * valid object value if such an element is cached.
198:             *
199:             * @param cacheName The name of the cache from which the object is being
200:             *  retrieved.
201:             * @param key The key for the record that is being retrieved from the
202:             *  cache.
203:             * @return A new <code>Element</code> object containing the key and cached
204:             *  object value.
205:             */
206:            public static Element retrieveFromCache(String cacheName, Object key) {
207:                Cache cache = WikiCache.getCache(cacheName);
208:                return cache.get(key);
209:            }
210:
211:            /**
212:             * Retrieve a cached element from the cache.  This method will return
213:             * <code>null</code> if no matching element is cached, an element with
214:             * no value if a <code>null</code> value is cached, or an element with a
215:             * valid object value if such an element is cached.
216:             *
217:             * @param cacheName The name of the cache from which the object is being
218:             *  retrieved.
219:             * @param key The key for the record that is being retrieved from the
220:             *  cache.
221:             * @return A new <code>Element</code> object containing the key and cached
222:             *  object value.
223:             */
224:            public static Element retrieveFromCache(String cacheName, int key) {
225:                return WikiCache.retrieveFromCache(cacheName, new Integer(key));
226:            }
227:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.