diff --git a/common/src/main/java/org/apache/seata/common/loader/EnhancedServiceLoader.java b/common/src/main/java/org/apache/seata/common/loader/EnhancedServiceLoader.java index 589eeff6735..442b891c97a 100644 --- a/common/src/main/java/org/apache/seata/common/loader/EnhancedServiceLoader.java +++ b/common/src/main/java/org/apache/seata/common/loader/EnhancedServiceLoader.java @@ -43,6 +43,8 @@ * */ public class EnhancedServiceLoader { + private static final String APACHE_SEATA_PACKAGE_NAME="org.apache.seata"; + private static final String IO_SEATA_PACKAGE_NAME="io.seata"; /** * Class->InnerEnhancedServiceLoader map @@ -50,6 +52,16 @@ public class EnhancedServiceLoader { private static final ConcurrentMap, InnerEnhancedServiceLoader> SERVICE_LOADERS = new ConcurrentHashMap<>(); + private static Class getCompatibleService(Class originService) { + String apacheSeataName = originService.getName(); + String ioSeataName = apacheSeataName.replace(APACHE_SEATA_PACKAGE_NAME, IO_SEATA_PACKAGE_NAME); + try { + Class clasz = Class.forName(ioSeataName); + return clasz; + } catch (ClassNotFoundException e) { + return null; + } + } /** * Specify classLoader to load the service provider * @@ -60,6 +72,16 @@ public class EnhancedServiceLoader { * @throws EnhancedServiceNotFoundException the enhanced service not found exception */ public static S load(Class service, ClassLoader loader) throws EnhancedServiceNotFoundException { + Class compatibleService = getCompatibleService(service); + if (compatibleService != null) { + try { + S s = InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(loader); + if (s != null) { + return s; + } + } catch (EnhancedServiceNotFoundException ignore) { + } + } return InnerEnhancedServiceLoader.getServiceLoader(service).load(loader); } @@ -72,6 +94,16 @@ public static S load(Class service, ClassLoader loader) throws EnhancedSe * @throws EnhancedServiceNotFoundException the enhanced service not found exception */ public static S load(Class service) throws EnhancedServiceNotFoundException { + Class compatibleService = getCompatibleService(service); + if (compatibleService != null) { + try { + S s = InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(findClassLoader()); + if (s != null) { + return s; + } + } catch (EnhancedServiceNotFoundException ignore) { + } + } return InnerEnhancedServiceLoader.getServiceLoader(service).load(findClassLoader()); } @@ -85,6 +117,16 @@ public static S load(Class service) throws EnhancedServiceNotFoundExcepti * @throws EnhancedServiceNotFoundException the enhanced service not found exception */ public static S load(Class service, String activateName) throws EnhancedServiceNotFoundException { + Class compatibleService = getCompatibleService(service); + if (compatibleService != null) { + try { + S s = InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(activateName, findClassLoader()); + if (s != null) { + return s; + } + } catch (EnhancedServiceNotFoundException ignore) { + } + } return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, findClassLoader()); } @@ -100,6 +142,16 @@ public static S load(Class service, String activateName) throws EnhancedS */ public static S load(Class service, String activateName, ClassLoader loader) throws EnhancedServiceNotFoundException { + Class compatibleService = getCompatibleService(service); + if (compatibleService != null) { + try { + S s = InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(activateName, loader); + if (s != null) { + return s; + } + } catch (EnhancedServiceNotFoundException ignore) { + } + } return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, loader); } @@ -115,6 +167,16 @@ public static S load(Class service, String activateName, ClassLoader load */ public static S load(Class service, String activateName, Object[] args) throws EnhancedServiceNotFoundException { + Class compatibleService = getCompatibleService(service); + if (compatibleService != null) { + try { + S s = InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(activateName, args, findClassLoader()); + if (s != null) { + return s; + } + } catch (EnhancedServiceNotFoundException ignore) { + } + } return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, args, findClassLoader()); } @@ -131,6 +193,16 @@ public static S load(Class service, String activateName, Object[] args) */ public static S load(Class service, String activateName, Class[] argsType, Object[] args) throws EnhancedServiceNotFoundException { + Class compatibleService = getCompatibleService(service); + if (compatibleService != null) { + try { + S s = InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(activateName, argsType, args, findClassLoader()); + if (s != null) { + return s; + } + } catch (EnhancedServiceNotFoundException ignore) { + } + } return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, argsType, args, findClassLoader()); } @@ -142,6 +214,16 @@ public static S load(Class service, String activateName, Class[] argsT * @return list list */ public static List loadAll(Class service) { + Class compatibleService = getCompatibleService(service); + if (compatibleService != null) { + try { + List sList = InnerEnhancedServiceLoader.getServiceLoader(compatibleService).loadAll(findClassLoader()); + if (CollectionUtils.isNotEmpty(sList)) { + return sList; + } + } catch (EnhancedServiceNotFoundException ignore) { + } + } return InnerEnhancedServiceLoader.getServiceLoader(service).loadAll(findClassLoader()); } @@ -155,6 +237,16 @@ public static List loadAll(Class service) { * @return list list */ public static List loadAll(Class service, Class[] argsType, Object[] args) { + Class compatibleService = getCompatibleService(service); + if (compatibleService != null) { + try { + List sList = InnerEnhancedServiceLoader.getServiceLoader(compatibleService).loadAll(argsType, args, findClassLoader()); + if (CollectionUtils.isNotEmpty(sList)) { + return sList; + } + } catch (EnhancedServiceNotFoundException ignore) { + } + } return InnerEnhancedServiceLoader.getServiceLoader(service).loadAll(argsType, args, findClassLoader()); } @@ -172,6 +264,16 @@ public static void unloadAll() { * @param service the service */ public static void unload(Class service) { + Class compatibleService = getCompatibleService(service); + if (compatibleService != null) { + try { + InnerEnhancedServiceLoader s = InnerEnhancedServiceLoader.removeServiceLoader(compatibleService); + if (s != null) { + return; + } + } catch (Exception ignore) { + } + } InnerEnhancedServiceLoader.removeServiceLoader(service); } @@ -183,11 +285,23 @@ public static void unload(Class service) { * @param activateName the activate name */ public static void unload(Class service, String activateName) { - if (activateName == null) { throw new IllegalArgumentException("activateName is null"); } + Class compatibleService = getCompatibleService(service); + if (compatibleService != null) { + try { + InnerEnhancedServiceLoader serviceLoader = InnerEnhancedServiceLoader.getServiceLoader(compatibleService); + doUnload(serviceLoader, activateName); + return; + } catch (Exception ignore) { + } + } InnerEnhancedServiceLoader serviceLoader = InnerEnhancedServiceLoader.getServiceLoader(service); + doUnload(serviceLoader, activateName); + } + + private static void doUnload(InnerEnhancedServiceLoader serviceLoader, String activateName) { ConcurrentMap, ExtensionDefinition> classToDefinitionMap = serviceLoader.classToDefinitionMap; List> extensionDefinitions = new ArrayList<>(); for (Map.Entry, ExtensionDefinition> entry : classToDefinitionMap.entrySet()) { @@ -207,7 +321,6 @@ public static void unload(Class service, String activateName) { } } - } @@ -259,6 +372,8 @@ private InnerEnhancedServiceLoader(Class type) { this.type = type; } + + /** * Get the ServiceLoader for the specified Class * diff --git a/compatible/src/main/java/io/seata/common/util/StringUtils.java b/compatible/src/main/java/io/seata/common/util/StringUtils.java new file mode 100644 index 00000000000..4d98b7132ff --- /dev/null +++ b/compatible/src/main/java/io/seata/common/util/StringUtils.java @@ -0,0 +1,210 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.common.util; + + +import java.io.InputStream; +import java.util.Iterator; +import java.util.regex.Pattern; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The type String utils. + * + * Compatible for dubbo dubbo-filter-seata + */ +@Deprecated +public class StringUtils { + + private static final Logger LOGGER = LoggerFactory.getLogger(org.apache.seata.common.util.StringUtils.class); + private static final Pattern CAMEL_PATTERN = Pattern.compile("[A-Z]"); + private static final Pattern LINE_PATTERN = Pattern.compile("-(\\w)"); + + private StringUtils() { + } + + /** + * empty string + */ + public static final String EMPTY = ""; + + /** + * Space string + */ + public static final String SPACE = " "; + + /** + * Is empty boolean. + * + * @param str the str + * @return the boolean + */ + public static boolean isNullOrEmpty(String str) { + return org.apache.seata.common.util.StringUtils.isNullOrEmpty(str); + } + + /** + * Is blank string ? + * + * @param str the str + * @return boolean boolean + */ + public static boolean isBlank(String str) { + return org.apache.seata.common.util.StringUtils.isBlank(str); + } + + /** + * Is Not blank string ? + * + * @param str the str + * @return boolean boolean + */ + public static boolean isNotBlank(String str) { + return org.apache.seata.common.util.StringUtils.isNotBlank(str); + } + + /** + * Equals boolean. + * + * @param a the a + * @param b the b + * @return boolean + */ + public static boolean equals(String a, String b) { + return org.apache.seata.common.util.StringUtils.equals(a,b); + } + + /** + * Equals ignore case boolean. + * + * @param a the a + * @param b the b + * @return the boolean + */ + public static boolean equalsIgnoreCase(String a, String b) { + return org.apache.seata.common.util.StringUtils.equalsIgnoreCase(a,b); + } + + /** + * Input stream 2 string string. + * + * @param is the is + * @return the string + */ + public static String inputStream2String(InputStream is) { + return org.apache.seata.common.util.StringUtils.inputStream2String(is); + } + + /** + * Input stream to byte array + * + * @param is the is + * @return the byte array + */ + public static byte[] inputStream2Bytes(InputStream is) { + return org.apache.seata.common.util.StringUtils.inputStream2Bytes(is); + } + + /** + * Object.toString() + * + * @param obj the obj + * @return string string + */ + @SuppressWarnings("deprecation") + public static String toString(final Object obj) { + return org.apache.seata.common.util.StringUtils.toString(obj); + } + + /** + * Trim string to null if empty(""). + * + * @param str the String to be trimmed, may be null + * @return the trimmed String + */ + public static String trimToNull(final String str) { + return org.apache.seata.common.util.StringUtils.trimToNull(str); + } + + /** + * Trim string, or null if string is null. + * + * @param str the String to be trimmed, may be null + * @return the trimmed string, {@code null} if null String input + */ + public static String trim(final String str) { + return org.apache.seata.common.util.StringUtils.trim(str); + } + + /** + * Checks if a CharSequence is empty ("") or null. + * + * @param cs the CharSequence to check, may be null + * @return {@code true} if the CharSequence is empty or null + */ + public static boolean isEmpty(final CharSequence cs) { + return org.apache.seata.common.util.StringUtils.isEmpty(cs); + } + + /** + * Checks if a CharSequence is not empty ("") and not null. + * + * @param cs the CharSequence to check, may be null + * @return {@code true} if the CharSequence is not empty and not null + */ + public static boolean isNotEmpty(final CharSequence cs) { + return org.apache.seata.common.util.StringUtils.isNotEmpty(cs); + } + + /** + * hump to Line or line to hump, only spring environment use + * + * @param str str + * @return string string + */ + public static String hump2Line(String str) { + return org.apache.seata.common.util.StringUtils.hump2Line(str); + } + + /** + * check string data size + * + * @param data the str + * @param dataName the data name + * @param errorSize throw exception if size > errorSize + * @return boolean + */ + public static boolean checkDataSize(String data, String dataName, int errorSize, boolean throwIfErr) { + return org.apache.seata.common.util.StringUtils.checkDataSize(data,dataName,errorSize,throwIfErr); + } + + public static boolean hasLowerCase(String str) { + return org.apache.seata.common.util.StringUtils.hasLowerCase(str); + } + + public static boolean hasUpperCase(String str) { + return org.apache.seata.common.util.StringUtils.hasUpperCase(str); + } + + public static String join(Iterator iterator, String separator) { + return org.apache.seata.common.util.StringUtils.join(iterator,separator); + } + +} + diff --git a/compatible/src/main/java/io/seata/core/constants/DubboConstants.java b/compatible/src/main/java/io/seata/core/constants/DubboConstants.java new file mode 100644 index 00000000000..18915e002db --- /dev/null +++ b/compatible/src/main/java/io/seata/core/constants/DubboConstants.java @@ -0,0 +1,8 @@ +package io.seata.core.constants; + +/** + * Compatible for dubbo dubbo-filter-seata + */ +@Deprecated +public class DubboConstants extends org.apache.seata.core.constants.DubboConstants { +} diff --git a/compatible/src/main/java/io/seata/core/context/RootContext.java b/compatible/src/main/java/io/seata/core/context/RootContext.java index ac50d0125b8..97ac14edada 100644 --- a/compatible/src/main/java/io/seata/core/context/RootContext.java +++ b/compatible/src/main/java/io/seata/core/context/RootContext.java @@ -16,12 +16,11 @@ */ package io.seata.core.context; -import java.util.Map; +import io.seata.core.model.BranchType; import javax.annotation.Nonnull; import javax.annotation.Nullable; - -import org.apache.seata.core.model.BranchType; +import java.util.Map; /** * The type Root context. @@ -29,13 +28,21 @@ @Deprecated public class RootContext { + private static org.apache.seata.core.model.BranchType convertApacheSeata(BranchType branchType) { + return org.apache.seata.core.model.BranchType.get(branchType.name()); + } + + private static BranchType convertIoSeata(org.apache.seata.core.model.BranchType branchType) { + return BranchType.get(branchType.name()); + } + /** * Sets default branch type. * * @param defaultBranchType the default branch type */ public static void setDefaultBranchType(BranchType defaultBranchType) { - org.apache.seata.core.context.RootContext.setDefaultBranchType(defaultBranchType); + org.apache.seata.core.context.RootContext.setDefaultBranchType(convertApacheSeata(defaultBranchType)); } /** @@ -133,7 +140,7 @@ public static boolean inSagaBranch() { */ @Nullable public static BranchType getBranchType() { - return org.apache.seata.core.context.RootContext.getBranchType(); + return convertIoSeata(org.apache.seata.core.context.RootContext.getBranchType()); } /** @@ -142,7 +149,7 @@ public static BranchType getBranchType() { * @param branchType the branch type */ public static void bindBranchType(@Nonnull BranchType branchType) { - org.apache.seata.core.context.RootContext.bindBranchType(branchType); + org.apache.seata.core.context.RootContext.bindBranchType(convertApacheSeata(branchType)); } /** @@ -152,7 +159,7 @@ public static void bindBranchType(@Nonnull BranchType branchType) { */ @Nullable public static BranchType unbindBranchType() { - return org.apache.seata.core.context.RootContext.unbindBranchType(); + return convertIoSeata(org.apache.seata.core.context.RootContext.unbindBranchType()); } /** diff --git a/compatible/src/main/java/io/seata/core/model/BranchType.java b/compatible/src/main/java/io/seata/core/model/BranchType.java new file mode 100644 index 00000000000..706421a5a6f --- /dev/null +++ b/compatible/src/main/java/io/seata/core/model/BranchType.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.core.model; + +/** + * The enum Branch type. + * Compatible for dubbo dubbo-filter-seata + * + */ +@Deprecated +public enum BranchType { + + /** + * The At. + */ + // AT Branch + AT, + + /** + * The TCC. + */ + TCC, + + /** + * The SAGA. + */ + SAGA, + + /** + * The XA. + */ + XA; + + /** + * Get branch type. + * + * @param ordinal the ordinal + * @return the branch type + */ + public static BranchType get(byte ordinal) { + return get((int)ordinal); + } + + /** + * Get branch type. + * + * @param ordinal the ordinal + * @return the branch type + */ + public static BranchType get(int ordinal) { + for (BranchType branchType : values()) { + if (branchType.ordinal() == ordinal) { + return branchType; + } + } + throw new IllegalArgumentException("Unknown BranchType[" + ordinal + "]"); + } + + /** + * Get branch type. + * + * @param name the name + * @return the branch type + */ + public static BranchType get(String name) { + for (BranchType branchType : values()) { + if (branchType.name().equalsIgnoreCase(name)) { + return branchType; + } + } + throw new IllegalArgumentException("Unknown BranchType[" + name + "]"); + } +}