Skip to content

Commit

Permalink
Merge pull request #211 from jpmsilva/trim-spaces-jar-scan-filter
Browse files Browse the repository at this point in the history
Allow all trimSpaces options, and expose jar scan filter options.
  • Loading branch information
leonardehrenfried authored Feb 14, 2024
2 parents 80d5941 + 141488b commit e4a1477
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.36</version>
</dependency>


</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@

import java.io.IOException;
import java.util.Map;

import org.apache.jasper.*;
import org.apache.jasper.compiler.*;
import org.apache.jasper.JasperException;
import org.apache.jasper.JspC;
import org.apache.jasper.compiler.JspConfig;
import org.apache.jasper.compiler.TldCache;
import org.apache.jasper.servlet.JspCServletContext;
import org.apache.tomcat.JarScanner;
import org.apache.tomcat.util.scan.StandardJarScanFilter;
import org.apache.tomcat.util.scan.StandardJarScanner;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;

public class JspCContextAccessor extends JspC {

private Map<String, NameEnvironmentAnswer> resourcesCache;
private String compilerClass;

private String tldSkip;

private String tldScan;

private Boolean defaultTldScan;

public JspCContextAccessor() {
super();
}
Expand Down Expand Up @@ -50,6 +60,18 @@ public void setcompilerClass(String compilerClass) {
this.compilerClass = compilerClass;
}

public void setTldSkip(String tldSkip) {
this.tldSkip = tldSkip;
}

public void setTldScan(String tldScan) {
this.tldScan = tldScan;
}

public void setDefaultTldScan(Boolean defaultTldScan) {
this.defaultTldScan = defaultTldScan;
}

protected void initContext(JspCContextAccessor topJspC) {
this.context = topJspC.context;
scanner = topJspC.scanner;
Expand All @@ -65,4 +87,25 @@ protected void initContext(JspCContextAccessor topJspC) {
public String getCompilerClassName() {
return getcompilerClass();
}

@Override
protected void initTldScanner(JspCServletContext context, ClassLoader classLoader) {
if (tldSkip != null || tldScan != null || defaultTldScan != null) {
JarScanner scanner = new StandardJarScanner();
StandardJarScanFilter filter = new StandardJarScanFilter();
if (tldSkip != null) {
filter.setTldSkip(tldSkip);
}
if (tldScan != null) {
filter.setTldScan(tldScan);
}
if (defaultTldScan != null) {
filter.setDefaultTldScan(defaultTldScan);
}
scanner.setJarScanFilter(filter);
// As seen in org.apache.jasper.compiler.JarScannerFactory.getJarScanner
context.setAttribute(JarScanner.class.getName(), scanner);
}
super.initTldScanner(context, classLoader);
}
}
62 changes: 59 additions & 3 deletions src/main/java/io/leonard/maven/plugins/jspc/JspcMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -63,6 +66,7 @@
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.slf4j.bridge.SLF4JBridgeHandler;
import org.xml.sax.SAXException;

/**
Expand Down Expand Up @@ -283,7 +287,7 @@ public class JspcMojo extends AbstractMojo {
* trimmed?
*/
@Parameter(defaultValue = "false")
private boolean trimSpaces;
private String trimSpaces;

/**
* Should text strings be generated as char arrays, to improve performance in
Expand Down Expand Up @@ -319,8 +323,40 @@ public class JspcMojo extends AbstractMojo {
@Parameter(defaultValue = "false", property = "jspc.skip")
private boolean skip;

/**
* The comma separated list of JAR file name patterns to skip when scanning for tag libraries (TLDs).
* See {@link org.apache.tomcat.util.scan.StandardJarScanFilter StandardJarScanFilter}.
*
* @see <a href="https://tomcat.apache.org/tomcat-10.1-doc/config/jar-scan-filter.html#Standard_Implementation">Standard implementation</a>
*
*/
@Parameter
private String tldSkip;

/**
* The comma separated list of JAR file name patterns to scan when scanning for tag libraries (TLDs).
* See {@link org.apache.tomcat.util.scan.StandardJarScanFilter StandardJarScanFilter}.
*
* @see <a href="https://tomcat.apache.org/tomcat-10.1-doc/config/jar-scan-filter.html#Standard_Implementation">Standard implementation</a>
*
*/
@Parameter
private String tldScan;

/**
* Controls if JARs are scanned or skipped by default when scanning for TLDs.
* See {@link org.apache.tomcat.util.scan.StandardJarScanFilter StandardJarScanFilter}.
*
* @see <a href="https://tomcat.apache.org/tomcat-10.1-doc/config/jar-scan-filter.html#Standard_Implementation">Standard implementation</a>
*
*/
@Parameter
private Boolean defaultTldScan;

private Map<String, NameEnvironmentAnswer> resourcesCache = new ConcurrentHashMap<>();

private Handler[] handlers;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (getLog().isDebugEnabled()) {
Expand Down Expand Up @@ -356,6 +392,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
try {
long start = System.currentTimeMillis();

installLogHandler();
prepare();
compile();
cleanupSrcs();
Expand All @@ -370,6 +407,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Compilation completed in " + time);
} catch (Exception e) {
throw new MojoExecutionException("Failure processing jsps", e);
} finally {
uninstallLogHandler();
}
}

Expand Down Expand Up @@ -457,13 +496,16 @@ private JspCContextAccessor initJspc(StringBuilder classpathStr, int threadIndex
jspc.setJavaEncoding(javaEncoding);
jspc.setFailOnError(stopAtFirstError);
jspc.setPoolingEnabled(enableJspTagPooling);
jspc.setTrimSpaces(trimSpaces ? TrimSpacesOption.TRUE : TrimSpacesOption.FALSE);
jspc.setTrimSpaces(TrimSpacesOption.valueOf(trimSpaces.toUpperCase()));
jspc.setGenStringAsCharArray(genStringAsCharArray);
jspc.setCompilerSourceVM(compilerVersion);
jspc.setCompilerTargetVM(compilerVersion);
jspc.setcompilerClass(compilerClass);
jspc.setResourcesCache(resourcesCache);
jspc.setStrictQuoteEscaping(strictQuoteEscaping);
jspc.setTldSkip(tldSkip);
jspc.setTldScan(tldScan);
jspc.setDefaultTldScan(defaultTldScan);
if (topJspC == null) {
jspc.initClassLoader();
jspc.initServletContext();
Expand Down Expand Up @@ -699,6 +741,20 @@ private void writeXmlFragments(Path mergedWebXmlPath) throws IOException {
}
}

private void installLogHandler() {
handlers = LogManager.getLogManager().getLogger("").getHandlers();
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}

private void uninstallLogHandler() {
SLF4JBridgeHandler.uninstall();
Logger rootLogger = LogManager.getLogManager().getLogger("");
for (Handler handler : handlers) {
rootLogger.addHandler(handler);
}
}

private void prepare() {
// For some reason JspC doesn't like it if the dir doesn't
// already exist and refuses to create the web.xml fragment
Expand Down Expand Up @@ -770,7 +826,7 @@ private File getWebXmlFile() throws IOException {
* don't add suffix to maintain the same behavior as in the mode without
* multithreading.
*
* @param threadNumber the index of current thread
* @param threadIndex the index of current thread
* @return web xml fragment filename with thread index
*/
private String getwebXmlFragmentFilename(int threadIndex) {
Expand Down

0 comments on commit e4a1477

Please sign in to comment.