Skip to content

Commit

Permalink
merge 2.2.16 to 3.1.9 (#1050)
Browse files Browse the repository at this point in the history
* add param to determine whether unpack biz when install  (#1042)

* add param to judge whether unpack biz when install (#325)

---------

Co-authored-by: beikai <beikai.yzw@antgroup.com>
(cherry picked from commit e9e7f34)

* add event after all biz startup when bizs playback in base restart (#1046)

* set logback context name=default

* add event after all biz startup when bizs playback in base restart

(cherry picked from commit f4e4c1d)

* fix: 修复委托基座加载资源过程中,模块未声明的依赖存在该资源时,无法正确获取资源(#1030) (#1031)

* fix: correctly retrieve resources when undeclared dependencies exist during the loading of the delegate base from

* fix: spaces map type forcible transfer

* fix: test

---------

Co-authored-by: fanyue <dengyongjun.dyj@alibaba-inc.com>
Co-authored-by: leo james <leojames.googol@gmail.com>

(cherry picked from commit cd1e640)

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

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

* fix format

---------

Co-authored-by: leo james <leojames.googol@gmail.com>
(cherry picked from commit a282aa5)

---------

Co-authored-by: Kennan <51430841+KennanYang@users.noreply.github.com>
Co-authored-by: Jackisome <35559702+jackisome@users.noreply.github.com>
Co-authored-by: 刘丰 <liufeng_xiwo@outlook.com>
  • Loading branch information
4 people authored Dec 10, 2024
1 parent 42a8cb8 commit 2fea83a
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,18 @@ 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 @@ -52,19 +52,7 @@
import java.util.Set;
import java.util.jar.Attributes;

import static com.alipay.sofa.ark.spi.constant.Constants.ARK_BIZ_NAME;
import static com.alipay.sofa.ark.spi.constant.Constants.ARK_BIZ_VERSION;
import static com.alipay.sofa.ark.spi.constant.Constants.DENY_IMPORT_CLASSES;
import static com.alipay.sofa.ark.spi.constant.Constants.DENY_IMPORT_PACKAGES;
import static com.alipay.sofa.ark.spi.constant.Constants.DENY_IMPORT_RESOURCES;
import static com.alipay.sofa.ark.spi.constant.Constants.INJECT_EXPORT_PACKAGES;
import static com.alipay.sofa.ark.spi.constant.Constants.INJECT_PLUGIN_DEPENDENCIES;
import static com.alipay.sofa.ark.spi.constant.Constants.MAIN_CLASS_ATTRIBUTE;
import static com.alipay.sofa.ark.spi.constant.Constants.MASTER_BIZ;
import static com.alipay.sofa.ark.spi.constant.Constants.PRIORITY_ATTRIBUTE;
import static com.alipay.sofa.ark.spi.constant.Constants.START_CLASS_ATTRIBUTE;
import static com.alipay.sofa.ark.spi.constant.Constants.WEB_CONTEXT_PATH;
import static com.alipay.sofa.ark.spi.constant.Constants.DECLARED_LIBRARIES;
import static com.alipay.sofa.ark.spi.constant.Constants.*;

/**
* {@link BizFactoryService}
Expand Down Expand Up @@ -116,7 +104,9 @@ public Biz createBiz(BizArchive bizArchive) throws IOException {
@Override
public Biz createBiz(File file) throws IOException {
BizArchive bizArchive;
if (ArkConfigs.isEmbedEnable()) {
boolean unpackBizWhenInstall = Boolean.parseBoolean(ArkConfigs.getStringValue(
UNPACK_BIZ_WHEN_INSTALL, "true"));
if (ArkConfigs.isEmbedEnable() && unpackBizWhenInstall) {
File unpackFile = FileUtils.file(file.getAbsolutePath() + "-unpack");
if (!unpackFile.exists()) {
unpackFile = FileUtils.unzip(file, file.getAbsolutePath() + "-unpack");
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,40 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

import static com.alipay.sofa.ark.api.ArkConfigs.putStringValue;
import static com.alipay.sofa.ark.container.service.ArkServiceContainerHolder.getContainer;
import static com.alipay.sofa.ark.spi.constant.Constants.ARK_PLUGIN_MARK_ENTRY;
import static com.alipay.sofa.ark.spi.constant.Constants.MASTER_BIZ;
import static com.alipay.sofa.ark.spi.constant.Constants.*;
import static com.alipay.sofa.ark.spi.constant.Constants.UNPACK_BIZ_WHEN_INSTALL;
import static java.lang.Thread.currentThread;
import static org.junit.Assert.assertNotNull;

Expand Down Expand Up @@ -82,6 +82,32 @@ public void test() throws Throwable {
"com/alipay/sofa/ark/container/service/biz/"));
}

@Test
public void testCreateBizWithoutBizOperation() throws IOException {
String originalEmbed = System.getProperty(EMBED_ENABLE);
String originalUnpack = System.getProperty(UNPACK_BIZ_WHEN_INSTALL);
try {
ClassLoader cl = currentThread().getContextClassLoader();
URL sampleBiz = cl.getResource("sample-biz.jar");
System.setProperty(EMBED_ENABLE, "true");
System.setProperty(UNPACK_BIZ_WHEN_INSTALL, "false");
Biz biz = bizFactoryService.createBiz(FileUtils.file(sampleBiz.getFile()));
assertNotNull(biz);

} finally {
if (originalEmbed != null) {
System.setProperty(EMBED_ENABLE, originalEmbed);
} else {
System.clearProperty(EMBED_ENABLE);
}
if (originalUnpack != null) {
System.setProperty(UNPACK_BIZ_WHEN_INSTALL, originalUnpack);
} else {
System.clearProperty(UNPACK_BIZ_WHEN_INSTALL);
}
}
}

@Test
public void testCreateBiz() throws IOException {
ClassLoader cl = currentThread().getContextClassLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void testLoadClassFromPluginClassLoader() throws Exception {
.setExportClasses("")
.setExportPackages(ClassUtils.getPackageName(ITest.class.getName()))
.setImportResources(StringUtils.EMPTY_STRING)
.setExportResources("META-INF/services/sofa-ark/com.alipay.sofa.ark.container.service.extension.spi.ServiceB")
.setExportResources("META-INF/services/sofa-ark/com.alipay.sofa.ark.container.service.extension.spi.ServiceB, Sample_Resource_Exported_A")
.setPluginClassLoader(
new PluginClassLoader(pluginA.getPluginName(), pluginA.getClassPath()));

Expand All @@ -157,7 +157,7 @@ public void testLoadClassFromPluginClassLoader() throws Exception {
.setExportClasses("com.alipay.sofa.ark.sample.common.SampleClassExported,org.aopalliance.aop.Advice")
.setExportPackages("")
.setImportResources(StringUtils.EMPTY_STRING)
.setExportResources("Sample_Resource_Exported, META-INF/spring/service.xml")
.setExportResources("Sample_Resource_Exported, META-INF/spring/service.xml, Sample_Resource_Exported_A")
.setPluginClassLoader(
new PluginClassLoader(pluginB.getPluginName(), pluginB.getClassPath()));

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public class Constants {
/**
* Event
*/
public final static String BIZ_EVENT_TOPIC_AFTER_INVOKE_ALL_BIZ_START = "AFTER-INVOKE-ALL-BIZ-START";
public final static String BIZ_EVENT_TOPIC_AFTER_INVOKE_BIZ_START = "AFTER-INVOKE-BIZ-START";
public final static String BIZ_EVENT_TOPIC_AFTER_BIZ_FAILED = "BIZ_EVENT_TOPIC_AFTER_BIZ_FAILED";
public final static String BIZ_EVENT_TOPIC_AFTER_INVOKE_BIZ_STOP = "AFTER-INVOKE-BIZ-STOP";
Expand Down Expand Up @@ -203,12 +204,18 @@ 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
*/
public final static String AUTO_UNINSTALL_WHEN_FAILED_ENABLE = "sofa.ark.auto.uninstall.when.failed.enable";

/**
* unpack the biz when install
*/
public final static String UNPACK_BIZ_WHEN_INSTALL = "sofa.ark.unpack.biz.when.install";

/**
* support multiple version biz as activated
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.ark.spi.event.biz;

import com.alipay.sofa.ark.spi.constant.Constants;
import com.alipay.sofa.ark.spi.event.AbstractArkEvent;
import com.alipay.sofa.ark.spi.model.Biz;

/**
* @author lvjing2 (leojames.googol@gmail.com) 2024/12/1 6:17 PM
* @since v2.2.16
* this only used when the base restart to recovery all the bizs which had been installed before the restart
**/
public class AfterAllBizStartupEvent extends AbstractArkEvent<Biz> {

public AfterAllBizStartupEvent() {
super(null);
this.topic = Constants.BIZ_EVENT_TOPIC_AFTER_INVOKE_ALL_BIZ_START;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,17 @@ public URL postFindResource(String name, ClassLoaderService classLoaderService,
if (resourceUrl != null && biz.isDeclared(resourceUrl, name)) {
return resourceUrl;
}
return null;

Enumeration<URL> matchResourceUrls = postFindResources(name, classLoaderService, biz);

// get the first resource url when match multiple resources
if (matchResourceUrls != null && matchResourceUrls.hasMoreElements()) {
return matchResourceUrls.nextElement();
}
} catch (Exception e) {
return null;
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ public void testLoadClassFromClassLoaderHook() throws Exception {
Assert.assertNotNull(bizModel.getBizClassLoader().getResource(
"sample-ark-1.0.0-ark-biz.jar"));

// case 7: find resource from master in jar
Assert.assertNotNull(bizModel.getBizClassLoader().getResource("Sample_Resource_Exported"));
// case 7: find resource from master in multiple jar
Assert.assertNull(bizModel.getBizClassLoader().getResource("Sample_Resource_Exported_A"));

// case 8: find resources from master but not set provided in biz model
Assert.assertFalse(bizModel.getBizClassLoader().getResources("org/slf4j/ILoggerFactory.class")
Expand Down

0 comments on commit 2fea83a

Please sign in to comment.