Skip to content

Commit

Permalink
Handle Java version above 8 for compileJSPs task (#171)
Browse files Browse the repository at this point in the history
* Handle Java version above 8 for compileJSPs task

* Determine correct jspEngine attribute based on Liberty version

* Fix Liberty version comparison

* Update to recent Liberty version for testing
  • Loading branch information
cherylking committed May 6, 2024
1 parent d943de9 commit c360e7b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ jobs:
matrix:
# test against latest update of each major Java version, as well as specific updates of LTS versions:
os: [ubuntu-latest, windows-latest]
WLP_VERSION: [23.0.0_12]
WLP_VERSION: [24.0.0_03]
java: [21, 17, 11, 8]
include:
# match up licenses to WLP versions
# http://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/downloads/wlp/index.yml
- WLP_VERSION: 23.0.0_12
WLP_LICENSE: L-FBWC-95ADJK
- WLP_VERSION: 24.0.0_03
WLP_LICENSE: L-KZJT-27HQXL

runs-on: ${{ matrix.os }}
name: WL ${{ matrix.WLP_VERSION }}, Java ${{ matrix.java }}, ${{ matrix.os }}
Expand Down
61 changes: 59 additions & 2 deletions src/main/java/io/openliberty/tools/ant/jsp/CompileJSPs.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2014, 2020.
* (C) Copyright IBM Corporation 2014, 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class CompileJSPs extends Task {
private String classpath = "";
private String classpathRef;
private String source;
private boolean useJdkSourceLevel = false;

// By default allow 30 seconds to compile the jsps
private int timeout = 30;
Expand Down Expand Up @@ -365,7 +367,54 @@ private ServerTask createServerTask(File usrDir) {
return server;
}

private boolean isVersion24xxOrLater(String version) {
boolean flag = false;

if (version != null && version.contains(".")) {
String majorVersion = version.substring(0,version.indexOf("."));
try {
Integer majorVersionInt = new Integer(majorVersion);
if (majorVersionInt.intValue() >= 24) {
flag = true;
}
} catch (NumberFormatException e) {
}
}

return flag;
}

private String determineSourceAttribute(File serverDir) {
String sourceAttrToUse = "jdkSourceLevel";
File f = new File(wlpHome,"lib/versions/openliberty.properties");

if (f.exists()) {
try (FileInputStream input = new FileInputStream(f);) {
Properties libertyProductProperties = new Properties();

libertyProductProperties.load(input);
String version = libertyProductProperties.getProperty("com.ibm.websphere.productVersion");
if (isVersion24xxOrLater(version)) {
if (useJdkSourceLevel && source.equals("18")) {
// change source 18 to 8 and use javaSourceLevel instead
source = "8";
useJdkSourceLevel = false;
sourceAttrToUse = "javaSourceLevel";
} else if (!useJdkSourceLevel) {
sourceAttrToUse = "javaSourceLevel";
}
}
} catch (IOException e) {
}
}
return sourceAttrToUse;
}

private void writeServerXML(File serverDir) throws FileNotFoundException {
// Need to determine the version of Liberty installed. For versions prior to 24.0.0.1, use the jdkSourceLevel attribute
// on the jspEngine element. For versions 24.0.0.1 and later, use the javaSourceLevel attribute.
String sourceAttribute = determineSourceAttribute(serverDir);

PrintStream ps = new PrintStream(new File(serverDir, "server.xml"));
ps.println("<server>");
ps.println("<featureManager>");
Expand All @@ -384,7 +433,7 @@ private void writeServerXML(File serverDir) throws FileNotFoundException {
ps.println("<webApplication name=\"jspCompile\" location=\"fake.war\"/>");
}
ps.println("<httpEndpoint id=\"defaultHttpEndpoint\" host=\"localhost\" httpPort=\"0\"/>");
ps.print("<jspEngine prepareJsps=\"0\" scratchdir=\"" + serverDir.getAbsolutePath() + "/jsps\" jdkSourceLevel=\"" + source + "\"/>");
ps.println("<jspEngine prepareJsps=\"0\" scratchdir=\"" + serverDir.getAbsolutePath() + "/jsps\" "+sourceAttribute+"=\"" + source + "\"/>");
ps.println("<webContainer deferServletLoad=\"false\"/>");
ps.println("<keyStore password=\"dummyKeystore\"/>");
ps.println("</server>");
Expand Down Expand Up @@ -489,6 +538,8 @@ public void setClasspathRef(String classpathRef) {
}

public void setSource(String src) {
useJdkSourceLevel = false;
source = null;
if ("1.3".equals(src)) {
source = "13";
} else if ("1.4".equals(src)) {
Expand All @@ -502,6 +553,12 @@ public void setSource(String src) {
} else if ("1.8".equals(src) || "8".equals(src)) {
source = "18";
}

if (source != null) {
useJdkSourceLevel = true;
} else { // for Java 11 and beyond
source = src;
}
}

public void setTimeout(int timeout) {
Expand Down

0 comments on commit c360e7b

Please sign in to comment.