diff --git a/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/ArkContainer.java b/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/ArkContainer.java index 878b435ef..18094e4a2 100644 --- a/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/ArkContainer.java +++ b/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/ArkContainer.java @@ -197,7 +197,7 @@ private void addStaticBiz(List bizArchives) throws IOException { for (BizArchive bizArchive : bizArchives) { Biz biz = ArkClient.getBizFactoryService().createBiz(bizArchive); - ArkClient.getBizManagerService().registerBiz(biz); + ArkClient.getBizManagerService().registerBizIfAbsent(biz); } } diff --git a/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/pipeline/HandleArchiveStage.java b/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/pipeline/HandleArchiveStage.java index ebeb5a5df..4b7b30c7a 100644 --- a/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/pipeline/HandleArchiveStage.java +++ b/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/pipeline/HandleArchiveStage.java @@ -197,7 +197,7 @@ public void processStaticBizFromClasspath(PipelineContext pipelineContext) throw List bizArchives = executableArchive.getBizArchives(); for (BizArchive bizArchive : bizArchives) { Biz biz = bizFactoryService.createBiz(bizArchive); - bizManagerService.registerBiz(biz); + bizManagerService.registerBizIfAbsent(biz); } } diff --git a/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/service/biz/BizManagerServiceImpl.java b/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/service/biz/BizManagerServiceImpl.java index 8d042d186..4fb6b4ce9 100644 --- a/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/service/biz/BizManagerServiceImpl.java +++ b/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/service/biz/BizManagerServiceImpl.java @@ -221,4 +221,13 @@ public boolean removeAndAddBiz(Biz addingBiz, Biz removingBiz) { public ConcurrentHashMap> getBizRegistration() { return bizRegistration; } + + @Override + public boolean registerBizIfAbsent(Biz biz) { + AssertUtils.assertNotNull(biz, "Biz must not be null."); + AssertUtils.isTrue(biz.getBizState() == BizState.RESOLVED, "BizState must be RESOLVED."); + bizRegistration.putIfAbsent(biz.getBizName(), new ConcurrentHashMap<>(16)); + ConcurrentHashMap bizCache = bizRegistration.get(biz.getBizName()); + return bizCache.putIfAbsent(biz.getBizVersion(), biz) == null; + } } diff --git a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/pipeline/HandleArchiveStageTest.java b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/pipeline/HandleArchiveStageTest.java index 84ad590b4..9c83f5c18 100644 --- a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/pipeline/HandleArchiveStageTest.java +++ b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/pipeline/HandleArchiveStageTest.java @@ -16,12 +16,14 @@ */ package com.alipay.sofa.ark.container.pipeline; +import com.alipay.sofa.ark.api.ArkClient; import com.alipay.sofa.ark.container.model.BizModel; import com.alipay.sofa.ark.loader.DirectoryBizArchive; import com.alipay.sofa.ark.loader.JarBizArchive; import com.alipay.sofa.ark.spi.archive.BizArchive; import com.alipay.sofa.ark.spi.archive.ExecutableArchive; import com.alipay.sofa.ark.spi.archive.PluginArchive; +import com.alipay.sofa.ark.spi.model.Biz; import com.alipay.sofa.ark.spi.model.Plugin; import com.alipay.sofa.ark.spi.pipeline.PipelineContext; import com.alipay.sofa.ark.spi.service.biz.BizFactoryService; @@ -31,16 +33,17 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.MockedStatic; import java.lang.reflect.Field; import java.util.HashSet; +import java.util.concurrent.ConcurrentHashMap; import java.util.jar.Attributes; import java.util.jar.Manifest; import static com.alipay.sofa.ark.api.ArkConfigs.getStringValue; import static com.alipay.sofa.ark.api.ArkConfigs.setSystemProperty; -import static com.alipay.sofa.ark.spi.constant.Constants.CONFIG_SERVER_ADDRESS; -import static com.alipay.sofa.ark.spi.constant.Constants.MASTER_BIZ; +import static com.alipay.sofa.ark.spi.constant.Constants.*; import static java.util.Arrays.asList; import static org.mockito.Mockito.*; @@ -152,13 +155,25 @@ public void testProcess() throws Exception { @Test public void testProcessStaticBizFromClasspath() throws Exception { - - BizArchive bizArchive = mock(BizArchive.class); - when(executableArchive.getBizArchives()).thenReturn(asList(bizArchive)); - - handleArchiveStage.processStaticBizFromClasspath(pipelineContext); - verify(bizFactoryService, times(1)).createBiz((bizArchive)); - verify(bizManagerService, times(1)).registerBiz(null); + MockedStatic arkClientMockedStatic = mockStatic(ArkClient.class); + arkClientMockedStatic.when(ArkClient::getBizManagerService).thenReturn(bizManagerService); + try { + BizArchive bizArchive = mock(BizArchive.class); + Manifest manifest = new Manifest(); + Attributes mainAttributes = manifest.getMainAttributes(); + mainAttributes.putValue(ARK_BIZ_NAME, "biz1"); + mainAttributes.putValue(ARK_BIZ_VERSION, "0.0.1"); + when(bizArchive.getManifest()).thenReturn(manifest); + ConcurrentHashMap> map = new ConcurrentHashMap<>(); + when(bizManagerService.getBizRegistration()).thenReturn(map); + when(executableArchive.getBizArchives()).thenReturn(asList(bizArchive)); + + handleArchiveStage.processStaticBizFromClasspath(pipelineContext); + verify(bizFactoryService, times(1)).createBiz((bizArchive)); + verify(bizManagerService, times(1)).registerBizIfAbsent(null); + }finally { + arkClientMockedStatic.close(); + } } @Test diff --git a/sofa-ark-parent/core/spi/src/main/java/com/alipay/sofa/ark/spi/service/biz/BizManagerService.java b/sofa-ark-parent/core/spi/src/main/java/com/alipay/sofa/ark/spi/service/biz/BizManagerService.java index 5d577d7d5..b595e14f6 100644 --- a/sofa-ark-parent/core/spi/src/main/java/com/alipay/sofa/ark/spi/service/biz/BizManagerService.java +++ b/sofa-ark-parent/core/spi/src/main/java/com/alipay/sofa/ark/spi/service/biz/BizManagerService.java @@ -157,4 +157,6 @@ public interface BizManagerService { ConcurrentHashMap> getBizRegistration(); + boolean registerBizIfAbsent(Biz biz); + } \ No newline at end of file