Skip to content

Commit

Permalink
Fixed file path to uri conversions
Browse files Browse the repository at this point in the history
- Were missing on some places
- That fact broke some tests on some Windows file systems which used
  "suspicious" file path/uri strings refused by java while the string should
  have always been an URI.
- I pushed the conversion from getters to setters to make it clear what kind of
  URI/path was used - then we can do more appropriate coversion earlier and just
  once (however GlassFishProperties are still just a bunch of strings so
  there will still be redundant conversions for now).

Signed-off-by: David Matějček <[email protected]>
  • Loading branch information
dmatej committed Nov 5, 2024
1 parent 41ec342 commit f08b251
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -39,7 +40,7 @@ public class Util {
private static GlassFishRuntime glassfishRuntime;

public static synchronized GlassFish startGlassFish(String serverID, String installRoot,
String instanceRoot, String configFileURI,
String instanceRoot, String configFile,
boolean configFileReadOnly, int httpPort)
throws GlassFishException {
GlassFish glassfish = gfMap.get(serverID);
Expand All @@ -58,12 +59,12 @@ public static synchronized GlassFish startGlassFish(String serverID, String inst
if (instanceRoot != null) {
glassfishProperties.setInstanceRoot(instanceRoot);
}
if (configFileURI != null) {
glassfishProperties.setConfigFileURI(configFileURI);
if (configFile != null) {
glassfishProperties.setConfigFileURI(new File(configFile).toURI().toString());
glassfishProperties.setConfigFileReadOnly(configFileReadOnly);
}

if (instanceRoot==null && configFileURI==null) {
if (instanceRoot == null && configFile == null) {
// only set port if embedded domain.xml is used
if (httpPort != -1) {
glassfishProperties.setPort("http-listener", httpPort);
Expand Down Expand Up @@ -92,7 +93,7 @@ public static void deploy(String app, String serverId, List<String> deployParams
Deployer deployer = glassfish.getDeployer();
final int len = deployParams.size();
if (len > 0) {
deployer.deploy(new File(app).toURI(), deployParams.toArray(new String[len]));
deployer.deploy(new File(app).toURI(), deployParams.toArray(String[]::new));
System.out.println("Deployed [" + app + "] with parameters " + deployParams);
} else {
deployer.deploy(new File(app).toURI());
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) 2009, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -184,8 +184,9 @@ private Locations createContainer(Map<?, ?> properties, Locations l) throws EJBE
_logger.info("[EJBContainerProviderImpl] Using instance location: " + l.instance_root.getCanonicalPath());
glassFishProperties.setInstanceRoot(l.instance_root.getCanonicalPath());
} else if (l.domain_file != null) {
_logger.info("[EJBContainerProviderImpl] Using config file location: " + l.domain_file.toURI().toString());
glassFishProperties.setConfigFileURI(l.domain_file.toURI().toString());
String cfgFileUri = l.domain_file.toURI().toString();
_logger.info("[EJBContainerProviderImpl] Using config file location: " + cfgFileUri);
glassFishProperties.setConfigFileURI(cfgFileUri);
}
addWebContainerIfRequested(properties, glassFishProperties);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,55 @@ public String getInstanceRoot() {
return gfProperties.getProperty(INSTANCE_ROOT_PROP_NAME);
}


/**
* Optionally set the location of configuration file (i.e., domain.xml) using
* which the GlassFish should run.
* <p/>
* <p>
* If the parameter is not an absolute URI, it is resolved as a local file path relative to the
* current directory.
* <p>
* Unless specified, the configuration file is operated on read only mode.
* To writeback any changes, call {@link #setConfigFileReadOnly(boolean)} with 'false'.
*
* @param configFileURI Location of configuration file. File path, or full URI with the schema, e.g. file:
* @param configFileURI Location of configuration file. File path, or full URI with the schema,
* e.g. <code>file:</code>
*/
public void setConfigFileURI(String configFileURI) {
gfProperties.setProperty(CONFIG_FILE_URI_PROP_NAME, configFileURI);
if (configFileURI == null) {
gfProperties.remove(CONFIG_FILE_URI_PROP_NAME);
return;
}
setConfigFileURI(URI.create(configFileURI));
}

/**
* Optionally set the location of configuration file (i.e., domain.xml) using
* which the GlassFish should run. The parameter is converted to an absolute URI.
* <p>
* If the parameter is not an absolute URI, it is resolved as a local file path relative to the
* current directory.
* <p>
* Unless specified, the configuration file is operated on read only mode.
* To writeback any changes, call {@link #setConfigFileReadOnly(boolean)} with 'false'.
*
* @param configFileURI Location of configuration file. File path, or full URI with the schema,
* e.g. <code>file:</code>
*/
public void setConfigFileURI(URI configFileURI) {
if (configFileURI == null) {
gfProperties.remove(CONFIG_FILE_URI_PROP_NAME);
return;
}
if (configFileURI.isAbsolute()) {
gfProperties.setProperty(CONFIG_FILE_URI_PROP_NAME, configFileURI.toString());
return;
}
gfProperties.setProperty(CONFIG_FILE_URI_PROP_NAME, new File(configFileURI.toString()).toURI().toString());
}

/**
* Get the configurationFileURI set using {@link #setConfigFileURI(String)}
* Get the absolute configurationFileURI set using {@link #setConfigFileURI(String)}
*
* @return The configurationFileURI set using {@link #setConfigFileURI(String)}
*/
Expand All @@ -153,12 +187,13 @@ public String getConfigFileURI() {
}

/**
* Get the absolute URI, with schema, set using {@link #setConfigFileURI(String)}. Internally uses {@link #filePathToAbsoluteURI(java.lang.String)}.
* Get the absolute URI, with schema, set using {@link #setConfigFileURI(String)}.
* Internally uses {@link #toAbsoluteURI(java.lang.String)}.
*
* @return The configurationFileURI set using {@link #setConfigFileURI(String)} converted to URI
*/
public URI getAbsoluteConfigFileURI() {
return filePathToAbsoluteURI(getConfigFileURI());
return URI.create(getConfigFileURI());
}

/**
Expand Down Expand Up @@ -255,22 +290,4 @@ public int getPort(String networkListener) {
}
return port;
}

/**
* Converts to absolute URI with absolute path.
* If the filePath doesn't contain schema, it will add file: schema
*
* @param filePath Path to create the URI
* @return absolute URI
*/
public static URI filePathToAbsoluteURI(String filePath) {
if (filePath == null) {
return null;
}
URI uri = URI.create(filePath);
if (!uri.isAbsolute()) {
return new File(filePath).getAbsoluteFile().toURI();
}
return uri;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -24,6 +24,7 @@

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;

import org.glassfish.embeddable.GlassFishProperties;
Expand All @@ -47,13 +48,15 @@ protected URL getDomainXml(ServerEnvironmentImpl env) throws IOException {

static URL getDomainXml(StartupContext startupContext) throws IOException {
String configFileURI = startupContext.getArguments().getProperty(GlassFishProperties.CONFIG_FILE_URI_PROP_NAME);
if (configFileURI != null) { // user specified domain.xml
return GlassFishProperties.filePathToAbsoluteURI(configFileURI).toURL();
if (configFileURI != null) {
// user specified domain.xml
return URI.create(configFileURI).toURL();
}
String instanceRoot = startupContext.getArguments().getProperty(
"com.sun.aas.instanceRoot");
File domainXml = new File(instanceRoot, "config/domain.xml");
if (domainXml.exists()) { // domain/config/domain.xml, if exists.
if (domainXml.exists()) {
// domain/config/domain.xml, if exists.
return domainXml.toURI().toURL();
}
return EmbeddedDomainXml.class.getClassLoader().getResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void handle(String value, Arguments arguments) {
"Set the location of domain configuration file (i.e., domain.xml) using which GlassFish should run.") {
@Override
public void handle(String value, Arguments arguments) {
arguments.glassFishProperties.setConfigFileURI(value);
arguments.glassFishProperties.setConfigFileURI(new File(value).toURI().toString());
}
},
DOMAIN_DIR("domainDir", Set.of("instanceRoot"), "--domainDir=DIRECTORY, --instanceRoot=DIRECTORY",
Expand Down

0 comments on commit f08b251

Please sign in to comment.