Skip to content

Commit

Permalink
Fixing appclient and Ant tests
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <[email protected]>
  • Loading branch information
dmatej committed Dec 6, 2024
1 parent 280c10d commit 7a9173b
Show file tree
Hide file tree
Showing 152 changed files with 2,587 additions and 3,039 deletions.
52 changes: 11 additions & 41 deletions appserver/appclient/client/acc-standalone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.glassfish.main.appclient</groupId>
<artifactId>client</artifactId>
<version>7.0.21-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>gf-client</artifactId>
Expand All @@ -35,9 +36,9 @@
This module builds the free-standing JAR file for the app client
container which can be run without using an OSGi implementation.

This module contains little source of its own but mostly packages a JAR
with a manifest Class-Path that specifies all the OSGi JARs on which
it directly or indirectly depends.
However it still depends on many of jars in the modules directory.
The class loader hierarchy tries to reduce efforts required to run
the Application Client especially related to the classpath.
</description>

<developers>
Expand All @@ -54,46 +55,15 @@
</developers>

<dependencies>
<!-- Dependencies are used just to generate the Class-Path attribute of the manifest -->
<dependency>
<groupId>org.glassfish.main.appclient</groupId>
<artifactId>gf-client-module</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion><!-- weld-se-shaded has an explicit path, would cause redundant line -->
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-shaded</artifactId>
</exclusion>
</exclusions>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.main.jdbc</groupId>
<artifactId>jdbc-runtime</artifactId>
<groupId>org.glassfish.main.common</groupId>
<artifactId>simple-glassfish-api</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.glassfish.main.resources</groupId>
<artifactId>resources-runtime</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.glassfish.main.persistence</groupId>
<artifactId>entitybean-container</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-osgi-bundle</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
<optional>true</optional>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -18,17 +18,10 @@
package org.glassfish.appclient.client.acc.agent;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandlerFactory;
import java.security.PrivilegedAction;
import java.util.Enumeration;

import org.glassfish.appclient.common.ClassPathUtils;

import static java.security.AccessController.doPrivileged;
import java.util.Arrays;

/**
* Used as the system class loader during app client launch.
Expand All @@ -40,75 +33,51 @@
* <p>
* This class and it's dependencies must not use logging, which could cause recursion in class
* loading. So don't extend GlassfishUrlClassLoader. Reproducer: TCK tests use this classloader.
* <p>
* The name of this class must not be changed - it is explicitly used in the TCK Platform Test
* package.
*
* @author tjquinn
* @author David Matejcek
*/
public class ACCAgentClassLoader extends URLClassLoader {

private boolean isActive = true;
static {
registerAsParallelCapable();
}

/**
* This constructor is used by the VM to create a system class loader (as specified by -Djava.system.class.loader on the
* java command created from the appclient script).
* This constructor is used by the VM to create a system class loader (as specified by
* -Djava.system.class.loader on the java command created from the appclient script).
* <p>
* This class loader ignores the parent and uses {@link GFDependenciesClassLoader}
* with the {@link ClassLoader#getPlatformClassLoader()} as its parent instead.
*/
public ACCAgentClassLoader(ClassLoader parent) {
super(new URL[] {}, prepareLoader(parent));
}


private static URLClassLoader prepareLoader(ClassLoader parent) {
PrivilegedAction<URLClassLoader> action = () -> new URLClassLoader(
new URL[] {ClassPathUtils.getGFClientJarURL()}, new ClassLoaderWrapper(parent));
return doPrivileged(action);
}


public ACCAgentClassLoader(URL[] urls) {
super(urls);
}

public ACCAgentClassLoader(URL[] urls, ClassLoader parent) {
super(urls, new ClassLoaderWrapper(parent));
}

public ACCAgentClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
super(urls, parent, factory);
super("Agent", new URL[0], new UserClassLoader());
}

// a custom system class loader need to define this method in order to load the java agent.
/**
* A custom system class loader need to define this method in order to load the java agent.
*
* @param path
* @throws MalformedURLException
*/
public void appendToClassPathForInstrumentation(String path) throws MalformedURLException {
addURL(new File(path).toURI().toURL());
}

/**
* Returns class name, hash code and list of managed urls and info about parent.
*/
@Override
public synchronized Class<?> loadClass(String name) throws ClassNotFoundException {
if (isActive && isStillActive()) {
return super.loadClass(name);
}
return getParent().loadClass(name);
}

@Override
public URL getResource(String name) {
if (isActive && isStillActive()) {
return super.getResource(name);
}
return getParent().getResource(name);
}

@Override
public Enumeration<URL> getResources(String name) throws IOException {
if (isActive && isStillActive()) {
return super.getResources(name);
}
return getParent().getResources(name);
}

private boolean isStillActive() {
if (isActive) {
String propValue = System.getProperty("org.glassfish.appclient.acc.agentLoaderDone");
isActive = (propValue != null);
}
return isActive;
public String toString() {
final StringBuilder text = new StringBuilder(1024);
text.append(getClass().getName()).append('@').append(Integer.toHexString(hashCode()));
text.append("[name=").append(getName()).append("], urls=[\n");
Arrays.stream(getURLs()).forEach(u -> text.append(u).append('\n'));
text.append(']');
text.append(", parent=").append(getParent());
return text.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -18,17 +19,16 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;

import org.glassfish.appclient.client.AppClientFacade;
import org.glassfish.appclient.client.acc.UserError;

import static java.util.logging.Level.FINE;
import static org.glassfish.appclient.client.CLIBootstrap.FILE_OPTIONS_INTRODUCER;
import static org.glassfish.appclient.client.acc.agent.CLIBootstrap.FILE_OPTIONS_INTRODUCER;

/**
* Agent which prepares the ACC before the VM launches the selected main program.
Expand All @@ -48,26 +48,29 @@
*/
public class AppClientContainerAgent {

private static Logger logger = Logger.getLogger(AppClientContainerAgent.class.getName());
private static final Logger LOG = Logger.getLogger(AppClientContainerAgent.class.getName());

public static void premain(String agentArgsText, Instrumentation instrumentation) {
ClassLoader loader = ClassLoader.getSystemClassLoader().getParent();
try {
long now = System.currentTimeMillis();

/*
* The agent prepares the ACC but does not launch the client.
*/
AppClientFacade.prepareACC(optionsValue(agentArgsText), instrumentation);

logger.fine("AppClientContainerAgent finished after " + (System.currentTimeMillis() - now) + " ms");

} catch (UserError ue) {
ue.displayAndExit();
} catch (Exception e) {
// The agent prepares the ACC but does not launch the client.
// The thread class loader is used in init method.
Thread.currentThread().setContextClassLoader(loader);
Class<?> containerInitClass = loader.loadClass("org.glassfish.appclient.client.AppClientContainerHolder");
Method initContainer = containerInitClass.getMethod("init", String.class, Instrumentation.class);
try {
initContainer.invoke(null, optionsValue(agentArgsText), instrumentation);
} catch (InvocationTargetException e) {
throw e.getCause();
}

LOG.fine("AppClientContainerAgent finished after " + (System.currentTimeMillis() - now) + " ms");
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}

}

private static String optionsValue(final String agentArgsText) throws FileNotFoundException, IOException {
Expand All @@ -79,19 +82,14 @@ private static String optionsValue(final String agentArgsText) throws FileNotFou
return agentArgsText;
}

File argsFile = new File(agentArgsText.substring(FILE_OPTIONS_INTRODUCER.length()));
String result;

try (LineNumberReader reader = new LineNumberReader(new FileReader(argsFile))) {
result = reader.readLine();
}
final Path argsFile = new File(agentArgsText.substring(FILE_OPTIONS_INTRODUCER.length())).toPath();
final String result = Files.readString(argsFile).trim();

if (Boolean.getBoolean("keep.argsfile")) {
System.err.println("Agent arguments file retained: " + argsFile.getAbsolutePath());
} else if (!argsFile.delete()) {
logger.log(FINE, "Unable to delete temporary args file {0}; continuing", argsFile.getAbsolutePath());
System.err.println("Agent arguments file retained: " + argsFile);
} else if (!Files.deleteIfExists(argsFile)) {
LOG.log(FINE, "Unable to delete temporary args file {0}; continuing", argsFile);
}

return result;
}
}
Loading

0 comments on commit 7a9173b

Please sign in to comment.