Skip to content

Commit

Permalink
#322, enable to control the exec:java interaction with JVM classloade…
Browse files Browse the repository at this point in the history
…r more finely
  • Loading branch information
rmannibucau committed Sep 8, 2022
1 parent 716a232 commit 1bab77f
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/it/projects/github322/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = clean compile exec:java
43 changes: 43 additions & 0 deletions src/it/projects/github322/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<project>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.codehaus.mojo.exec.it</groupId>
<artifactId>parent</artifactId>
<version>0.1</version>
</parent>

<groupId>org.cb.maven.plugins.exec</groupId>
<artifactId>github322</artifactId>
<version>0.1</version>

<dependencies>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>@pom.version@</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.codehaus.mojo.exec.it.github322.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.codehaus.mojo.exec.it.github322;

import javax.xml.transform.sax.SAXTransformerFactory;

public class Main {
public static void main(final String... args) {
System.out.println(
"Main Result: <" +
(
SAXTransformerFactory.class.getProtectionDomain().getCodeSource() != null ?
SAXTransformerFactory.class.getProtectionDomain().getCodeSource().getLocation() :
null
) +
">");
}
}
20 changes: 20 additions & 0 deletions src/it/projects/github322/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

new IntegrationBase().checkExistenceAndContentOfAFile(new File( basedir, "build.log" ), [ "Main Result: <null>" ])
2 changes: 1 addition & 1 deletion src/it/projects/mexec-29/invoker.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
invoker.goals = clean verify

invoker.debug = true
invoker.buildResult = failure
invoker.buildResult = success
19 changes: 19 additions & 0 deletions src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ public class ExecJavaMojo
@Parameter
private List<String> classpathFilenameExclusions;

/**
* Additional packages to load from the jvm even if a classpath dependency matches.
*
* @since 3.1.1
*/
@Parameter
private List<String> forcedJvmPackages;

/**
* Additional packages to NOT load from the jvm even if it is in a flat classpath.
* Can enable to reproduce a webapp behavior for example where library is loaded over the JVM.
*
* @since 3.1.1
*/
@Parameter
private List<String> excludedJvmPackages;

/**
* Execute goal.
*
Expand Down Expand Up @@ -573,6 +590,8 @@ private URLClassLoader getClassLoader()
.setLogger( getLog() )
.setPaths( classpathURLs )
.setExclusions( classpathFilenameExclusions )
.setForcedJvmPackages( forcedJvmPackages )
.setExcludedJvmPackages( excludedJvmPackages )
.build();
}
catch ( NullPointerException | IOException e )
Expand Down
66 changes: 48 additions & 18 deletions src/main/java/org/codehaus/mojo/exec/URLClassLoaderBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
*/
class URLClassLoaderBuilder
{
private List<String> forcedJvmPackages;
private List<String> excludedJvmPackages;
private Log logger;
private Collection<Path> paths;
private Collection<String> exclusions;
Expand All @@ -57,6 +59,18 @@ static URLClassLoaderBuilder builder()
return new URLClassLoaderBuilder();
}

URLClassLoaderBuilder setExcludedJvmPackages(List<String> excludedJvmPackages )
{
this.excludedJvmPackages = excludedJvmPackages;
return this;
}

URLClassLoaderBuilder setForcedJvmPackages(List<String> forcedJvmPackages )
{
this.forcedJvmPackages = forcedJvmPackages;
return this;
}

URLClassLoaderBuilder setLogger(Log logger )
{
this.logger = logger;
Expand Down Expand Up @@ -99,7 +113,7 @@ URLClassLoader build() throws IOException
}
}

return new ExecJavaClassLoader( urls.toArray( new URL[0] ) );
return new ExecJavaClassLoader( urls.toArray( new URL[0] ), forcedJvmPackages, excludedJvmPackages );
}

// child first strategy
Expand All @@ -118,11 +132,15 @@ private static class ExecJavaClassLoader extends URLClassLoader
}

private final String jre;
private final List<String> forcedJvmPackages;
private final List<String> excludedJvmPackages;

public ExecJavaClassLoader(URL[] urls )
public ExecJavaClassLoader(URL[] urls, List<String> forcedJvmPackages, List<String> excludedJvmPackages )
{
super(urls);
jre = getJre();
this.jre = getJre();
this.forcedJvmPackages = forcedJvmPackages;
this.excludedJvmPackages = excludedJvmPackages;
}

@Override
Expand Down Expand Up @@ -334,64 +352,77 @@ private boolean postLoad( boolean resolve, Class<?> clazz )
return false;
}

// not all jvm classes, for ex "javax" can be overriden so don't list it here
// not all jvm classes, for ex "javax" can be overridden so don't list it them all here (javax.resource for ex)
private boolean isDirectJvmClass(final String name) {
if (excludedJvmPackages != null && excludedJvmPackages.stream().anyMatch( name::startsWith ))
{
return false;
}

if (name.startsWith( "java." ) )
{
return true;
}
if ( name.startsWith( "sun." ) )
else if (name.startsWith( "javax." ) )
{
final String sub = name.substring( "javax.".length() );
if ( sub.startsWith( "xml." ) )
{
return true;
}
}
else if ( name.startsWith( "sun." ) )
{
return true;
}
if ( name.startsWith( "jdk." ) )
else if ( name.startsWith( "jdk." ) )
{
return true;
}
if ( name.startsWith( "oracle." ) )
else if ( name.startsWith( "oracle." ) )
{
return true;
}
if ( name.startsWith( "javafx." ) )
else if ( name.startsWith( "javafx." ) )
{
return true;
}
if ( name.startsWith( "netscape." ) )
else if ( name.startsWith( "netscape." ) )
{
return true;
}
if ( name.startsWith( "org." ) )
else if ( name.startsWith( "org." ) )
{
final String sub = name.substring( "org.".length() );
if ( sub.startsWith( "w3c.dom." ) )
{
return true;
}
if ( sub.startsWith( "omg." ) )
else if ( sub.startsWith( "omg." ) )
{
return true;
}
if ( sub.startsWith( "xml.sax." ) )
else if ( sub.startsWith( "xml.sax." ) )
{
return true;
}
if ( sub.startsWith( "ietf.jgss." ) )
else if ( sub.startsWith( "ietf.jgss." ) )
{
return true;
}
if ( sub.startsWith( "jcp.xml.dsig.internal." ) )
else if ( sub.startsWith( "jcp.xml.dsig.internal." ) )
{
return true;
}
}
if ( name.startsWith( "com." ) )
else if ( name.startsWith( "com." ) )
{
final String sub = name.substring( "com.".length() );
if ( sub.startsWith( "oracle." ) )
{
return true;
}
if ( sub.startsWith( "sun." ) )
else if ( sub.startsWith( "sun." ) )
{
final String subSun = sub.substring( "sun.".length() );
if ( subSun.startsWith( "accessibility." ) )
Expand Down Expand Up @@ -494,10 +525,9 @@ private boolean isDirectJvmClass(final String name) {
{
return true;
}
return false;
}
}
return false;
return forcedJvmPackages != null && forcedJvmPackages.stream().anyMatch( name::startsWith );
}

private class FilteringUrlEnum implements Enumeration<URL>
Expand Down

0 comments on commit 1bab77f

Please sign in to comment.