Skip to content
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 @@ -31,9 +31,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.util.Collection;
import java.util.Map;
import java.util.jar.JarFile;

/**
* ShardingSphere agent.
Expand All @@ -51,9 +49,8 @@ public final class ShardingSphereAgent {
public static void premain(final String args, final Instrumentation instrumentation) throws IOException {
File rootPath = AgentPath.getRootPath();
Map<String, PluginConfiguration> pluginConfigs = PluginConfigurationLoader.load(rootPath);
Collection<JarFile> pluginJars = PluginJarLoader.load(rootPath);
Map<String, AdvisorConfiguration> advisorConfigs = AdvisorConfigurationLoader.load(pluginJars, pluginConfigs.keySet());
AgentBuilderFactory.create(pluginConfigs, pluginJars, advisorConfigs, isEnhancedForProxy()).installOn(instrumentation);
Map<String, AdvisorConfiguration> advisorConfigs = AdvisorConfigurationLoader.load(PluginJarLoader.load(rootPath), pluginConfigs.keySet());
AgentBuilderFactory.create(pluginConfigs, PluginJarLoader.load(rootPath), advisorConfigs, isEnhancedForProxy()).installOn(instrumentation);
}

private static boolean isEnhancedForProxy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -50,18 +49,16 @@ public final class AdvisorConfigurationLoader {
*/
public static Map<String, AdvisorConfiguration> load(final Collection<JarFile> pluginJars, final Collection<String> pluginTypes) {
Map<String, AdvisorConfiguration> result = new HashMap<>();
AgentPluginClassLoader agentPluginClassLoader = new AgentPluginClassLoader(Thread.currentThread().getContextClassLoader(), pluginJars);
for (String each : pluginTypes) {
InputStream advisorsResourceStream = getResourceStream(agentPluginClassLoader, each);
if (null == advisorsResourceStream) {
LOGGER.log(Level.WARNING, "The configuration file for advice of plugin `{0}` is not found", new String[]{each});
}
Optional.ofNullable(advisorsResourceStream)
.ifPresent(optional -> mergeConfigurations(result, YamlAdvisorsConfigurationSwapper.swap(YamlAdvisorsConfigurationLoader.load(optional), each)));
if (null != advisorsResourceStream) {
try {
advisorsResourceStream.close();
} catch (final IOException ignored) {
try (AgentPluginClassLoader agentPluginClassLoader = new AgentPluginClassLoader(Thread.currentThread().getContextClassLoader(), pluginJars)) {
for (String each : pluginTypes) {
try (InputStream advisorsResourceStream = getResourceStream(agentPluginClassLoader, each)) {
if (null == advisorsResourceStream) {
LOGGER.log(Level.WARNING, "The configuration file for advice of plugin `{0}` is not found", new String[]{each});
continue;
}
mergeConfigurations(result, YamlAdvisorsConfigurationSwapper.swap(YamlAdvisorsConfigurationLoader.load(advisorsResourceStream), each));
} catch (final IOException ex) {
LOGGER.log(Level.WARNING, "Load the configuration file for advice of plugin `{0}` error, reason is: {1}", new String[]{each, ex.getMessage()});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.agent.core.plugin.classloader;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
Expand All @@ -36,7 +37,7 @@
/**
* Agent plugin class loader.
*/
public final class AgentPluginClassLoader extends ClassLoader {
public final class AgentPluginClassLoader extends ClassLoader implements Closeable {

static {
registerAsParallelCapable();
Expand Down Expand Up @@ -94,8 +95,10 @@ private void definePackage(final String name, final Manifest manifest) {
}

private Class<?> defineClass(final String name, final JarFile extraJar, final ZipEntry entry) throws IOException {
byte[] data = toByteArray(extraJar.getInputStream(entry));
return defineClass(name, data, 0, data.length);
try (InputStream inputStream = extraJar.getInputStream(entry)) {
byte[] data = toByteArray(inputStream);
return defineClass(name, data, 0, data.length);
}
}

private static byte[] toByteArray(final InputStream inStream) throws IOException {
Expand All @@ -109,7 +112,6 @@ private static byte[] toByteArray(final InputStream inStream) throws IOException
}
} finally {
result.close();
inStream.close();
}
return result.toByteArray();
}
Expand Down Expand Up @@ -139,4 +141,14 @@ private Optional<URL> findResource(final String name, final JarFile extraJar) {
return Optional.empty();
}
}

@Override
public void close() {
for (JarFile each : extraJars) {
try {
each.close();
} catch (final IOException ignored) {
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ class AgentPathTest {
@Test
void assertGetRootPath() throws IOException {
Path jarPath = createAgentJar(tempDir.resolve("agent-path.jar"));
URLClassLoader customClassLoader = new URLClassLoader(new URL[]{jarPath.toUri().toURL()}, null);
try (SystemClassLoaderContext ignored = new SystemClassLoaderContext(customClassLoader)) {
try (
URLClassLoader customClassLoader = new URLClassLoader(new URL[]{jarPath.toUri().toURL()}, null);
SystemClassLoaderContext ignored = new SystemClassLoaderContext(customClassLoader)) {
assertThat(AgentPath.getRootPath(), is(jarPath.getParent().toFile()));
}
}
Expand Down