Skip to content

Commit

Permalink
[Fix #3569] Refining for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Jul 4, 2024
1 parent 6ece5f8 commit 000c558
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,41 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileContentLoader extends CachedContentLoader {

private final Path path;

private static final Logger logger = LoggerFactory.getLogger(FileContentLoader.class);

FileContentLoader(String uri, URIContentLoader... fallbackContentLoaders) {
super(uri, fallbackContentLoaders);
this.path = Path.of(uriToPath(uri));
this.path = obtainPath(uri);
}

@Override
public URIContentLoaderType type() {
return URIContentLoaderType.FILE;
}

private static Path obtainPath(String uri) {
if (uri.startsWith(URIContentLoaderType.FILE.scheme())) {
try {
return Path.of(URI.create(uri));
} catch (Exception ex) {
logger.info("URI {} is not valid one according to Java, trying alternative approach", uri, ex);
}
}
return Path.of(uriToPath(uri));
}

@Override
protected Optional<Path> internalGetPath() {
return Files.exists(path) ? Optional.of(path) : Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public static byte[] readBytes(String uriStr, Workflow workflow, Optional<Parser
}

public static Builder builder(URI uri) {
return new Builder(uri);
return new Builder(uri.toString());
}

public static Builder builder(String uri) {
return new Builder(URI.create(uri));
return new Builder(uri);
}

public static class Builder {
Expand Down Expand Up @@ -134,11 +134,7 @@ public URIContentLoader build() {
}

public static String compoundURI(String baseURI, String uri) {
if (URIContentLoaderType.scheme(uri).isPresent()) {
return uri;
}
final URIContentLoaderType baseType = URIContentLoaderType.from(baseURI);
return baseType.addScheme(baseType.isAbsolutePath(uri) ? uri : baseType.concat(baseType.trimLast(baseType.uriToPath(baseURI)), uri));
return URIContentLoaderType.scheme(uri).isPresent() ? uri : URIContentLoaderType.from(baseURI).concat(baseURI, uri);
}

private URIContentLoaderFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static Optional<String> scheme(String uri) {
public static URIContentLoaderType from(String uri) {
return scheme(uri).map(scheme -> {
switch (scheme) {
default:
case "file":
return FILE;
case "classpath":
Expand All @@ -54,17 +55,10 @@ public static URIContentLoaderType from(String uri) {
return HTTP;
case "https":
return HTTPS;
default:
throw new IllegalArgumentException("Unrecognized protocol " + scheme + "for uri " + uri);
}
}).orElse(FILE);
}

public String addScheme(String path) {
String lowerCasePath = path.toLowerCase();
return lowerCasePath.startsWith(scheme) ? path : scheme + path;
}

public boolean isAbsolutePath(String path) {
if (!path.isBlank()) {
char firstChar = path.trim().charAt(0);
Expand All @@ -78,16 +72,19 @@ public boolean isAbsolutePath(String path) {
}

public String concat(String basePath, String additionalPath) {
char separator = separator();
if (!basePath.isBlank() && !isAbsolutePath(basePath)) {
basePath = separator + basePath;
if (isAbsolutePath(additionalPath)) {
return keepSchemaIfPresent(basePath, additionalPath);
}
int indexOf = lastIndexOf(basePath);
if (indexOf != -1) {
return keepSchemaIfPresent(basePath, basePath.substring(0, indexOf + 1) + additionalPath);
} else {
return keepSchemaIfPresent(basePath, additionalPath);
}
return basePath + separator + additionalPath;
}

public String trimLast(String path) {
int indexOf = lastIndexOf(path);
return indexOf != -1 ? path.substring(0, indexOf) : "";
private String keepSchemaIfPresent(String basePath, String resultPath) {
return basePath.startsWith(scheme) && !resultPath.startsWith(scheme) ? scheme + resultPath : resultPath;
}

public String lastPart(String path) {
Expand All @@ -112,8 +109,4 @@ private int lastIndexOf(String path) {
return indexOf;
}

private char separator() {
return additionalSeparators.length > 0 ? additionalSeparators[0] : '/';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ class URIContentLoaderTest {

@Test
void testExistingFile() throws URISyntaxException {
assertThat(readString(builder(Thread.currentThread().getContextClassLoader().getResource("pepe.txt").toURI()))).isEqualTo("my name is javierito");
assertThat(readString(builder(Thread.currentThread().getContextClassLoader().getResource("pepe a pepa.txt").toURI()))).isEqualTo("my name is javierito");
}

@Test
void testExistingClasspath() {
assertThat(readString(builder("classpath:pepe.txt"))).isEqualTo("my name is javierito");
assertThat(readString(builder("classpath:pepe a pepa.txt"))).isEqualTo("my name is javierito");
}

@Test
Expand All @@ -56,13 +56,14 @@ void testNotExistingClasspath() {

@Test
void testCompoundURI() {
assertThat(compoundURI("classpath:pepe.json", "pepa.json")).isEqualTo("classpath:/pepa.json");
assertThat(compoundURI("classpath:pepe.json", "file:///pepa.json")).isEqualTo("file:///pepa.json");
assertThat(compoundURI("classpath:pepe.json", "pepa.json")).isEqualTo("classpath:pepa.json");
assertThat(compoundURI("classpath:pepe.json", "file:/pepa.json")).isEqualTo("file:/pepa.json");
assertThat(compoundURI("classpath:schema/pepe.json", "/pepa.json")).isEqualTo("classpath:/pepa.json");
assertThat(compoundURI("classpath:schema/pepe.json", "pepa.json")).isEqualTo("classpath:/schema/pepa.json");
assertThat(compoundURI("pepe.json", "pepa.json")).isEqualTo("file:/pepa.json");
assertThat(compoundURI("schema/pepe.json", "pepa.json")).isEqualTo("file:/schema/pepa.json");
assertThat(compoundURI("schema/pepe.json", "/pepa.json")).isEqualTo("file:/pepa.json");
assertThat(compoundURI("classpath:schema/pepe.json", "pepa.json")).isEqualTo("classpath:schema/pepa.json");
assertThat(compoundURI("pepe.json", "pepa.json")).isEqualTo("pepa.json");
assertThat(compoundURI("schema/pepe.json", "pepa.json")).isEqualTo("schema/pepa.json");
assertThat(compoundURI("schema/pepe.json", "/pepa.json")).isEqualTo("/pepa.json");
assertThat(compoundURI("pepe.json", "classpath:pepa.json")).isEqualTo("classpath:pepa.json");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
}
ProtocUtils.generateDescriptor(protoFiles, context);
return true;
} catch (IOException io) {
} catch (Exception io) {
logger.error("Exception generating RPC code", io);
throw new CodeGenException(io);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
*/
package org.kie.kogito.serverless.workflow.io;

import java.net.URI;
import java.util.function.Function;
import java.util.function.Supplier;

import io.quarkus.cache.Cache;
import io.quarkus.cache.CacheName;
Expand All @@ -40,7 +39,7 @@ void init() {
ResourceCacheFactory.setResourceCache(this::get);
}

private byte[] get(URI uri, Function<URI, byte[]> retrieveCall) {
return cache.get(uri, retrieveCall).await().indefinitely();
private byte[] get(String uri, Supplier<byte[]> retrieveCall) {
return cache.get(uri, u -> retrieveCall.get()).await().indefinitely();
}
}

0 comments on commit 000c558

Please sign in to comment.