Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持模块启动时通过传递env参数动态指定模块的启动类 (#999) #1029

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ public void start(String[] args, Map<String, String> envs) throws Throwable {

private void doStart(String[] args, Map<String, String> 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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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<String, String> 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");
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public class Constants {

public final static String EMBED_STATIC_BIZ_IN_RESOURCE_ENABLE = "sofa.ark.embed.static.biz.in.resource.enable";
public final static String ACTIVATE_NEW_MODULE = "activate.new.module";
public final static String BIZ_MAIN_CLASS = "sofa.ark.biz.main.class";

/**
* uninstall the biz if it starts up failed
Expand Down
Loading