001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.support.seata;
017
018
019import io.jboot.Jboot;
020import io.jboot.core.spi.JbootSpiLoader;
021import io.jboot.exception.JbootIllegalConfigException;
022import io.jboot.utils.ClassUtil;
023import io.jboot.utils.StrUtil;
024import io.seata.rm.GlobalLockTemplate;
025import io.seata.rm.datasource.DataSourceProxy;
026import io.seata.tm.api.DefaultFailureHandlerImpl;
027import io.seata.tm.api.FailureHandler;
028import io.seata.tm.api.TransactionalTemplate;
029
030import javax.sql.DataSource;
031
032public class JbootSeataManager {
033
034    private static JbootSeataManager seataManager = new JbootSeataManager();
035
036    private JbootSeataManager() {
037
038    }
039
040    public static JbootSeataManager me() {
041        return seataManager;
042    }
043
044    private SeataConfig config = Jboot.config(SeataConfig.class);
045    private boolean enable = false;
046
047    private TransactionalTemplate transactionalTemplate;
048    private GlobalLockTemplate globalLockTemplate;
049    private SeataGlobalTransactionManager transactionManager;
050
051
052    public void init() {
053
054        if (!config.isEnable()) {
055            return;
056        }
057
058        if (!config.isConfigOk()) {
059            throw new JbootIllegalConfigException("please config applicationId and txServiceGroup for seata");
060        }
061
062
063        transactionManager = new SeataGlobalTransactionManager(
064                config.getApplicationId(),
065                config.getTxServiceGroup(),
066                config.getMode()
067        );
068
069        transactionManager.init();
070
071        this.transactionalTemplate = new TransactionalTemplate();
072        this.globalLockTemplate = new GlobalLockTemplate();
073        this.enable = true;
074    }
075
076    public boolean isEnable() {
077        return enable;
078    }
079
080    public void stop() {
081        if (isEnable()) {
082            transactionManager.destroy();
083        }
084    }
085
086    private Object handler;
087
088    public FailureHandler getFailureHandler() {
089        if (handler == null) {
090            String failureHandlerClassOrSpiName = config.getFailureHandler();
091            if (StrUtil.isBlank(failureHandlerClassOrSpiName)) {
092                handler = new DefaultFailureHandlerImpl();
093            } else {
094                if (failureHandlerClassOrSpiName.contains(".")) {
095                    handler = ClassUtil.newInstance(failureHandlerClassOrSpiName);
096                }
097                if (handler == null) {
098                    handler = JbootSpiLoader.load(FailureHandler.class, failureHandlerClassOrSpiName);
099                }
100                if (handler == null) {
101                    handler = new DefaultFailureHandlerImpl();
102                }
103            }
104        }
105        return (FailureHandler) handler;
106    }
107
108
109    public TransactionalTemplate getTransactionalTemplate() {
110        return transactionalTemplate;
111    }
112
113    public void setTransactionalTemplate(TransactionalTemplate transactionalTemplate) {
114        this.transactionalTemplate = transactionalTemplate;
115    }
116
117    public GlobalLockTemplate getGlobalLockTemplate() {
118        return globalLockTemplate;
119    }
120
121    public void setGlobalLockTemplate(GlobalLockTemplate globalLockTemplate) {
122        this.globalLockTemplate = globalLockTemplate;
123    }
124
125    public DataSource wrapDataSource(DataSource dataSource) {
126        return config.isEnable() && config.isConfigOk()
127                ? new DataSourceProxy(dataSource)
128                : dataSource;
129    }
130
131
132}