Source Code Cross Referenced for CacheConfiguration.java in  » Cache » shiftone-cache » org » shiftone » cache » 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
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Cache » shiftone cache » org.shiftone.cache 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.shiftone.cache;
002:
003:        import org.shiftone.cache.config.ConfigurationInternals;
004:        import org.shiftone.cache.policy.zero.ZeroCacheFactory;
005:        import org.shiftone.cache.util.Log;
006:
007:        import java.io.File;
008:        import java.io.FileInputStream;
009:        import java.io.InputStream;
010:        import java.util.Properties;
011:
012:        /**
013:         * @version $Revision: 1.13 $
014:         * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
015:         */
016:        public class CacheConfiguration {
017:
018:            private static final Log LOG = new Log(CacheConfiguration.class);
019:            private static final String DEFAULT_CACHE_CONFIG = "cache.properties";
020:            private ConfigurationInternals internals;
021:
022:            /**
023:             * create a default cache configuration
024:             */
025:            public CacheConfiguration() throws ConfigurationException {
026:                this (new String[] { DEFAULT_CACHE_CONFIG });
027:            }
028:
029:            public CacheConfiguration(String fileName)
030:                    throws ConfigurationException {
031:                this (new String[] { fileName });
032:            }
033:
034:            public CacheConfiguration(String[] fileNames)
035:                    throws ConfigurationException {
036:
037:                Properties properties = new Properties();
038:
039:                for (int i = 0; i < fileNames.length; i++) {
040:                    init(properties, fileNames[i]);
041:                }
042:
043:                internals = new ConfigurationInternals(properties);
044:            }
045:
046:            public CacheConfiguration(Properties properties)
047:                    throws ConfigurationException {
048:                internals = new ConfigurationInternals(properties);
049:            }
050:
051:            public void init(Properties properties, String fileName)
052:                    throws ConfigurationException {
053:
054:                File file;
055:                InputStream inputStream = null;
056:
057:                try {
058:                    file = new File(fileName);
059:
060:                    if (file.isFile()) {
061:                        inputStream = new FileInputStream(fileName);
062:
063:                        LOG.info("file: " + file.getAbsolutePath());
064:                    } else {
065:                        inputStream = getClass().getResourceAsStream(fileName);
066:
067:                        LOG.info("resource: " + fileName);
068:                    }
069:
070:                    properties.load(inputStream);
071:                } catch (Throwable e) {
072:                    throw new ConfigurationException(e);
073:                } finally {
074:                    close(inputStream);
075:                }
076:            }
077:
078:            private void close(InputStream inputStream) {
079:
080:                if (inputStream != null) {
081:                    try {
082:                        inputStream.close();
083:                    } catch (Throwable e) {
084:                    }
085:                }
086:            }
087:
088:            /**
089:             * Obtain a configured cache factory by it's name.  If no factory
090:             * exists by this name, a ConfigurationException is thrown.
091:             */
092:            public CacheFactory getCacheFactory(String factoryName)
093:                    throws ConfigurationException {
094:
095:                CacheFactory cacheFactory = null;
096:
097:                cacheFactory = internals.getFactory(factoryName);
098:
099:                if (cacheFactory == null) {
100:                    throw new ConfigurationException(
101:                            "cache factory not configured : " + cacheFactory);
102:                }
103:
104:                return cacheFactory;
105:            }
106:
107:            /**
108:             * Create a new cache, using the configured values for the
109:             * factory, timeout, and maxSize.
110:             */
111:            public Cache createConfiguredCache(String cacheName)
112:                    throws ConfigurationException {
113:
114:                CacheFactory factory = getConfiguredFactoryForCache(cacheName);
115:                long timeout = getConfiguredTimeoutForCache(cacheName);
116:                int maxSize = getConfiguredMaxSizeForCache(cacheName);
117:
118:                return factory.newInstance(cacheName, timeout, maxSize);
119:            }
120:
121:            /**
122:             * Create a new cache by looking up the configured factory, and then using supplied
123:             * name, timeout and max size.  Method requested by Neville.
124:             */
125:            public Cache createConfiguredCache(String cacheName, long timeout,
126:                    int maxSize) throws ConfigurationException {
127:
128:                CacheFactory factory = getConfiguredFactoryForCache(cacheName);
129:
130:                return factory.newInstance(cacheName, timeout, maxSize);
131:            }
132:
133:            /**
134:             * Attempt to create a configured cache, as in createConfiguredCache, except if
135:             * an error occures, a "zero cache" will be returned.  In other words, any exception
136:             * is supressed, and the failure is hidden from the application (except there won't
137:             * be any caching).
138:             */
139:            public Cache createConfiguredCacheSafely(String cacheName) {
140:
141:                try {
142:                    return createConfiguredCache(cacheName);
143:                } catch (Exception e) {
144:                    LOG.error("error with configuration for cache : "
145:                            + cacheName, e);
146:
147:                    return ZeroCacheFactory.NULL_CACHE;
148:                }
149:            }
150:
151:            public CacheFactory getConfiguredFactoryForCache(String cacheName)
152:                    throws ConfigurationException {
153:
154:                String factoryName = internals.getConfiguredCacheProperty(
155:                        "factory", cacheName);
156:
157:                return getCacheFactory(factoryName);
158:            }
159:
160:            public long getConfiguredTimeoutForCache(String cacheName)
161:                    throws ConfigurationException {
162:
163:                String timeout = internals.getConfiguredCacheProperty(
164:                        "timeout", cacheName);
165:
166:                return Long.parseLong(timeout);
167:            }
168:
169:            public int getConfiguredMaxSizeForCache(String cacheName)
170:                    throws ConfigurationException {
171:
172:                String maxsize = internals.getConfiguredCacheProperty(
173:                        "maxsize", cacheName);
174:
175:                return Integer.parseInt(maxsize);
176:            }
177:
178:            public static void main(String[] args) throws Exception {
179:
180:                try {
181:                    System.out.println("test");
182:
183:                    CacheConfiguration config = new CacheConfiguration();
184:                    CacheFactory factory;
185:
186:                    factory = config.getCacheFactory("lru");
187:                    factory = config.getCacheFactory("missTest");
188:                    factory = config.getCacheFactory("statLru");
189:                    factory = config.getCacheFactory("softLfu");
190:
191:                    Cache cache = config
192:                            .createConfiguredCache("com.indemand.royalty.organization.Channel");
193:
194:                    LOG.info("cache = " + cache);
195:
196:                    // factory.newInstance("test", 100, 100);
197:                    LOG.info(factory.newInstance("xxx", 1, 2));
198:                } catch (Throwable e) {
199:                    LOG.error("main", e);
200:                }
201:            }
202:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.