From 50b6b6e75bd50a0004adb87d177198aff6cfbc41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E4=B8=B0?= Date: Thu, 14 Nov 2024 16:44:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6=E9=80=9A=E8=BF=87=E4=BC=A0=E9=80=92?= =?UTF-8?q?env=E5=8F=82=E6=95=B0=E5=8A=A8=E6=80=81=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E7=9A=84=E5=90=AF=E5=8A=A8=E7=B1=BB=20(#999)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sofa/ark/container/model/BizModel.java | 11 +++++ .../container/service/api/ArkClientTest.java | 43 ++++++++++++++++++ .../resources/sample-ark-5.0.0-ark-biz.jar | Bin 0 -> 3814 bytes .../sofa/ark/spi/constant/Constants.java | 1 + 4 files changed, 55 insertions(+) create mode 100644 sofa-ark-parent/core-impl/container/src/test/resources/sample-ark-5.0.0-ark-biz.jar diff --git a/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/model/BizModel.java b/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/model/BizModel.java index d157a7b45..22b0a7b2d 100644 --- a/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/model/BizModel.java +++ b/sofa-ark-parent/core-impl/container/src/main/java/com/alipay/sofa/ark/container/model/BizModel.java @@ -323,6 +323,17 @@ public void start(String[] args, Map envs) throws Throwable { private void doStart(String[] args, Map envs) throws Throwable { AssertUtils.isTrue(bizState == BizState.RESOLVED, "BizState must be RESOLVED"); + + // support specify mainClass by env + if (envs != null) { + String mainClassFromEnv = envs.get(Constants.BIZ_MAIN_CLASS); + if (mainClassFromEnv != null) { + mainClass = mainClassFromEnv; + ArkLoggerFactory.getDefaultLogger().info("Ark biz {} will start with main class {} from envs", + getIdentity(), mainClassFromEnv); + } + } + if (mainClass == null) { throw new ArkRuntimeException(String.format("biz: %s has no main method", getBizName())); } diff --git a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/api/ArkClientTest.java b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/api/ArkClientTest.java index cbfb255f6..5ed36f452 100644 --- a/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/api/ArkClientTest.java +++ b/sofa-ark-parent/core-impl/container/src/test/java/com/alipay/sofa/ark/container/service/api/ArkClientTest.java @@ -16,10 +16,17 @@ */ package com.alipay.sofa.ark.container.service.api; +import com.alipay.sofa.ark.api.ArkClient; import com.alipay.sofa.ark.api.ArkConfigs; import com.alipay.sofa.ark.api.ClientResponse; +import com.alipay.sofa.ark.common.util.FileUtils; import com.alipay.sofa.ark.container.BaseTest; import com.alipay.sofa.ark.container.service.biz.BizManagerServiceImpl; +import com.alipay.sofa.ark.loader.JarBizArchive; +import com.alipay.sofa.ark.loader.archive.JarFileArchive; +import com.alipay.sofa.ark.loader.jar.JarFile; +import com.alipay.sofa.ark.spi.archive.BizArchive; +import com.alipay.sofa.ark.spi.constant.Constants; import com.alipay.sofa.ark.spi.event.ArkEvent; import com.alipay.sofa.ark.spi.model.Biz; import com.alipay.sofa.ark.spi.model.BizInfo; @@ -33,9 +40,12 @@ import org.junit.Test; import java.io.File; +import java.io.InputStream; import java.net.URL; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Map; import static com.alipay.sofa.ark.api.ArkClient.checkBiz; import static com.alipay.sofa.ark.api.ArkClient.checkOperation; @@ -93,6 +103,8 @@ public class ArkClientTest extends BaseTest { private URL bizUrl3; // bizName=biz-demo, bizVersion=4.0.0 private URL bizUrl4; + // bizName=biz-demo, bizVersion=5.0.0 + private URL bizUrl5; @Before public void before() { @@ -105,6 +117,8 @@ public void before() { bizUrl3 = this.getClass().getClassLoader().getResource("sample-ark-3.0.0-ark-biz.jar"); // bizName=biz-demo, bizVersion=4.0.0 bizUrl4 = this.getClass().getClassLoader().getResource("sample-ark-4.0.0-ark-biz.jar"); + // bizName=biz-demo, bizVersion=5.0.0 + bizUrl5 = this.getClass().getClassLoader().getResource("sample-ark-5.0.0-ark-biz.jar"); } @Test @@ -305,6 +319,35 @@ public void testInstallOperation() throws Throwable { assertEquals(SUCCESS, response.getCode()); } + @Test + public void testInstallOperationWithDynamicMainClass() throws Throwable { + + // the biz module will start with dynamic mainClass specified in env parameters, which is org.example.Main2 + BizOperation bizOperation = new BizOperation(); + bizOperation.setOperationType(INSTALL); + bizOperation.getParameters().put(CONFIG_BIZ_URL, bizUrl5.toString()); + bizOperation.setBizName("biz-demo"); + bizOperation.setBizVersion("5.0.0"); + + Map envs = Collections.singletonMap(Constants.BIZ_MAIN_CLASS, "org.example.Main2"); + + ClientResponse response2 = installOperation(bizOperation, new String[] {}, envs); + assertEquals(SUCCESS, response2.getCode()); + assertEquals("org.example.Main2", (new ArrayList<>(response2.getBizInfos())).get(0).getMainClass()); + + // but in fact, the biz module was packaged with mainClass as org.example.Main1 + URL url = new URL(bizOperation.getParameters().get(Constants.CONFIG_BIZ_URL)); + File file = ArkClient.createBizSaveFile(bizOperation.getBizName(), bizOperation.getBizVersion()); + try (InputStream inputStream = url.openStream()) { + FileUtils.copyInputStreamToFile(inputStream, file); + } + JarFile bizFile = new JarFile(file); + JarFileArchive jarFileArchive = new JarFileArchive(bizFile); + BizArchive bizArchive = new JarBizArchive(jarFileArchive); + assertEquals("org.example.Main1", bizArchive.getManifest().getMainAttributes().getValue(Constants.MAIN_CLASS_ATTRIBUTE)); + assertEquals("org.example.Main1", bizArchive.getManifest().getMainAttributes().getValue(Constants.START_CLASS_ATTRIBUTE)); + } + @Test public void testInstallBizFailed() throws Throwable { File bizFile = createBizSaveFile("biz-install-failed-demo", "1.0.0"); diff --git a/sofa-ark-parent/core-impl/container/src/test/resources/sample-ark-5.0.0-ark-biz.jar b/sofa-ark-parent/core-impl/container/src/test/resources/sample-ark-5.0.0-ark-biz.jar new file mode 100644 index 0000000000000000000000000000000000000000..2b6653a326f0367c788a0b40df7fbef5c1926504 GIT binary patch literal 3814 zcmWIWW@Zs#;Nak3FzC;VWIzI(3@i-3t|5-Po_=on|4uP5Ff#;rvvYt{FhP|C;M6Pv zQ~}rQ>*(j{<{BKL=j&EF;b4BVg2d7HDu4Lzh|h_dZFVm!-#2Dj=}p;>tADF8Io^5m z?^k1<_vBRbPY?dey}#=8nAPok{DIz#l9QWMmOf#Y((CXvm_ih=IXBW#NjevQQl zR{~QBFyVoG%s*#odjc>VG=WJ=9L+zziJ5suddWG7#l^XS-rk2D1paMXZ(?|B`r7>5 zy%DanmAVp+c0bUxTX3m+x$o{J(VsLjwO;MN)ZD~g-(c@G=ZKTw?)IN$`)2-3OZ)!% z_haD$g4;|}Hmq3G<0a-THErXfgwTUh&d+?RjBL_9u9aK!tmNLWE>X1X%aN})wz?D? z*KMD%=w%>Mm&fwo@i6}N6x>A+chtdXp*t;{xfKC0arKh-+#>ZJ4R zwhwmd=u& zEajQ|F7eEAlysZt96kN@=4o!cnZG(;s2o@ndScZR$(A02z2Eu13ALMUnmNNKsc+4{ z*$x)|E0P#@dVciOal4}}bo7aIP%vY!f!3)>S3+D|D$KJ zhddUa`pR)oLD847v;D@dzS)MQk4}``;;U|v=xt%qE~{bHNjrGnK=cpmy183CXHUsn zHK+RA-hKWh+q_&iBzP6i>^;o*qMh;dik`}8J%_KapXajtq2#-TF(=D=W*yjMY|E$L zd(vHCS;|2phax9^$q8DG`t$rBFkjdnSLOJ%c+y3$&uepb#xy;xRhRhooB2ms(Z@?w z9-c>xb3NQ5W7qxkuzuaTvy_FgNJpIi zETr1~C$nd*^?@*zv#ib=m~2}$4yU(Bale=p&}_4Ojip@9Bb`}_Ia`abEoRzUlBm<9Isr>z5TL zHol(dGV#W}_X6$rx)<5+-}m=##pZvN)#ZQZuBhDb@5|SB&kq0kJpKN5`$ZZ3@AsM< zbUU51s)1wPWbNOVByVO%I|t=2T-B%6a99wy<&Hqn412X`Zq*VY_YjJ*BB1!r|ts{_gp;m+SCl+OsWQGI|Gmw@YOjL=5hLrXXuGD-sd!ZJ$)ICM7lm-7Akc#+WAv+spZNlaLaIl zv}czZ&@>Go4)A7V5@A4%ELf`%)UZSa@RlN|nTf6yxtRnCI0Uc-GT~a08kryyxEOFZ zrw}Hz1DhO>#uaWIpym|