-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
Signed-off-by: Yaxx19 <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,44 @@ | ||
package io.sermant.core.plugin.agent.enhance;public class AbstractClassLoaderInterceptor { | ||
package io.sermant.core.plugin.agent.enhance; | ||
Check failure on line 1 in sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/AbstractClassLoaderInterceptor.java GitHub Actions / Checkstyle
|
||
|
||
import io.sermant.core.config.ConfigManager; | ||
import io.sermant.core.plugin.agent.interceptor.Interceptor; | ||
import io.sermant.core.service.inject.config.InjectConfig; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* ClassLoader 增强抽象类 | ||
* | ||
* @author Yaxx19 | ||
* @since 2024-06-04 | ||
*/ | ||
public abstract class AbstractClassLoaderInterceptor implements Interceptor { | ||
|
||
private final Set<String> essentialPackage; | ||
|
||
/** | ||
* constructor | ||
*/ | ||
public AbstractClassLoaderInterceptor() { | ||
essentialPackage = ConfigManager.getConfig(InjectConfig.class).getEssentialPackage(); | ||
} | ||
|
||
protected boolean isSermantClass(String name) { | ||
Check failure on line 26 in sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/AbstractClassLoaderInterceptor.java GitHub Actions / Checkstyle
|
||
for (String prefix : essentialPackage) { | ||
if (name.startsWith(prefix)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
protected boolean isSermantResource(String path) { | ||
Check failure on line 35 in sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/AbstractClassLoaderInterceptor.java GitHub Actions / Checkstyle
|
||
String name = path.replace('/', '.'); | ||
for (String prefix : essentialPackage) { | ||
if (name.startsWith(prefix)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,50 @@ | ||
package io.sermant.core.plugin.agent.enhance;public class WebappClassLoaderGetResourceAsStreamInterceptor { | ||
package io.sermant.core.plugin.agent.enhance; | ||
Check failure on line 1 in sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/plugin/agent/enhance/WebappClassLoaderGetResourceAsStreamInterceptor.java GitHub Actions / Checkstyle
|
||
|
||
import io.sermant.core.classloader.ClassLoaderManager; | ||
import io.sermant.core.common.LoggerFactory; | ||
import io.sermant.core.plugin.agent.entity.ExecuteContext; | ||
|
||
import java.net.URL; | ||
import java.util.Optional; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* 发行版Tomcat ClassLoader getResourceAsStream 增强 | ||
* | ||
* @author Yaxx19 | ||
* @since 2024-06-04 | ||
*/ | ||
public class WebappClassLoaderGetResourceAsStreamInterceptor extends AbstractClassLoaderInterceptor { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(); | ||
|
||
@Override | ||
public ExecuteContext before(ExecuteContext context) throws Exception { | ||
return context; | ||
} | ||
|
||
@Override | ||
public ExecuteContext after(ExecuteContext context) throws Exception { | ||
if (context.getResult() != null) { | ||
return context; | ||
} | ||
|
||
String path = (String) context.getArguments()[0]; | ||
if (isSermantResource(path)) { | ||
Optional<URL> url = ClassLoaderManager.getPluginClassFinder().findSermantResource(path); | ||
if (!url.isPresent()) { | ||
LOGGER.log(Level.WARNING, "Can not get resource stream [{0}] by sermant.And then find by {1}. ", | ||
new Object[]{path, context.getObject()}); | ||
} else { | ||
context.changeResult(url.get().openStream()); | ||
LOGGER.log(Level.INFO, "Get resource stream: {0} successfully by sermant.", path); | ||
} | ||
} | ||
return context; | ||
} | ||
|
||
@Override | ||
public ExecuteContext onThrow(ExecuteContext context) throws Exception { | ||
return context; | ||
} | ||
} |