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.MalformedURLException;
19 import java.net.URL;
20 import java.util.Properties;
21
22 import net.sf.ehcache.CacheManager;
23 import net.sf.ehcache.config.Configuration;
24 import net.sf.ehcache.config.ConfigurationFactory;
25
26 import org.hibernate.cache.CacheException;
27 import org.hibernate.cfg.Settings;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /***
33 * A non-singleton EhCacheRegionFactory implementation.
34 *
35 * @author Chris Dennis
36 * @author Greg Luck
37 * @author Emmanuel Bernard
38 * @author Abhishek Sanoujam
39 */
40 public class EhCacheRegionFactory extends AbstractEhcacheRegionFactory {
41
42 private static final Logger LOG = LoggerFactory.getLogger(EhCacheRegionFactory.class);
43
44 /***
45 * Creates a non-singleton EhCacheRegionFactory
46 */
47 public EhCacheRegionFactory(Properties prop) {
48 super();
49 }
50
51 /***
52 * {@inheritDoc}
53 */
54 public void start(Settings settings, Properties properties) throws CacheException {
55 this.settings = settings;
56 if (manager != null) {
57 LOG.warn("Attempt to restart an already started EhCacheRegionFactory. Use sessionFactory.close() " +
58 " between repeated calls to buildSessionFactory. Using previously created EhCacheRegionFactory." +
59 " If this behaviour is required, consider using SingletonEhCacheRegionFactory.");
60 return;
61 }
62
63 try {
64 String configurationResourceName = null;
65 if (properties != null) {
66 configurationResourceName = (String) properties.get(NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
67 }
68 if (configurationResourceName == null || configurationResourceName.length() == 0) {
69 Configuration configuration = ConfigurationFactory.parseConfiguration();
70 manager = new CacheManager(configuration);
71 } else {
72 URL url;
73 try {
74 url = new URL(configurationResourceName);
75 } catch (MalformedURLException e) {
76 url = loadResource(configurationResourceName);
77 }
78 Configuration configuration = HibernateUtil.loadAndCorrectConfiguration(url);
79 manager = new CacheManager(configuration);
80 }
81 mbeanRegistrationHelper.registerMBean(manager, properties);
82 } catch (net.sf.ehcache.CacheException e) {
83 if (e.getMessage().startsWith("Cannot parseConfiguration CacheManager. Attempt to create a new instance of " +
84 "CacheManager using the diskStorePath")) {
85 throw new CacheException("Attempt to restart an already started EhCacheRegionFactory. " +
86 "Use sessionFactory.close() between repeated calls to buildSessionFactory. " +
87 "Consider using SingletonEhCacheRegionFactory. Error from ehcache was: " + e.getMessage());
88 } else {
89 throw new CacheException(e);
90 }
91 }
92 }
93
94 /***
95 * {@inheritDoc}
96 */
97 public void stop() {
98 try {
99 if (manager != null) {
100 mbeanRegistrationHelper.unregisterMBean();
101 manager.shutdown();
102 manager = null;
103 }
104 } catch (net.sf.ehcache.CacheException e) {
105 throw new CacheException(e);
106 }
107 }
108 }