-
Notifications
You must be signed in to change notification settings - Fork 766
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from peachyy/dev
issues 299 Unified management SPI
- Loading branch information
Showing
13 changed files
with
125 additions
and
30 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
common/src/main/java/org/dromara/dynamictp/common/util/ExtensionServiceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.dromara.dynamictp.common.util; | ||
|
||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Unified ServiceLoader Help | ||
* | ||
* @author xs.Tao | ||
* @since 1.1.4 | ||
*/ | ||
|
||
public class ExtensionServiceLoader { | ||
|
||
|
||
private static final Map<Class<?>, List<?>> extensionListMap = new ConcurrentHashMap<>(); | ||
|
||
private ExtensionServiceLoader() { | ||
} | ||
|
||
/** | ||
* loader service | ||
* @param clazz SPI interface | ||
* @return services | ||
* @param <T> interface class | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T> List<T> loader(Class<T> clazz) { | ||
List<T> services = (List<T>) extensionListMap.get(clazz); | ||
if (services == null) { | ||
services = reLoaderList(clazz); | ||
if (!services.isEmpty()) { | ||
extensionListMap.put(clazz, services); | ||
} | ||
} | ||
return services; | ||
} | ||
|
||
/** | ||
* loader the first service | ||
* @param clazz SPI interface | ||
* @return service | ||
* @param <T> interface class | ||
*/ | ||
public static <T> T loaderFirst(Class<T> clazz){ | ||
List<T> services=loader(clazz); | ||
return CollectionUtils.isEmpty(services)?null:services.get(0); | ||
} | ||
|
||
private static <T> List<T> reLoaderList(Class<T> clazz) { | ||
ServiceLoader<T> serviceLoader = ServiceLoader.load(clazz); | ||
List<T> services = new ArrayList<>(); | ||
for (T service : serviceLoader) { | ||
services.add(service); | ||
} | ||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...mmon/src/test/java/org/dromara/dynamictp/test/common/util/ExtensionServiceLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.dromara.dynamictp.test.common.util; | ||
|
||
import org.dromara.dynamictp.common.util.ExtensionServiceLoader; | ||
import org.dromara.dynamictp.core.plugin.DtpInterceptor; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.Assertions; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* ExtensionServiceLoader test case | ||
*/ | ||
public class ExtensionServiceLoaderTest { | ||
|
||
@Test | ||
public void test1(){ | ||
List<DtpInterceptor> loader = ExtensionServiceLoader.loader(DtpInterceptor.class); | ||
Assertions.assertTrue(loader.stream().anyMatch(it->it instanceof TestInterceptorLoader)); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...st-common/src/test/java/org/dromara/dynamictp/test/common/util/TestInterceptorLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.dromara.dynamictp.test.common.util; | ||
|
||
import org.dromara.dynamictp.core.plugin.DtpInterceptor; | ||
import org.dromara.dynamictp.core.plugin.DtpInvocation; | ||
|
||
public class TestInterceptorLoader implements DtpInterceptor { | ||
|
||
@Override | ||
public Object intercept(DtpInvocation invocation) throws Throwable { | ||
return invocation.proceed(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...mon/src/test/resources/META-INF/services/org.dromara.dynamictp.core.plugin.DtpInterceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.dromara.dynamictp.test.common.util.TestInterceptorLoader |