Skip to content

Commit

Permalink
Compatible with Apache dubbo
Browse files Browse the repository at this point in the history
  • Loading branch information
xingfudeshi committed Jan 31, 2024
1 parent 663ec71 commit 272cf82
Show file tree
Hide file tree
Showing 5 changed files with 436 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,25 @@
*
*/
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
*/
private static final ConcurrentMap<Class<?>, InnerEnhancedServiceLoader<?>> SERVICE_LOADERS =
new ConcurrentHashMap<>();

private static <S> Class<S> getCompatibleService(Class<S> 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
*
Expand All @@ -60,6 +72,16 @@ public class EnhancedServiceLoader {
* @throws EnhancedServiceNotFoundException the enhanced service not found exception
*/
public static <S> S load(Class<S> service, ClassLoader loader) throws EnhancedServiceNotFoundException {
Class<S> 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);
}

Expand All @@ -72,6 +94,16 @@ public static <S> S load(Class<S> service, ClassLoader loader) throws EnhancedSe
* @throws EnhancedServiceNotFoundException the enhanced service not found exception
*/
public static <S> S load(Class<S> service) throws EnhancedServiceNotFoundException {
Class<S> 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());
}

Expand All @@ -85,6 +117,16 @@ public static <S> S load(Class<S> service) throws EnhancedServiceNotFoundExcepti
* @throws EnhancedServiceNotFoundException the enhanced service not found exception
*/
public static <S> S load(Class<S> service, String activateName) throws EnhancedServiceNotFoundException {
Class<S> 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());
}

Expand All @@ -100,6 +142,16 @@ public static <S> S load(Class<S> service, String activateName) throws EnhancedS
*/
public static <S> S load(Class<S> service, String activateName, ClassLoader loader)
throws EnhancedServiceNotFoundException {
Class<S> 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);
}

Expand All @@ -115,6 +167,16 @@ public static <S> S load(Class<S> service, String activateName, ClassLoader load
*/
public static <S> S load(Class<S> service, String activateName, Object[] args)
throws EnhancedServiceNotFoundException {
Class<S> 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());
}

Expand All @@ -131,6 +193,16 @@ public static <S> S load(Class<S> service, String activateName, Object[] args)
*/
public static <S> S load(Class<S> service, String activateName, Class<?>[] argsType, Object[] args)
throws EnhancedServiceNotFoundException {
Class<S> 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());
}

Expand All @@ -142,6 +214,16 @@ public static <S> S load(Class<S> service, String activateName, Class<?>[] argsT
* @return list list
*/
public static <S> List<S> loadAll(Class<S> service) {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
List<S> sList = InnerEnhancedServiceLoader.getServiceLoader(compatibleService).loadAll(findClassLoader());
if (CollectionUtils.isNotEmpty(sList)) {
return sList;
}
} catch (EnhancedServiceNotFoundException ignore) {
}
}
return InnerEnhancedServiceLoader.getServiceLoader(service).loadAll(findClassLoader());
}

Expand All @@ -155,6 +237,16 @@ public static <S> List<S> loadAll(Class<S> service) {
* @return list list
*/
public static <S> List<S> loadAll(Class<S> service, Class<?>[] argsType, Object[] args) {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
List<S> 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());
}

Expand All @@ -172,6 +264,16 @@ public static void unloadAll() {
* @param service the service
*/
public static <S> void unload(Class<S> service) {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
InnerEnhancedServiceLoader<S> s = InnerEnhancedServiceLoader.removeServiceLoader(compatibleService);
if (s != null) {
return;
}
} catch (Exception ignore) {
}
}
InnerEnhancedServiceLoader.removeServiceLoader(service);
}

Expand All @@ -183,11 +285,23 @@ public static <S> void unload(Class<S> service) {
* @param activateName the activate name
*/
public static <S> void unload(Class<S> service, String activateName) {

if (activateName == null) {
throw new IllegalArgumentException("activateName is null");
}
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
InnerEnhancedServiceLoader<S> serviceLoader = InnerEnhancedServiceLoader.getServiceLoader(compatibleService);
doUnload(serviceLoader, activateName);
return;
} catch (Exception ignore) {
}
}
InnerEnhancedServiceLoader<S> serviceLoader = InnerEnhancedServiceLoader.getServiceLoader(service);
doUnload(serviceLoader, activateName);
}

private static <S> void doUnload(InnerEnhancedServiceLoader<S> serviceLoader, String activateName) {
ConcurrentMap<Class<?>, ExtensionDefinition<S>> classToDefinitionMap = serviceLoader.classToDefinitionMap;
List<ExtensionDefinition<S>> extensionDefinitions = new ArrayList<>();
for (Map.Entry<Class<?>, ExtensionDefinition<S>> entry : classToDefinitionMap.entrySet()) {
Expand All @@ -207,7 +321,6 @@ public static <S> void unload(Class<S> service, String activateName) {

}
}

}


Expand Down Expand Up @@ -259,6 +372,8 @@ private InnerEnhancedServiceLoader(Class<S> type) {
this.type = type;
}



/**
* Get the ServiceLoader for the specified Class
*
Expand Down
Loading

0 comments on commit 272cf82

Please sign in to comment.