Skip to content

Commit

Permalink
Rework FileSystemOptions data object (#2638)
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Viet <julien@julienviet.com>
  • Loading branch information
vietj committed Sep 24, 2018
1 parent ce267b6 commit f3fbf41
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 36 deletions.
10 changes: 8 additions & 2 deletions src/main/asciidoc/dataobjects.adoc
Expand Up @@ -584,8 +584,14 @@ Set whether Netty pooled buffers are enabled
[frame="topbot"]
|===
^|Name | Type ^| Description
|[[classPathResolvingEnabled]]`@classPathResolvingEnabled`|`Boolean`|-
|[[fileResolverCachingEnabled]]`@fileResolverCachingEnabled`|`Boolean`|-
|[[classPathResolvingEnabled]]`@classPathResolvingEnabled`|`Boolean`|+++
When vert.x cannot find the file on the filesystem it tries to resolve the
file from the class path when this is set to <code>true</code>.
+++
|[[fileCachingEnabled]]`@fileCachingEnabled`|`Boolean`|+++
Set to <code>true</code> to cache files on the real file system
when the filesystem performs class path resolving.
+++
|===

[[GoAway]]
Expand Down
4 changes: 2 additions & 2 deletions src/main/asciidoc/filesystem.adoc
Expand Up @@ -137,12 +137,12 @@ a `/`.
Due to the fact that Java does not offer async access to classpath
resources, the file is copied to the filesystem in a worker thread when the
classpath resource is accessed the very first time and served from there
asynchrously. When the same resource is accessed a second time, the file from
asynchronously. When the same resource is accessed a second time, the file from
the filesystem is served directly from the filesystem. The original content
is served even if the classpath resource changes (e.g. in a development
system).

This caching behaviour can be set on the {@link io.vertx.core.file.FileSystemOptions#setFileResolverCachingEnabled(boolean)}
This caching behaviour can be set on the {@link io.vertx.core.file.FileSystemOptions#setFileCachingEnabled(boolean)}
option. The default value of this option is `true` unless the system property `vertx.disableFileCaching` is
defined.

Expand Down
Expand Up @@ -19,9 +19,9 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, FileSys
obj.setClassPathResolvingEnabled((Boolean)member.getValue());
}
break;
case "fileResolverCachingEnabled":
case "fileCachingEnabled":
if (member.getValue() instanceof Boolean) {
obj.setFileResolverCachingEnabled((Boolean)member.getValue());
obj.setFileCachingEnabled((Boolean)member.getValue());
}
break;
}
Expand All @@ -34,6 +34,6 @@ static void toJson(FileSystemOptions obj, JsonObject json) {

static void toJson(FileSystemOptions obj, java.util.Map<String, Object> json) {
json.put("classPathResolvingEnabled", obj.isClassPathResolvingEnabled());
json.put("fileResolverCachingEnabled", obj.isFileResolverCachingEnabled());
json.put("fileCachingEnabled", obj.isFileCachingEnabled());
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/vertx/core/VertxOptions.java
Expand Up @@ -726,7 +726,7 @@ public boolean isFileResolverCachingEnabled() {
if (fileSystemOptions == null) {
return DEFAULT_FILE_CACHING_ENABLED;
}
return fileSystemOptions.isFileResolverCachingEnabled();
return fileSystemOptions.isFileCachingEnabled();
}

/**
Expand All @@ -742,7 +742,7 @@ public VertxOptions setFileResolverCachingEnabled(boolean fileResolverCachingEna
if (fileSystemOptions == null) {
fileSystemOptions = new FileSystemOptions();
}
this.fileSystemOptions.setFileResolverCachingEnabled(fileResolverCachingEnabled);
this.fileSystemOptions.setFileCachingEnabled(fileResolverCachingEnabled);
return this;
}

Expand Down
52 changes: 39 additions & 13 deletions src/main/java/io/vertx/core/file/FileSystemOptions.java
Expand Up @@ -21,15 +21,22 @@
/**
* Vert.x file system base configuration, this class can be extended by provider implementations to configure
* those specific implementations.
*
*/
@DataObject(generateConverter = true, publicConverter = false)
public class FileSystemOptions {

/**
* The default behavior for caching files for class path resolution = {@code false} if and only if the system property {@code "vertx.disableFileCaching"} exists and is set to the string {@code "false"}
*/
public static final boolean DEFAULT_FILE_CACHING_ENABLED = !Boolean.getBoolean(DISABLE_FILE_CACHING_PROP_NAME);

/**
* The default behavior to cache or not class path resolution = {@code false} if and only if the system property {@code "vertx.disableFileCPResolving"} exists and is set to the string {@code "false"}
*/
public static final boolean DEFAULT_CLASS_PATH_RESOLVING_ENABLED = !Boolean.getBoolean(DISABLE_CP_RESOLVING_PROP_NAME);

private boolean classPathResolvingEnabled = DEFAULT_CLASS_PATH_RESOLVING_ENABLED;
private boolean fileResolverCachingEnabled = DEFAULT_FILE_CACHING_ENABLED;
private boolean fileCachingEnabled = DEFAULT_FILE_CACHING_ENABLED;

/**
* Default constructor
Expand All @@ -44,7 +51,7 @@ public FileSystemOptions() {
*/
public FileSystemOptions(FileSystemOptions other) {
this.classPathResolvingEnabled = other.isClassPathResolvingEnabled();
this.fileResolverCachingEnabled = other.isFileResolverCachingEnabled();
this.fileCachingEnabled = other.isFileCachingEnabled();
}

/**
Expand All @@ -70,21 +77,41 @@ public JsonObject toJson() {
return json;
}

/**
* @return whether classpath resolving is enabled
*/
public boolean isClassPathResolvingEnabled() {
return this.classPathResolvingEnabled;
}

public boolean isFileResolverCachingEnabled() {
return this.fileResolverCachingEnabled;
}

/**
* When vert.x cannot find the file on the filesystem it tries to resolve the
* file from the class path when this is set to {@code true}.
*
* @param classPathResolvingEnabled the value
* @return a reference to this, so the API can be used fluently
*/
public FileSystemOptions setClassPathResolvingEnabled(boolean classPathResolvingEnabled) {
this.classPathResolvingEnabled = classPathResolvingEnabled;
return this;
}

public FileSystemOptions setFileResolverCachingEnabled(boolean fileResolverCachingEnabled) {
this.fileResolverCachingEnabled = fileResolverCachingEnabled;
/**
* @return whether file caching is enabled for class path resolving
*/
public boolean isFileCachingEnabled() {
return this.fileCachingEnabled;
}

/**
* Set to {@code true} to cache files on the real file system
* when the filesystem performs class path resolving.
*
* @param fileCachingEnabled the value
* @return a reference to this, so the API can be used fluently
*/
public FileSystemOptions setFileCachingEnabled(boolean fileCachingEnabled) {
this.fileCachingEnabled = fileCachingEnabled;
return this;
}

Expand All @@ -96,22 +123,21 @@ public boolean equals(Object o) {
FileSystemOptions that = (FileSystemOptions) o;

if (classPathResolvingEnabled != that.isClassPathResolvingEnabled()) return false;
return fileResolverCachingEnabled == that.isFileResolverCachingEnabled();
return fileCachingEnabled == that.isFileCachingEnabled();
}

@Override
public int hashCode() {
int result = (classPathResolvingEnabled ? 1 : 0);
result += (fileResolverCachingEnabled ? 1 : 0);
result += (fileCachingEnabled ? 1 : 0);
return 31 * result;
}

@Override
public String toString() {
return "FileSystemOptions{" +
"classPathResolvingEnabled=" + classPathResolvingEnabled +
", fileResolverCachingEnabled=" + fileResolverCachingEnabled +
", fileCachingEnabled=" + fileCachingEnabled +
'}';
}

}
5 changes: 2 additions & 3 deletions src/main/java/io/vertx/core/file/impl/FileResolver.java
Expand Up @@ -13,7 +13,6 @@

import io.vertx.core.VertxException;
import io.vertx.core.file.FileSystemOptions;
import io.vertx.core.VertxOptions;

import java.io.Closeable;
import java.io.File;
Expand Down Expand Up @@ -72,11 +71,11 @@ public FileResolver() {
}

public FileResolver(boolean enableCaching) {
this(new FileSystemOptions().setFileResolverCachingEnabled(enableCaching));
this(new FileSystemOptions().setFileCachingEnabled(enableCaching));
}

public FileResolver(FileSystemOptions fileSystemOptions) {
this.enableCaching = fileSystemOptions.isFileResolverCachingEnabled();
this.enableCaching = fileSystemOptions.isFileCachingEnabled();
this.enableCpResolving = fileSystemOptions.isClassPathResolvingEnabled();
String cwdOverride = System.getProperty("vertx.cwd");
if (cwdOverride != null) {
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/io/vertx/core/VertxOptionsTest.java
Expand Up @@ -11,7 +11,6 @@

package io.vertx.core;

import io.vertx.core.VertxOptions;
import io.vertx.core.file.FileSystemOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.core.metrics.MetricsOptions;
Expand Down Expand Up @@ -388,7 +387,7 @@ public void testJsonOptions() {
put("warningExceptionTime", warningExceptionTime).
put("fileSystemOptions", new JsonObject().
put("classPathResolvingEnabled", classPathResolvingEnabled).
put("fileResolverCachingEnabled", fileResolverCachingEnabled)).
put("fileCachingEnabled", fileResolverCachingEnabled)).
put("metricsOptions", new JsonObject().
put("enabled", metricsEnabled).
put("jmxEnabled", jmxEnabled).
Expand Down Expand Up @@ -417,7 +416,7 @@ public void testJsonOptions() {
assertEquals(haGroup, options.getHAGroup());
FileSystemOptions fileSystemOptions = options.getFileSystemOptions();
assertEquals(classPathResolvingEnabled, fileSystemOptions.isClassPathResolvingEnabled());
assertEquals(fileResolverCachingEnabled, fileSystemOptions.isFileResolverCachingEnabled());
assertEquals(fileResolverCachingEnabled, fileSystemOptions.isFileCachingEnabled());
MetricsOptions metricsOptions = options.getMetricsOptions();
assertEquals(metricsEnabled, metricsOptions.isEnabled());
assertEquals(warningExceptionTime, options.getWarningExceptionTime());
Expand Down
15 changes: 7 additions & 8 deletions src/test/java/io/vertx/core/file/FileSystemOptionsTest.java
Expand Up @@ -11,7 +11,6 @@

package io.vertx.core.file;

import io.vertx.core.file.FileSystemOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.test.core.VertxTestBase;
import org.junit.Test;
Expand All @@ -23,7 +22,7 @@ public class FileSystemOptionsTest extends VertxTestBase {
public void testDefaults() {
FileSystemOptions options = new FileSystemOptions();

assertTrue(options.isFileResolverCachingEnabled());
assertTrue(options.isFileCachingEnabled());
assertTrue(options.isClassPathResolvingEnabled());
}

Expand All @@ -33,17 +32,17 @@ public void testCopy() {

Random rand = new Random();
boolean enabled = rand.nextBoolean();
options.setFileResolverCachingEnabled(enabled);
options.setFileCachingEnabled(enabled);
options.setClassPathResolvingEnabled(enabled);
options = new FileSystemOptions(options);
assertEquals(enabled, options.isClassPathResolvingEnabled());
assertEquals(enabled, options.isFileResolverCachingEnabled());
assertEquals(enabled, options.isFileCachingEnabled());
}

@Test
public void testEmptyJsonOptions() {
FileSystemOptions options = new FileSystemOptions(new JsonObject());
assertTrue(options.isFileResolverCachingEnabled());
assertTrue(options.isFileCachingEnabled());
assertTrue(options.isClassPathResolvingEnabled());
}

Expand All @@ -52,11 +51,11 @@ public void testJsonOptions() {
Random rand = new Random();
boolean enabled = rand.nextBoolean();
FileSystemOptions options = new FileSystemOptions(new JsonObject().
put("fileResolverCachingEnabled", enabled).
put("fileCachingEnabled", enabled).
put("classPathResolvingEnabled", enabled)
);
assertEquals(enabled, options.isFileResolverCachingEnabled());
assertEquals(enabled, options.toJson().getBoolean("fileResolverCachingEnabled"));
assertEquals(enabled, options.isFileCachingEnabled());
assertEquals(enabled, options.toJson().getBoolean("fileCachingEnabled"));
assertEquals(enabled, options.isClassPathResolvingEnabled());
assertEquals(enabled, options.toJson().getBoolean("classPathResolvingEnabled"));
}
Expand Down

0 comments on commit f3fbf41

Please sign in to comment.