Skip to content

Commit

Permalink
plugin default export package without activator, modify to set (#818)
Browse files Browse the repository at this point in the history
* support delegate from classLoaderHook for getResourceAsStream

* Revert "support delegate from classLoaderHook for getResourceAsStream"

This reverts commit 5bd306b.

* fix typo

* plugin default export package without activator, modify to set

* fix format
  • Loading branch information
lvjing2 committed Jan 2, 2024
1 parent 3bcf96f commit 9c5ce37
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,4 @@ public boolean removeAndAddBiz(Biz addingBiz, Biz removingBiz) {
public ConcurrentHashMap<String, ConcurrentHashMap<String, Biz>> getBizRegistration() {
return bizRegistration;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.ark.container.service.classloader;

import com.alipay.sofa.ark.api.ArkClient;
import com.alipay.sofa.ark.bootstrap.AgentClassLoader;
import com.alipay.sofa.ark.common.log.ArkLogger;
import com.alipay.sofa.ark.common.log.ArkLoggerFactory;
Expand Down Expand Up @@ -286,6 +287,12 @@ public ClassLoader getBizClassLoader(String bizIdentity) {
return biz == null ? null : biz.getBizClassLoader();
}

@Override
public ClassLoader getMasterBizClassLoader() {
Biz biz = ArkClient.getMasterBiz();
return biz == null ? null : biz.getBizClassLoader();
}

@Override
public ClassLoader getPluginClassLoader(String pluginName) {
Plugin plugin = pluginManagerService.getPluginByName(pluginName);
Expand Down Expand Up @@ -391,4 +398,4 @@ public boolean isDeniedImportResource(String bizIdentity, String resourceName) {
public int getPriority() {
return DEFAULT_PRECEDENCE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ public static String getPackageName(String className) {
* @param classNames class name list
* @return common package path
*/
public static List<String> findCommonPackage(List<String> classNames) {
public static Set<String> findCommonPackage(Set<String> classNames) {
Set<String> packages = new HashSet<>();

if (classNames == null || classNames.isEmpty()) {
return new ArrayList<>(packages);
return new HashSet<>(packages);
}

classNames.forEach(className -> packages.add(getPackageName(className)));
// delete default package
packages.remove(".");
return new ArrayList<>(packages);
return new HashSet<>(packages);
}

/**
Expand Down Expand Up @@ -114,4 +114,4 @@ public static String getCodeBase(Class<?> cls) {
}
return location.getFile();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

import static com.alipay.sofa.ark.common.util.ClassUtils.*;
import static com.alipay.sofa.ark.spi.constant.Constants.DEFAULT_PACKAGE;
Expand All @@ -42,7 +42,7 @@ public void testGetPackageName() {
@Test
public void testFindCommonPackage() {
assertEquals(findCommonPackage(null).size(), 0);
List<String> classNames = new ArrayList<>();
Set<String> classNames = new HashSet<>();
classNames.add("com.example.project.subpackage1.classE");
classNames.add("com.example.project.classA");
classNames.add("com.example.project.classB");
Expand All @@ -63,7 +63,7 @@ public void testCollectClasses() throws Exception {
return;
}

List<String> classNames = collectClasses(dir2);
Set<String> classNames = new HashSet<>(collectClasses(dir2));
assertTrue(classNames.contains("com.alipay.sofa.ark.common.util.ClassUtils"));
assertTrue(findCommonPackage(classNames).contains("com.alipay.sofa.ark.common.util"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public interface ClassLoaderService extends ArkService {
*/
ClassLoader getBizClassLoader(String bizIdentity);

/**
* Get Ark Master Biz ClassLoader
* @return
*/
ClassLoader getMasterBizClassLoader();

/**
* Get Ark Plugin ClassLoader
* @param pluginName
Expand All @@ -160,4 +166,4 @@ public interface ClassLoaderService extends ArkService {
* @return
*/
boolean isDeniedImportResource(String bizIdentity, String resourceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Set;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.stream.Collectors;

import com.alipay.sofa.ark.common.util.ClassUtils;
import com.alipay.sofa.ark.common.util.StringUtils;
Expand Down Expand Up @@ -449,7 +450,7 @@ private Properties collectArkPluginExport() throws MojoExecutionException {
exported = new ExportConfig();
}
if (exportPackage) {
List<String> projectPackages = findProjectPackages();
Set<String> projectPackages = findProjectPackages();
for (String projectPackage : projectPackages) {
if (!StringUtils.isEmpty(projectPackage)) {
exported.addPackage(projectPackage + ".*");
Expand All @@ -460,17 +461,20 @@ private Properties collectArkPluginExport() throws MojoExecutionException {
return properties;
}

private List<String> findProjectPackages() throws MojoExecutionException {
private Set<String> findProjectPackages() throws MojoExecutionException {
try {
// Accessing the target/classes directory where compiled classes are located
File outputDirectory = new File(project.getBuild().getOutputDirectory());
// Ensure the directory exists
if (outputDirectory.exists()) {
return ClassUtils.findCommonPackage(ClassUtils.collectClasses(outputDirectory));
Set<String> classes = new HashSet<>(ClassUtils.collectClasses(outputDirectory));
classes = classes.stream().filter(className -> !className.equals(this.activator)).collect(
Collectors.toSet());
return ClassUtils.findCommonPackage(classes);
} else {
getLog().warn("Output directory does not exist!");
}
return new ArrayList<>();
return new HashSet<>();
} catch (IOException e) {
throw new MojoExecutionException("Error finding compiled classes", e);
}
Expand Down Expand Up @@ -520,4 +524,4 @@ private void addArkPluginConfig(Archiver archiver, String path, LinkedProperties
private void addArkPluginMark(Archiver archiver) throws MojoExecutionException {
addArkPluginConfig(archiver, Constants.ARK_PLUGIN_MARK_ENTRY, new LinkedProperties());
}
}
}

0 comments on commit 9c5ce37

Please sign in to comment.