Skip to content

Commit

Permalink
KNOX-3051: Rename Extender class, modified Extender to not retain ref…
Browse files Browse the repository at this point in the history
…erence to the properties
  • Loading branch information
hanicz committed Dec 4, 2024
1 parent b83c75d commit 4663864
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ class Command {
Boolean fork = Boolean.FALSE;
Boolean redirect = Boolean.FALSE; // Controls redirecting stderr to stdout if forking.
Boolean restream = Boolean.TRUE; // Controls creation of threads to read/write stdin, stdout, stderr of child if forking.
Extender extender;
GatewayServerClasspathExtender gatewayServerClasspathExtender;

Command( File base, Properties config, String[] args ) throws IOException {
this.base = base;
this.mainArgs = args ;
this.extender = new Extender( base, config );
this.gatewayServerClasspathExtender = new GatewayServerClasspathExtender( base );
consumeConfig( config );
}

void consumeConfig( Properties config ) throws IOException {
extender.extendClassPathProperty();
gatewayServerClasspathExtender.extendClassPathProperty(config);
mainClass = config.getProperty( MAIN_CLASS );
config.remove( MAIN_CLASS );
mainMethod = config.getProperty( MAIN_METHOD, mainMethod );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Extender {
public class GatewayServerClasspathExtender {

private static final String CLASSPATH_EXTENSION_PROPERTY = "gateway.server.classpath.extension";
private static final String CLASSPATH_PROPERTY_PATTERN = "<property>\\s*<name>" + CLASSPATH_EXTENSION_PROPERTY + "</name>\\s*<value>(.*?)</value>\\s*</property>";
Expand All @@ -40,23 +40,21 @@ public class Extender {
private static final String[] CLASS_PATH_DELIMITERS = new String[]{",", ";"};

private final File base;
private final Properties properties;
private final Pattern pattern = Pattern.compile(CLASSPATH_PROPERTY_PATTERN, Pattern.DOTALL);

public Extender(File base, Properties properties) {
public GatewayServerClasspathExtender(File base) {
this.base = base;
this.properties = properties;
}

public void extendClassPathProperty() throws IOException {
public void extendClassPathProperty(Properties properties) throws IOException {
Path configFilePath = Paths.get(base.getPath(), CONFIG_PATH);
if (GATEWAY_SERVER_MAIN_CLASS.equals(properties.getProperty(MAIN_CLASS_PROPERTY)) && Files.isReadable(configFilePath)) {
String configContent = new String(Files.readAllBytes(configFilePath), StandardCharsets.UTF_8);
extractExtensionPathIntoProperty(configContent);
extractExtensionPathIntoProperty(configContent, properties);
}
}

protected void extractExtensionPathIntoProperty(String configContent) {
protected void extractExtensionPathIntoProperty(String configContent, Properties properties) {
final Matcher matcher = pattern.matcher(configContent);

if (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import static org.junit.Assert.assertEquals;

public class ExtenderTest {
public class GatewayServerClasspathExtenderTest {

private Path tempDir;
private Path confDir;
Expand All @@ -56,11 +56,11 @@ public void extendClassPathPropertyTest() throws IOException {
Properties properties = new Properties();
properties.setProperty("class.path", "classpath");
properties.setProperty("main.class", "org.apache.knox.gateway.GatewayServer");
Extender extender = new Extender(confDir.toFile(), properties);
GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(confDir.toFile());

String configContent = this.getConfigContent("/new/classp/*");
Files.write(configFilePath, configContent.getBytes(StandardCharsets.UTF_8));
extender.extendClassPathProperty();
gatewayServerClasspathExtender.extendClassPathProperty(properties);

assertEquals("/new/classp/*;classpath", properties.getProperty("class.path"));
}
Expand All @@ -70,11 +70,11 @@ public void extendClassPathPropertyDifferentMainClassTest() throws IOException {
Properties properties = new Properties();
properties.setProperty("class.path", "classpath");
properties.setProperty("main.class", "org.apache.knox.gateway.KnoxCLI");
Extender extender = new Extender(confDir.toFile(), properties);
GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(confDir.toFile());

String configContent = this.getConfigContent("/new/classp/*");
Files.write(configFilePath, configContent.getBytes(StandardCharsets.UTF_8));
extender.extendClassPathProperty();
gatewayServerClasspathExtender.extendClassPathProperty(properties);

assertEquals("classpath", properties.getProperty("class.path"));
}
Expand All @@ -83,10 +83,10 @@ public void extendClassPathPropertyDifferentMainClassTest() throws IOException {
public void extractExtensionPathIntoPropertyNoDelimTest() {
Properties properties = new Properties();
properties.setProperty("class.path", "classpath");
Extender extender = new Extender(null, properties);
GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null);

String configContent = this.getConfigContent("/new/classp/*");
extender.extractExtensionPathIntoProperty(configContent);
gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties);

assertEquals("/new/classp/*;classpath", properties.getProperty("class.path"));
}
Expand All @@ -95,10 +95,10 @@ public void extractExtensionPathIntoPropertyNoDelimTest() {
public void extractExtensionPathIntoPropertyXMLFormatTest() {
Properties properties = new Properties();
properties.setProperty("class.path", "classpath");
Extender extender = new Extender(null, properties);
GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null);

String configContent = this.getConfigContent("/new/classp/*;");
extender.extractExtensionPathIntoProperty(configContent);
gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties);

assertEquals("/new/classp/*;classpath", properties.getProperty("class.path"));
}
Expand All @@ -107,10 +107,10 @@ public void extractExtensionPathIntoPropertyXMLFormatTest() {
public void extractExtensionPathIntoPropertyWhitespaceTest() {
Properties properties = new Properties();
properties.setProperty("class.path", "classpath");
Extender extender = new Extender(null, properties);
GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null);

String configContent = this.getConfigContent(" /new/classp/*; ");
extender.extractExtensionPathIntoProperty(configContent);
gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties);

assertEquals("/new/classp/*;classpath", properties.getProperty("class.path"));
}
Expand All @@ -119,10 +119,10 @@ public void extractExtensionPathIntoPropertyWhitespaceTest() {
public void extractExtensionPathIntoPropertyMultipleTest() {
Properties properties = new Properties();
properties.setProperty("class.path", "classpath");
Extender extender = new Extender(null, properties);
GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null);

String configContent = this.getConfigContent("/new/classp/*,../classp");
extender.extractExtensionPathIntoProperty(configContent);
gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties);

assertEquals("/new/classp/*,../classp;classpath", properties.getProperty("class.path"));
}
Expand All @@ -131,10 +131,10 @@ public void extractExtensionPathIntoPropertyMultipleTest() {
public void extractExtensionPathIntoPropertyEmptyTest() {
Properties properties = new Properties();
properties.setProperty("class.path", "classpath");
Extender extender = new Extender(null, properties);
GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null);

String configContent = this.getConfigContent("");
extender.extractExtensionPathIntoProperty(configContent);
gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties);

assertEquals("classpath", properties.getProperty("class.path"));
}
Expand All @@ -143,10 +143,10 @@ public void extractExtensionPathIntoPropertyEmptyTest() {
public void extractExtensionPathIntoPropertyEmptyWhitespaceTest() {
Properties properties = new Properties();
properties.setProperty("class.path", "classpath");
Extender extender = new Extender(null, properties);
GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null);

String configContent = this.getConfigContent(" ");
extender.extractExtensionPathIntoProperty(configContent);
gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties);

assertEquals("classpath", properties.getProperty("class.path"));
}
Expand All @@ -155,12 +155,12 @@ public void extractExtensionPathIntoPropertyEmptyWhitespaceTest() {
public void extractExtensionPathIntoPropertyNoConfigTest() throws IOException {
Properties properties = new Properties();
properties.setProperty("class.path", "classpath");
Extender extender = new Extender(null, properties);
GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null);

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("gateway-site-test.xml").getFile());

extender.extractExtensionPathIntoProperty(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8));
gatewayServerClasspathExtender.extractExtensionPathIntoProperty(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8), properties);

assertEquals("classpath", properties.getProperty("class.path"));
}
Expand Down

0 comments on commit 4663864

Please sign in to comment.