├── .github └── FUNDING.yml ├── .gitignore ├── LICENSE ├── README-zh.md ├── README.md ├── bytejta-core ├── pom.xml └── src │ └── main │ └── java │ └── org │ └── bytesoft │ ├── bytejta │ ├── TransactionBeanFactoryImpl.java │ ├── TransactionCoordinator.java │ ├── TransactionImpl.java │ ├── TransactionManagerImpl.java │ ├── TransactionRecoveryImpl.java │ ├── TransactionRepositoryImpl.java │ ├── TransactionStrategy.java │ ├── UserTransactionImpl.java │ ├── VacantTransactionLock.java │ ├── logging │ │ ├── ArchiveDeserializerImpl.java │ │ ├── SampleTransactionLogger.java │ │ ├── deserializer │ │ │ ├── TransactionArchiveDeserializer.java │ │ │ └── XAResourceArchiveDeserializer.java │ │ └── store │ │ │ ├── VirtualLoggingFile.java │ │ │ └── VirtualLoggingSystemImpl.java │ ├── resource │ │ ├── XATerminatorImpl.java │ │ └── XATerminatorOptd.java │ ├── strategy │ │ ├── CommonTransactionStrategy.java │ │ ├── LastResourceOptimizeStrategy.java │ │ ├── SimpleTransactionStrategy.java │ │ └── VacantTransactionStrategy.java │ ├── supports │ │ ├── jdbc │ │ │ ├── DataSourceHolder.java │ │ │ ├── LocalXACompatible.java │ │ │ ├── LocalXAConnection.java │ │ │ ├── LocalXAResource.java │ │ │ ├── LogicalConnection.java │ │ │ └── RecoveredResource.java │ │ └── resource │ │ │ ├── CommonResourceDescriptor.java │ │ │ ├── LocalXAResourceDescriptor.java │ │ │ ├── RemoteResourceDescriptor.java │ │ │ └── UnidentifiedResourceDescriptor.java │ ├── work │ │ └── TransactionWork.java │ └── xa │ │ └── XidFactoryImpl.java │ ├── common │ └── utils │ │ ├── ByteUtils.java │ │ ├── CommonUtils.java │ │ └── SerializeUtils.java │ └── transaction │ ├── CommitRequiredException.java │ ├── RemoteSystemException.java │ ├── RollbackRequiredException.java │ ├── Transaction.java │ ├── TransactionBeanFactory.java │ ├── TransactionContext.java │ ├── TransactionException.java │ ├── TransactionLock.java │ ├── TransactionManager.java │ ├── TransactionParticipant.java │ ├── TransactionRecovery.java │ ├── TransactionRepository.java │ ├── adapter │ └── ResourceAdapterImpl.java │ ├── archive │ ├── TransactionArchive.java │ └── XAResourceArchive.java │ ├── aware │ ├── TransactionBeanFactoryAware.java │ ├── TransactionDebuggable.java │ └── TransactionEndpointAware.java │ ├── cmd │ └── CommandDispatcher.java │ ├── internal │ ├── SynchronizationImpl.java │ ├── SynchronizationList.java │ ├── TransactionListenerList.java │ └── TransactionResourceListenerList.java │ ├── logging │ ├── ArchiveDeserializer.java │ ├── LoggingFlushable.java │ ├── TransactionLogger.java │ └── store │ │ ├── VirtualLoggingKey.java │ │ ├── VirtualLoggingListener.java │ │ ├── VirtualLoggingRecord.java │ │ ├── VirtualLoggingSystem.java │ │ └── VirtualLoggingTrigger.java │ ├── recovery │ ├── TransactionRecoveryCallback.java │ └── TransactionRecoveryListener.java │ ├── remote │ ├── RemoteAddr.java │ ├── RemoteCoordinator.java │ ├── RemoteNode.java │ └── RemoteSvc.java │ ├── resource │ └── XATerminator.java │ ├── supports │ ├── TransactionExtra.java │ ├── TransactionListener.java │ ├── TransactionListenerAdapter.java │ ├── TransactionResourceListener.java │ ├── TransactionResourceListenerAdapter.java │ ├── TransactionStatistic.java │ ├── TransactionTimer.java │ ├── resource │ │ └── XAResourceDescriptor.java │ ├── rpc │ │ ├── TransactionInterceptor.java │ │ ├── TransactionRequest.java │ │ └── TransactionResponse.java │ └── serialize │ │ └── XAResourceDeserializer.java │ ├── work │ ├── SimpleWork.java │ ├── SimpleWorkListener.java │ └── SimpleWorkManager.java │ └── xa │ ├── TransactionXid.java │ └── XidFactory.java ├── bytejta-supports-dubbo ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── bytesoft │ │ └── bytejta │ │ └── supports │ │ └── dubbo │ │ ├── DubboRemoteCoordinator.java │ │ ├── InvocationContextRegistry.java │ │ ├── TransactionBeanRegistry.java │ │ ├── config │ │ └── DubboSupportConfiguration.java │ │ ├── ext │ │ └── ILoadBalancer.java │ │ ├── internal │ │ ├── TransactionBeanConfigValidator.java │ │ ├── TransactionEndpointAutoInjector.java │ │ └── TransactionParticipantRegistrant.java │ │ ├── serialize │ │ └── XAResourceDeserializerImpl.java │ │ └── spi │ │ ├── TransactionLoadBalance.java │ │ ├── TransactionLoadBalancer.java │ │ └── TransactionServiceFilter.java │ └── resources │ ├── META-INF │ └── dubbo │ │ ├── com.alibaba.dubbo.rpc.Filter │ │ ├── com.alibaba.dubbo.rpc.cluster.LoadBalance │ │ └── org.bytesoft.bytejta.supports.dubbo.ext.ILoadBalancer │ └── bytejta-supports-dubbo.xml ├── bytejta-supports-springcloud ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── bytesoft │ │ └── bytejta │ │ └── supports │ │ └── springcloud │ │ ├── SpringCloudBeanRegistry.java │ │ ├── SpringCloudCoordinator.java │ │ ├── SpringCloudEndpointPostProcessor.java │ │ ├── config │ │ └── SpringCloudConfiguration.java │ │ ├── controller │ │ └── TransactionCoordinatorController.java │ │ ├── dbcp │ │ └── CommonDBCPXADataSourceWrapper.java │ │ ├── feign │ │ ├── TransactionClientRegistry.java │ │ ├── TransactionFeignBeanPostProcessor.java │ │ ├── TransactionFeignContract.java │ │ ├── TransactionFeignDecoder.java │ │ ├── TransactionFeignErrorDecoder.java │ │ ├── TransactionFeignHandler.java │ │ └── TransactionFeignInterceptor.java │ │ ├── hystrix │ │ ├── TransactionHystrixBeanPostProcessor.java │ │ ├── TransactionHystrixFallbackFactoryHandler.java │ │ ├── TransactionHystrixFallbackHandler.java │ │ ├── TransactionHystrixFeignHandler.java │ │ ├── TransactionHystrixInvocation.java │ │ ├── TransactionHystrixInvocationHandler.java │ │ └── TransactionHystrixMethodHandler.java │ │ ├── loadbalancer │ │ ├── TransactionLoadBalancerInterceptor.java │ │ └── TransactionLoadBalancerRuleImpl.java │ │ ├── property │ │ ├── TransactionPropertySource.java │ │ └── TransactionPropertySourceFactory.java │ │ ├── rule │ │ ├── TransactionRule.java │ │ └── TransactionRuleImpl.java │ │ ├── serialize │ │ └── XAResourceDeserializerImpl.java │ │ └── web │ │ ├── TransactionHandlerInterceptor.java │ │ └── TransactionRequestInterceptor.java │ └── resources │ └── bytejta-supports-springcloud.xml ├── bytejta-supports ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── bytesoft │ │ └── bytejta │ │ └── supports │ │ ├── boot │ │ └── jdbc │ │ │ ├── DataSourceCciBuilder.java │ │ │ └── DataSourceSpiBuilder.java │ │ ├── internal │ │ ├── RemoteCoordinatorRegistry.java │ │ └── TransactionCommandDispatcher.java │ │ ├── jdbc │ │ └── LocalXADataSource.java │ │ ├── jpa │ │ └── hibernate │ │ │ └── HibernateJtaPlatform.java │ │ ├── resource │ │ ├── ManagedConnectionFactoryHandler.java │ │ ├── ManagedConnectionHandler.java │ │ ├── ManagedXASessionHandler.java │ │ ├── jdbc │ │ │ ├── CallableStatementImpl.java │ │ │ ├── ConnectionImpl.java │ │ │ ├── DatabaseMetaDataImpl.java │ │ │ ├── PreparedStatementImpl.java │ │ │ ├── StatementImpl.java │ │ │ ├── XAConnectionImpl.java │ │ │ └── XADataSourceImpl.java │ │ └── properties │ │ │ ├── ConnectorResourcePropertySource.java │ │ │ └── ConnectorResourcePropertySourceFactory.java │ │ ├── rpc │ │ ├── TransactionInterceptorImpl.java │ │ ├── TransactionRequestImpl.java │ │ └── TransactionResponseImpl.java │ │ ├── serialize │ │ └── XAResourceDeserializerImpl.java │ │ └── spring │ │ ├── ManagedConnectionFactoryPostProcessor.java │ │ ├── TransactionBeanFactoryAutoInjector.java │ │ └── TransactionDebuggablePostProcessor.java │ └── resources │ ├── bytejta-supports-core.xml │ ├── bytejta-supports-standalone.xml │ └── bytejta-supports-task.xml └── pom.xml /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ['https://raw.githubusercontent.com/wiki/liuyangming/ByteTCC/resources/donation/liuyangming%40alipay.png', 'https://raw.githubusercontent.com/wiki/liuyangming/ByteTCC/resources/donation/liuyangming%40weixin.png'] # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | .settings 14 | /bytejta-core/.classpath 15 | /bytejta-core/.project 16 | /bytejta-core/.gitignore 17 | /bytejta-supports/.classpath 18 | /bytejta-supports/.gitignore 19 | /bytejta-supports/.project 20 | target 21 | /bytejta-supports-dubbo/.classpath 22 | /bytejta-supports-dubbo/.project 23 | /bytejta-supports-springcloud/.classpath 24 | /bytejta-supports-springcloud/.project 25 | /bytejta-logger/.classpath 26 | /bytejta-logger/.project 27 | /bytejta-transaction-logger/.classpath 28 | /bytejta-transaction-logger/.project 29 | -------------------------------------------------------------------------------- /README-zh.md: -------------------------------------------------------------------------------- 1 | ByteJTA是一个基于XA/2PC机制的分布式事务管理器。实现了JTA接口,可以很好的与EJB、Spring等容器(本文档下文说明中将以Spring容器为例)进行集成。 2 | 3 | ## 一、文档 & 样例 4 | * 使用文档: https://github.com/liuyangming/ByteJTA/wiki 5 | * 使用样例: https://github.com/liuyangming/ByteJTA-sample 6 | 7 | ## 二、ByteJTA特性 8 | * 1、支持Spring容器的声明式事务管理; 9 | * 2、支持多数据源、跨应用、跨服务器等分布式事务场景; 10 | * 3、支持spring cloud; 11 | * 4、支持dubbo服务框架; 12 | 13 | ## 三、建议及改进 14 | 若您有任何建议,可以通过1)加入qq群537445956/606453172向群主提出,或2)发送邮件至bytefox#126.com向我反馈。本人承诺,任何建议都将会被认真考虑,优秀的建议将会被采用,但不保证一定会在当前版本中实现。 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |  2 | **ByteJTA** is an implementation of Distributed Transaction Manager, based on the XA/2PC mechanism. 3 | 4 | **ByteJTA** is comptible with JTA and could be seamlessly integrated with Spring and other Java containers. 5 | 6 | 7 | ## 1. Quick Start 8 | 9 | #### 1.1 Add maven depenency 10 | ###### 1.1.1. Spring Cloud 11 | ```xml 12 | 13 | org.bytesoft 14 | bytejta-supports-springcloud 15 | 0.5.0-BETA9 16 | 17 | ``` 18 | ###### 1.1.2. dubbo 19 | ```xml 20 | 21 | org.bytesoft 22 | bytejta-supports-dubbo 23 | 0.5.0-BETA9 24 | 25 | ``` 26 | 27 | 28 | 29 | ## 2. Documentation & Samples 30 | * [Document](https://github.com/liuyangming/ByteJTA/wiki) 31 | * [Sample](https://github.com/liuyangming/ByteJTA-sample) 32 | 33 | 34 | 35 | ## 3. Features 36 | * support declarative transaction management 37 | * support distributed transaction scenarios. e.g. multi-datasource, cross-applications and cross-servers transaction 38 | * support Dubbo framework 39 | * support Spring Cloud 40 | 41 | 42 | 43 | ## 4. Contact Me 44 | If you have any questions or comments regarding this project, please feel free to contact me at: 45 | 46 | 1. send mail to _[bytefox#126.com](bytefox@126.com)_ 47 | ~OR~ 48 | 2. add Tecent QQ group 537445956/606453172 49 | 50 | We will review all the suggestions and implement good ones in future release. 51 | -------------------------------------------------------------------------------- /bytejta-core/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | org.bytesoft 7 | bytejta-parent 8 | 0.5.0-BETA9 9 | 10 | bytejta-core 11 | 12 | jar 13 | 14 | bytejta-core 15 | The bytejta-core project is the core module of ByteJTA. 16 | http://www.bytesoft.org 17 | 18 | 19 | UTF-8 20 | 21 | 22 | 23 | 24 | javax.transaction 25 | javax.transaction-api 26 | 27 | 28 | javax.jms 29 | javax.jms-api 30 | 31 | 32 | javax.resource 33 | javax.resource-api 34 | 35 | 36 | org.slf4j 37 | slf4j-api 38 | 39 | 40 | 41 | org.apache.commons 42 | commons-lang3 43 | 44 | 45 | commons-io 46 | commons-io 47 | 48 | 49 | 50 | com.caucho 51 | hessian 52 | 53 | 54 | com.esotericsoftware 55 | kryo 56 | 57 | 58 | 59 | javax.annotation 60 | javax.annotation-api 61 | 62 | 63 | javax.inject 64 | javax.inject 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/bytejta/TransactionRepositoryImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | import java.util.Map; 21 | import java.util.concurrent.ConcurrentHashMap; 22 | 23 | import org.bytesoft.transaction.Transaction; 24 | import org.bytesoft.transaction.TransactionRepository; 25 | import org.bytesoft.transaction.xa.TransactionXid; 26 | 27 | public class TransactionRepositoryImpl implements TransactionRepository { 28 | private final Map xidToTxMap = new ConcurrentHashMap(); 29 | private final Map xidToErrTxMap = new ConcurrentHashMap(); 30 | 31 | public void putTransaction(TransactionXid globalXid, Transaction transaction) { 32 | this.xidToTxMap.put(globalXid, transaction); 33 | } 34 | 35 | public Transaction getTransaction(TransactionXid globalXid) { 36 | return this.xidToTxMap.get(globalXid); 37 | } 38 | 39 | public Transaction removeTransaction(TransactionXid globalXid) { 40 | return this.xidToTxMap.remove(globalXid); 41 | } 42 | 43 | public void putErrorTransaction(TransactionXid globalXid, Transaction transaction) { 44 | this.xidToErrTxMap.put(globalXid, transaction); 45 | } 46 | 47 | public Transaction getErrorTransaction(TransactionXid globalXid) { 48 | return this.xidToErrTxMap.get(globalXid); 49 | } 50 | 51 | public Transaction removeErrorTransaction(TransactionXid globalXid) { 52 | return this.xidToErrTxMap.remove(globalXid); 53 | } 54 | 55 | public List getErrorTransactionList() { 56 | return new ArrayList(this.xidToErrTxMap.values()); 57 | } 58 | 59 | public List getActiveTransactionList() { 60 | return new ArrayList(this.xidToTxMap.values()); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/bytejta/TransactionStrategy.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta; 17 | 18 | import javax.transaction.HeuristicCommitException; 19 | import javax.transaction.HeuristicMixedException; 20 | import javax.transaction.HeuristicRollbackException; 21 | import javax.transaction.SystemException; 22 | import javax.transaction.xa.Xid; 23 | 24 | import org.bytesoft.transaction.CommitRequiredException; 25 | import org.bytesoft.transaction.RollbackRequiredException; 26 | 27 | public interface TransactionStrategy /* extends TransactionBeanFactoryAware */ { 28 | 29 | public int TRANSACTION_STRATEGY_VACANT = 0; 30 | public int TRANSACTION_STRATEGY_SIMPLE = 1; 31 | public int TRANSACTION_STRATEGY_COMMON = 2; 32 | public int TRANSACTION_STRATEGY_LRO = 3; 33 | 34 | public int prepare(Xid xid) throws RollbackRequiredException, CommitRequiredException; 35 | 36 | public void commit(Xid xid, boolean onePhaseCommit) 37 | throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, SystemException; 38 | 39 | public void rollback(Xid xid) 40 | throws HeuristicMixedException, HeuristicCommitException, IllegalStateException, SystemException; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/bytejta/UserTransactionImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta; 17 | 18 | import java.io.Serializable; 19 | 20 | import javax.naming.NamingException; 21 | import javax.naming.Reference; 22 | import javax.naming.Referenceable; 23 | import javax.transaction.HeuristicMixedException; 24 | import javax.transaction.HeuristicRollbackException; 25 | import javax.transaction.NotSupportedException; 26 | import javax.transaction.RollbackException; 27 | import javax.transaction.SystemException; 28 | import javax.transaction.TransactionManager; 29 | import javax.transaction.UserTransaction; 30 | 31 | public class UserTransactionImpl implements UserTransaction, Referenceable, Serializable { 32 | private static final long serialVersionUID = 1L; 33 | 34 | @javax.inject.Inject 35 | private transient TransactionManager transactionManager; 36 | 37 | public void begin() throws NotSupportedException, SystemException { 38 | this.transactionManager.begin(); 39 | } 40 | 41 | public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, 42 | RollbackException, SecurityException, SystemException { 43 | this.transactionManager.commit(); 44 | } 45 | 46 | public int getStatus() throws SystemException { 47 | return this.transactionManager.getStatus(); 48 | } 49 | 50 | public void rollback() throws IllegalStateException, SecurityException, SystemException { 51 | this.transactionManager.rollback(); 52 | } 53 | 54 | public void setRollbackOnly() throws IllegalStateException, SystemException { 55 | this.transactionManager.setRollbackOnly(); 56 | } 57 | 58 | public void setTransactionTimeout(int timeout) throws SystemException { 59 | this.transactionManager.setTransactionTimeout(timeout); 60 | } 61 | 62 | public Reference getReference() throws NamingException { 63 | throw new NamingException("Not supported yet!"); 64 | } 65 | 66 | public TransactionManager getTransactionManager() { 67 | return transactionManager; 68 | } 69 | 70 | public void setTransactionManager(TransactionManager transactionManager) { 71 | this.transactionManager = transactionManager; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/bytejta/VacantTransactionLock.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta; 17 | 18 | import org.bytesoft.transaction.TransactionLock; 19 | import org.bytesoft.transaction.xa.TransactionXid; 20 | 21 | public class VacantTransactionLock implements TransactionLock { 22 | 23 | public boolean lockTransaction(TransactionXid transactionXid, String identifier) { 24 | return true; 25 | } 26 | 27 | public void unlockTransaction(TransactionXid transactionXid, String identifier) { 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/bytejta/logging/ArchiveDeserializerImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.logging; 17 | 18 | import org.bytesoft.transaction.archive.TransactionArchive; 19 | import org.bytesoft.transaction.archive.XAResourceArchive; 20 | import org.bytesoft.transaction.logging.ArchiveDeserializer; 21 | import org.bytesoft.transaction.xa.TransactionXid; 22 | 23 | public class ArchiveDeserializerImpl implements ArchiveDeserializer { 24 | static final byte TYPE_TRANSACTION = 0x0; 25 | static final byte TYPE_XA_RESOURCE = 0x1; 26 | 27 | private ArchiveDeserializer xaResourceArchiveDeserializer; 28 | private ArchiveDeserializer transactionArchiveDeserializer; 29 | 30 | public byte[] serialize(TransactionXid xid, Object archive) { 31 | 32 | if (TransactionArchive.class.isInstance(archive)) { 33 | byte[] array = this.transactionArchiveDeserializer.serialize(xid, archive); 34 | byte[] byteArray = new byte[array.length + 1]; 35 | byteArray[0] = TYPE_TRANSACTION; 36 | System.arraycopy(array, 0, byteArray, 1, array.length); 37 | return byteArray; 38 | } else if (XAResourceArchive.class.isInstance(archive)) { 39 | byte[] array = this.xaResourceArchiveDeserializer.serialize(xid, archive); 40 | byte[] byteArray = new byte[array.length + 1]; 41 | byteArray[0] = TYPE_XA_RESOURCE; 42 | System.arraycopy(array, 0, byteArray, 1, array.length); 43 | return byteArray; 44 | } else { 45 | throw new IllegalArgumentException(); 46 | } 47 | 48 | } 49 | 50 | public Object deserialize(TransactionXid xid, byte[] array) { 51 | if (array == null || array.length <= 1) { 52 | throw new IllegalArgumentException(); 53 | } 54 | 55 | byte type = array[0]; 56 | if (type == TYPE_TRANSACTION) { 57 | byte[] byteArray = new byte[array.length - 1]; 58 | System.arraycopy(array, 1, byteArray, 0, byteArray.length); 59 | return this.transactionArchiveDeserializer.deserialize(xid, byteArray); 60 | } else if (type == TYPE_XA_RESOURCE) { 61 | byte[] byteArray = new byte[array.length - 1]; 62 | System.arraycopy(array, 1, byteArray, 0, byteArray.length); 63 | return this.xaResourceArchiveDeserializer.deserialize(xid, byteArray); 64 | } else { 65 | throw new IllegalArgumentException(); 66 | } 67 | 68 | } 69 | 70 | public ArchiveDeserializer getXaResourceArchiveDeserializer() { 71 | return xaResourceArchiveDeserializer; 72 | } 73 | 74 | public void setXaResourceArchiveDeserializer(ArchiveDeserializer xaResourceArchiveDeserializer) { 75 | this.xaResourceArchiveDeserializer = xaResourceArchiveDeserializer; 76 | } 77 | 78 | public ArchiveDeserializer getTransactionArchiveDeserializer() { 79 | return transactionArchiveDeserializer; 80 | } 81 | 82 | public void setTransactionArchiveDeserializer(ArchiveDeserializer transactionArchiveDeserializer) { 83 | this.transactionArchiveDeserializer = transactionArchiveDeserializer; 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/bytejta/strategy/SimpleTransactionStrategy.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.strategy; 17 | 18 | import javax.transaction.HeuristicCommitException; 19 | import javax.transaction.HeuristicMixedException; 20 | import javax.transaction.HeuristicRollbackException; 21 | import javax.transaction.SystemException; 22 | import javax.transaction.xa.XAException; 23 | import javax.transaction.xa.Xid; 24 | 25 | import org.bytesoft.bytejta.TransactionStrategy; 26 | import org.bytesoft.transaction.CommitRequiredException; 27 | import org.bytesoft.transaction.RollbackRequiredException; 28 | import org.bytesoft.transaction.resource.XATerminator; 29 | import org.slf4j.Logger; 30 | import org.slf4j.LoggerFactory; 31 | 32 | public class SimpleTransactionStrategy implements TransactionStrategy { 33 | static final Logger logger = LoggerFactory.getLogger(SimpleTransactionStrategy.class); 34 | 35 | private final XATerminator terminator; 36 | 37 | public SimpleTransactionStrategy(XATerminator terminator) { 38 | if (terminator == null || terminator.getResourceArchives().isEmpty()) { 39 | throw new IllegalStateException(); 40 | } 41 | 42 | this.terminator = terminator; 43 | } 44 | 45 | public int prepare(Xid xid) throws RollbackRequiredException, CommitRequiredException { 46 | 47 | try { 48 | return this.terminator.prepare(xid); 49 | } catch (XAException xaex) { 50 | throw new RollbackRequiredException(); 51 | } catch (RuntimeException xaex) { 52 | throw new RollbackRequiredException(); 53 | } 54 | 55 | } 56 | 57 | public void commit(Xid xid, boolean onePhaseCommit) 58 | throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, SystemException { 59 | try { 60 | this.terminator.commit(xid, onePhaseCommit); 61 | } catch (XAException xaex) { 62 | switch (xaex.errorCode) { 63 | case XAException.XA_HEURCOM: 64 | break; 65 | case XAException.XA_HEURMIX: 66 | throw new HeuristicMixedException(); 67 | case XAException.XA_HEURRB: 68 | throw new HeuristicRollbackException(); 69 | default: 70 | logger.error("Unknown state in committing transaction phase.", xaex); 71 | throw new SystemException(); 72 | } 73 | } catch (RuntimeException rex) { 74 | logger.error("Unknown state in committing transaction phase.", rex); 75 | throw new SystemException(); 76 | } 77 | } 78 | 79 | public void rollback(Xid xid) 80 | throws HeuristicMixedException, HeuristicCommitException, IllegalStateException, SystemException { 81 | try { 82 | this.terminator.rollback(xid); 83 | } catch (XAException xaex) { 84 | switch (xaex.errorCode) { 85 | case XAException.XA_HEURRB: 86 | break; 87 | case XAException.XA_HEURMIX: 88 | throw new HeuristicMixedException(); 89 | case XAException.XA_HEURCOM: 90 | throw new HeuristicCommitException(); 91 | default: 92 | logger.error("Unknown state in rollingback transaction phase.", xaex); 93 | throw new SystemException(); 94 | } 95 | } catch (RuntimeException rex) { 96 | logger.error("Unknown state in rollingback transaction phase.", rex); 97 | throw new SystemException(); 98 | } 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/bytejta/strategy/VacantTransactionStrategy.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.strategy; 17 | 18 | import javax.transaction.HeuristicCommitException; 19 | import javax.transaction.HeuristicMixedException; 20 | import javax.transaction.HeuristicRollbackException; 21 | import javax.transaction.SystemException; 22 | import javax.transaction.xa.XAResource; 23 | import javax.transaction.xa.Xid; 24 | 25 | import org.bytesoft.bytejta.TransactionStrategy; 26 | import org.bytesoft.transaction.CommitRequiredException; 27 | import org.bytesoft.transaction.RollbackRequiredException; 28 | 29 | public class VacantTransactionStrategy implements TransactionStrategy { 30 | 31 | public int prepare(Xid xid) throws RollbackRequiredException, CommitRequiredException { 32 | return XAResource.XA_RDONLY; 33 | } 34 | 35 | public void commit(Xid xid, boolean onePhaseCommit) 36 | throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, SystemException { 37 | } 38 | 39 | public void rollback(Xid xid) 40 | throws HeuristicMixedException, HeuristicCommitException, IllegalStateException, SystemException { 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/bytejta/supports/jdbc/DataSourceHolder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.jdbc; 17 | 18 | import javax.sql.DataSource; 19 | 20 | public interface DataSourceHolder { 21 | 22 | public DataSource getDataSource(); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/bytejta/supports/jdbc/LocalXACompatible.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.jdbc; 17 | 18 | public interface LocalXACompatible { 19 | 20 | public boolean compatibleLoggingLRO(); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/bytejta/supports/resource/UnidentifiedResourceDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.resource; 17 | 18 | import javax.transaction.xa.XAException; 19 | import javax.transaction.xa.XAResource; 20 | import javax.transaction.xa.Xid; 21 | 22 | import org.bytesoft.transaction.supports.resource.XAResourceDescriptor; 23 | 24 | public class UnidentifiedResourceDescriptor implements XAResourceDescriptor { 25 | 26 | private String identifier; 27 | private XAResource delegate; 28 | 29 | public boolean isTransactionCommitted(Xid xid) throws IllegalStateException { 30 | throw new IllegalStateException(); 31 | } 32 | 33 | public String toString() { 34 | return String.format("unknown-resource[%s]", this.delegate); 35 | } 36 | 37 | public void setTransactionTimeoutQuietly(int timeout) { 38 | try { 39 | this.delegate.setTransactionTimeout(timeout); 40 | } catch (Exception ex) { 41 | return; 42 | } 43 | } 44 | 45 | public void commit(Xid arg0, boolean arg1) throws XAException { 46 | if (this.delegate == null) { 47 | return; 48 | } 49 | delegate.commit(arg0, arg1); 50 | } 51 | 52 | public void end(Xid arg0, int arg1) throws XAException { 53 | if (this.delegate == null) { 54 | return; 55 | } 56 | delegate.end(arg0, arg1); 57 | } 58 | 59 | public void forget(Xid arg0) throws XAException { 60 | if (this.delegate == null) { 61 | return; 62 | } 63 | delegate.forget(arg0); 64 | } 65 | 66 | public int getTransactionTimeout() throws XAException { 67 | if (this.delegate == null) { 68 | return 0; 69 | } 70 | return delegate.getTransactionTimeout(); 71 | } 72 | 73 | public boolean isSameRM(XAResource arg0) throws XAException { 74 | if (this.delegate == null) { 75 | return false; 76 | } 77 | return delegate.isSameRM(arg0); 78 | } 79 | 80 | public int prepare(Xid arg0) throws XAException { 81 | if (this.delegate == null) { 82 | return XAResource.XA_RDONLY; 83 | } 84 | return delegate.prepare(arg0); 85 | } 86 | 87 | public Xid[] recover(int arg0) throws XAException { 88 | if (this.delegate == null) { 89 | return new Xid[0]; 90 | } 91 | return delegate.recover(arg0); 92 | } 93 | 94 | public void rollback(Xid arg0) throws XAException { 95 | if (this.delegate == null) { 96 | return; 97 | } 98 | delegate.rollback(arg0); 99 | } 100 | 101 | public boolean setTransactionTimeout(int arg0) throws XAException { 102 | if (this.delegate == null) { 103 | return false; 104 | } 105 | return delegate.setTransactionTimeout(arg0); 106 | } 107 | 108 | public void start(Xid arg0, int arg1) throws XAException { 109 | if (this.delegate == null) { 110 | return; 111 | } 112 | delegate.start(arg0, arg1); 113 | } 114 | 115 | public String getIdentifier() { 116 | return identifier; 117 | } 118 | 119 | public void setIdentifier(String identifier) { 120 | this.identifier = identifier; 121 | } 122 | 123 | public XAResource getDelegate() { 124 | return delegate; 125 | } 126 | 127 | public void setDelegate(XAResource delegate) { 128 | this.delegate = delegate; 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/CommitRequiredException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | import javax.transaction.SystemException; 19 | 20 | public class CommitRequiredException extends SystemException { 21 | private static final long serialVersionUID = 1L; 22 | 23 | public CommitRequiredException() { 24 | super(); 25 | } 26 | 27 | public CommitRequiredException(String s) { 28 | super(s); 29 | } 30 | 31 | public CommitRequiredException(int errcode) { 32 | super(errcode); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/RemoteSystemException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | import java.rmi.RemoteException; 19 | 20 | import javax.transaction.SystemException; 21 | 22 | public class RemoteSystemException extends SystemException { 23 | private static final long serialVersionUID = 1L; 24 | 25 | public RemoteSystemException() { 26 | super(); 27 | } 28 | 29 | public RemoteSystemException(String s) { 30 | super(s); 31 | } 32 | 33 | public RemoteSystemException(int errcode) { 34 | super(errcode); 35 | } 36 | 37 | public Throwable initCause(Throwable cause) { 38 | if (RemoteException.class.isInstance(cause) == false) { 39 | throw new IllegalArgumentException(); 40 | } 41 | return super.initCause(cause); 42 | } 43 | 44 | public RemoteException getRemoteException() { 45 | Throwable thrown = super.getCause(); 46 | return RemoteException.class.cast(thrown); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/RollbackRequiredException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | import javax.transaction.SystemException; 19 | 20 | public class RollbackRequiredException extends SystemException { 21 | private static final long serialVersionUID = 1L; 22 | 23 | public RollbackRequiredException() { 24 | super(); 25 | } 26 | 27 | public RollbackRequiredException(String s) { 28 | super(s); 29 | } 30 | 31 | public RollbackRequiredException(int errcode) { 32 | super(errcode); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/Transaction.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | import javax.transaction.HeuristicMixedException; 19 | import javax.transaction.HeuristicRollbackException; 20 | import javax.transaction.RollbackException; 21 | import javax.transaction.SystemException; 22 | 23 | import org.bytesoft.transaction.archive.TransactionArchive; 24 | import org.bytesoft.transaction.remote.RemoteSvc; 25 | import org.bytesoft.transaction.supports.TransactionExtra; 26 | import org.bytesoft.transaction.supports.TransactionListener; 27 | import org.bytesoft.transaction.supports.TransactionResourceListener; 28 | import org.bytesoft.transaction.supports.resource.XAResourceDescriptor; 29 | 30 | public interface Transaction extends javax.transaction.Transaction, TransactionExtra { 31 | 32 | public void fireBeforeTransactionCompletion() throws RollbackRequiredException, SystemException; 33 | 34 | public void fireBeforeTransactionCompletionQuietly(); 35 | 36 | public void fireAfterTransactionCompletion(); 37 | 38 | public boolean isLocalTransaction(); 39 | 40 | public boolean isMarkedRollbackOnly(); 41 | 42 | public void setRollbackOnlyQuietly(); 43 | 44 | public int getTransactionStatus(); 45 | 46 | public void setTransactionStatus(int status); 47 | 48 | public void resume() throws SystemException; 49 | 50 | public void suspend() throws SystemException; 51 | 52 | public boolean isTiming(); 53 | 54 | public void setTransactionTimeout(int seconds); 55 | 56 | public void registerTransactionListener(TransactionListener listener); 57 | 58 | public void registerTransactionResourceListener(TransactionResourceListener listener); 59 | 60 | public TransactionExtra getTransactionalExtra(); 61 | 62 | public void setTransactionalExtra(TransactionExtra transactionalExtra); 63 | 64 | public XAResourceDescriptor getResourceDescriptor(String beanName); 65 | 66 | public XAResourceDescriptor getRemoteCoordinator(RemoteSvc remoteSvc); 67 | 68 | public XAResourceDescriptor getRemoteCoordinator(String application); 69 | 70 | public TransactionContext getTransactionContext(); 71 | 72 | public TransactionArchive getTransactionArchive(); 73 | 74 | public int participantPrepare() throws RollbackRequiredException, CommitRequiredException; 75 | 76 | public void participantCommit(boolean opc) throws RollbackException, HeuristicMixedException, HeuristicRollbackException, 77 | SecurityException, IllegalStateException, CommitRequiredException, SystemException; 78 | 79 | public void participantRollback() throws IllegalStateException, RollbackRequiredException, SystemException; 80 | 81 | public void forget() throws SystemException; 82 | 83 | public void forgetQuietly(); 84 | 85 | public void recover() throws SystemException; 86 | 87 | public void recoveryCommit() throws CommitRequiredException, SystemException; 88 | 89 | public void recoveryRollback() throws RollbackRequiredException, SystemException; 90 | 91 | public Exception getCreatedAt(); 92 | 93 | } 94 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/TransactionBeanFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | import org.bytesoft.transaction.logging.ArchiveDeserializer; 19 | import org.bytesoft.transaction.logging.TransactionLogger; 20 | import org.bytesoft.transaction.supports.TransactionTimer; 21 | import org.bytesoft.transaction.supports.rpc.TransactionInterceptor; 22 | import org.bytesoft.transaction.supports.serialize.XAResourceDeserializer; 23 | import org.bytesoft.transaction.xa.XidFactory; 24 | 25 | public interface TransactionBeanFactory { 26 | 27 | public TransactionLock getTransactionLock(); 28 | 29 | public TransactionManager getTransactionManager(); 30 | 31 | public XidFactory getXidFactory(); 32 | 33 | public TransactionTimer getTransactionTimer(); 34 | 35 | public TransactionRepository getTransactionRepository(); 36 | 37 | public TransactionInterceptor getTransactionInterceptor(); 38 | 39 | public TransactionRecovery getTransactionRecovery(); 40 | 41 | public TransactionParticipant getNativeParticipant(); 42 | 43 | public TransactionLogger getTransactionLogger(); 44 | 45 | public ArchiveDeserializer getArchiveDeserializer(); 46 | 47 | public XAResourceDeserializer getResourceDeserializer(); 48 | 49 | } 50 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/TransactionContext.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | import java.io.Serializable; 19 | 20 | import org.bytesoft.transaction.xa.TransactionXid; 21 | 22 | public class TransactionContext implements Serializable, Cloneable { 23 | private static final long serialVersionUID = 1L; 24 | 25 | protected transient Object propagatedBy; 26 | protected transient boolean propagated; 27 | protected transient boolean coordinator; 28 | protected transient boolean recoveried; 29 | protected transient int recoveredTimes; 30 | 31 | protected TransactionXid xid; 32 | protected long createdTime; 33 | protected long expiredTime; 34 | protected boolean rollbackOnly; 35 | protected long configFlags; 36 | 37 | public TransactionContext clone() { 38 | TransactionContext that = new TransactionContext(); 39 | that.xid = this.xid.clone(); 40 | that.createdTime = System.currentTimeMillis(); 41 | that.expiredTime = this.expiredTime; 42 | that.rollbackOnly = this.rollbackOnly; 43 | that.configFlags = this.configFlags; 44 | return that; 45 | } 46 | 47 | public int getRecoveredTimes() { 48 | return recoveredTimes; 49 | } 50 | 51 | public void setRecoveredTimes(int recoveredTimes) { 52 | this.recoveredTimes = recoveredTimes; 53 | } 54 | 55 | public boolean isCoordinator() { 56 | return coordinator; 57 | } 58 | 59 | public void setCoordinator(boolean coordinator) { 60 | this.coordinator = coordinator; 61 | } 62 | 63 | public boolean isRecoveried() { 64 | return recoveried; 65 | } 66 | 67 | public void setRecoveried(boolean recoveried) { 68 | this.recoveried = recoveried; 69 | } 70 | 71 | public TransactionXid getXid() { 72 | return xid; 73 | } 74 | 75 | public void setXid(TransactionXid xid) { 76 | this.xid = xid; 77 | } 78 | 79 | public long getCreatedTime() { 80 | return createdTime; 81 | } 82 | 83 | public void setCreatedTime(long createdTime) { 84 | this.createdTime = createdTime; 85 | } 86 | 87 | public long getExpiredTime() { 88 | return expiredTime; 89 | } 90 | 91 | public void setExpiredTime(long expiredTime) { 92 | this.expiredTime = expiredTime; 93 | } 94 | 95 | public long getConfigFlags() { 96 | return configFlags; 97 | } 98 | 99 | public void setConfigFlags(long configFlags) { 100 | this.configFlags = configFlags; 101 | } 102 | 103 | public boolean isPropagated() { 104 | return propagated; 105 | } 106 | 107 | public void setPropagated(boolean propagated) { 108 | this.propagated = propagated; 109 | } 110 | 111 | public Object getPropagatedBy() { 112 | return propagatedBy; 113 | } 114 | 115 | public void setPropagatedBy(Object propagatedBy) { 116 | this.propagatedBy = propagatedBy; 117 | } 118 | 119 | public boolean isRollbackOnly() { 120 | return rollbackOnly; 121 | } 122 | 123 | public void setRollbackOnly(boolean rollbackOnly) { 124 | this.rollbackOnly = rollbackOnly; 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/TransactionException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | public class TransactionException extends RuntimeException { 19 | private static final long serialVersionUID = 1L; 20 | 21 | /* XAException.errorCode */ 22 | public int errorCode; 23 | 24 | public TransactionException(int errcode) { 25 | super(); 26 | errorCode = errcode; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/TransactionLock.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | import org.bytesoft.transaction.xa.TransactionXid; 19 | 20 | public interface TransactionLock { 21 | 22 | public boolean lockTransaction(TransactionXid transactionXid, String identifier); 23 | 24 | public void unlockTransaction(TransactionXid transactionXid, String identifier); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/TransactionManager.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | import javax.transaction.SystemException; 19 | 20 | public interface TransactionManager extends javax.transaction.TransactionManager { 21 | 22 | public int getTimeoutSeconds(); 23 | 24 | public void setTimeoutSeconds(int timeoutSeconds); 25 | 26 | public void associateThread(Transaction transaction); 27 | 28 | public Transaction desociateThread(); 29 | 30 | public Transaction getTransaction(Thread thread); 31 | 32 | public Transaction getTransactionQuietly(); 33 | 34 | public Transaction getTransaction() throws SystemException; 35 | 36 | public Transaction suspend() throws SystemException; 37 | 38 | public void setRollbackOnlyQuietly(); 39 | 40 | } 41 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/TransactionParticipant.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | import javax.transaction.xa.XAException; 19 | import javax.transaction.xa.XAResource; 20 | import javax.transaction.xa.Xid; 21 | 22 | public interface TransactionParticipant extends XAResource { 23 | 24 | public Transaction end(TransactionContext transactionContext, int flags) throws XAException; 25 | 26 | public void forgetQuietly(Xid xid); 27 | 28 | public Transaction start(TransactionContext transactionContext, int flags) throws XAException; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/TransactionRecovery.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | import org.bytesoft.transaction.archive.TransactionArchive; 19 | 20 | public interface TransactionRecovery { 21 | 22 | public Transaction reconstruct(TransactionArchive archive); 23 | 24 | public void timingRecover(); 25 | 26 | public void startRecovery(); 27 | 28 | public void branchRecover(); 29 | 30 | public boolean isInitialized(); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/TransactionRepository.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction; 17 | 18 | import java.util.List; 19 | 20 | import org.bytesoft.transaction.xa.TransactionXid; 21 | 22 | public interface TransactionRepository { 23 | 24 | // active-transaction & error-transaction 25 | public void putTransaction(TransactionXid xid, Transaction transaction); 26 | 27 | public Transaction getTransaction(TransactionXid xid); 28 | 29 | public Transaction removeTransaction(TransactionXid xid); 30 | 31 | // error-transaction 32 | public void putErrorTransaction(TransactionXid xid, Transaction transaction); 33 | 34 | public Transaction getErrorTransaction(TransactionXid xid); 35 | 36 | public Transaction removeErrorTransaction(TransactionXid xid); 37 | 38 | public List getErrorTransactionList(); 39 | 40 | public List getActiveTransactionList(); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/adapter/ResourceAdapterImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.adapter; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | import javax.resource.ResourceException; 22 | import javax.resource.spi.ActivationSpec; 23 | import javax.resource.spi.BootstrapContext; 24 | import javax.resource.spi.ResourceAdapter; 25 | import javax.resource.spi.ResourceAdapterInternalException; 26 | import javax.resource.spi.endpoint.MessageEndpointFactory; 27 | import javax.resource.spi.work.Work; 28 | import javax.resource.spi.work.WorkException; 29 | import javax.resource.spi.work.WorkManager; 30 | import javax.transaction.xa.XAResource; 31 | 32 | import org.slf4j.Logger; 33 | import org.slf4j.LoggerFactory; 34 | 35 | public class ResourceAdapterImpl implements ResourceAdapter { 36 | static final Logger logger = LoggerFactory.getLogger(ResourceAdapterImpl.class); 37 | 38 | private List workList = new ArrayList();; 39 | private WorkManager workManager; 40 | 41 | public void start(BootstrapContext ctx) throws ResourceAdapterInternalException { 42 | this.workManager = ctx.getWorkManager(); 43 | for (int i = 0; this.workList != null && i < this.workList.size(); i++) { 44 | Work work = this.workList.get(i); 45 | try { 46 | this.workManager.startWork(work); 47 | } catch (WorkException ex) { 48 | this.stop(); 49 | throw new ResourceAdapterInternalException(ex); 50 | } catch (RuntimeException ex) { 51 | this.stop(); 52 | throw new ResourceAdapterInternalException(ex); 53 | } 54 | } 55 | } 56 | 57 | public void stop() { 58 | for (int i = 0; this.workList != null && i < this.workList.size(); i++) { 59 | Work work = this.workList.get(i); 60 | try { 61 | work.release(); 62 | } catch (RuntimeException rex) { 63 | logger.debug(rex.getMessage(), rex); 64 | } 65 | } 66 | } 67 | 68 | public void endpointActivation(MessageEndpointFactory endpointFactory, ActivationSpec spec) throws ResourceException { 69 | } 70 | 71 | public void endpointDeactivation(MessageEndpointFactory endpointFactory, ActivationSpec spec) { 72 | } 73 | 74 | public XAResource[] getXAResources(ActivationSpec[] specs) throws ResourceException { 75 | return new XAResource[0]; 76 | } 77 | 78 | public List getWorkList() { 79 | return workList; 80 | } 81 | 82 | public void setWorkList(List workList) { 83 | this.workList = workList; 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/archive/TransactionArchive.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.archive; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | import javax.transaction.xa.Xid; 22 | 23 | public class TransactionArchive { 24 | private transient String endpoint; 25 | private Xid xid; 26 | private int status; 27 | private int vote; 28 | private boolean coordinator; 29 | private Object propagatedBy; 30 | private final List nativeResources = new ArrayList(); 31 | private final List remoteResources = new ArrayList(); 32 | 33 | private int transactionStrategyType; 34 | private XAResourceArchive optimizedResource; 35 | 36 | private int recoveredTimes; 37 | private long recoveredAt; 38 | 39 | public int getRecoveredTimes() { 40 | return recoveredTimes; 41 | } 42 | 43 | public void setRecoveredTimes(int recoveredTimes) { 44 | this.recoveredTimes = recoveredTimes; 45 | } 46 | 47 | public long getRecoveredAt() { 48 | return recoveredAt; 49 | } 50 | 51 | public void setRecoveredAt(long recoveredAt) { 52 | this.recoveredAt = recoveredAt; 53 | } 54 | 55 | public String getEndpoint() { 56 | return endpoint; 57 | } 58 | 59 | public void setEndpoint(String endpoint) { 60 | this.endpoint = endpoint; 61 | } 62 | 63 | public Xid getXid() { 64 | return xid; 65 | } 66 | 67 | public void setXid(Xid xid) { 68 | this.xid = xid; 69 | } 70 | 71 | public int getStatus() { 72 | return status; 73 | } 74 | 75 | public void setStatus(int status) { 76 | this.status = status; 77 | } 78 | 79 | public int getVote() { 80 | return vote; 81 | } 82 | 83 | public void setVote(int vote) { 84 | this.vote = vote; 85 | } 86 | 87 | public boolean isCoordinator() { 88 | return coordinator; 89 | } 90 | 91 | public void setCoordinator(boolean coordinator) { 92 | this.coordinator = coordinator; 93 | } 94 | 95 | public Object getPropagatedBy() { 96 | return propagatedBy; 97 | } 98 | 99 | public void setPropagatedBy(Object propagatedBy) { 100 | this.propagatedBy = propagatedBy; 101 | } 102 | 103 | public List getNativeResources() { 104 | return nativeResources; 105 | } 106 | 107 | public List getRemoteResources() { 108 | return remoteResources; 109 | } 110 | 111 | public XAResourceArchive getOptimizedResource() { 112 | return optimizedResource; 113 | } 114 | 115 | public void setOptimizedResource(XAResourceArchive optimizedResource) { 116 | this.optimizedResource = optimizedResource; 117 | } 118 | 119 | public int getTransactionStrategyType() { 120 | return transactionStrategyType; 121 | } 122 | 123 | public void setTransactionStrategyType(int transactionStrategyType) { 124 | this.transactionStrategyType = transactionStrategyType; 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/aware/TransactionBeanFactoryAware.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.aware; 17 | 18 | import org.bytesoft.transaction.TransactionBeanFactory; 19 | 20 | public interface TransactionBeanFactoryAware { 21 | public static final String BEAN_FACTORY_FIELD_NAME = "beanFactory"; 22 | 23 | public TransactionBeanFactory getBeanFactory(); 24 | 25 | public void setBeanFactory(TransactionBeanFactory tbf); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/aware/TransactionDebuggable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.aware; 17 | 18 | public interface TransactionDebuggable { 19 | 20 | public boolean isDebuggingEnabled(); 21 | 22 | public void setDebuggingEnabled(boolean debuggingEnabled); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/aware/TransactionEndpointAware.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.aware; 17 | 18 | public interface TransactionEndpointAware { 19 | public static final String ENDPOINT_FIELD_NAME = "endpoint"; 20 | 21 | public String getEndpoint(); 22 | 23 | public void setEndpoint(String identifier); 24 | } 25 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/cmd/CommandDispatcher.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.cmd; 17 | 18 | import java.util.concurrent.Callable; 19 | 20 | public interface CommandDispatcher { 21 | 22 | public Object dispatch(Callable callable) throws Exception; 23 | 24 | public void dispatch(Runnable runnable) throws Exception; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/internal/SynchronizationImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.internal; 17 | 18 | import javax.transaction.Synchronization; 19 | 20 | import org.slf4j.Logger; 21 | import org.slf4j.LoggerFactory; 22 | 23 | 24 | public class SynchronizationImpl implements Synchronization { 25 | static final Logger logger = LoggerFactory.getLogger(SynchronizationImpl.class); 26 | 27 | private Synchronization delegate; 28 | private boolean beforeRequired; 29 | private boolean finishRequired; 30 | 31 | public SynchronizationImpl(Synchronization sync) { 32 | if (sync == null) { 33 | throw new IllegalArgumentException(); 34 | } else { 35 | this.delegate = sync; 36 | this.beforeRequired = true; 37 | this.finishRequired = true; 38 | } 39 | } 40 | 41 | public void beforeCompletion() { 42 | if (this.beforeRequired) { 43 | try { 44 | this.delegate.beforeCompletion(); 45 | } catch (RuntimeException rex) { 46 | // ignore 47 | } finally { 48 | this.beforeRequired = false; 49 | } 50 | } 51 | } 52 | 53 | public void afterCompletion(int status) { 54 | if (this.finishRequired) { 55 | try { 56 | this.delegate.afterCompletion(status); 57 | } catch (RuntimeException rex) { 58 | // ignore 59 | } finally { 60 | this.finishRequired = false; 61 | } 62 | } 63 | } 64 | 65 | public String toString() { 66 | return String.format("[%s] delegate: %s", this.getClass().getSimpleName(), this.delegate); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/internal/SynchronizationList.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.internal; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | import javax.transaction.Synchronization; 22 | 23 | import org.slf4j.Logger; 24 | import org.slf4j.LoggerFactory; 25 | 26 | public class SynchronizationList implements Synchronization { 27 | private static final Logger logger = LoggerFactory.getLogger(SynchronizationList.class); 28 | private final List synchronizations = new ArrayList(); 29 | 30 | private boolean beforeCompletionInvoked; 31 | private boolean finishCompletionInvoked; 32 | 33 | public void registerSynchronizationQuietly(Synchronization sync) { 34 | SynchronizationImpl synchronization = new SynchronizationImpl(sync); 35 | this.synchronizations.add(synchronization); 36 | } 37 | 38 | public synchronized void beforeCompletion() { 39 | if (this.beforeCompletionInvoked == false) { 40 | int length = this.synchronizations.size(); 41 | for (int i = 0; i < length; i++) { 42 | Synchronization synchronization = this.synchronizations.get(i); 43 | try { 44 | synchronization.beforeCompletion(); 45 | } catch (RuntimeException error) { 46 | logger.error(error.getMessage(), error); 47 | } 48 | } // end-for 49 | 50 | this.beforeCompletionInvoked = true; 51 | } // end-if (this.beforeCompletionInvoked == false) 52 | } 53 | 54 | public synchronized void afterCompletion(int status) { 55 | if (this.finishCompletionInvoked == false) { 56 | int length = this.synchronizations.size(); 57 | for (int i = 0; i < length; i++) { 58 | Synchronization synchronization = this.synchronizations.get(i); 59 | try { 60 | synchronization.afterCompletion(status); 61 | } catch (RuntimeException error) { 62 | logger.error(error.getMessage(), error); 63 | } 64 | } // end-for 65 | 66 | this.finishCompletionInvoked = true; 67 | } // end-if (this.finishCompletionInvoked == false) 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/internal/TransactionResourceListenerList.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.internal; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | import javax.transaction.xa.XAResource; 22 | import javax.transaction.xa.Xid; 23 | 24 | import org.bytesoft.transaction.supports.TransactionResourceListener; 25 | import org.slf4j.Logger; 26 | import org.slf4j.LoggerFactory; 27 | 28 | public class TransactionResourceListenerList implements TransactionResourceListener { 29 | static final Logger logger = LoggerFactory.getLogger(TransactionResourceListenerList.class); 30 | 31 | private final List listeners = new ArrayList(); 32 | 33 | public void registerTransactionResourceListener(TransactionResourceListener listener) { 34 | this.listeners.add(listener); 35 | } 36 | 37 | public void onEnlistResource(Xid xid, XAResource xares) { 38 | for (int i = 0; i < this.listeners.size(); i++) { 39 | try { 40 | TransactionResourceListener listener = this.listeners.get(i); 41 | listener.onEnlistResource(xid, xares); 42 | } catch (RuntimeException rex) { 43 | logger.error(rex.getMessage(), rex); 44 | } 45 | } 46 | } 47 | 48 | public void onDelistResource(Xid xid, XAResource xares) { 49 | for (int i = 0; i < this.listeners.size(); i++) { 50 | try { 51 | TransactionResourceListener listener = this.listeners.get(i); 52 | listener.onDelistResource(xid, xares); 53 | } catch (RuntimeException rex) { 54 | logger.error(rex.getMessage(), rex); 55 | } 56 | } 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/logging/ArchiveDeserializer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.logging; 17 | 18 | import org.bytesoft.transaction.xa.TransactionXid; 19 | 20 | public interface ArchiveDeserializer { 21 | 22 | public byte[] serialize(TransactionXid xid, Object archive); 23 | 24 | public Object deserialize(TransactionXid xid, byte[] array); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/logging/LoggingFlushable.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.logging; 17 | 18 | public interface LoggingFlushable { 19 | 20 | public void flushImmediately(); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/logging/TransactionLogger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.logging; 17 | 18 | import org.bytesoft.transaction.archive.TransactionArchive; 19 | import org.bytesoft.transaction.archive.XAResourceArchive; 20 | import org.bytesoft.transaction.recovery.TransactionRecoveryCallback; 21 | 22 | public interface TransactionLogger { 23 | 24 | /* transaction */ 25 | public void createTransaction(TransactionArchive archive); 26 | 27 | public void updateTransaction(TransactionArchive archive); 28 | 29 | public void deleteTransaction(TransactionArchive archive); 30 | 31 | /* resource */ 32 | public void createResource(XAResourceArchive archive); 33 | 34 | public void updateResource(XAResourceArchive archive); 35 | 36 | public void deleteResource(XAResourceArchive archive); 37 | 38 | /* participant */ 39 | public void createParticipant(XAResourceArchive archive); 40 | 41 | public void updateParticipant(XAResourceArchive archive); 42 | 43 | public void deleteParticipant(XAResourceArchive archive); 44 | 45 | /* recovery */ 46 | public void recover(TransactionRecoveryCallback callback); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/logging/store/VirtualLoggingKey.java: -------------------------------------------------------------------------------- 1 | package org.bytesoft.transaction.logging.store; 2 | 3 | import java.util.Arrays; 4 | 5 | import javax.transaction.xa.Xid; 6 | 7 | public class VirtualLoggingKey implements Xid { 8 | public static final int FORMAT_ID = 9257; 9 | 10 | private byte[] globalTransactionId = new byte[0]; 11 | private byte[] branchQualifier = new byte[0]; 12 | 13 | public int hashCode() { 14 | int hash = 11; 15 | hash += 13 * this.getFormatId(); 16 | hash += 17 * Arrays.hashCode(branchQualifier); 17 | hash += 19 * Arrays.hashCode(globalTransactionId); 18 | return hash; 19 | } 20 | 21 | public boolean equals(Object obj) { 22 | if (obj == null) { 23 | return false; 24 | } else if (VirtualLoggingKey.class.isInstance(obj) == false) { 25 | return false; 26 | } 27 | VirtualLoggingKey that = (VirtualLoggingKey) obj; 28 | if (this.getFormatId() != that.getFormatId()) { 29 | return false; 30 | } else if (Arrays.equals(branchQualifier, that.branchQualifier) == false) { 31 | return false; 32 | } else if (Arrays.equals(globalTransactionId, that.globalTransactionId) == false) { 33 | return false; 34 | } 35 | return true; 36 | } 37 | 38 | public int getFormatId() { 39 | return FORMAT_ID; 40 | } 41 | 42 | public byte[] getGlobalTransactionId() { 43 | return globalTransactionId; 44 | } 45 | 46 | public void setGlobalTransactionId(byte[] globalTransactionId) { 47 | this.globalTransactionId = globalTransactionId; 48 | } 49 | 50 | public byte[] getBranchQualifier() { 51 | return branchQualifier; 52 | } 53 | 54 | public void setBranchQualifier(byte[] branchQualifier) { 55 | this.branchQualifier = branchQualifier; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/logging/store/VirtualLoggingListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.logging.store; 17 | 18 | public interface VirtualLoggingListener { 19 | 20 | public void recvOperation(VirtualLoggingRecord action); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/logging/store/VirtualLoggingRecord.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.logging.store; 17 | 18 | import javax.transaction.xa.Xid; 19 | 20 | public class VirtualLoggingRecord { 21 | 22 | private Xid identifier; 23 | private int operator; 24 | private byte[] value; 25 | private byte[] content; 26 | 27 | public int getOperator() { 28 | return operator; 29 | } 30 | 31 | public void setOperator(int operator) { 32 | this.operator = operator; 33 | } 34 | 35 | public Xid getIdentifier() { 36 | return identifier; 37 | } 38 | 39 | public void setIdentifier(Xid identifier) { 40 | this.identifier = identifier; 41 | } 42 | 43 | public byte[] getValue() { 44 | return value; 45 | } 46 | 47 | public void setValue(byte[] value) { 48 | this.value = value; 49 | } 50 | 51 | public byte[] getContent() { 52 | return content; 53 | } 54 | 55 | public void setContent(byte[] content) { 56 | this.content = content; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/logging/store/VirtualLoggingSystem.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.logging.store; 17 | 18 | import javax.transaction.xa.Xid; 19 | 20 | public interface VirtualLoggingSystem { 21 | public static final int OPERATOR_CREATE = 1; 22 | public static final int OPERATOR_MOFIFY = 2; 23 | public static final int OPERATOR_DELETE = 3; 24 | 25 | public void create(Xid xid, byte[] byteArray); 26 | 27 | public void delete(Xid xid); 28 | 29 | public void modify(Xid xid, byte[] byteArray); 30 | 31 | public void traversal(VirtualLoggingListener listener); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/logging/store/VirtualLoggingTrigger.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.logging.store; 17 | 18 | public interface VirtualLoggingTrigger { 19 | 20 | public void fireSwapImmediately(); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/recovery/TransactionRecoveryCallback.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.recovery; 17 | 18 | import org.bytesoft.transaction.archive.TransactionArchive; 19 | 20 | public interface TransactionRecoveryCallback { 21 | 22 | public void recover(TransactionArchive archive); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/recovery/TransactionRecoveryListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.recovery; 17 | 18 | import org.bytesoft.transaction.Transaction; 19 | 20 | public interface TransactionRecoveryListener { 21 | 22 | public void onRecovery(Transaction transaction); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/remote/RemoteAddr.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.remote; 17 | 18 | import java.io.Serializable; 19 | 20 | import org.apache.commons.lang3.StringUtils; 21 | 22 | public class RemoteAddr implements Serializable { 23 | private static final long serialVersionUID = 1L; 24 | 25 | protected String serverHost; 26 | protected int serverPort; 27 | 28 | public int hashCode() { 29 | int hash = 7; 30 | hash += 11 * (this.serverHost == null ? 0 : this.serverHost.hashCode()); 31 | hash += 13 * this.serverPort; 32 | return hash; 33 | } 34 | 35 | public boolean equals(Object obj) { 36 | if (obj == null) { 37 | return false; 38 | } else if (RemoteAddr.class.isInstance(obj) == false) { 39 | return false; 40 | } 41 | RemoteAddr that = (RemoteAddr) obj; 42 | boolean hostEquals = StringUtils.equals(this.serverHost, that.serverHost); 43 | boolean portEquals = this.serverPort == that.serverPort; 44 | return hostEquals && portEquals; 45 | } 46 | 47 | public String toString() { 48 | return String.format("", this.serverHost, this.serverPort); 49 | } 50 | 51 | public String getServerHost() { 52 | return serverHost; 53 | } 54 | 55 | public void setServerHost(String serverHost) { 56 | this.serverHost = serverHost; 57 | } 58 | 59 | public int getServerPort() { 60 | return serverPort; 61 | } 62 | 63 | public void setServerPort(int serverPort) { 64 | this.serverPort = serverPort; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/remote/RemoteCoordinator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.remote; 17 | 18 | import org.bytesoft.transaction.TransactionParticipant; 19 | 20 | public interface RemoteCoordinator extends TransactionParticipant { 21 | 22 | public String getIdentifier(); 23 | 24 | public RemoteAddr getRemoteAddr(); 25 | 26 | public RemoteNode getRemoteNode(); 27 | 28 | public String getApplication(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/remote/RemoteNode.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.remote; 17 | 18 | import org.apache.commons.lang3.StringUtils; 19 | 20 | public class RemoteNode extends RemoteAddr { 21 | private static final long serialVersionUID = 1L; 22 | 23 | protected String serviceKey; 24 | 25 | public int hashCode() { 26 | int hash = 7; 27 | hash += 11 * (this.serverHost == null ? 0 : this.serverHost.hashCode()); 28 | hash += 13 * this.serverPort; 29 | hash += 17 * (this.serviceKey == null ? 0 : this.serviceKey.hashCode()); 30 | return hash; 31 | } 32 | 33 | public boolean equals(Object obj) { 34 | if (obj == null) { 35 | return false; 36 | } else if (RemoteNode.class.isInstance(obj) == false) { 37 | return false; 38 | } 39 | RemoteNode that = (RemoteNode) obj; 40 | boolean hostEquals = StringUtils.equals(this.serverHost, that.serverHost); 41 | boolean portEquals = this.serverPort == that.serverPort; 42 | boolean servEquals = StringUtils.equals(this.serviceKey, that.serviceKey); 43 | return hostEquals && portEquals && servEquals; 44 | } 45 | 46 | public String toString() { 47 | return String.format("", this.serverHost, this.serviceKey, this.serverPort); 48 | } 49 | 50 | public String getServiceKey() { 51 | return serviceKey; 52 | } 53 | 54 | public void setServiceKey(String serviceKey) { 55 | this.serviceKey = serviceKey; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/remote/RemoteSvc.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.remote; 17 | 18 | import org.apache.commons.lang3.StringUtils; 19 | 20 | public class RemoteSvc extends RemoteAddr { 21 | private static final long serialVersionUID = 1L; 22 | 23 | protected String serviceKey; 24 | 25 | public int hashCode() { 26 | int hash = 19; 27 | hash += 23 * (this.serviceKey == null ? 0 : this.serviceKey.hashCode()); 28 | return hash; 29 | } 30 | 31 | public boolean equals(Object obj) { 32 | if (obj == null) { 33 | return false; 34 | } else if (RemoteSvc.class.isInstance(obj) == false) { 35 | return false; 36 | } 37 | RemoteSvc that = (RemoteSvc) obj; 38 | return StringUtils.equals(this.serviceKey, that.serviceKey); 39 | } 40 | 41 | public String toString() { 42 | return String.format("", this.serviceKey); 43 | } 44 | 45 | public String getServiceKey() { 46 | return serviceKey; 47 | } 48 | 49 | public void setServiceKey(String serviceKey) { 50 | this.serviceKey = serviceKey; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/resource/XATerminator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.resource; 17 | 18 | import java.util.List; 19 | 20 | import org.bytesoft.transaction.archive.XAResourceArchive; 21 | 22 | public interface XATerminator extends javax.transaction.xa.XAResource { 23 | 24 | public List getResourceArchives(); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/TransactionExtra.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports; 17 | 18 | import org.bytesoft.transaction.xa.TransactionXid; 19 | 20 | public interface TransactionExtra { 21 | 22 | public TransactionXid getTransactionXid(); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/TransactionListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports; 17 | 18 | import org.bytesoft.transaction.xa.TransactionXid; 19 | 20 | public interface TransactionListener { 21 | 22 | public void onPrepareStart(TransactionXid xid); 23 | 24 | public void onPrepareSuccess(TransactionXid xid); 25 | 26 | public void onPrepareFailure(TransactionXid xid); 27 | 28 | public void onCommitStart(TransactionXid xid); 29 | 30 | public void onCommitSuccess(TransactionXid xid); 31 | 32 | public void onCommitFailure(TransactionXid xid); 33 | 34 | public void onCommitHeuristicMixed(TransactionXid xid); 35 | 36 | public void onCommitHeuristicRolledback(TransactionXid xid); 37 | 38 | public void onRollbackStart(TransactionXid xid); 39 | 40 | public void onRollbackSuccess(TransactionXid xid); 41 | 42 | public void onRollbackFailure(TransactionXid xid); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/TransactionListenerAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports; 17 | 18 | import org.bytesoft.transaction.xa.TransactionXid; 19 | 20 | public class TransactionListenerAdapter implements TransactionListener { 21 | 22 | public void onPrepareStart(TransactionXid xid) { 23 | } 24 | 25 | public void onPrepareSuccess(TransactionXid xid) { 26 | } 27 | 28 | public void onPrepareFailure(TransactionXid xid) { 29 | } 30 | 31 | public void onCommitStart(TransactionXid xid) { 32 | } 33 | 34 | public void onCommitSuccess(TransactionXid xid) { 35 | } 36 | 37 | public void onCommitFailure(TransactionXid xid) { 38 | } 39 | 40 | public void onCommitHeuristicMixed(TransactionXid xid) { 41 | } 42 | 43 | public void onCommitHeuristicRolledback(TransactionXid xid) { 44 | } 45 | 46 | public void onRollbackStart(TransactionXid xid) { 47 | } 48 | 49 | public void onRollbackSuccess(TransactionXid xid) { 50 | } 51 | 52 | public void onRollbackFailure(TransactionXid xid) { 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/TransactionResourceListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports; 17 | 18 | import javax.transaction.xa.XAResource; 19 | import javax.transaction.xa.Xid; 20 | 21 | public interface TransactionResourceListener { 22 | 23 | public void onEnlistResource(Xid xid, XAResource xares); 24 | 25 | public void onDelistResource(Xid xid, XAResource xares); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/TransactionResourceListenerAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports; 17 | 18 | import javax.transaction.xa.XAResource; 19 | import javax.transaction.xa.Xid; 20 | 21 | public class TransactionResourceListenerAdapter implements TransactionResourceListener { 22 | 23 | public void onEnlistResource(Xid xid, XAResource xares) { 24 | } 25 | 26 | public void onDelistResource(Xid xid, XAResource xares) { 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/TransactionStatistic.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports; 17 | 18 | import org.bytesoft.bytejta.TransactionImpl; 19 | 20 | public interface TransactionStatistic { 21 | public static final long FLAGS_ACTIVE = 0x1; 22 | public static final long FLAGS_PREPARING = 0x2; 23 | public static final long FLAGS_PREPARED = 0x4; 24 | public static final long FLAGS_COMMITTING = 0x8; 25 | public static final long FLAGS_COMMITTED = 0x10; 26 | public static final long FLAGS_ROLLINGBACK = 0x20; 27 | public static final long FLAGS_ROLEDBACK = 0x40; 28 | public static final long FLAGS_ERROR = 0x80; 29 | public static final long FLAGS_ERROR_TOTAL = 0x100; 30 | 31 | public void fireBeginTransaction(TransactionImpl transaction); 32 | 33 | public void firePreparingTransaction(TransactionImpl transaction); 34 | 35 | public void firePreparedTransaction(TransactionImpl transaction); 36 | 37 | public void fireCommittingTransaction(TransactionImpl transaction); 38 | 39 | public void fireCommittedTransaction(TransactionImpl transaction); 40 | 41 | public void fireRollingBackTransaction(TransactionImpl transaction); 42 | 43 | public void fireRolledbackTransaction(TransactionImpl transaction); 44 | 45 | public void fireCompleteFailure(TransactionImpl transaction); 46 | 47 | public void fireCleanupTransaction(TransactionImpl transaction); 48 | 49 | public void fireRecoverTransaction(TransactionImpl transaction); 50 | 51 | } 52 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/TransactionTimer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports; 17 | 18 | import org.bytesoft.transaction.Transaction; 19 | 20 | public interface TransactionTimer { 21 | 22 | public void timingExecution(); 23 | 24 | public void stopTiming(Transaction tx); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/resource/XAResourceDescriptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports.resource; 17 | 18 | import javax.transaction.xa.XAResource; 19 | import javax.transaction.xa.Xid; 20 | 21 | public interface XAResourceDescriptor extends XAResource { 22 | 23 | public void setIdentifier(String identifier); 24 | 25 | public String getIdentifier(); 26 | 27 | public XAResource getDelegate(); 28 | 29 | public void setTransactionTimeoutQuietly(int timeout); 30 | 31 | public boolean isTransactionCommitted(Xid xid) throws IllegalStateException; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/rpc/TransactionInterceptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports.rpc; 17 | 18 | public interface TransactionInterceptor { 19 | 20 | public void beforeSendRequest(TransactionRequest request) throws IllegalStateException; 21 | 22 | public void beforeSendResponse(TransactionResponse response) throws IllegalStateException; 23 | 24 | public void afterReceiveRequest(TransactionRequest request) throws IllegalStateException; 25 | 26 | public void afterReceiveResponse(TransactionResponse response) throws IllegalStateException; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/rpc/TransactionRequest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports.rpc; 17 | 18 | import org.bytesoft.transaction.TransactionContext; 19 | import org.bytesoft.transaction.remote.RemoteCoordinator; 20 | 21 | public interface TransactionRequest { 22 | 23 | public RemoteCoordinator getTargetTransactionCoordinator(); 24 | 25 | public TransactionContext getTransactionContext(); 26 | 27 | public void setTransactionContext(TransactionContext transactionContext); 28 | 29 | public Object getHeader(String name); 30 | 31 | public void setHeader(String name, Object value); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/rpc/TransactionResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports.rpc; 17 | 18 | import org.bytesoft.transaction.TransactionContext; 19 | import org.bytesoft.transaction.remote.RemoteCoordinator; 20 | 21 | public interface TransactionResponse { 22 | 23 | public boolean isParticipantStickyRequired(); 24 | 25 | public boolean isParticipantRollbackOnly(); 26 | 27 | public RemoteCoordinator getSourceTransactionCoordinator(); 28 | 29 | public TransactionContext getTransactionContext(); 30 | 31 | public void setTransactionContext(TransactionContext transactionContext); 32 | 33 | public Object getHeader(String name); 34 | 35 | public void setHeader(String name, Object value); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/supports/serialize/XAResourceDeserializer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.supports.serialize; 17 | 18 | import org.bytesoft.transaction.supports.resource.XAResourceDescriptor; 19 | 20 | public interface XAResourceDeserializer { 21 | 22 | public XAResourceDescriptor deserialize(String identifier); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/work/SimpleWork.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.work; 17 | 18 | import javax.resource.spi.work.Work; 19 | import javax.resource.spi.work.WorkEvent; 20 | import javax.resource.spi.work.WorkListener; 21 | 22 | public class SimpleWork implements Runnable { 23 | 24 | private Object source; 25 | private Work work; 26 | private WorkListener workListener; 27 | 28 | public void run() { 29 | this.workListener.workStarted(new WorkEvent(this.source, WorkEvent.WORK_STARTED, this.work, null)); 30 | this.work.run(); 31 | this.workListener.workCompleted(new WorkEvent(this.source, WorkEvent.WORK_COMPLETED, this.work, null)); 32 | } 33 | 34 | public void setSource(Object source) { 35 | this.source = source; 36 | } 37 | 38 | public void setWork(Work work) { 39 | this.work = work; 40 | } 41 | 42 | public void setWorkListener(WorkListener workListener) { 43 | this.workListener = workListener; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/work/SimpleWorkListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.work; 17 | 18 | import java.util.concurrent.locks.Condition; 19 | import java.util.concurrent.locks.Lock; 20 | import java.util.concurrent.locks.ReentrantLock; 21 | 22 | import javax.resource.spi.work.WorkEvent; 23 | import javax.resource.spi.work.WorkListener; 24 | 25 | import org.slf4j.Logger; 26 | import org.slf4j.LoggerFactory; 27 | 28 | public class SimpleWorkListener implements WorkListener { 29 | static final Logger logger = LoggerFactory.getLogger(SimpleWorkListener.class); 30 | 31 | private long acceptedTime = -1; 32 | private long startedTime = -1; 33 | 34 | private final WorkListener delegate; 35 | private final Lock lock = new ReentrantLock(); 36 | private final Condition condition = this.lock.newCondition(); 37 | 38 | public SimpleWorkListener(WorkListener workListener) { 39 | this.delegate = workListener; 40 | } 41 | 42 | public long waitForStart() { 43 | try { 44 | this.lock.lock(); 45 | while (this.acceptedTime < 0 || this.startedTime < 0) { 46 | try { 47 | this.condition.await(); 48 | } catch (InterruptedException ex) { 49 | logger.debug(ex.getMessage()); 50 | } 51 | } 52 | return this.startedTime - this.acceptedTime; 53 | } finally { 54 | this.lock.unlock(); 55 | } 56 | } 57 | 58 | public void signalStarted() { 59 | try { 60 | this.lock.lock(); 61 | this.condition.signalAll(); 62 | } finally { 63 | this.lock.unlock(); 64 | } 65 | } 66 | 67 | public void workAccepted(WorkEvent event) { 68 | if (this.delegate != null) { 69 | delegate.workAccepted(event); 70 | } 71 | this.acceptedTime = System.currentTimeMillis(); 72 | } 73 | 74 | public void workCompleted(WorkEvent event) { 75 | if (this.delegate != null) { 76 | delegate.workCompleted(event); 77 | } 78 | } 79 | 80 | public void workRejected(WorkEvent event) { 81 | if (this.delegate != null) { 82 | delegate.workRejected(event); 83 | } 84 | } 85 | 86 | public void workStarted(WorkEvent event) { 87 | if (this.delegate != null) { 88 | delegate.workStarted(event); 89 | } 90 | this.startedTime = System.currentTimeMillis(); 91 | this.signalStarted(); 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/work/SimpleWorkManager.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.work; 17 | 18 | import java.util.concurrent.CancellationException; 19 | import java.util.concurrent.ExecutionException; 20 | import java.util.concurrent.Future; 21 | import java.util.concurrent.LinkedBlockingQueue; 22 | import java.util.concurrent.ThreadPoolExecutor; 23 | import java.util.concurrent.TimeUnit; 24 | 25 | import javax.resource.spi.work.ExecutionContext; 26 | import javax.resource.spi.work.Work; 27 | import javax.resource.spi.work.WorkEvent; 28 | import javax.resource.spi.work.WorkException; 29 | import javax.resource.spi.work.WorkListener; 30 | import javax.resource.spi.work.WorkManager; 31 | 32 | import org.slf4j.Logger; 33 | import org.slf4j.LoggerFactory; 34 | 35 | public class SimpleWorkManager implements WorkManager { 36 | static final Logger logger = LoggerFactory.getLogger(SimpleWorkManager.class); 37 | 38 | private final ThreadPoolExecutor executor = new ThreadPoolExecutor(5, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, 39 | new LinkedBlockingQueue()); 40 | // private final ScheduledThreadPoolExecutor scheduled = new ScheduledThreadPoolExecutor(1); 41 | 42 | public void doWork(Work work) throws WorkException { 43 | this.doWork(work, 1800 * 1000L, null, null); 44 | } 45 | 46 | public void doWork(Work work, long startTimeout, ExecutionContext execContext, WorkListener workListener) 47 | throws WorkException { 48 | SimpleWorkListener wrappedListener = new SimpleWorkListener(workListener); 49 | wrappedListener.workAccepted(new WorkEvent(this, WorkEvent.WORK_ACCEPTED, work, null)); 50 | SimpleWork task = new SimpleWork(); 51 | task.setWork(work); 52 | task.setWorkListener(wrappedListener); 53 | Future future = this.executor.submit(task); 54 | try { 55 | future.get(); 56 | } catch (CancellationException ex) { 57 | wrappedListener.workCompleted(new WorkEvent(this, WorkEvent.WORK_REJECTED, work, new WorkException(ex))); 58 | } catch (InterruptedException ex) { 59 | wrappedListener.workCompleted(new WorkEvent(this, WorkEvent.WORK_COMPLETED, work, new WorkException(ex))); 60 | } catch (ExecutionException ex) { 61 | wrappedListener.workCompleted(new WorkEvent(this, WorkEvent.WORK_COMPLETED, work, new WorkException(ex))); 62 | } 63 | } 64 | 65 | public long startWork(Work work) throws WorkException { 66 | return this.startWork(work, 1800 * 1000L, null, null); 67 | } 68 | 69 | public long startWork(Work work, long startTimeout, ExecutionContext execContext, WorkListener workListener) 70 | throws WorkException { 71 | SimpleWorkListener wrappedListener = new SimpleWorkListener(workListener); 72 | wrappedListener.workAccepted(new WorkEvent(this, WorkEvent.WORK_ACCEPTED, work, null)); 73 | SimpleWork task = new SimpleWork(); 74 | task.setSource(this); 75 | task.setWork(work); 76 | task.setWorkListener(wrappedListener); 77 | this.executor.submit(task); 78 | return wrappedListener.waitForStart(); 79 | } 80 | 81 | public void scheduleWork(Work work) throws WorkException { 82 | throw new WorkException("not supported yet!"); 83 | } 84 | 85 | public void scheduleWork(Work work, long startTimeout, ExecutionContext execContext, WorkListener workListener) 86 | throws WorkException { 87 | // SimpleWorkListener wrappedListener = new SimpleWorkListener(workListener); 88 | // wrappedListener.workAccepted(new WorkEvent(this, WorkEvent.WORK_ACCEPTED, work, null)); 89 | // SimpleWork task = new SimpleWork(); 90 | // task.setSource(this); 91 | // task.setWork(work); 92 | // task.setWorkListener(wrappedListener); 93 | // // ScheduledFuture future = 94 | // this.scheduled.scheduleAtFixedRate(task, 0, 1000, TimeUnit.MILLISECONDS); 95 | throw new WorkException("not supported yet!"); 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /bytejta-core/src/main/java/org/bytesoft/transaction/xa/XidFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.transaction.xa; 17 | 18 | public interface XidFactory { 19 | public static final int JTA_FORMAT_ID = 1207; 20 | public static final int TCC_FORMAT_ID = 8127; 21 | 22 | public static final int GLOBAL_TRANSACTION_LENGTH = 16; 23 | public static final int BRANCH_QUALIFIER_LENGTH = 16; 24 | 25 | public TransactionXid createGlobalXid(); 26 | 27 | public TransactionXid createGlobalXid(byte[] globalTransactionId); 28 | 29 | public TransactionXid createBranchXid(TransactionXid globalXid); 30 | 31 | public TransactionXid createBranchXid(TransactionXid globalXid, byte[] branchQualifier); 32 | } 33 | -------------------------------------------------------------------------------- /bytejta-supports-dubbo/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | org.bytesoft 7 | bytejta-parent 8 | 0.5.0-BETA9 9 | 10 | 11 | bytejta-supports-dubbo 12 | jar 13 | 14 | bytejta-supports-dubbo 15 | http://www.bytesoft.org 16 | 17 | 18 | UTF-8 19 | 20 | 21 | 22 | 23 | org.bytesoft 24 | bytejta-supports 25 | 26 | 27 | com.alibaba 28 | dubbo 29 | 30 | 31 | org.springframework 32 | spring 33 | 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-autoconfigure 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /bytejta-supports-dubbo/src/main/java/org/bytesoft/bytejta/supports/dubbo/InvocationContextRegistry.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.dubbo; 17 | 18 | import java.util.Map; 19 | import java.util.concurrent.ConcurrentHashMap; 20 | 21 | import org.bytesoft.transaction.remote.RemoteNode; 22 | 23 | public final class InvocationContextRegistry { 24 | private static final InvocationContextRegistry instance = new InvocationContextRegistry(); 25 | 26 | private final Map contexts = new ConcurrentHashMap(); 27 | 28 | private InvocationContextRegistry() { 29 | if (instance != null) { 30 | throw new IllegalStateException(); 31 | } 32 | } 33 | 34 | public static InvocationContextRegistry getInstance() { 35 | return instance; 36 | } 37 | 38 | public void associateInvocationContext(RemoteNode context) { 39 | this.contexts.put(Thread.currentThread(), context); 40 | } 41 | 42 | public RemoteNode desociateInvocationContext() { 43 | return this.contexts.remove(Thread.currentThread()); 44 | } 45 | 46 | public RemoteNode getInvocationContext() { 47 | return this.contexts.get(Thread.currentThread()); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /bytejta-supports-dubbo/src/main/java/org/bytesoft/bytejta/supports/dubbo/config/DubboSupportConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.dubbo.config; 17 | 18 | import javax.transaction.UserTransaction; 19 | 20 | import org.bytesoft.bytejta.supports.resource.properties.ConnectorResourcePropertySourceFactory; 21 | import org.bytesoft.transaction.TransactionManager; 22 | import org.slf4j.Logger; 23 | import org.slf4j.LoggerFactory; 24 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 25 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 26 | import org.springframework.context.ApplicationContext; 27 | import org.springframework.context.ApplicationContextAware; 28 | import org.springframework.context.EnvironmentAware; 29 | import org.springframework.context.annotation.EnableAspectJAutoProxy; 30 | import org.springframework.context.annotation.ImportResource; 31 | import org.springframework.context.annotation.PropertySource; 32 | import org.springframework.core.env.Environment; 33 | import org.springframework.transaction.PlatformTransactionManager; 34 | import org.springframework.transaction.annotation.EnableTransactionManagement; 35 | import org.springframework.transaction.annotation.TransactionManagementConfigurer; 36 | import org.springframework.transaction.jta.JtaTransactionManager; 37 | 38 | @PropertySource(value = "bytejta:connector.config", factory = ConnectorResourcePropertySourceFactory.class) 39 | @ImportResource({ "classpath:bytejta-supports-dubbo.xml" }) 40 | @EnableAspectJAutoProxy(proxyTargetClass = true) 41 | @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) 42 | @EnableTransactionManagement 43 | public class DubboSupportConfiguration implements TransactionManagementConfigurer, ApplicationContextAware, EnvironmentAware { 44 | static final Logger logger = LoggerFactory.getLogger(DubboSupportConfiguration.class); 45 | 46 | private Environment environment; 47 | private ApplicationContext applicationContext; 48 | 49 | public PlatformTransactionManager annotationDrivenTransactionManager() { 50 | JtaTransactionManager jtaTransactionManager = new JtaTransactionManager(); 51 | jtaTransactionManager.setTransactionManager(this.applicationContext.getBean(TransactionManager.class)); 52 | jtaTransactionManager.setUserTransaction(this.applicationContext.getBean(UserTransaction.class)); 53 | return jtaTransactionManager; 54 | } 55 | 56 | public Environment getEnvironment() { 57 | return environment; 58 | } 59 | 60 | public void setEnvironment(Environment environment) { 61 | this.environment = environment; 62 | } 63 | 64 | public ApplicationContext getApplicationContext() { 65 | return applicationContext; 66 | } 67 | 68 | public void setApplicationContext(ApplicationContext applicationContext) { 69 | this.applicationContext = applicationContext; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /bytejta-supports-dubbo/src/main/java/org/bytesoft/bytejta/supports/dubbo/ext/ILoadBalancer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.dubbo.ext; 17 | 18 | import java.util.List; 19 | 20 | import com.alibaba.dubbo.common.URL; 21 | import com.alibaba.dubbo.common.extension.SPI; 22 | import com.alibaba.dubbo.rpc.Invocation; 23 | import com.alibaba.dubbo.rpc.Invoker; 24 | import com.alibaba.dubbo.rpc.RpcException; 25 | 26 | @SPI 27 | public interface ILoadBalancer { 28 | 29 | public Invoker select(List> invokers, URL url, Invocation invocation) throws RpcException; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /bytejta-supports-dubbo/src/main/java/org/bytesoft/bytejta/supports/dubbo/spi/TransactionLoadBalancer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.dubbo.spi; 17 | 18 | import java.util.List; 19 | import java.util.Random; 20 | 21 | import org.bytesoft.bytejta.supports.dubbo.ext.ILoadBalancer; 22 | 23 | import com.alibaba.dubbo.common.URL; 24 | import com.alibaba.dubbo.rpc.Invocation; 25 | import com.alibaba.dubbo.rpc.Invoker; 26 | import com.alibaba.dubbo.rpc.RpcException; 27 | 28 | public class TransactionLoadBalancer implements ILoadBalancer { 29 | static final Random random = new Random(); 30 | 31 | public Invoker select(List> invokers, URL url, Invocation invocation) throws RpcException { 32 | if (invokers == null || invokers.isEmpty()) { 33 | throw new RpcException("No invoker is found!"); 34 | } 35 | 36 | int lengthOfInvokerList = invokers == null ? 0 : invokers.size(); 37 | return invokers.get(random.nextInt(lengthOfInvokerList)); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /bytejta-supports-dubbo/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.Filter: -------------------------------------------------------------------------------- 1 | bytejta=org.bytesoft.bytejta.supports.dubbo.spi.TransactionServiceFilter -------------------------------------------------------------------------------- /bytejta-supports-dubbo/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.cluster.LoadBalance: -------------------------------------------------------------------------------- 1 | bytejta=org.bytesoft.bytejta.supports.dubbo.spi.TransactionLoadBalance -------------------------------------------------------------------------------- /bytejta-supports-dubbo/src/main/resources/META-INF/dubbo/org.bytesoft.bytejta.supports.dubbo.ext.ILoadBalancer: -------------------------------------------------------------------------------- 1 | default=org.bytesoft.bytejta.supports.dubbo.spi.TransactionLoadBalancer -------------------------------------------------------------------------------- /bytejta-supports-dubbo/src/main/resources/bytejta-supports-dubbo.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/SpringCloudEndpointPostProcessor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | import org.bytesoft.common.utils.CommonUtils; 22 | import org.bytesoft.transaction.TransactionBeanFactory; 23 | import org.bytesoft.transaction.aware.TransactionBeanFactoryAware; 24 | import org.bytesoft.transaction.aware.TransactionEndpointAware; 25 | import org.slf4j.Logger; 26 | import org.slf4j.LoggerFactory; 27 | import org.springframework.beans.BeansException; 28 | import org.springframework.beans.MutablePropertyValues; 29 | import org.springframework.beans.factory.config.BeanDefinition; 30 | import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 31 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 32 | import org.springframework.context.EnvironmentAware; 33 | import org.springframework.core.env.Environment; 34 | 35 | public class SpringCloudEndpointPostProcessor 36 | implements BeanFactoryPostProcessor, TransactionBeanFactoryAware, EnvironmentAware { 37 | static final Logger logger = LoggerFactory.getLogger(SpringCloudEndpointPostProcessor.class); 38 | 39 | @javax.inject.Inject 40 | private TransactionBeanFactory beanFactory; 41 | private Environment environment; 42 | 43 | public void setEnvironment(Environment environment) { 44 | this.environment = environment; 45 | } 46 | 47 | public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 48 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); 49 | 50 | List beanDefList = new ArrayList(); 51 | String[] beanNameArray = beanFactory.getBeanDefinitionNames(); 52 | for (int i = 0; i < beanNameArray.length; i++) { 53 | String beanName = beanNameArray[i]; 54 | BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName); 55 | String beanClassName = beanDef.getBeanClassName(); 56 | 57 | Class beanClass = null; 58 | try { 59 | beanClass = cl.loadClass(beanClassName); 60 | } catch (Exception ex) { 61 | logger.debug("Cannot load class {}, beanId= {}!", beanClassName, beanName, ex); 62 | continue; 63 | } 64 | 65 | if (TransactionEndpointAware.class.isAssignableFrom(beanClass)) { 66 | beanDefList.add(beanDef); 67 | } 68 | } 69 | 70 | String host = CommonUtils.getInetAddress(); 71 | String name = this.environment.getProperty("spring.application.name"); 72 | String port = this.environment.getProperty("server.port"); 73 | String identifier = String.format("%s:%s:%s", host, name, port); 74 | 75 | for (int i = 0; i < beanDefList.size(); i++) { 76 | BeanDefinition beanDef = beanDefList.get(i); 77 | MutablePropertyValues mpv = beanDef.getPropertyValues(); 78 | mpv.addPropertyValue(TransactionEndpointAware.ENDPOINT_FIELD_NAME, identifier); 79 | } 80 | 81 | } 82 | 83 | public TransactionBeanFactory getBeanFactory() { 84 | return beanFactory; 85 | } 86 | 87 | public void setBeanFactory(TransactionBeanFactory beanFactory) { 88 | this.beanFactory = beanFactory; 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/dbcp/CommonDBCPXADataSourceWrapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.dbcp; 17 | 18 | import javax.sql.DataSource; 19 | import javax.sql.XADataSource; 20 | 21 | import org.apache.commons.dbcp2.managed.BasicManagedDataSource; 22 | import org.bytesoft.transaction.TransactionBeanFactory; 23 | import org.bytesoft.transaction.TransactionManager; 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.boot.jdbc.XADataSourceWrapper; 26 | 27 | public class CommonDBCPXADataSourceWrapper implements XADataSourceWrapper { 28 | @Autowired 29 | private TransactionBeanFactory beanFactory; 30 | 31 | public DataSource wrapDataSource(XADataSource xaDataSource) throws Exception { 32 | TransactionManager transactionManager = this.beanFactory.getTransactionManager(); 33 | 34 | BasicManagedDataSource bds = new BasicManagedDataSource(); 35 | bds.setXaDataSourceInstance(xaDataSource); 36 | bds.setTransactionManager(transactionManager); 37 | 38 | return bds; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/feign/TransactionClientRegistry.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.feign; 17 | 18 | import java.util.HashSet; 19 | import java.util.Set; 20 | 21 | public class TransactionClientRegistry { 22 | private static final TransactionClientRegistry instance = new TransactionClientRegistry(); 23 | 24 | private final Set clients = new HashSet(); 25 | 26 | private TransactionClientRegistry() { 27 | if (instance != null) { 28 | throw new IllegalStateException(); 29 | } 30 | } 31 | 32 | public static TransactionClientRegistry getInstance() { 33 | return instance; 34 | } 35 | 36 | public void registerClient(String name) { 37 | this.clients.add(name); 38 | } 39 | 40 | public void unRegisterClient(String name) { 41 | this.clients.remove(name); 42 | } 43 | 44 | public boolean containsClient(String name) { 45 | return this.clients.contains(name); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/feign/TransactionFeignContract.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.feign; 17 | 18 | import java.util.List; 19 | 20 | import org.springframework.beans.BeansException; 21 | import org.springframework.beans.factory.InitializingBean; 22 | import org.springframework.cloud.openfeign.support.SpringMvcContract; 23 | import org.springframework.context.ApplicationContext; 24 | import org.springframework.context.ApplicationContextAware; 25 | 26 | import feign.MethodMetadata; 27 | 28 | public class TransactionFeignContract implements feign.Contract, InitializingBean, ApplicationContextAware { 29 | private ApplicationContext applicationContext; 30 | private feign.Contract delegate; 31 | 32 | public TransactionFeignContract() { 33 | } 34 | 35 | public TransactionFeignContract(feign.Contract contract) { 36 | this.delegate = contract; 37 | } 38 | 39 | public void afterPropertiesSet() throws Exception { 40 | if (this.delegate == null) { 41 | this.invokeAfterPropertiesSet(); 42 | } // end-if (this.delegate == null) 43 | } 44 | 45 | public void invokeAfterPropertiesSet() throws Exception { 46 | feign.Contract feignContract = null; 47 | 48 | String[] beanNameArray = this.applicationContext.getBeanNamesForType(feign.Contract.class); 49 | for (int i = 0; beanNameArray != null && i < beanNameArray.length; i++) { 50 | String beanName = beanNameArray[i]; 51 | Object beanInst = this.applicationContext.getBean(beanName); 52 | if (TransactionFeignContract.class.isInstance(beanInst)) { 53 | continue; 54 | } else if (feignContract != null) { 55 | throw new RuntimeException("There are more than one feign.Contract exists!"); 56 | } else { 57 | feignContract = (feign.Contract) beanInst; 58 | } 59 | } 60 | 61 | if (feignContract == null) { 62 | feignContract = new SpringMvcContract(); 63 | } // end-if (feignContract == null) 64 | 65 | this.delegate = feignContract; 66 | } 67 | 68 | public List parseAndValidatateMetadata(Class targetType) { 69 | List metas = this.delegate.parseAndValidatateMetadata(targetType); 70 | for (int i = 0; i < metas.size(); i++) { 71 | MethodMetadata meta = metas.get(i); 72 | if (meta.returnType() == void.class) { 73 | meta.returnType(String.class); 74 | } 75 | } 76 | return metas; 77 | } 78 | 79 | public feign.Contract getDelegate() { 80 | return delegate; 81 | } 82 | 83 | public void setDelegate(feign.Contract delegate) { 84 | this.delegate = delegate; 85 | } 86 | 87 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 88 | this.applicationContext = applicationContext; 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/feign/TransactionFeignInterceptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.feign; 17 | 18 | import java.io.IOException; 19 | import java.util.Base64; 20 | import java.util.Collection; 21 | import java.util.Map; 22 | 23 | import org.bytesoft.bytejta.supports.springcloud.SpringCloudBeanRegistry; 24 | import org.bytesoft.common.utils.SerializeUtils; 25 | import org.bytesoft.transaction.Transaction; 26 | import org.bytesoft.transaction.TransactionBeanFactory; 27 | import org.bytesoft.transaction.TransactionContext; 28 | import org.bytesoft.transaction.TransactionManager; 29 | import org.bytesoft.transaction.aware.TransactionEndpointAware; 30 | import org.springframework.beans.BeansException; 31 | import org.springframework.context.ApplicationContext; 32 | import org.springframework.context.ApplicationContextAware; 33 | 34 | public class TransactionFeignInterceptor 35 | implements feign.RequestInterceptor, TransactionEndpointAware, ApplicationContextAware { 36 | static final String HEADER_TRANCACTION_KEY = "X-BYTEJTA-TRANSACTION"; // org.bytesoft.bytejta.transaction 37 | static final String HEADER_PROPAGATION_KEY = "X-BYTEJTA-PROPAGATION"; // org.bytesoft.bytejta.propagation 38 | 39 | private String identifier; 40 | private ApplicationContext applicationContext; 41 | 42 | public void apply(feign.RequestTemplate template) { 43 | final SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance(); 44 | TransactionBeanFactory beanFactory = beanRegistry.getBeanFactory(); 45 | TransactionManager transactionManager = beanFactory.getTransactionManager(); 46 | Transaction transaction = transactionManager.getTransactionQuietly(); 47 | if (transaction == null) { 48 | return; 49 | } 50 | 51 | try { 52 | TransactionContext transactionContext = transaction.getTransactionContext(); 53 | byte[] byteArray = SerializeUtils.serializeObject(transactionContext); 54 | 55 | String transactionText = Base64.getEncoder().encodeToString(byteArray); 56 | 57 | Map> headers = template.headers(); 58 | if (headers.containsKey(HEADER_TRANCACTION_KEY) == false) { 59 | template.header(HEADER_TRANCACTION_KEY, transactionText); 60 | } 61 | 62 | if (headers.containsKey(HEADER_PROPAGATION_KEY) == false) { 63 | template.header(HEADER_PROPAGATION_KEY, identifier); 64 | } 65 | 66 | } catch (IOException ex) { 67 | throw new RuntimeException("Error occurred while preparing the transaction context!", ex); 68 | } 69 | } 70 | 71 | public String getEndpoint() { 72 | return this.identifier; 73 | } 74 | 75 | public void setEndpoint(String identifier) { 76 | this.identifier = identifier; 77 | } 78 | 79 | public ApplicationContext getApplicationContext() { 80 | return applicationContext; 81 | } 82 | 83 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 84 | this.applicationContext = applicationContext; 85 | } 86 | 87 | } -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/hystrix/TransactionHystrixFallbackFactoryHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.hystrix; 17 | 18 | import java.lang.reflect.InvocationHandler; 19 | import java.lang.reflect.Method; 20 | import java.lang.reflect.Proxy; 21 | 22 | import feign.hystrix.FallbackFactory; 23 | 24 | public class TransactionHystrixFallbackFactoryHandler implements InvocationHandler { 25 | private final FallbackFactory fallbackFactory; 26 | private final Class fallbackType; 27 | 28 | public TransactionHystrixFallbackFactoryHandler(FallbackFactory fallbackFactory, Class fallbackType) { 29 | this.fallbackFactory = fallbackFactory; 30 | this.fallbackType = fallbackType; 31 | } 32 | 33 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 34 | Object fallback = method.invoke(this.fallbackFactory, args); 35 | TransactionHystrixFallbackHandler fallbackHandler = new TransactionHystrixFallbackHandler(fallback); 36 | ClassLoader classLoader = fallback.getClass().getClassLoader(); 37 | Class[] interfaces = new Class[] { TransactionHystrixInvocationHandler.class, this.fallbackType }; 38 | return Proxy.newProxyInstance(classLoader, interfaces, fallbackHandler); 39 | } 40 | 41 | public FallbackFactory getFallbackFactory() { 42 | return fallbackFactory; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/hystrix/TransactionHystrixFallbackHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.hystrix; 17 | 18 | import java.lang.reflect.InvocationHandler; 19 | import java.lang.reflect.Method; 20 | 21 | public class TransactionHystrixFallbackHandler implements InvocationHandler { 22 | private final Object fallback; 23 | 24 | public TransactionHystrixFallbackHandler(Object fallback) { 25 | this.fallback = fallback; 26 | } 27 | 28 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 29 | TransactionHystrixInvocation invocation = (TransactionHystrixInvocation) args[0]; 30 | Method targetMethod = invocation.getMethod(); // (Method) args[1]; 31 | Object[] targetArgs = invocation.getArgs(); // (Object[]) args[2]; 32 | return targetMethod.invoke(this.fallback, targetArgs); 33 | } 34 | 35 | public Object getFallback() { 36 | return fallback; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/hystrix/TransactionHystrixFeignHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.hystrix; 17 | 18 | import java.lang.reflect.InvocationHandler; 19 | import java.lang.reflect.Method; 20 | 21 | import org.bytesoft.bytejta.TransactionImpl; 22 | import org.bytesoft.bytejta.supports.springcloud.SpringCloudBeanRegistry; 23 | import org.bytesoft.transaction.TransactionBeanFactory; 24 | import org.bytesoft.transaction.TransactionManager; 25 | import org.slf4j.Logger; 26 | import org.slf4j.LoggerFactory; 27 | 28 | public class TransactionHystrixFeignHandler implements InvocationHandler { 29 | static final Logger logger = LoggerFactory.getLogger(TransactionHystrixFeignHandler.class); 30 | 31 | private InvocationHandler delegate; 32 | 33 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 34 | if (Object.class.equals(method.getDeclaringClass())) { 35 | return this.delegate.invoke(proxy, method, args); 36 | } else { 37 | final SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance(); 38 | TransactionBeanFactory beanFactory = beanRegistry.getBeanFactory(); 39 | TransactionManager transactionManager = beanFactory.getTransactionManager(); 40 | 41 | TransactionImpl transaction = // 42 | (TransactionImpl) transactionManager.getTransactionQuietly(); 43 | if (transaction == null) { 44 | return this.delegate.invoke(proxy, method, args); 45 | } else { 46 | Method targetMethod = TransactionHystrixInvocationHandler.class.getDeclaredMethod( 47 | TransactionHystrixBeanPostProcessor.HYSTRIX_INVOKER_NAME, 48 | new Class[] { TransactionHystrixInvocation.class }); 49 | TransactionHystrixInvocation invocation = new TransactionHystrixInvocation(); 50 | invocation.setThread(Thread.currentThread()); 51 | invocation.setMethod(method); 52 | invocation.setArgs(args); 53 | Object[] targetArgs = new Object[] { invocation }; 54 | return this.delegate.invoke(proxy, targetMethod, targetArgs); 55 | } 56 | } 57 | } 58 | 59 | public InvocationHandler getDelegate() { 60 | return delegate; 61 | } 62 | 63 | public void setDelegate(InvocationHandler delegate) { 64 | this.delegate = delegate; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/hystrix/TransactionHystrixInvocation.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.hystrix; 17 | 18 | import java.lang.reflect.Method; 19 | 20 | public class TransactionHystrixInvocation { 21 | 22 | private Thread thread; 23 | private Method method; 24 | private Object[] args; 25 | 26 | public Thread getThread() { 27 | return thread; 28 | } 29 | 30 | public void setThread(Thread thread) { 31 | this.thread = thread; 32 | } 33 | 34 | public Method getMethod() { 35 | return method; 36 | } 37 | 38 | public void setMethod(Method method) { 39 | this.method = method; 40 | } 41 | 42 | public Object[] getArgs() { 43 | return args; 44 | } 45 | 46 | public void setArgs(Object[] args) { 47 | this.args = args; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/hystrix/TransactionHystrixInvocationHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.hystrix; 17 | 18 | public interface TransactionHystrixInvocationHandler { 19 | 20 | public Object invoke(TransactionHystrixInvocation invocation); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/loadbalancer/TransactionLoadBalancerInterceptor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.loadbalancer; 17 | 18 | import java.util.List; 19 | 20 | import com.netflix.loadbalancer.Server; 21 | 22 | public interface TransactionLoadBalancerInterceptor { 23 | 24 | public List beforeCompletion(List servers); 25 | 26 | public void afterCompletion(Server server); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/loadbalancer/TransactionLoadBalancerRuleImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.loadbalancer; 17 | 18 | import java.util.List; 19 | 20 | import org.apache.commons.lang3.StringUtils; 21 | import org.bytesoft.bytejta.supports.springcloud.SpringCloudBeanRegistry; 22 | import org.bytesoft.bytejta.supports.springcloud.rule.TransactionRule; 23 | import org.bytesoft.bytejta.supports.springcloud.rule.TransactionRuleImpl; 24 | import org.slf4j.Logger; 25 | import org.slf4j.LoggerFactory; 26 | import org.springframework.core.env.Environment; 27 | 28 | import com.netflix.client.config.IClientConfig; 29 | import com.netflix.loadbalancer.AbstractLoadBalancerRule; 30 | import com.netflix.loadbalancer.ILoadBalancer; 31 | import com.netflix.loadbalancer.IRule; 32 | import com.netflix.loadbalancer.Server; 33 | 34 | public class TransactionLoadBalancerRuleImpl extends AbstractLoadBalancerRule implements IRule { 35 | static final String CONSTANT_RULE_KEY = "org.bytesoft.bytejta.NFTransactionRuleClassName"; 36 | static Logger logger = LoggerFactory.getLogger(TransactionLoadBalancerRuleImpl.class); 37 | 38 | static Class transactionRuleClass; 39 | 40 | private IClientConfig clientConfig; 41 | 42 | public Server choose(Object key) { 43 | SpringCloudBeanRegistry registry = SpringCloudBeanRegistry.getInstance(); 44 | TransactionLoadBalancerInterceptor interceptor = registry.getLoadBalancerInterceptor(); 45 | 46 | if (transactionRuleClass == null) { 47 | Environment environment = registry.getEnvironment(); 48 | String clazzName = environment.getProperty(CONSTANT_RULE_KEY); 49 | if (StringUtils.isNotBlank(clazzName)) { 50 | try { 51 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 52 | transactionRuleClass = classLoader.loadClass(clazzName); 53 | } catch (Exception ex) { 54 | logger.error("Error occurred while loading class {}.", clazzName, ex); 55 | transactionRuleClass = TransactionRuleImpl.class; 56 | } 57 | } else { 58 | transactionRuleClass = TransactionRuleImpl.class; 59 | } 60 | } 61 | 62 | TransactionRule transactionRule = null; 63 | if (TransactionRuleImpl.class.equals(transactionRuleClass)) { 64 | transactionRule = new TransactionRuleImpl(); 65 | } else { 66 | try { 67 | transactionRule = (TransactionRule) transactionRuleClass.newInstance(); 68 | } catch (Exception ex) { 69 | logger.error("Can not create an instance of class {}.", transactionRuleClass.getName(), ex); 70 | transactionRule = new TransactionRuleImpl(); 71 | } 72 | } 73 | transactionRule.initWithNiwsConfig(this.clientConfig); 74 | transactionRule.setLoadBalancer(this.getLoadBalancer()); 75 | 76 | if (interceptor == null) { 77 | return transactionRule.chooseServer(key); 78 | } // end-if (interceptor == null) 79 | 80 | ILoadBalancer loadBalancer = this.getLoadBalancer(); 81 | List servers = loadBalancer.getAllServers(); 82 | 83 | Server server = null; 84 | try { 85 | List serverList = interceptor.beforeCompletion(servers); 86 | 87 | server = transactionRule.chooseServer(key, serverList); 88 | } finally { 89 | interceptor.afterCompletion(server); 90 | } 91 | 92 | return server; 93 | } 94 | 95 | public void initWithNiwsConfig(IClientConfig clientConfig) { 96 | this.clientConfig = clientConfig; 97 | } 98 | 99 | public IClientConfig getClientConfig() { 100 | return clientConfig; 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/property/TransactionPropertySource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.property; 17 | 18 | import org.apache.commons.lang3.StringUtils; 19 | import org.bytesoft.bytejta.supports.springcloud.feign.TransactionClientRegistry; 20 | import org.bytesoft.bytejta.supports.springcloud.loadbalancer.TransactionLoadBalancerRuleImpl; 21 | import org.springframework.core.env.PropertySource; 22 | import org.springframework.core.io.AbstractResource; 23 | import org.springframework.core.io.support.EncodedResource; 24 | 25 | public class TransactionPropertySource extends PropertySource { 26 | 27 | private boolean enabled; 28 | 29 | public TransactionPropertySource(String name, EncodedResource source) { 30 | super(name, source); 31 | 32 | EncodedResource encoded = (EncodedResource) this.getSource(); 33 | AbstractResource resource = (AbstractResource) encoded.getResource(); 34 | String path = resource.getFilename(); 35 | 36 | if (StringUtils.isBlank(path)) { 37 | return; 38 | } 39 | 40 | String[] values = path.split(":"); 41 | if (values.length != 2) { 42 | return; 43 | } 44 | 45 | String protocol = values[0]; 46 | String resName = values[1]; 47 | if ("bytejta".equalsIgnoreCase(protocol) == false) { 48 | return; 49 | } else if ("loadbalancer.config".equalsIgnoreCase(resName) == false) { 50 | return; 51 | } 52 | 53 | this.enabled = true; 54 | 55 | } 56 | 57 | public Object getProperty(String name) { 58 | if (this.enabled == false || name == null || StringUtils.isBlank(name) || name.indexOf(".") < 0) { 59 | return null; 60 | } 61 | 62 | TransactionClientRegistry registry = TransactionClientRegistry.getInstance(); 63 | 64 | int index = name.indexOf("."); 65 | String client = name.substring(0, index); 66 | String suffix = name.substring(index); 67 | if (registry.containsClient(client) && ".ribbon.NFLoadBalancerRuleClassName".equals(suffix)) { 68 | return TransactionLoadBalancerRuleImpl.class.getName(); 69 | } 70 | 71 | return null; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/property/TransactionPropertySourceFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.property; 17 | 18 | import java.io.IOException; 19 | 20 | import org.springframework.core.env.PropertySource; 21 | import org.springframework.core.io.support.EncodedResource; 22 | import org.springframework.core.io.support.PropertySourceFactory; 23 | 24 | public class TransactionPropertySourceFactory implements PropertySourceFactory { 25 | 26 | public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { 27 | if (name == null) { 28 | name = String.format("%s@%s", TransactionPropertySource.class, System.identityHashCode(resource)); 29 | } // end-if (name == null) 30 | 31 | return new TransactionPropertySource(name, resource); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/rule/TransactionRule.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.rule; 17 | 18 | import java.util.List; 19 | 20 | import com.netflix.client.IClientConfigAware; 21 | import com.netflix.loadbalancer.ILoadBalancer; 22 | import com.netflix.loadbalancer.Server; 23 | 24 | public interface TransactionRule extends IClientConfigAware { 25 | 26 | public Server chooseServer(Object key, List serverList); 27 | 28 | public Server chooseServer(Object key); 29 | 30 | public ILoadBalancer getLoadBalancer(); 31 | 32 | public void setLoadBalancer(ILoadBalancer loadBalancer); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/java/org/bytesoft/bytejta/supports/springcloud/rule/TransactionRuleImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.springcloud.rule; 17 | 18 | import java.util.List; 19 | import java.util.Random; 20 | 21 | import com.netflix.client.config.IClientConfig; 22 | import com.netflix.loadbalancer.ILoadBalancer; 23 | import com.netflix.loadbalancer.Server; 24 | 25 | public class TransactionRuleImpl implements TransactionRule { 26 | static final Random RANDOM = new Random(); 27 | private IClientConfig clientConfig; 28 | private ILoadBalancer loadBalancer; 29 | 30 | public void initWithNiwsConfig(IClientConfig clientConfig) { 31 | this.clientConfig = clientConfig; 32 | } 33 | 34 | public Server chooseServer(Object key, List serverList) { 35 | if (serverList == null || serverList.isEmpty()) { 36 | return null; 37 | } else if (serverList.size() == 1) { 38 | return serverList.get(0); 39 | } else { 40 | return serverList.get(RANDOM.nextInt(serverList.size())); 41 | } 42 | } 43 | 44 | public Server chooseServer(Object key) { 45 | List reachableServers = this.loadBalancer.getReachableServers(); 46 | List allServers = this.loadBalancer.getAllServers(); 47 | 48 | if (reachableServers != null && reachableServers.isEmpty() == false) { 49 | return reachableServers.get(RANDOM.nextInt(reachableServers.size())); 50 | } else if (allServers != null && allServers.isEmpty() == false) { 51 | return allServers.get(RANDOM.nextInt(allServers.size())); 52 | } else { 53 | return null; 54 | } 55 | } 56 | 57 | public IClientConfig getClientConfig() { 58 | return clientConfig; 59 | } 60 | 61 | public ILoadBalancer getLoadBalancer() { 62 | return this.loadBalancer; 63 | } 64 | 65 | public void setLoadBalancer(ILoadBalancer loadBalancer) { 66 | this.loadBalancer = loadBalancer; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /bytejta-supports-springcloud/src/main/resources/bytejta-supports-springcloud.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /bytejta-supports/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | 6 | org.bytesoft 7 | bytejta-parent 8 | 0.5.0-BETA9 9 | 10 | 11 | bytejta-supports 12 | jar 13 | 14 | bytejta-supports 15 | The bytejta-supports project is the module of ByteJTA for integrating with third-party open source project. 16 | http://www.bytesoft.org 17 | 18 | 19 | UTF-8 20 | 21 | 22 | 23 | 24 | org.bytesoft 25 | bytejta-core 26 | 27 | 28 | org.springframework 29 | spring-context 30 | 31 | 32 | org.springframework 33 | spring-tx 34 | 35 | 36 | org.springframework 37 | spring-jdbc 38 | 39 | 40 | org.springframework 41 | spring-aop 42 | 43 | 44 | aopalliance 45 | aopalliance 46 | 47 | 48 | cglib 49 | cglib 50 | 51 | 52 | asm 53 | asm 54 | 55 | 56 | 57 | 58 | org.ow2.asm 59 | asm 60 | 61 | 62 | org.aspectj 63 | aspectjweaver 64 | 65 | 66 | org.apache.zookeeper 67 | zookeeper 68 | 69 | 70 | org.apache.curator 71 | curator-recipes 72 | 73 | 74 | org.springframework.boot 75 | spring-boot 76 | 77 | 78 | org.springframework.boot 79 | spring-boot-autoconfigure 80 | 81 | 82 | org.apache.commons 83 | commons-dbcp2 84 | 85 | 86 | org.hibernate 87 | hibernate-core 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/java/org/bytesoft/bytejta/supports/boot/jdbc/DataSourceCciBuilder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.boot.jdbc; 17 | 18 | import java.lang.reflect.InvocationHandler; 19 | import java.lang.reflect.Proxy; 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | import javax.sql.DataSource; 24 | import javax.sql.XADataSource; 25 | 26 | import org.apache.commons.dbcp2.managed.BasicManagedDataSource; 27 | import org.apache.commons.lang3.StringUtils; 28 | import org.bytesoft.bytejta.supports.resource.ManagedConnectionFactoryHandler; 29 | import org.slf4j.Logger; 30 | import org.slf4j.LoggerFactory; 31 | import org.springframework.beans.BeanUtils; 32 | import org.springframework.beans.factory.BeanNameAware; 33 | import org.springframework.boot.context.properties.bind.Bindable; 34 | import org.springframework.boot.context.properties.bind.Binder; 35 | import org.springframework.boot.context.properties.source.ConfigurationPropertyName; 36 | import org.springframework.boot.context.properties.source.ConfigurationPropertyNameAliases; 37 | import org.springframework.boot.context.properties.source.ConfigurationPropertySource; 38 | import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; 39 | 40 | public class DataSourceCciBuilder { 41 | static final Logger logger = LoggerFactory.getLogger(DataSourceCciBuilder.class); 42 | 43 | private XADataSource xaDataSourceInstance; 44 | private final Map properties = new HashMap(); 45 | 46 | private DataSourceCciBuilder(XADataSource xaDataSourceInstance) { 47 | if (xaDataSourceInstance == null) { 48 | throw new IllegalArgumentException("the xaDataSourceInstance cannot be null!"); 49 | } 50 | this.xaDataSourceInstance = xaDataSourceInstance; 51 | } 52 | 53 | @SuppressWarnings("rawtypes") 54 | public static DataSourceCciBuilder create(XADataSource xaDataSourceInstance) { 55 | return new DataSourceCciBuilder(xaDataSourceInstance); 56 | } 57 | 58 | @SuppressWarnings("unchecked") 59 | public DataSource build() { 60 | this.validateXADataSourceInstance(); 61 | 62 | BasicManagedDataSource dataSource = BeanUtils.instantiateClass(BasicManagedDataSource.class); 63 | this.bind(dataSource); 64 | 65 | dataSource.setXaDataSourceInstance(this.xaDataSourceInstance); 66 | 67 | return (T) dataSource; 68 | } 69 | 70 | private void validateXADataSourceInstance() { 71 | InvocationHandler handler = Proxy.isProxyClass(this.xaDataSourceInstance.getClass()) 72 | ? Proxy.getInvocationHandler(this.xaDataSourceInstance) : null; 73 | ManagedConnectionFactoryHandler mcfh = // 74 | (handler == null || ManagedConnectionFactoryHandler.class.isInstance(handler) == false) ? null 75 | : (ManagedConnectionFactoryHandler) handler; 76 | if (BeanNameAware.class.isInstance(this.xaDataSourceInstance) == false 77 | && (mcfh == null || StringUtils.isBlank(mcfh.getIdentifier()))) { 78 | throw new IllegalStateException("XADataSource is not properly configured!"); 79 | } 80 | } 81 | 82 | private void bind(DataSource dataSource) { 83 | ConfigurationPropertySource source = new MapConfigurationPropertySource(this.properties); 84 | ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases(); 85 | Binder binder = new Binder(source.withAliases(aliases)); 86 | binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(dataSource)); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/java/org/bytesoft/bytejta/supports/jpa/hibernate/HibernateJtaPlatform.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.jpa.hibernate; 17 | 18 | import org.bytesoft.bytejta.TransactionBeanFactoryImpl; 19 | import org.bytesoft.transaction.TransactionBeanFactory; 20 | import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform; 21 | 22 | public class HibernateJtaPlatform extends AbstractJtaPlatform { 23 | private static final long serialVersionUID = 1L; 24 | 25 | protected javax.transaction.TransactionManager locateTransactionManager() { 26 | TransactionBeanFactory beanFactory = TransactionBeanFactoryImpl.getInstance(); 27 | return beanFactory == null ? null : beanFactory.getTransactionManager(); 28 | } 29 | 30 | protected javax.transaction.UserTransaction locateUserTransaction() { 31 | return null; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/java/org/bytesoft/bytejta/supports/resource/ManagedConnectionHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.resource; 17 | 18 | import java.lang.reflect.InvocationHandler; 19 | import java.lang.reflect.InvocationTargetException; 20 | import java.lang.reflect.Method; 21 | import java.lang.reflect.Proxy; 22 | 23 | import javax.jms.XASession; 24 | import javax.resource.spi.ManagedConnection; 25 | import javax.transaction.xa.XAResource; 26 | 27 | import org.bytesoft.transaction.supports.resource.XAResourceDescriptor; 28 | 29 | public class ManagedConnectionHandler implements InvocationHandler { 30 | 31 | private final Object delegate; 32 | private String identifier; 33 | 34 | public ManagedConnectionHandler(Object managed) { 35 | this.delegate = managed; 36 | } 37 | 38 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 39 | Class declaringClass = method.getDeclaringClass(); 40 | Class returningClass = method.getReturnType(); 41 | 42 | Object resultObject = null; 43 | try { 44 | resultObject = method.invoke(this.delegate, args); 45 | } catch (InvocationTargetException ex) { 46 | throw ex.getTargetException(); 47 | } catch (IllegalAccessException ex) { 48 | throw new RuntimeException(ex); 49 | } 50 | 51 | if (resultObject != null && XAResourceDescriptor.class.isInstance(resultObject)) { 52 | return resultObject; 53 | } 54 | 55 | CommonResourceDescriptor descriptor = new CommonResourceDescriptor(); 56 | descriptor.setIdentifier(this.identifier); 57 | if (javax.sql.XAConnection.class.equals(declaringClass) && XAResource.class.equals(returningClass)) { 58 | descriptor.setDelegate((XAResource) resultObject); 59 | } else if (javax.jms.XAConnection.class.equals(declaringClass) && XAResource.class.equals(returningClass)) { 60 | descriptor.setDelegate((XAResource) resultObject); 61 | } else if (javax.jms.XAConnection.class.equals(declaringClass) && XASession.class.equals(returningClass)) { 62 | Class clazz = resultObject.getClass(); 63 | ClassLoader cl = clazz.getClassLoader(); 64 | Class[] interfaces = clazz.getInterfaces(); 65 | boolean containsReturningClass = false; 66 | for (int i = 0; i < interfaces.length; i++) { 67 | Class interfaceClass = interfaces[i]; 68 | if (interfaceClass.equals(returningClass)) { 69 | containsReturningClass = true; 70 | break; 71 | } 72 | } // end-for (int i = 0; i < interfaces.length; i++) 73 | 74 | ManagedXASessionHandler interceptor = new ManagedXASessionHandler(resultObject); 75 | interceptor.setIdentifier(this.identifier); 76 | 77 | if (containsReturningClass) { 78 | return Proxy.newProxyInstance(cl, interfaces, interceptor); 79 | } else { 80 | Class[] interfaceArray = new Class[interfaces.length + 1]; 81 | System.arraycopy(interfaces, 0, interfaceArray, 0, interfaces.length); 82 | interfaceArray[interfaces.length] = javax.jms.XASession.class; 83 | return Proxy.newProxyInstance(cl, interfaceArray, interceptor); 84 | } 85 | } else if (ManagedConnection.class.equals(declaringClass) && XAResource.class.equals(returningClass)) { 86 | descriptor.setDelegate((XAResource) resultObject); 87 | } 88 | 89 | return descriptor.getDelegate() != null ? descriptor : resultObject; 90 | 91 | } 92 | 93 | public String getIdentifier() { 94 | return identifier; 95 | } 96 | 97 | public void setIdentifier(String identifier) { 98 | this.identifier = identifier; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/java/org/bytesoft/bytejta/supports/resource/ManagedXASessionHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.resource; 17 | 18 | import java.lang.reflect.InvocationHandler; 19 | import java.lang.reflect.InvocationTargetException; 20 | import java.lang.reflect.Method; 21 | 22 | import javax.transaction.xa.XAResource; 23 | 24 | import org.bytesoft.transaction.supports.resource.XAResourceDescriptor; 25 | 26 | public class ManagedXASessionHandler implements InvocationHandler { 27 | 28 | private final Object delegate; 29 | private String identifier; 30 | 31 | public ManagedXASessionHandler(Object managed) { 32 | this.delegate = managed; 33 | } 34 | 35 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 36 | Class declaringClass = method.getDeclaringClass(); 37 | Class returningClass = method.getReturnType(); 38 | 39 | Object resultObject = null; 40 | try { 41 | resultObject = method.invoke(this.delegate, args); 42 | } catch (InvocationTargetException ex) { 43 | throw ex.getTargetException(); 44 | } catch (IllegalAccessException ex) { 45 | throw new RuntimeException(ex); 46 | } 47 | 48 | if (resultObject != null && XAResourceDescriptor.class.isInstance(resultObject)) { 49 | return resultObject; 50 | } 51 | 52 | CommonResourceDescriptor descriptor = new CommonResourceDescriptor(); 53 | descriptor.setIdentifier(this.identifier); 54 | if (javax.jms.XASession.class.equals(declaringClass) && XAResource.class.equals(returningClass)) { 55 | descriptor.setDelegate((XAResource) resultObject); 56 | } 57 | 58 | return descriptor.getDelegate() != null ? descriptor : resultObject; 59 | 60 | } 61 | 62 | public String getIdentifier() { 63 | return identifier; 64 | } 65 | 66 | public void setIdentifier(String identifier) { 67 | this.identifier = identifier; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/java/org/bytesoft/bytejta/supports/resource/jdbc/XADataSourceImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2017 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.resource.jdbc; 17 | 18 | import java.io.PrintWriter; 19 | import java.sql.SQLException; 20 | import java.sql.SQLFeatureNotSupportedException; 21 | import java.util.logging.Logger; 22 | 23 | import javax.sql.XAConnection; 24 | import javax.sql.XADataSource; 25 | 26 | import org.springframework.beans.factory.BeanNameAware; 27 | 28 | public class XADataSourceImpl implements XADataSource, BeanNameAware { 29 | private String identifier; 30 | private XADataSource xaDataSource; 31 | 32 | public PrintWriter getLogWriter() throws SQLException { 33 | return this.xaDataSource.getLogWriter(); 34 | } 35 | 36 | public void setLogWriter(PrintWriter out) throws SQLException { 37 | this.xaDataSource.setLogWriter(out); 38 | } 39 | 40 | public void setLoginTimeout(int seconds) throws SQLException { 41 | this.xaDataSource.setLoginTimeout(seconds); 42 | } 43 | 44 | public int getLoginTimeout() throws SQLException { 45 | return this.xaDataSource.getLoginTimeout(); 46 | } 47 | 48 | public Logger getParentLogger() throws SQLFeatureNotSupportedException { 49 | return this.xaDataSource.getParentLogger(); 50 | } 51 | 52 | public XAConnection getXAConnection() throws SQLException { 53 | XAConnection delegate = this.xaDataSource.getXAConnection(); 54 | 55 | XAConnectionImpl managed = new XAConnectionImpl(); 56 | managed.setIdentifier(this.identifier); 57 | managed.setDelegate(delegate); 58 | 59 | delegate.addConnectionEventListener(managed); 60 | delegate.addStatementEventListener(managed); 61 | 62 | return managed; 63 | } 64 | 65 | public XAConnection getXAConnection(String user, String password) throws SQLException { 66 | XAConnection delegate = this.xaDataSource.getXAConnection(user, password); 67 | 68 | XAConnectionImpl managed = new XAConnectionImpl(); 69 | managed.setIdentifier(this.identifier); 70 | managed.setDelegate(delegate); 71 | 72 | delegate.addConnectionEventListener(managed); 73 | delegate.addStatementEventListener(managed); 74 | 75 | return managed; 76 | } 77 | 78 | public void setBeanName(String name) { 79 | this.setIdentifier(name); 80 | } 81 | 82 | public String getIdentifier() { 83 | return identifier; 84 | } 85 | 86 | public void setIdentifier(String identifier) { 87 | this.identifier = identifier; 88 | } 89 | 90 | public XADataSource getXaDataSource() { 91 | return xaDataSource; 92 | } 93 | 94 | public void setXaDataSource(XADataSource xaDataSource) { 95 | this.xaDataSource = xaDataSource; 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/java/org/bytesoft/bytejta/supports/resource/properties/ConnectorResourcePropertySource.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.resource.properties; 17 | 18 | import java.util.HashMap; 19 | import java.util.Map; 20 | 21 | import org.apache.commons.lang3.StringUtils; 22 | import org.springframework.core.env.PropertySource; 23 | import org.springframework.core.io.AbstractResource; 24 | import org.springframework.core.io.support.EncodedResource; 25 | 26 | public class ConnectorResourcePropertySource extends PropertySource { 27 | private final Map aliases = new HashMap(); 28 | 29 | private boolean enabled; 30 | 31 | public ConnectorResourcePropertySource(String name, EncodedResource source) { 32 | this(name, source, new HashMap()); 33 | } 34 | 35 | public ConnectorResourcePropertySource(String name, EncodedResource source, Map aliases) { 36 | super(name, source); 37 | this.aliases.putAll(aliases); 38 | 39 | EncodedResource encoded = (EncodedResource) this.getSource(); 40 | AbstractResource resource = (AbstractResource) encoded.getResource(); 41 | String path = resource.getFilename(); 42 | 43 | if (StringUtils.isBlank(path)) { 44 | return; 45 | } 46 | 47 | String[] values = path.split(":"); 48 | if (values.length != 2) { 49 | return; 50 | } 51 | 52 | String protocol = values[0]; 53 | String resName = values[1]; 54 | if ("bytejta".equalsIgnoreCase(protocol) == false) { 55 | return; 56 | } else if ("connector.config".equalsIgnoreCase(resName) == false) { 57 | return; 58 | } 59 | 60 | this.enabled = true; 61 | } 62 | 63 | public Object getProperty(String name) { 64 | if (this.enabled == false) { 65 | return null; 66 | } else if (StringUtils.isBlank(name)) { 67 | return null; 68 | } else if (StringUtils.startsWith(name, "spring.datasource.") == false) { 69 | return null; 70 | } 71 | 72 | int dotIndex = name.lastIndexOf("."); 73 | String prefix = name.substring(0, dotIndex); 74 | String suffix = name.substring(dotIndex + 1); 75 | 76 | String alias = this.aliases.get(suffix); 77 | if (StringUtils.isBlank(alias)) { 78 | return null; 79 | } 80 | 81 | return String.format("${%s.%s}", prefix, alias); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/java/org/bytesoft/bytejta/supports/resource/properties/ConnectorResourcePropertySourceFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.resource.properties; 17 | 18 | import java.io.IOException; 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | import org.springframework.core.env.PropertySource; 23 | import org.springframework.core.io.support.EncodedResource; 24 | import org.springframework.core.io.support.PropertySourceFactory; 25 | 26 | public class ConnectorResourcePropertySourceFactory implements PropertySourceFactory { 27 | static final Map aliases = new HashMap(); 28 | static { 29 | aliases.put("user", "username"); 30 | aliases.put("jdbcUrl", "url"); 31 | } 32 | 33 | public static Map getAliases() { 34 | return aliases; 35 | } 36 | 37 | public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { 38 | if (name == null) { 39 | name = String.format("%s@%s", ConnectorResourcePropertySource.class, System.identityHashCode(resource)); 40 | } // end-if (name == null) 41 | 42 | return new ConnectorResourcePropertySource(name, resource, aliases); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/java/org/bytesoft/bytejta/supports/rpc/TransactionRequestImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.rpc; 17 | 18 | import java.util.HashMap; 19 | import java.util.Map; 20 | 21 | import org.bytesoft.transaction.TransactionContext; 22 | import org.bytesoft.transaction.remote.RemoteCoordinator; 23 | import org.bytesoft.transaction.supports.rpc.TransactionRequest; 24 | 25 | public class TransactionRequestImpl implements TransactionRequest { 26 | 27 | private RemoteCoordinator participantCoordinator; 28 | private TransactionContext transactionContext; 29 | 30 | private transient boolean participantEnlistFlag; 31 | 32 | private final Map headers = new HashMap(); 33 | 34 | public Object getHeader(String name) { 35 | return this.headers.get(name); 36 | } 37 | 38 | public void setHeader(String name, Object value) { 39 | this.headers.put(name, value); 40 | } 41 | 42 | public RemoteCoordinator getTargetTransactionCoordinator() { 43 | return this.participantCoordinator; 44 | } 45 | 46 | public void setTargetTransactionCoordinator(RemoteCoordinator remoteCoordinator) { 47 | this.participantCoordinator = remoteCoordinator; 48 | } 49 | 50 | public TransactionContext getTransactionContext() { 51 | return this.transactionContext; 52 | } 53 | 54 | public void setTransactionContext(TransactionContext transactionContext) { 55 | this.transactionContext = transactionContext; 56 | } 57 | 58 | public boolean isParticipantEnlistFlag() { 59 | return participantEnlistFlag; 60 | } 61 | 62 | public void setParticipantEnlistFlag(boolean participantEnlistFlag) { 63 | this.participantEnlistFlag = participantEnlistFlag; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/java/org/bytesoft/bytejta/supports/rpc/TransactionResponseImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.rpc; 17 | 18 | import java.util.HashMap; 19 | import java.util.Map; 20 | 21 | import org.bytesoft.transaction.TransactionContext; 22 | import org.bytesoft.transaction.remote.RemoteCoordinator; 23 | import org.bytesoft.transaction.supports.rpc.TransactionResponse; 24 | 25 | public class TransactionResponseImpl implements TransactionResponse { 26 | 27 | private boolean participantStickyRequired = true; 28 | private boolean participantRollbackOnly; 29 | 30 | private RemoteCoordinator participantCoordinator; 31 | private TransactionContext transactionContext; 32 | 33 | private transient boolean participantEnlistFlag; 34 | private transient boolean participantDelistFlag; 35 | 36 | private final Map headers = new HashMap(); 37 | 38 | public Object getHeader(String name) { 39 | return this.headers.get(name); 40 | } 41 | 42 | public void setHeader(String name, Object value) { 43 | this.headers.put(name, value); 44 | } 45 | 46 | public RemoteCoordinator getSourceTransactionCoordinator() { 47 | return this.participantCoordinator; 48 | } 49 | 50 | public void setSourceTransactionCoordinator(RemoteCoordinator remoteCoordinator) { 51 | this.participantCoordinator = remoteCoordinator; 52 | } 53 | 54 | public TransactionContext getTransactionContext() { 55 | return this.transactionContext; 56 | } 57 | 58 | public void setTransactionContext(TransactionContext transactionContext) { 59 | this.transactionContext = transactionContext; 60 | } 61 | 62 | public boolean isParticipantRollbackOnly() { 63 | return participantRollbackOnly; 64 | } 65 | 66 | public void setParticipantRollbackOnly(boolean participantRollbackOnly) { 67 | this.participantRollbackOnly = participantRollbackOnly; 68 | } 69 | 70 | public boolean isParticipantStickyRequired() { 71 | return participantStickyRequired; 72 | } 73 | 74 | public void setParticipantStickyRequired(boolean participantStickyRequired) { 75 | this.participantStickyRequired = participantStickyRequired; 76 | } 77 | 78 | public boolean isParticipantDelistFlag() { 79 | return participantDelistFlag; 80 | } 81 | 82 | public void setParticipantDelistFlag(boolean participantDelistFlag) { 83 | this.participantDelistFlag = participantDelistFlag; 84 | } 85 | 86 | public boolean isParticipantEnlistFlag() { 87 | return participantEnlistFlag; 88 | } 89 | 90 | public void setParticipantEnlistFlag(boolean participantEnlistFlag) { 91 | this.participantEnlistFlag = participantEnlistFlag; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/java/org/bytesoft/bytejta/supports/spring/TransactionBeanFactoryAutoInjector.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2016 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.spring; 17 | 18 | import java.util.Iterator; 19 | import java.util.Map; 20 | 21 | import org.bytesoft.transaction.TransactionBeanFactory; 22 | import org.bytesoft.transaction.aware.TransactionBeanFactoryAware; 23 | import org.slf4j.Logger; 24 | import org.slf4j.LoggerFactory; 25 | import org.springframework.beans.BeansException; 26 | import org.springframework.beans.factory.SmartInitializingSingleton; 27 | import org.springframework.beans.factory.config.BeanPostProcessor; 28 | import org.springframework.context.ApplicationContext; 29 | import org.springframework.context.ApplicationContextAware; 30 | import org.springframework.core.Ordered; 31 | 32 | @org.springframework.core.annotation.Order(org.springframework.core.Ordered.HIGHEST_PRECEDENCE) 33 | public class TransactionBeanFactoryAutoInjector 34 | implements BeanPostProcessor, Ordered, SmartInitializingSingleton, ApplicationContextAware { 35 | static final Logger logger = LoggerFactory.getLogger(TransactionBeanFactoryAutoInjector.class); 36 | 37 | private ApplicationContext applicationContext; 38 | 39 | public void afterSingletonsInstantiated() { 40 | Map beanMap = // 41 | this.applicationContext.getBeansOfType(TransactionBeanFactoryAware.class); 42 | Iterator> iterator = // 43 | (beanMap == null) ? null : beanMap.entrySet().iterator(); 44 | while (iterator != null && iterator.hasNext()) { 45 | Map.Entry entry = iterator.next(); 46 | TransactionBeanFactoryAware bean = entry.getValue(); 47 | this.initializeTransactionBeanFactoryIfNecessary(bean); 48 | } 49 | } 50 | 51 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 52 | return bean; 53 | } 54 | 55 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 56 | if (TransactionBeanFactoryAware.class.isInstance(bean)) { 57 | this.initializeTransactionBeanFactoryIfNecessary((TransactionBeanFactoryAware) bean); 58 | } // end-if (TransactionBeanFactoryAware.class.isInstance(bean)) 59 | 60 | return bean; 61 | } 62 | 63 | private void initializeTransactionBeanFactoryIfNecessary(TransactionBeanFactoryAware aware) { 64 | if (aware.getBeanFactory() == null) { 65 | TransactionBeanFactory beanFactory = // 66 | this.applicationContext.getBean(TransactionBeanFactory.class); 67 | aware.setBeanFactory(beanFactory); 68 | } // end-if (aware.getBeanFactory() == null) 69 | } 70 | 71 | public int getOrder() { 72 | return Ordered.HIGHEST_PRECEDENCE; 73 | } 74 | 75 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 76 | this.applicationContext = applicationContext; 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/java/org/bytesoft/bytejta/supports/spring/TransactionDebuggablePostProcessor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2014-2018 yangming.liu. 3 | * 4 | * This copyrighted material is made available to anyone wishing to use, modify, 5 | * copy, or redistribute it subject to the terms and conditions of the GNU 6 | * Lesser General Public License, as published by the Free Software Foundation. 7 | * 8 | * This program is distributed in the hope that it will be useful, 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 10 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 11 | * for more details. 12 | * 13 | * You should have received a copy of the GNU Lesser General Public License 14 | * along with this distribution; if not, see . 15 | */ 16 | package org.bytesoft.bytejta.supports.spring; 17 | 18 | import org.apache.commons.lang3.StringUtils; 19 | import org.bytesoft.transaction.aware.TransactionDebuggable; 20 | import org.springframework.beans.BeansException; 21 | import org.springframework.beans.factory.config.BeanPostProcessor; 22 | import org.springframework.context.EnvironmentAware; 23 | import org.springframework.core.env.Environment; 24 | 25 | public class TransactionDebuggablePostProcessor implements BeanPostProcessor, EnvironmentAware { 26 | static final String KEY_DEBUGGABLE = "org.bytesoft.bytejta.debuggable"; 27 | 28 | private Environment environment; 29 | 30 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 31 | this.setDebuggableIfNecessary(bean); 32 | return bean; 33 | } 34 | 35 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 36 | this.setDebuggableIfNecessary(bean); 37 | return bean; 38 | } 39 | 40 | private void setDebuggableIfNecessary(Object bean) { 41 | if (TransactionDebuggable.class.isInstance(bean)) { 42 | String debuggable = this.environment.getProperty(KEY_DEBUGGABLE); 43 | TransactionDebuggable target = (TransactionDebuggable) bean; 44 | target.setDebuggingEnabled(StringUtils.equalsIgnoreCase(Boolean.TRUE.toString(), debuggable)); 45 | } 46 | } 47 | 48 | public void setEnvironment(Environment environment) { 49 | this.environment = environment; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/resources/bytejta-supports-core.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | The bytejta transaction manager module 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/resources/bytejta-supports-standalone.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /bytejta-supports/src/main/resources/bytejta-supports-task.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | --------------------------------------------------------------------------------