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

Fix validation error when using AppClassLoader in over JDK9 #714

Merged
Merged
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 @@ -25,7 +25,6 @@

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLClassLoader;

import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -67,8 +66,8 @@ public void download(@PathVariable String fileName, HttpServletResponse response
@GetMapping("")
public String download(ModelMap model) {
try {
final File monitorPackage = agentPackageService.createPackage(monitorPackageHandler, (URLClassLoader) getClass().getClassLoader(),
"", null, config.getMonitorPort(), "");
final File monitorPackage = agentPackageService.createPackage(monitorPackageHandler, "",
null, config.getMonitorPort(), "");
model.clear();
return "redirect:/monitor/download/" + monitorPackage.getName();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

import java.io.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Set;

import lombok.RequiredArgsConstructor;

import static net.grinder.util.AbstractGrinderClassPathProcessor.getClassPaths;
import static org.apache.commons.lang.StringUtils.isNotEmpty;
import static org.ngrinder.common.util.EncodingUtils.decodePathWithUTF8;
import static org.ngrinder.common.util.StringUtils.replaceLast;
Expand All @@ -39,15 +39,15 @@ public class AgentPackageService {
*
* @return File package.
*/
public File createPackage(PackageHandler packageHandler, URLClassLoader classLoader, String regionName, String connectionIP, int port, String owner) {
public File createPackage(PackageHandler packageHandler, String regionName, String connectionIP, int port, String owner) {
synchronized (AgentPackageService.class) {
File packageFile = packageHandler.getPackageFile(regionName, connectionIP, owner, false);
if (packageFile.exists()) {
return packageFile;
}
FileUtils.deleteQuietly(packageFile);
try (TarArchiveOutputStream tarOutputStream = createTarArchiveStream(packageFile)) {
addDependentLibToTarStream(packageHandler, tarOutputStream, classLoader);
addDependentLibToTarStream(packageHandler, tarOutputStream);
if (!(packageHandler instanceof AgentPackageHandler) || isNotEmpty(connectionIP)) {
packageHandler.addConfigToPackage(tarOutputStream, packageHandler.getConfigParam(regionName, connectionIP, port, owner));
}
Expand Down Expand Up @@ -77,19 +77,16 @@ public File createAgentPackage() {
*/
public File createAgentPackage(String region, String connectionIP, int port, String owner) {
synchronized (AgentPackageService.class) {
return createPackage(agentPackageHandler, (URLClassLoader) getClass().getClassLoader(), region, connectionIP, port, owner);
return createPackage(agentPackageHandler, region, connectionIP, port, owner);
}
}

private void addDependentLibToTarStream(PackageHandler packageHandler, TarArchiveOutputStream tarOutputStream, URLClassLoader classLoader) throws IOException {
if (classLoader == null) {
classLoader = (URLClassLoader) getClass().getClassLoader();
}
private void addDependentLibToTarStream(PackageHandler packageHandler, TarArchiveOutputStream tarOutputStream) throws IOException {
packageHandler.addBaseFolderToPackage(tarOutputStream);
Set<String> libs = packageHandler.getPackageDependentLibs(classLoader);
Set<String> libs = packageHandler.getPackageDependentLibs();
packageHandler.copyShellFile(tarOutputStream);

for (URL eachUrl : classLoader.getURLs()) {
for (URL eachUrl : getClassPaths(getClass().getClassLoader())) {
File eachClassPath = new File(decodePathWithUTF8(eachUrl.getFile()));
if (!isJar(eachClassPath)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.URLClassLoader;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -74,8 +73,8 @@ public Map<String, Object> getConfigParam(String regionName, String controllerIP
}

@Override
public Set<String> getPackageDependentLibs(URLClassLoader urlClassLoader) {
Set<String> libs = getDependentLibs(urlClassLoader);
public Set<String> getPackageDependentLibs() {
Set<String> libs = getDependentLibs();
libs.add("ngrinder-core");
libs.add("ngrinder-runtime");
libs.add("ngrinder-groovy");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.stereotype.Component;

import java.net.URLClassLoader;
import java.util.Map;
import java.util.Set;

Expand All @@ -17,8 +16,8 @@ public Map<String, Object> getConfigParam(String regionName, String controllerIP
}

@Override
public Set<String> getPackageDependentLibs(URLClassLoader urlClassLoader) {
return super.getDependentLibs(urlClassLoader);
public Set<String> getPackageDependentLibs() {
return getDependentLibs();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,37 @@
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.*;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static freemarker.template.Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS;
import static java.nio.charset.Charset.defaultCharset;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang.StringUtils.isNotEmpty;
import static org.apache.commons.lang.StringUtils.trimToEmpty;
import static org.ngrinder.common.util.CompressionUtils.*;
import static org.ngrinder.common.util.ExceptionUtils.processException;

public abstract class PackageHandler {

protected final int TIME_MILLIS_OF_DAY = 1000 * 60 * 60 * 24;
private final int EXEC = 0x81ed;
private static final Logger LOGGER = LoggerFactory.getLogger(PackageHandler.class);
private static final int EXEC = 0x81ed;

protected static final int TIME_MILLIS_OF_DAY = 1000 * 60 * 60 * 24;

@Autowired
private Config config;

private Logger LOGGER = LoggerFactory.getLogger(PackageHandler.class);

protected Set<String> getDependentLibs(URLClassLoader urlClassLoader) {
protected Set<String> getDependentLibs() {
Set<String> libs = new HashSet<>();
try (InputStream dependencyStream = urlClassLoader.getResourceAsStream(this.getDependenciesFileName())) {
final String dependencies = IOUtils.toString(dependencyStream, defaultCharset());
try (InputStream dependencyStream = getClass().getClassLoader().getResourceAsStream(getDependenciesFileName())) {
final String dependencies = IOUtils.toString(requireNonNull(dependencyStream), defaultCharset());
for (String each : StringUtils.split(dependencies, ";")) {
libs.add(each.trim().replace("-SNAPSHOT", ""));
}
} catch (Exception e) {
LOGGER.error("Error while loading " + this.getDependenciesFileName(), e);
LOGGER.error("Error while loading " + getDependenciesFileName(), e);
}
return libs;
}
Expand Down Expand Up @@ -172,7 +171,7 @@ private String getPackageName() {

public abstract Map<String, Object> getConfigParam(String regionName, String controllerIP, int port, String owner);

public abstract Set<String> getPackageDependentLibs(URLClassLoader urlClassLoader);
public abstract Set<String> getPackageDependentLibs();

protected abstract String getModuleName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

import java.io.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;

import static net.grinder.util.AbstractGrinderClassPathProcessor.getClassPaths;
import static org.ngrinder.common.constants.GrinderConstants.GRINDER_PROP_JVM_CLASSPATH;
import static org.ngrinder.common.util.EncodingUtils.decodePathWithUTF8;
import static org.ngrinder.common.util.NoOp.noOp;
Expand Down Expand Up @@ -232,12 +232,12 @@ private String getHomeLibraryPath(String classPath) {
}

private boolean isRunningOnWas() {
return ((URLClassLoader) LocalScriptTestDriveService.class.getClassLoader()).getURLs()[0].getProtocol().equals("jar");
return getClassPaths(LocalScriptTestDriveService.class.getClassLoader())[0].getProtocol().equals("jar");
}

private String runtimeClassPath() {
StringBuilder runtimeClassPath = new StringBuilder();
for (URL url : ((URLClassLoader) LocalScriptTestDriveService.class.getClassLoader()).getURLs()) {
for (URL url : getClassPaths(LocalScriptTestDriveService.class.getClassLoader())) {
if (url.getPath().contains("ngrinder-runtime") || url.getPath().contains("ngrinder-groovy")) {
runtimeClassPath.append(decodePathWithUTF8(url.getFile())).append(File.pathSeparator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import org.slf4j.Logger;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import static org.ngrinder.common.util.CollectionUtils.newArrayList;
import static org.ngrinder.common.util.EncodingUtils.decodePathWithUTF8;
Expand Down Expand Up @@ -209,9 +212,9 @@ private boolean isUsefulJar(String jarFilename) {
* @return classpath optimized for grinder.
*/
public String buildForemostClasspathBasedOnCurrentClassLoader(Logger logger) {
URL[] urLs = ((URLClassLoader) AbstractGrinderClassPathProcessor.class.getClassLoader()).getURLs();
URL[] urls = getClassPaths(AbstractGrinderClassPathProcessor.class.getClassLoader());
StringBuilder builder = new StringBuilder();
for (URL each : urLs) {
for (URL each : urls) {
builder.append(decodePathWithUTF8(each.getFile())).append(File.pathSeparator);
}
return filterForeMostClassPath(builder.toString(), logger);
Expand All @@ -224,22 +227,41 @@ public String buildForemostClasspathBasedOnCurrentClassLoader(Logger logger) {
* @return classpath optimized for grinder.
*/
public String buildPatchClasspathBasedOnCurrentClassLoader(Logger logger) {
URL[] urLs = ((URLClassLoader) AbstractGrinderClassPathProcessor.class.getClassLoader()).getURLs();
URL[] urls = getClassPaths(AbstractGrinderClassPathProcessor.class.getClassLoader());
StringBuilder builder = new StringBuilder();
for (URL each : urLs) {
for (URL each : urls) {
builder.append(decodePathWithUTF8(each.getFile())).append(File.pathSeparator);
}
return filterPatchClassPath(builder.toString(), logger);
}

public static URL[] getClassPaths(ClassLoader classLoader) {
if (classLoader instanceof URLClassLoader) {
return ((URLClassLoader) classLoader).getURLs();
}
return Stream
.of(ManagementFactory.getRuntimeMXBean().getClassPath().split(File.pathSeparator))
.map(AbstractGrinderClassPathProcessor::toURL)
.toArray(URL[]::new);
}

private static URL toURL(String classPathEntry) {
try {
return new File(classPathEntry).toURI().toURL();
} catch (MalformedURLException ex) {
throw new IllegalArgumentException(
"URL could not be created from '" + classPathEntry + "'", ex);
}
}

/**
* Construct classPath from current classLoader.
*
* @param logger logger
* @return classpath optimized for grinder.
*/
public String buildClasspathBasedOnCurrentClassLoader(Logger logger) {
URL[] urls = ((URLClassLoader) AbstractGrinderClassPathProcessor.class.getClassLoader()).getURLs();
URL[] urls = getClassPaths(AbstractGrinderClassPathProcessor.class.getClassLoader());

StringBuilder builder = new StringBuilder();
for (URL each : urls) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed 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 org.ngrinder.common.util;

public abstract class TypeConvertUtils {

/**
* Convert the given object to the inferred return type.
*
* @param object object to be converted.
* @param <T> converted type
* @return converted object.
*/
@SuppressWarnings("unchecked")
public static <T> T cast(Object object) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

return (T) object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import java.util.List;
import java.util.SortedSet;

import static com.sun.jmx.mbeanserver.Util.cast;
import static java.util.Date.from;
import static org.apache.commons.lang.ObjectUtils.defaultIfNull;
import static org.ngrinder.common.util.AccessUtils.getSafe;
import static org.ngrinder.common.util.DateUtils.dateToString;
import static org.ngrinder.common.util.DateUtils.ms2Time;
import static org.ngrinder.common.util.TypeConvertUtils.cast;

/**
* Performance Test Entity.
Expand Down