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 Weblogic Server's JTA transaction manager
30  *
31  * @author Ludovic Orban
32  */

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

39     public WeblogicSelector() {
40         super("Weblogic""weblogic.transaction.TxHelper""getTransactionManager");
41     }
42
43     /**
44      * {@inheritDoc}
45      */

46     @Override
47     public void registerResource(EhcacheXAResource ehcacheXAResource, boolean forRecovery) {
48         if (!forRecovery) {
49             return;
50         }
51
52         String uniqueName = ehcacheXAResource.getCacheName();
53         try {
54             Class tmImplClass = ClassLoaderUtil.loadClass("weblogic.transaction.TransactionManager");
55
56             Class[] signature = new Class[] {String.class, XAResource.class};
57             Object[] args = new Object[] {uniqueName, ehcacheXAResource};
58             Method method = tmImplClass.getMethod("registerResource", signature);
59             method.invoke(getTransactionManager(), args);
60         } catch (Exception e) {
61             LOG.error("unable to register resource of cache " + uniqueName + " with Weblogic", e);
62         }
63     }
64
65     /**
66      * {@inheritDoc}
67      */

68     @Override
69     public void unregisterResource(final EhcacheXAResource ehcacheXAResource, final boolean forRecovery) {
70         if (!forRecovery) {
71             return;
72         }
73
74         String uniqueName = ehcacheXAResource.getCacheName();
75         try {
76             Class tmImplClass = ClassLoaderUtil.loadClass("weblogic.transaction.TransactionManager");
77
78             Class[] signature = new Class[] {String.class, Boolean.TYPE};
79             Object[] args = new Object[] {uniqueName, Boolean.TRUE};
80             Method method = tmImplClass.getMethod("unregisterResource", signature);
81             method.invoke(getTransactionManager(), args);
82         } catch (Exception e) {
83             LOG.error("unable to unregister resource of cache " + uniqueName + " with Weblogic", e);
84         }
85     }
86
87 }
88