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 import java.util.concurrent.atomic.AtomicInteger;
22
23 import net.sf.ehcache.CacheManager;
24 import net.sf.ehcache.config.Configuration;
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 singleton EhCacheRegionFactory implementation.
34 *
35 * @author Chris Dennis
36 * @author Greg Luck
37 * @author Emmanuel Bernard
38 */
39 public class SingletonEhCacheRegionFactory extends AbstractEhcacheRegionFactory {
40
41 private static final Logger LOG = LoggerFactory.getLogger(SingletonEhCacheRegionFactory.class);
42
43 private static final AtomicInteger REFERENCE_COUNT = new AtomicInteger();
44
45 /***
46 * Returns a representation of the singleton EhCacheRegionFactory
47 */
48 public SingletonEhCacheRegionFactory(Properties prop) {
49 super();
50 }
51
52 /***
53 * {@inheritDoc}
54 */
55 public void start(Settings settings, Properties properties) throws CacheException {
56 try {
57 String configurationResourceName = null;
58 if (properties != null) {
59 configurationResourceName = (String) properties.get(NET_SF_EHCACHE_CONFIGURATION_RESOURCE_NAME);
60 }
61 if (configurationResourceName == null || configurationResourceName.length() == 0) {
62 manager = CacheManager.create();
63 REFERENCE_COUNT.incrementAndGet();
64 } else {
65 URL url;
66 try {
67 url = new URL(configurationResourceName);
68 } catch (MalformedURLException e) {
69 if (!configurationResourceName.startsWith("/")) {
70 configurationResourceName = "/" + configurationResourceName;
71 LOG.debug("prepending / to {}. It should be placed in the root of the classpath rather than in a package.",
72 configurationResourceName);
73 }
74 url = loadResource(configurationResourceName);
75 }
76 Configuration configuration = HibernateUtil.loadAndCorrectConfiguration(url);
77 manager = CacheManager.create(configuration);
78 REFERENCE_COUNT.incrementAndGet();
79 }
80 mbeanRegistrationHelper.registerMBean(manager, properties);
81 } catch (net.sf.ehcache.CacheException e) {
82 throw new CacheException(e);
83 }
84 }
85
86 /***
87 * {@inheritDoc}
88 */
89 public void stop() {
90 try {
91 if (manager != null) {
92 if (REFERENCE_COUNT.decrementAndGet() == 0) {
93 manager.shutdown();
94 }
95 manager = null;
96 }
97 } catch (net.sf.ehcache.CacheException e) {
98 throw new CacheException(e);
99 }
100 }
101 }