Source Code Cross Referenced for EhCacheProvider.java in  » Cache » ehcache » net » sf » ehcache » hibernate » 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 » ehcache » net.sf.ehcache.hibernate 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *  Copyright 2003-2007 Luck Consulting Pty Ltd
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */package net.sf.ehcache.hibernate;
016:
017:        import net.sf.ehcache.CacheManager;
018:        import net.sf.ehcache.util.ClassLoaderUtil;
019:        import org.apache.commons.logging.Log;
020:        import org.apache.commons.logging.LogFactory;
021:        import org.hibernate.cache.Cache;
022:        import org.hibernate.cache.CacheException;
023:        import org.hibernate.cache.CacheProvider;
024:        import org.hibernate.cache.Timestamper;
025:
026:        import java.net.URL;
027:        import java.util.Properties;
028:
029:        /**
030:         * Cache Provider plugin for Hibernate 3.2 and ehcache-1.2. New in this provider are ehcache support for multiple
031:         * Hibernate session factories, each with its own ehcache configuration, and non Serializable keys and values.
032:         * Ehcache-1.2 also has many other features such as cluster support and listeners, which can be used seamlessly simply
033:         * by configurion in ehcache.xml.
034:         * <p/>
035:         * Use <code>hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider</code> in the Hibernate configuration
036:         * to enable this provider for Hibernate's second level cache.
037:         * <p/>
038:         * When configuring multiple ehcache CacheManagers, as you would where you have multiple Hibernate Configurations and
039:         * multiple SessionFactories, specify in each Hibernate configuration the ehcache configuration using
040:         * the property <code>net.sf.ehcache.configurationResourceName</code> An example to set an ehcach configuration
041:         * called ehcache-2.xml would be <code>net.sf.ehcache.configurationResourceName=/ehcache-2.xml</code>. If the leading
042:         * slash is not there one will be added. The configuration file will be looked for in the root of the classpath.
043:         * <p/>
044:         * Updated for ehcache-1.2. Note this provider requires ehcache-1.2.jar. Make sure ehcache-1.1.jar or earlier
045:         * is not in the classpath or it will not work.
046:         * <p/>
047:         * See http://ehcache.sf.net for documentation on ehcache
048:         * <p/>
049:         *
050:         * @author Greg Luck
051:         * @author Emmanuel Bernard
052:         * @version $Id: EhCacheProvider.java 519 2007-07-27 07:11:45Z gregluck $
053:         */
054:        public final class EhCacheProvider implements  CacheProvider {
055:
056:            /**
057:             * The Hibernate system property specifying the location of the ehcache configuration file name.
058:             * <p/
059:             * If not set, ehcache.xml will be looked for in the root of the classpath.
060:             * <p/>
061:             * If set to say ehcache-1.xml, ehcache-1.xml will be looked for in the root of the classpath.
062:             */
063:            public static final String NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME = "net.sf.ehcache.configurationResourceName";
064:
065:            private static final Log LOG = LogFactory
066:                    .getLog(EhCacheProvider.class);
067:
068:            private CacheManager manager;
069:
070:            /**
071:             * Builds a Cache.
072:             * <p/>
073:             * Even though this method provides properties, they are not used.
074:             * Properties for EHCache are specified in the ehcache.xml file.
075:             * Configuration will be read from ehcache.xml for a cache declaration
076:             * where the name attribute matches the name parameter in this builder.
077:             *
078:             * @param name       the name of the cache. Must match a cache configured in ehcache.xml
079:             * @param properties not used
080:             * @return a newly built cache will be built and initialised
081:             * @throws org.hibernate.cache.CacheException
082:             *          inter alia, if a cache of the same name already exists
083:             */
084:            public final Cache buildCache(String name, Properties properties)
085:                    throws CacheException {
086:                try {
087:                    net.sf.ehcache.Ehcache cache = manager.getEhcache(name);
088:                    if (cache == null) {
089:                        LOG
090:                                .warn("Could not find a specific ehcache configuration for cache named ["
091:                                        + name + "]; using defaults.");
092:                        manager.addCache(name);
093:                        cache = manager.getEhcache(name);
094:                        EhCacheProvider.LOG.debug("started EHCache region: "
095:                                + name);
096:                    }
097:                    return new net.sf.ehcache.hibernate.EhCache(cache);
098:                } catch (net.sf.ehcache.CacheException e) {
099:                    throw new CacheException(e);
100:                }
101:            }
102:
103:            /**
104:             * Returns the next timestamp.
105:             */
106:            public final long nextTimestamp() {
107:                return Timestamper.next();
108:            }
109:
110:            /**
111:             * Callback to perform any necessary initialization of the underlying cache implementation
112:             * during SessionFactory construction.
113:             * <p/>
114:             *
115:             * @param properties current configuration settings.
116:             */
117:            public final void start(Properties properties)
118:                    throws CacheException {
119:                if (manager != null) {
120:                    LOG
121:                            .warn("Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() "
122:                                    + " between repeated calls to buildSessionFactory. Using previously created EhCacheProvider."
123:                                    + " If this behaviour is required, consider using SingletonEhCacheProvider.");
124:                    return;
125:                }
126:                try {
127:                    String configurationResourceName = null;
128:                    if (properties != null) {
129:                        configurationResourceName = (String) properties
130:                                .get(NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
131:                    }
132:                    if (configurationResourceName == null
133:                            || configurationResourceName.length() == 0) {
134:                        manager = new CacheManager();
135:                    } else {
136:                        if (!configurationResourceName.startsWith("/")) {
137:                            configurationResourceName = "/"
138:                                    + configurationResourceName;
139:                            if (LOG.isDebugEnabled()) {
140:                                LOG
141:                                        .debug("prepending / to "
142:                                                + configurationResourceName
143:                                                + ". It should be placed in the root"
144:                                                + "of the classpath rather than in a package.");
145:                            }
146:                        }
147:                        URL url = loadResource(configurationResourceName);
148:                        manager = new CacheManager(url);
149:                    }
150:                } catch (net.sf.ehcache.CacheException e) {
151:                    if (e.getMessage().startsWith(
152:                            "Cannot parseConfiguration CacheManager. Attempt to create a new instance of "
153:                                    + "CacheManager using the diskStorePath")) {
154:                        throw new CacheException(
155:                                "Attempt to restart an already started EhCacheProvider. Use sessionFactory.close() "
156:                                        + " between repeated calls to buildSessionFactory. Consider using SingletonEhCacheProvider. Error from "
157:                                        + " ehcache was: " + e.getMessage());
158:                    } else {
159:                        throw e;
160:                    }
161:                }
162:            }
163:
164:            private URL loadResource(String configurationResourceName) {
165:                ClassLoader standardClassloader = ClassLoaderUtil
166:                        .getStandardClassLoader();
167:                URL url = null;
168:                if (standardClassloader != null) {
169:                    url = standardClassloader
170:                            .getResource(configurationResourceName);
171:                }
172:                if (url == null) {
173:                    url = this .getClass()
174:                            .getResource(configurationResourceName);
175:                }
176:                if (LOG.isDebugEnabled()) {
177:                    LOG
178:                            .debug("Creating EhCacheProvider from a specified resource: "
179:                                    + configurationResourceName
180:                                    + " Resolved to URL: " + url);
181:                }
182:                if (url == null) {
183:                    if (LOG.isWarnEnabled()) {
184:                        LOG
185:                                .warn("A configurationResourceName was set to "
186:                                        + configurationResourceName
187:                                        + " but the resource could not be loaded from the classpath."
188:                                        + "Ehcache will configure itself using defaults.");
189:                    }
190:                }
191:                return url;
192:            }
193:
194:            /**
195:             * Callback to perform any necessary cleanup of the underlying cache implementation
196:             * during SessionFactory.close().
197:             */
198:            public final void stop() {
199:                if (manager != null) {
200:                    manager.shutdown();
201:                    manager = null;
202:                }
203:            }
204:
205:            /**
206:             * Not sure what this is supposed to do.
207:             *
208:             * @return false to be safe
209:             */
210:            public final boolean isMinimalPutsEnabledByDefault() {
211:                return false;
212:            }
213:
214:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.