1 /**
2  *  Copyright 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.transaction.manager.selector;
17
18 import net.sf.ehcache.transaction.xa.EhcacheXAResource;
19 import net.sf.ehcache.util.ClassLoaderUtil;
20
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.lang.reflect.Method;
25
26 import javax.transaction.xa.XAResource;
27
28 /**
29  * A Selector for the Bitronix Transaction Manager.
30  *
31  * @author Ludovic Orban
32  */

33 public class BitronixSelector extends FactorySelector {
34     private static final Logger LOG = LoggerFactory.getLogger(BitronixSelector.class);
35
36     /**
37      * Constructor
38      */

39     public BitronixSelector() {
40         super("Bitronix""bitronix.tm.TransactionManagerServices""getTransactionManager");
41     }
42
43     /**
44      * {@inheritDoc}
45      */

46     @Override
47     public void registerResource(EhcacheXAResource ehcacheXAResource, boolean forRecovery) {
48         String uniqueName = ehcacheXAResource.getCacheName();
49         try {
50
51             Class producerClass = ClassLoaderUtil.loadClass("bitronix.tm.resource.ehcache.EhCacheXAResourceProducer");
52
53             Class[] signature = new Class[] {String.class, XAResource.class};
54             Object[] args = new Object[] {uniqueName, ehcacheXAResource};
55             Method method = producerClass.getMethod("registerXAResource", signature);
56             method.invoke(null, args);
57         } catch (Exception e) {
58             LOG.error("unable to register resource of cache " + uniqueName + " with BTM", e);
59         }
60     }
61
62     /**
63      * {@inheritDoc}
64      */

65     @Override
66     public void unregisterResource(EhcacheXAResource ehcacheXAResource, boolean forRecovery) {
67         String uniqueName = ehcacheXAResource.getCacheName();
68         try {
69             Class producerClass = ClassLoaderUtil.loadClass("bitronix.tm.resource.ehcache.EhCacheXAResourceProducer");
70
71             Class[] signature = new Class[] {String.class, XAResource.class};
72             Object[] args = new Object[] {uniqueName, ehcacheXAResource};
73             Method method = producerClass.getMethod("unregisterXAResource", signature);
74             method.invoke(null, args);
75         } catch (Exception e) {
76             LOG.error("unable to unregister resource of cache " + uniqueName + " with BTM", e);
77         }
78     }
79 }
80