View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.ehcache.hibernate;
17  
18  import java.net.URL;
19  
20  import net.sf.ehcache.Ehcache;
21  import net.sf.ehcache.config.CacheConfiguration;
22  import net.sf.ehcache.config.Configuration;
23  import net.sf.ehcache.config.ConfigurationFactory;
24  import net.sf.ehcache.config.NonstopConfiguration;
25  import net.sf.ehcache.config.TerracottaConfiguration;
26  import net.sf.ehcache.config.TerracottaConfiguration.ValueMode;
27  import net.sf.ehcache.config.TimeoutBehaviorConfiguration.TimeoutBehaviorType;
28  
29  import org.hibernate.cache.CacheException;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /***
34   *
35   * @author Chris Dennis
36   * @author Abhishek Sanoujam
37   */
38  public final class HibernateUtil {
39  
40      private static final Logger LOG = LoggerFactory.getLogger(HibernateUtil.class);
41  
42      private HibernateUtil() { }
43  
44      /***
45       * Create a cache manager configuration from the supplied url, correcting it for Hibernate compatibility.
46       * <p>
47       * Currently correcting for Hibernate compatibility means simply switching any identity based value modes to serialization.
48       */
49      static Configuration loadAndCorrectConfiguration(URL url) {
50          Configuration config = ConfigurationFactory.parseConfiguration(url);
51          if (config.getDefaultCacheConfiguration().isTerracottaClustered()) {
52              if (ValueMode.IDENTITY.equals(config.getDefaultCacheConfiguration().getTerracottaConfiguration().getValueMode())) {
53                  LOG.warn("The default cache value mode for this Ehcache configuration is \"identity\". This is incompatible with clustered "
54                          + "Hibernate caching - the value mode has therefore been switched to \"serialization\"");
55                  config.getDefaultCacheConfiguration().getTerracottaConfiguration().setValueMode(ValueMode.SERIALIZATION.name());
56              }
57              setupHibernateTimeoutBehavior(config.getDefaultCacheConfiguration().getTerracottaConfiguration().getNonstopConfiguration());
58          }
59  
60          for (CacheConfiguration cacheConfig : config.getCacheConfigurations().values()) {
61              if (cacheConfig.isTerracottaClustered()) {
62                  if (ValueMode.IDENTITY.equals(cacheConfig.getTerracottaConfiguration().getValueMode())) {
63                  LOG.warn("The value mode for the {0} cache is \"identity\". This is incompatible with clustered Hibernate caching - "
64                          + "the value mode has therefore been switched to \"serialization\"", cacheConfig.getName());
65                      cacheConfig.getTerracottaConfiguration().setValueMode(ValueMode.SERIALIZATION.name());
66                  }
67                  setupHibernateTimeoutBehavior(cacheConfig.getTerracottaConfiguration().getNonstopConfiguration());
68              }
69          }
70          return config;
71      }
72  
73      private static void setupHibernateTimeoutBehavior(NonstopConfiguration nonstopConfig) {
74          nonstopConfig.getTimeoutBehavior().setType(TimeoutBehaviorType.EXCEPTION.getTypeName());
75      }
76  
77      /***
78       * Validates that the supplied Ehcache instance is valid for use as a Hibernate cache.
79       */
80      static void validateEhcache(Ehcache cache) throws CacheException {
81          CacheConfiguration cacheConfig = cache.getCacheConfiguration();
82  
83          if (cacheConfig.isTerracottaClustered()) {
84              TerracottaConfiguration tcConfig = cacheConfig.getTerracottaConfiguration();
85              switch (tcConfig.getValueMode()) {
86                  case IDENTITY:
87                      throw new CacheException("The clustered Hibernate cache " + cache.getName() + " is using IDENTITY value mode.\n"
88                             + "Identity value mode cannot be used with Hibernate cache regions.");
89                  case SERIALIZATION:
90                  default:
91                      // this is the recommended valueMode
92                      break;
93              }
94          }
95      }
96  
97  
98  }