~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-distribution-3.3.2.GA/project/cache-jbosscache/src/main/java/org/hibernate/cache/TreeCacheProvider.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Hibernate, Relational Persistence for Idiomatic Java
3
 
 *
4
 
 * Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as
5
 
 * indicated by the @author tags or express copyright attribution
6
 
 * statements applied by the authors.  All third-party contributions are
7
 
 * distributed under license by Red Hat Middleware LLC.
8
 
 *
9
 
 * This copyrighted material is made available to anyone wishing to use, modify,
10
 
 * copy, or redistribute it subject to the terms and conditions of the GNU
11
 
 * Lesser General Public License, as published by the Free Software Foundation.
12
 
 *
13
 
 * This program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
16
 
 * for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU Lesser General Public License
19
 
 * along with this distribution; if not, write to:
20
 
 * Free Software Foundation, Inc.
21
 
 * 51 Franklin Street, Fifth Floor
22
 
 * Boston, MA  02110-1301  USA
23
 
 */
24
 
package org.hibernate.cache;
25
 
 
26
 
import java.util.Properties;
27
 
import javax.transaction.TransactionManager;
28
 
 
29
 
import org.slf4j.Logger;
30
 
import org.slf4j.LoggerFactory;
31
 
import org.hibernate.transaction.TransactionManagerLookup;
32
 
import org.hibernate.transaction.TransactionManagerLookupFactory;
33
 
import org.hibernate.cfg.Environment;
34
 
import org.jboss.cache.PropertyConfigurator;
35
 
 
36
 
/**
37
 
 * Support for a standalone JBossCache (TreeCache) instance.  The JBossCache is configured
38
 
 * via a local config resource.
39
 
 *
40
 
 * @author Gavin King
41
 
 */
42
 
public class TreeCacheProvider implements CacheProvider {
43
 
 
44
 
        /**
45
 
         * @deprecated use {@link org.hibernate.cfg.Environment#CACHE_PROVIDER_CONFIG}
46
 
         */
47
 
        public static final String CONFIG_RESOURCE = "hibernate.cache.tree_cache.config";
48
 
        public static final String DEFAULT_CONFIG = "treecache-optimistic.xml";
49
 
 
50
 
        private static final Logger log = LoggerFactory.getLogger( TreeCacheProvider.class );
51
 
 
52
 
        private org.jboss.cache.TreeCache cache;
53
 
        private TransactionManager transactionManager;
54
 
 
55
 
        /**
56
 
         * Construct and configure the Cache representation of a named cache region.
57
 
         *
58
 
         * @param regionName the name of the cache region
59
 
         * @param properties configuration settings
60
 
         * @return The Cache representation of the named cache region.
61
 
         * @throws CacheException Indicates an error building the cache region.
62
 
         */
63
 
        public Cache buildCache(String regionName, Properties properties) throws CacheException {
64
 
                return new TreeCache(cache, regionName, transactionManager);
65
 
        }
66
 
 
67
 
        public long nextTimestamp() {
68
 
                return System.currentTimeMillis() / 100;
69
 
        }
70
 
 
71
 
        /**
72
 
         * Prepare the underlying JBossCache TreeCache instance.
73
 
         *
74
 
         * @param properties All current config settings.
75
 
         *
76
 
         * @throws CacheException Indicates a problem preparing cache for use.
77
 
         */
78
 
        public void start(Properties properties) {
79
 
                String resource = properties.getProperty( Environment.CACHE_PROVIDER_CONFIG );
80
 
 
81
 
                if ( resource == null ) {
82
 
                        resource = properties.getProperty( CONFIG_RESOURCE );
83
 
                }
84
 
                if ( resource == null ) {
85
 
                        resource = DEFAULT_CONFIG;
86
 
                }
87
 
                log.debug( "Configuring TreeCache from resource [" + resource + "]" );
88
 
                try {
89
 
                        cache = new org.jboss.cache.TreeCache();
90
 
                        PropertyConfigurator config = new PropertyConfigurator();
91
 
                        config.configure( cache, resource );
92
 
                        TransactionManagerLookup transactionManagerLookup = TransactionManagerLookupFactory.getTransactionManagerLookup(properties);
93
 
                        if (transactionManagerLookup!=null) {
94
 
                                cache.setTransactionManagerLookup( new TransactionManagerLookupAdaptor(transactionManagerLookup, properties) );
95
 
                                transactionManager = transactionManagerLookup.getTransactionManager(properties);
96
 
                        }
97
 
                        cache.start();
98
 
                }
99
 
                catch (Exception e) {
100
 
                        throw new CacheException(e);
101
 
                }
102
 
        }
103
 
 
104
 
        public void stop() {
105
 
                if (cache!=null) {
106
 
                        cache.stop();
107
 
                        cache.destroy();
108
 
                        cache=null;
109
 
                }
110
 
        }
111
 
        
112
 
        public boolean isMinimalPutsEnabledByDefault() {
113
 
                return true;
114
 
        }
115
 
 
116
 
        static final class TransactionManagerLookupAdaptor implements org.jboss.cache.TransactionManagerLookup {
117
 
                private final TransactionManagerLookup tml;
118
 
                private final Properties props;
119
 
                TransactionManagerLookupAdaptor(TransactionManagerLookup tml, Properties props) {
120
 
                        this.tml=tml;
121
 
                        this.props=props;
122
 
                }
123
 
                public TransactionManager getTransactionManager() throws Exception {
124
 
                        return tml.getTransactionManager(props);
125
 
                }
126
 
        }
127
 
 
128
 
        public org.jboss.cache.TreeCache getUnderlyingCache() {
129
 
                return cache;
130
 
        }
131
 
}